Skip to content
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

Use getrandom on FreeBSD #35

Closed
valpackett opened this issue Jun 15, 2019 · 5 comments · Fixed by #57
Closed

Use getrandom on FreeBSD #35

valpackett opened this issue Jun 15, 2019 · 5 comments · Fixed by #57

Comments

@valpackett
Copy link

getentropy in libc uses sysctl only as a fallback and uses the getrandom syscall if available.

This crate should check for getrandom via dlopen and use it if available.

@josephlr
Copy link
Member

Is there a good reason to add this? If we have to keep the sysctl approach around regardless, it just seems like more maintenance to maintain two equivalent implementations for FreeBSD.

@valpackett
Copy link
Author

The sysctl is slower and does not block by itself (as in, it would occasionally return less bytes and your current code would return an error, it must be used in a loop)

@josephlr
Copy link
Member

The sysctl is slower

So it looks like the only reason this is true is because the sysctl only allows up to 256 bytes per call, while the getrandom syscall allows very large reads per call. This only seems relevant if we are bottlenecked by the ring transition overhead. Are there any metrics on this?

and does not block by itself (as in, it would occasionally return less bytes and your current code would return an error, it must be used in a loop)

Good catch, fixed in: #36

Looking at FreeBSD's getrandom(2) man page, it would seem that we would have to do this loop regardless, as it does not guarantee the completion of large buffer requests.

@myfreeweb to do this we need to add libc related bindings (this was done for Linux in rust-lang/libc#1399). Is there a better way to detect if the syscall is supported? On Linux, this is indicated by the syscall returning ENOSYS, does FreeBSD do something similar?

@valpackett
Copy link
Author

Do not call the syscall directly, FreeBSD's public ABI is the libc.

Just do what you're doing on Illumos:

let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) as usize };

@newpavlov
Copy link
Member

@myfreeweb
Can you prepare PR with suggested changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants