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

Mark black_box function as unsafe #20

Open
kitcatier opened this issue Mar 17, 2023 · 1 comment
Open

Mark black_box function as unsafe #20

kitcatier opened this issue Mar 17, 2023 · 1 comment

Comments

@kitcatier
Copy link

Hello, I found a soundness issue in this crate.

bencher/lib.rs

Lines 590 to 596 in 8afeeb1

pub fn black_box<T>(dummy: T) -> T {
unsafe {
let ret = ptr::read_volatile(&dummy);
forget(dummy);
ret
}
}

https://doc.rust-lang.org/std/ptr/fn.read_volatile.html
The unsafe function called needs to ensure that the parameter must be :

  • src must be valid for reads.

  • src must be properly aligned.

  • src must point to a properly initialized value of type T.

and the developer who calls the black_box function may not notice this safety requirement.
Marking them unsafe also means that callers must make sure they know what they're doing.

@bluss
Copy link
Owner

bluss commented Mar 18, 2023

please explain how black_box has different requirements than on a hypothetical function black_box2:

pub fn black_box2<T>(dummy: T) -> T {
}

I believe a user has to follow the same rules for calling black_box as black_box2, thus there is nothing that needs to be changed. Otherwise, please point out the details

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

No branches or pull requests

2 participants