-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix - Uses std::mem::transmute
and std::ptr::write
in unsafe code in append_vec.rs
#32711
Fix - Uses std::mem::transmute
and std::ptr::write
in unsafe code in append_vec.rs
#32711
Conversation
ab9750b
to
e3399d9
Compare
Codecov Report
@@ Coverage Diff @@
## master #32711 +/- ##
=======================================
Coverage 82.0% 82.0%
=======================================
Files 784 784
Lines 210979 210981 +2
=======================================
+ Hits 173026 173046 +20
+ Misses 37953 37935 -18 |
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.
Both set_data_len_unsafe()
and set_executable_as_byte()
are annotated with #[allow(clippy::cast_ref_to_mut)]
. I would not have expected a clippy error; do you know why this happened?
You are right, we can remove that lint now. It is deprecated see: rust-lang/rust#111567 |
Gotcha. Do you intend to do that within this PR, or a subsequent PR? |
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.
Also, code changes look good to me. These functions are only used in tests, so it's easy to audit all their callers.
We may even want to inline the functions into their callers, since they are only called once (get_executable_byte()
and set_executable_as_byte()
) or twice (set_data_len_unsafe()
). This would further restrict their usage and would also put the code directly within the context being called, making inspection easier. Not something that needs to happen in this PR though.
e3399d9
to
837bc1b
Compare
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.
lgtm
std::ptr::write( | ||
std::mem::transmute::<*const u64, *mut u64>(&self.meta.data_len), | ||
new_data_len, | ||
); |
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.
This is still UB. Casting/transmuting a immutable reference to a mutable reference is always UB. The only sound way of mutating an immutable reference to a mutable reference is to use a Cell
or UnsafeCell
.
See this playground example for proof with Miri.
error: Undefined Behavior: attempting a write access using <1679> at alloc852[0x0], but that tag only grants SharedReadOnly permission for this location
--> src/main.rs:7:9
|
7 | / std::ptr::write(
8 | | std::mem::transmute::<*const u64, *mut u64>(&data_len),
9 | | new_data_len,
10 | | );
| | ^
| | |
| |_________attempting a write access using <1679> at alloc852[0x0], but that tag only grants SharedReadOnly permission for this location
| this error occurs as part of an access at alloc852[0x0..0x8]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <1679> was created by a SharedReadOnly retag at offsets [0x0..0x8]
--> src/main.rs:8:57
|
8 | std::mem::transmute::<*const u64, *mut u64>(&data_len),
| ^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at src/main.rs:7:9: 10:10
NOTE: The current cast_ref_to_mut
or the now uplifted invalid_reference_casting
lint unfortunately do not (yet) lint against, but I'm planning on fixing it.
Problem
In nightly rust we will encounter the following error messages:
Summary of Changes
Uses explicit
std::mem::transmute
andstd::ptr::write
in unsafe code instead.