Skip to content

netbsd: Simplify weak lookup. - #484

Merged
josephlr merged 2 commits into
rust-random:masterfrom
briansmith:b/netbsd-weak
Jun 19, 2024
Merged

josephlr merged 2 commits into
rust-random:masterfrom
briansmith:b/netbsd-weak

Conversation

@briansmith

Copy link
Copy Markdown
Contributor

Remove LazyPtr. Avoid constructing an invalid pointer as a sentinel to indicate that the pointer is uninitialized. Now, a null pointer means it is uninitialized, and a non-null pointer means it is initialized. This is less questionable from a safety perspective, and should also be more efficient.

Reduce duplication between the "getrandom is available" and the fallback case.

@briansmith
briansmith force-pushed the b/netbsd-weak branch 6 times, most recently from c008fee to 1a0a475 Compare June 18, 2024 20:48
@newpavlov

newpavlov commented Jun 18, 2024 •

Copy link
Copy Markdown
Member

What about the following comment about KERN_ARND?

NetBSD will only return up to 256 bytes at a time, and older NetBSD kernels will fail on longer buffers.

Is it no longer relevant? To be safe it may be worth to use let buflen = min(buflen, 256) in the polyfill function.

@briansmith
briansmith force-pushed the b/netbsd-weak branch 2 times, most recently from e8095c6 to 5d152be Compare June 18, 2024 21:02
Comment thread src/netbsd.rs Resolved
@briansmith
briansmith force-pushed the b/netbsd-weak branch 5 times, most recently from 40669fd to ea3436e Compare June 18, 2024 21:41
@briansmith
briansmith marked this pull request as ready for review June 18, 2024 22:25
@briansmith

Copy link
Copy Markdown
Contributor Author

What about the following comment about KERN_ARND?

NetBSD will only return up to 256 bytes at a time, and older NetBSD kernels will fail on longer buffers.

Is it no longer relevant? To be safe it may be worth to use let buflen = min(buflen, 256) in the polyfill function.

Thanks. As I mentioned in the other comment, I did make this change.

@newpavlov newpavlov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outside of several minor nits, LGTM.

Comment thread src/netbsd.rs Resolved
Comment thread src/netbsd.rs Resolved
Comment thread src/netbsd.rs Outdated
let ptr = match unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) } {
ptr if !ptr.is_null() => ptr,
_ => polyfill_using_kern_arand as *mut libc::c_void,
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the following code will be a bit easier to read:

let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
// If getrandom(2) is not availalbe, use KERN_ARAND polyfill
if ptr.is_null() {
    ptr = polyfill_using_kern_arand as *mut libc::c_void;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other PRs, @josephlr asked me to write things the way I wrote it here, and to not write it the let mut ... way.

@briansmith briansmith Jun 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the other reason I use a match is because it helps with code coverage measurement. Though we don't have that yet, I hope we do soon.

@newpavlov newpavlov Jun 18, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. :) I don't have a strong preference here, so I will wait for him to confirm that he prefers this way.

it helps with code coverage measurement

I think the if branch should be handled by code coverage tools without issues.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other PRs, @josephlr asked me to write things the way I wrote it here, and to not write it the let mut ... way.

Looking at this, I think that was a bad call on my part (sorry). I agree w/ @newpavlov that the if is more readable here. @briansmith's point is a good one about code coverage, but we would ideally handle that via branch coverage (see #288 (comment))

Comment thread src/netbsd.rs Resolved

@josephlr josephlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! Thanks for simplifying this.

Comment thread src/netbsd.rs Outdated
let ptr = match unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) } {
ptr if !ptr.is_null() => ptr,
_ => polyfill_using_kern_arand as *mut libc::c_void,
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other PRs, @josephlr asked me to write things the way I wrote it here, and to not write it the let mut ... way.

Looking at this, I think that was a bad call on my part (sorry). I agree w/ @newpavlov that the if is more readable here. @briansmith's point is a good one about code coverage, but we would ideally handle that via branch coverage (see #288 (comment))

Comment thread src/netbsd.rs Outdated
@@ -24,28 +44,33 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {

type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the first argument be *mut c_void here? That's what the man pages has: https://man.freebsd.org/cgi/man.cgi?query=getrandom&sektion=2

I'm not sure if it particularly matters

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it particularly matters

In theory it could matter depending on the CFI rules. I changed it.

Comment thread src/netbsd.rs Resolved
Match the type signature that the NetBSD man page gives for
getrandom.
Remove LazyPtr. Avoid constructing an invalid pointer as a sentinel to
indicate that the pointer is uninitialized. Now, a null pointer means
it is uninitialized, and a non-null pointer means it is initialized.
This is less questionable from a safety perspective, and should also be
more efficient.

Reduce duplication between the "getrandom is available" and the
fallback case.
Comment thread src/netbsd.rs
if ptr.is_null() {
// Verify `polyfill_using_kern_arand` has the right signature.
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
ptr = POLYFILL as *mut c_void;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked this by splitting the conversion into two clearly-correct parts because polyfill_using_kern_arand as *mut c_void is quite a dangerous cast.

Comment thread src/netbsd.rs
}

type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this signature as @josephlr suggested.

Comment thread src/netbsd.rs
// the one in libstd, meaning that the use of non-Relaxed operations is
// probably unnecessary.
let mut fptr = GETRANDOM.load(Ordering::Acquire);
if fptr.is_null() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this from the match style that people didn't like.

Comment thread src/netbsd.rs
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }
let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
if ptr.is_null() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this from the match style that people didn't like.

@josephlr
josephlr merged commit 267639e into rust-random:master Jun 19, 2024
takumi-earth pushed a commit to earthlings-dev/getrandom that referenced this pull request Jan 27, 2026
Remove LazyPtr. Avoid constructing an invalid pointer as a sentinel to
indicate that the pointer is uninitialized. Now, a null pointer means it
is uninitialized, and a non-null pointer means it is initialized. This
is less questionable from a safety perspective, and should also be more
efficient.

Reduce duplication between the "getrandom is available" and the fallback
case.
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 this pull request may close these issues.

3 participants