-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clone() is broken and unsafe with CLONE_VM flag #360
Comments
|
The point of |
Coming back to this as I'd like to fix up some bugs, I think having both an unsafe and safe-but-restricted clone makes a lot of sense here. @arcnmx it seems like you have an understanding of what the API should be, would you be able to whip up a PR for this? @bugaevc Is there anything we should do to the existing |
Yes, we could make it accept the same kind of closures But then if a |
Something like this: extern "C" fn clone_cb<F>(data: *mut libc::c_void) -> *mut libc::c_void
where F: FnOnce() -> isize
{
let bf = unsafe {
Box::from_raw(data as *mut F);
};
(*bf)()
// (boxed) closure data gets freed here
}
pub fn clone<F>(f: F, other_args: OtherArgs) -> Result<Pid>
where F: FnOnce() -> isize + 'static
{
// box the closure data
let bf = Box::new(f);
// leave it on the heap
let p = Box::into_raw(bf) as *mut libc::void;
let res = unsafe {
libc::clone(clone_cb::<F>, /* data */ p, other_args);
};
make_nice_rusty_result(res)
}
pub fn clone_with_VM<F>(f: F, other_args: OtherArgs) -> Result<Pid>
where F: FnOnce() -> isize + 'static + Send
{
// same, or wrap clone()
} |
clone()
has a few issues when used withCLONE_VM
, unless it is used together withCLONE_VFORK
.Box<FnMut>
by value, which will be moved, dropped, and destroyed when the function returns back to the caller.CLONE_VM
clone()
call returns, even if the cloned process is still running, meaning you can modify the child process' stack as it runs.I was actually going to suggest that its prototype be changed to an unboxed
FnOnce
, since the caller's stack will never fall out from under you unlessCLONE_VM
is used... but I think it also needs to disallow theCLONE_VM
flag entirely (unlessCLONE_VM | CLONE_VFORK
). Perhaps offer a second unsafe version for that use.The text was updated successfully, but these errors were encountered: