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

avoid mem-forget-in-disguise #458

Merged
merged 1 commit into from Dec 31, 2020
Merged

Conversation

RalfJung
Copy link
Contributor

Box::into_raw(slice) (which is used just as a way to encode mem::forget here, from what I can tell) uses the Box and thus asserts its uniqueness as a pointer, which is not compatible with what the code here does. That's why the docs recommend ManuallyDrop over mem::forget. This adjusts the code accordingly.

I found this with Miri using the -Zmiri-track-raw-pointers flag. Unfortunately, the test suite cannot pass with that flag since bytes uses int-to-ptr casts, which are incompatible with -Zmiri-track-raw-pointers. But this fix seems worth landing nevertheless.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Huh, using Box::into_raw like that seems somewhat confusing.

src/bytes.rs Outdated
let ptr = slice.as_ptr();
drop(Box::into_raw(slice));
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this compare to just doing this?

let ptr = Box::into_raw(slice) as *const _;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where the as here is doing the wide-to-thin ptr conversion?

Yeah something like that should also work. The important part is not using slice any more after ptr got created.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm mainly worried about the as_ptr creating the pointer through a &self instead of &mut self, but that may not be a problem since this is for Bytes where we can't modify it anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. I updated my patch.

@RalfJung RalfJung changed the title use ManuallyDrop instead of mem-forget-in-disguise avoid mem-forget-in-disguise Dec 31, 2020
@Darksonn Darksonn merged commit df20a68 into tokio-rs:master Dec 31, 2020
@Darksonn Darksonn mentioned this pull request Jan 11, 2021
taiki-e added a commit to rust-lang/futures-rs that referenced this pull request Jan 12, 2022
This fixes stacked borrows violation violations.

See tokio-rs/bytes#458 for more.
taiki-e added a commit to rust-lang/futures-rs that referenced this pull request Jan 12, 2022
This fixes stacked borrows violation violations.

See tokio-rs/bytes#458 for more.
taiki-e added a commit to rust-lang/futures-rs that referenced this pull request Feb 6, 2022
This fixes stacked borrows violation violations.

See tokio-rs/bytes#458 for more.
taiki-e added a commit to rust-lang/futures-rs that referenced this pull request Feb 6, 2022
This fixes stacked borrows violation violations.

See tokio-rs/bytes#458 for more.
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