Skip to content

libc::clone() should take an unsafe callback #2198

Open

Description

@jstarks

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.:

    pub fn clone(
        cb: unsafe extern "C" fn(*mut ::c_void) -> ::c_int,
        child_stack: *mut ::c_void,
        flags: ::c_int,
        arg: *mut ::c_void,
        ...
    ) -> ::c_int;

I'm not certain, but I don't think this would be a breaking change.

Activity

  1. JohnTitor commented on Jun 1, 2021

    @JohnTitor
    Member

    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

    I'm not certain, but I don't think this would be a breaking change.

    Yup, it'll be a breaking change.

  2. jstarks commented on Jun 1, 2021

    @jstarks
    Author

    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?

  3. JohnTitor commented on Jun 1, 2021

    @JohnTitor
    Member

    If it's usable then I don't see any strong reason to change. Could you show some situation it should be changed?

  4. jstarks commented on Jun 1, 2021

    @jstarks
    Author

    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.

  5. JohnTitor commented on Jun 1, 2021

    @JohnTitor
    Member

    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.

  6. pro465 commented on Nov 5, 2021

    @pro465

    Yup, it'll be a breaking change.

    is not libc still 0.x.y, which means it can have breaking changes?

  7. joshtriplett commented on Nov 16, 2021

    @joshtriplett
    Member

    You don't have to use the context parameter.

  8. tgross35 commented on Aug 29, 2024

    @tgross35
    Member

    Let's just do this for 1.0, along with any other functions that take a callback with pointer arguments. PRs are welcome!

  9. added this to the 1.0 milestone on Aug 29, 2024
  10. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Aug 29, 2024
  11. purplesyringa commented on Sep 5, 2024

    @purplesyringa
    Contributor

    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.

    I'm glad this is going to be fixed in 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.breakage-candidate

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions