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

Make strict provenance compatible #542

Merged
merged 4 commits into from Apr 15, 2022
Merged

Make strict provenance compatible #542

merged 4 commits into from Apr 15, 2022

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Apr 6, 2022

This PR replaces all ptr2int2ptr casts with an wrapping_add equivalent, making it strict-provenance compatible.

src/bytes.rs Outdated
Comment on lines 1104 to 1112
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);
let diff = new_addr.wrapping_sub(old_addr);
ptr.wrapping_add(diff)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, it seems like this doesn't really optimize as well as I had hoped. You can view godbolt here to see the difference.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, that's kind of sad...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One option is to go through a normal pointer cast for non-miri release mode.

Copy link
Contributor

@saethlin saethlin Apr 6, 2022

Choose a reason for hiding this comment

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

Is this valid? This seems to optimize well, though I have no idea why. I just tried it out because I find this pattern for writing pointer packing more intuitive.
https://rust.godbolt.org/z/a949Wjs7f (I previously left a target-cpu in the flags, oops)

src/bytes.rs Outdated Show resolved Hide resolved
@Darksonn Darksonn requested a review from hawkw April 15, 2022 20:45
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.

this looks good to me!

src/bytes.rs Outdated Show resolved Hide resolved
src/bytes.rs Show resolved Hide resolved
@thomcc
Copy link

thomcc commented Apr 29, 2022

FYI, the optimization you do to avoid the bad codegen causes (non-theoretical) LLVM miscompiles -- See rust-lang/rust#96538 for some details. So I don't know if you should keep it.

@hawkw
Copy link
Member

hawkw commented Apr 29, 2022

welp. that's cool.

@Darksonn
Copy link
Contributor Author

@thomcc Thanks. We will uses ptr2int2ptr casts for now on non-miri builds then.

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

4 participants