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
Only avoid pointer casts when using miri #545
Conversation
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.
Overall, this seems fine, but I think we really ought to document what's going on here.
#[cfg(miri)] | ||
fn ptr_map<F>(ptr: *mut u8, f: F) -> *mut u8 | ||
where | ||
F: FnOnce(usize) -> usize, | ||
{ | ||
let old_addr = ptr as usize; | ||
let new_addr = f(old_addr); | ||
// this optimizes better than `ptr.wrapping_add(new_addr.wrapping_sub(old_addr))` | ||
ptr.wrapping_sub(old_addr).wrapping_add(new_addr) | ||
let diff = new_addr.wrapping_sub(old_addr); | ||
ptr.wrapping_add(diff) | ||
} | ||
|
||
#[cfg(not(miri))] | ||
fn ptr_map<F>(ptr: *mut u8, f: F) -> *mut u8 | ||
where | ||
F: FnOnce(usize) -> usize, | ||
{ | ||
let old_addr = ptr as usize; | ||
let new_addr = f(old_addr); | ||
new_addr as *mut u8 |
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.
Perhaps this is a bit pedantic, but this really feels like it ought to have a comment on it, IMO. To someone who doesn't know the specific reason for this, it seems quite suspect that we only do the "correct" thing under Miri, and the "real" code does the thing that's not strict provenance compliant. A comment saying that the thing that's theoretically "correct" under strict provenance actually results in miscompiles in real life, and references the appropriate rustc issue etc.
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.
You aren't being pedantic. I added a link to this PR, which has links to the previous PR and the rust issue.
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.
okay, the comment looks good, thank you!
Address this comment. See also rust-lang/rust#96538.