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

Only avoid pointer casts when using miri #545

Merged
merged 2 commits into from Apr 29, 2022
Merged

Only avoid pointer casts when using miri #545

merged 2 commits into from Apr 29, 2022

Conversation

Darksonn
Copy link
Contributor

Address this comment. See also rust-lang/rust#96538.

@Darksonn Darksonn requested a review from hawkw April 29, 2022 17:51
Copy link
Member

@hawkw hawkw left a 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.

Comment on lines +1107 to +1125
#[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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@hawkw hawkw left a 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!

@Darksonn Darksonn merged commit 716a0b1 into master Apr 29, 2022
@Darksonn Darksonn deleted the miri-strict branch April 29, 2022 20:01
lelongg pushed a commit to lelongg/bytes that referenced this pull request Jan 9, 2023
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.

None yet

2 participants