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

Fix UB in transmute #34181

Closed
wants to merge 2 commits into from
Closed

Fix UB in transmute #34181

wants to merge 2 commits into from

Conversation

gmorenz
Copy link

@gmorenz gmorenz commented Jun 9, 2016

The current code mutates through a & reference, this fixed that.

We could remove get_global_ptr and transmute entirely by using #![feature(drop_types_in_const)], I'm not sure if this would be preferred.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@brson
Copy link
Contributor

brson commented Jun 11, 2016

This is really tricky code and seems to have more problems than just transmuting &T to *mut T. @eddyb has opinions.

@brson brson assigned eddyb and unassigned brson Jun 11, 2016
@eddyb
Copy link
Member

eddyb commented Jun 11, 2016

@gmorenz So this change by itself doesn't fix much, borrowing static mut is by itself problematic.
I was worried about #![feature(drop_types_in_const)] not working at stage0, but that's not true.

I think we can defer the problem of replacing static mut with static and an UnsafeCell wrapper (eventually this should be Mutex<Vec<Vec<u8>>> anyway, @Amanieu's parking_lot would make that possible), if all accesses are direct.
Still needs an Option for now because Vec::new is not const fn but I would remove the Box wrapping anyway, it's only there so usize can be used.

@eddyb
Copy link
Member

eddyb commented Jun 11, 2016

In other words, yes, #![feature(drop_types_in_const)] instead of transmute is preferred.

@gmorenz
Copy link
Author

gmorenz commented Jun 12, 2016

@eddyb I've updated this to use drop_types_in_const and not mutably borrow the static mut. I'm somewhat curious about your comment that "borrowing static mut is by itself problematic.`, is it just something that we prefer to avoid because it's easy to break aliasing rules, or is it always undefined behaviour or something?

@eddyb
Copy link
Member

eddyb commented Jun 12, 2016

@bors r+ Thank you!

@gmorenz It's arguably not always UB, but it's really easy to misuse, and some strict memory models might make a lot of possible uses of static mut UB and MIR optimizations could then act on that.

To build a proper wrapper around it, you want to have an UnsafeCell(-based abstraction) inside the static mut and to never borrow it mutably directly - but then you can do the same thing with a static, which makes the mut part an unnecessary footgun.
The caveat is that to create an UnsafeCell in a static, you need the ability to call a const fn from its initializer, which is not something that has been stabilized yet, so we can't even deprecate static mut.

@bors
Copy link
Contributor

bors commented Jun 12, 2016

📌 Commit 80d2956 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Jun 12, 2016

⌛ Testing commit 80d2956 with merge 49829f3...

@bors
Copy link
Contributor

bors commented Jun 12, 2016

💔 Test failed - auto-linux-64-opt-mir

@eddyb
Copy link
Member

eddyb commented Jun 12, 2016

@alexcrichton Are there known valgrind issues around such globals? Should I hunt for codegen bugs?

@alexcrichton
Copy link
Member

Hm not that I know of, that does look suspicious though...

@eddyb
Copy link
Member

eddyb commented Jul 12, 2016

Sorry for letting this sit for this long, let's try again (just in case it was transient or a fixed MIR regression).

@bors retry

@bors
Copy link
Contributor

bors commented Jul 12, 2016

⌛ Testing commit 80d2956 with merge b15e775...

@bors
Copy link
Contributor

bors commented Jul 12, 2016

💔 Test failed - auto-linux-64-opt-mir

@mrhota
Copy link
Contributor

mrhota commented Aug 18, 2016

@gmorenz @eddyb anything else to do here?

@eddyb
Copy link
Member

eddyb commented Aug 18, 2016

I honestly have no idea how this goes wrong.

@gmorenz
Copy link
Author

gmorenz commented Aug 21, 2016

I also have no idea what the problem is, I think (though I'm not an expert) that the current set of optimizations doesn't take advantage of anything that might break the current code so merging this PR isn't high priority (from my point of view at least).

@bors
Copy link
Contributor

bors commented Oct 3, 2016

☔ The latest upstream changes (presumably #36807) made this pull request unmergeable. Please resolve the merge conflicts.

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to resubmit with a rebase!

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.

7 participants