-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
linux: try to use libc getrandom to allow interposition #78785
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ unsafe fn fetch(name: &str) -> usize { | |
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
#[cfg(not(any(target_os = "linux", target_os = "android")))] | ||
macro_rules! syscall { | ||
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( | ||
unsafe fn $name($($arg_name: $t),*) -> $ret { | ||
|
@@ -84,18 +84,26 @@ macro_rules! syscall { | |
) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
macro_rules! syscall { | ||
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( | ||
unsafe fn $name($($arg_name:$t),*) -> $ret { | ||
// This looks like a hack, but concat_idents only accepts idents | ||
// (not paths). | ||
use libc::*; | ||
|
||
syscall( | ||
concat_idents!(SYS_, $name), | ||
$($arg_name as c_long),* | ||
) as $ret | ||
weak! { fn $name($($t),*) -> $ret } | ||
|
||
// Use a weak symbol from libc when possible, allowing `LD_PRELOAD` | ||
// interposition, but if it's not found just use a raw syscall. | ||
if let Some(fun) = $name.get() { | ||
fun($($arg_name),*) | ||
} else { | ||
syscall( | ||
concat_idents!(SYS_, $name), | ||
$($arg_name as c_long),* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably where all arguments are made ptr-sized. It is not clear to me what that is done (we do have proper type information after all!), nor why it should even be allowed. Isn't it UB when the caller and callee types do not match? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to make sure they're all integral/pointer values that fit in a machine word. As long as that's the case, the exact type doesn't matter. The ABI of a variadic function like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a guarantee made by C? For all ABIs? And what happens with the value that is passed, logically, to the syscall? It should get truncated to the smaller of "type used" and "type expected", I guess? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose we do something like #79196. |
||
) as $ret | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this work for sendfile? The sendfile64 libc wrapper is available on 32bit and 64bit. But on 64bit only the raw sendfile syscall is available since it's already 64bit-wide. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The macro is assuming that the libc function It's also meant for cases where the function is relatively new, not yet in the minimum libc we intend to support, so the syscall is a fallback. I don't think that's the case for |
||
} | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though this says
c_uint
, in Miri we are seeing theflags
argument being passed as a ptr-sized value whendlsym
returns NULL. Before this change it was always of size 4. Did something go wrong with the type logic?