-
Notifications
You must be signed in to change notification settings - Fork 13.3k
std::rand::OsRng: Use getrandom
syscall on Linux
#18664
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
Conversation
: "{rax}"(GETRANDOM), "{rdi}"(buf.as_ptr()), "{rsi}"(buf.len()), "{rdx}"(flags) | ||
: "rcx", "r11", "memory" | ||
: "volatile"); | ||
} |
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.
Instead of using inline assembly, could this use the extern_weak
trick that __pthread_get_minstack
uses?
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.
That may end up simplifying a large amount of what's going on here as well...
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.
There's no symbol for getrandom
at all yet. Using syscall
to call it would work.
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.
Ah ok, using syscall
sounds like it would work just as well!
@klutzy: I think this should use |
Rust supports calls to variadic C functions so it should work fine. |
ret | ||
} | ||
|
||
#[cfg(not(any(target_os = "linux", target_arch = "x86_64", target_arch = "x86")))] |
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.
What happens to ARM Linux?
I think this line should be
#[cfg(not(all(target_os = "linux", any(target_arch = "x86", target_arch = "x86_64"))))]
Ah, if you switch to the variadic syscall(2)
, it should take care of ENOSYS
`getrandom(2)` system call [1] has been added on Linux 3.17. This patch makes `OsRng` use `getrandom` if available, and use traditional `/dev/urandom` fallback if not. [1]: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895
updated with @nodakai Added arm-eabi (syscall number 384), although I have no way to test it right now. |
getrandom(2)
system call 1 has been added on Linux 3.17.This patch makes
OsRng
usegetrandom
if available, and usetraditional
/dev/urandom
fallback if not.Fixes #17922.