You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
libc::clone() should take an unsafe callback #2198
On Linux, clone takes a callback function pointer. The function pointer type is not marked unsafe, but there is basically no way to implement this callback with a safe function since it takes its context parameter by pointer.
It seems to me that unsafe should be added to the function pointer type, e.g.:
Sure, it's possible to work around this via transmute. It just introduces another opportunity for error.
I guess this is a breaking change because someone might care about the type of the clone function, is that right? As long as clone is not used as a function pointer, then this change would not cause any existing program to fail to compile, no?
I don't think it's a bug but an improvement, though. So, adding unsafe helps some usage (and it doesn't break any build), I'm happy to accept that change. Therefore I'd like to see an example you consider.
Just for context, the problem is that Rust's best practices require all safe (even internal) functions to be sound when called with arbitrary arguments. However, a callback that doesn't just ignore its argument but casts it to *const UserData and then dereferences the pointer cannot be sound for arbitrary arguments, that is, be safe. So following safety-oriented best practices (marking the callback unsafe fn) requires jumping through extra unsafe hoops (transmuting the callback from unsafe fn to fn at the invocation of clone), which in practice leads to the best practices (if you can even call them that, they are more akin to strict rules) not being followed at all.
Activity
JohnTitor commentedon Jun 1, 2021
I'm not sure why this change is needed as that function should be usable currently.
For instance, it's normally used on the user codebase: https://github.com/nix-rust/nix/blob/b4d4b3183b75addf7cad83c57994a62a89235ed4/src/sched.rs#L194-L201
Yup, it'll be a breaking change.
jstarks commentedon Jun 1, 2021
Sure, it's possible to work around this via transmute. It just introduces another opportunity for error.
I guess this is a breaking change because someone might care about the type of the
clonefunction, is that right? As long ascloneis not used as a function pointer, then this change would not cause any existing program to fail to compile, no?JohnTitor commentedon Jun 1, 2021
If it's usable then I don't see any strong reason to change. Could you show some situation it should be changed?
jstarks commentedon Jun 1, 2021
We can agree it's a bug, right? Just one that can be worked around. I defer to your judgment whether it's worth fixing; feel free to close.
JohnTitor commentedon Jun 1, 2021
I don't think it's a bug but an improvement, though. So, adding
unsafehelps some usage (and it doesn't break any build), I'm happy to accept that change. Therefore I'd like to see an example you consider.pro465 commentedon Nov 5, 2021
is not libc still 0.x.y, which means it can have breaking changes?
joshtriplett commentedon Nov 16, 2021
You don't have to use the context parameter.
pro465 commentedon Nov 19, 2021
@JohnTitor
how? are not safe fn pointers implicitly castable to unsafe fn pointers?
link to working example with the same typed fn pointer as the OP's suggestion:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=use%20std%3A%3Affi%3A%3Ac_void%3B%0Ause%20std%3A%3Aos%3A%3Araw%3A%3Ac_int%3B%0A%0Aextern%20%22C%22%20fn%20foo(_%3A%20*mut%20c_void)%20-%3E%20c_int%20%7B%201%20%7D%0A%0Afn%20takes_unsafe_callback(callback%3A%20unsafe%20extern%20%22C%22%20fn(*mut%20c_void)%20-%3E%20c_int)%20%7B%0A%20%20%20%20unsafe%20%7B%20callback(0%20as%20_)%3B%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20takes_unsafe_callback(foo%20as%20extern%20%22C%22%20fn(*mut%20c_void)%20-%3E%20c_int)%3B%0A%7D
tgross35 commentedon Aug 29, 2024
Let's just do this for 1.0, along with any other functions that take a callback with pointer arguments. PRs are welcome!
purplesyringa commentedon Sep 5, 2024
Just for context, the problem is that Rust's best practices require all safe (even internal) functions to be sound when called with arbitrary arguments. However, a callback that doesn't just ignore its argument but casts it to
*const UserDataand then dereferences the pointer cannot be sound for arbitrary arguments, that is, be safe. So following safety-oriented best practices (marking the callbackunsafe fn) requires jumping through extra unsafe hoops (transmuting the callback fromunsafe fntofnat the invocation ofclone), which in practice leads to the best practices (if you can even call them that, they are more akin to strict rules) not being followed at all.I'm glad this is going to be fixed in 1.0.