-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Rust's optimization broken some code since 1.80 #134958
Comments
Your code has undefined behavior, both in
You used a pointer derived from a Additionally, it is currently undecided whether a This is how I would write this code. (The pub fn from_js_correct(value: &Float32Array) -> Mat4 {
let mut mat = Mat4::one();
unsafe {
let ptr: *mut f32 = (&raw mut mat).cast();
let dest = Float32Array::view_mut_raw(ptr, 16);
dest.set(&value, 0);
}
mat
} |
Use ptr here because removed unnecessary crate. Acutally code looks like this use pub fn from_js(value: &Float32Array) -> Mat4 {
let mat = [Mat4::one()];
unsafe {
let dest = Float32Array::view(bytemuck::cast_slice(&mat));
dest.set(&value, 0);
}
mat[0]
} Additionally, these code should be treated as UB. Is there any way to warn these ub? We need fix the legacy code when upgrade rust. Thanks. |
You should probably be using There is currently no reliable way to detect UB for code that interacts with FFI. Miri can be used for most code that doesn't use FFI. |
When mutate immutable bytes with FFI, the generated code's behavior changed between various versions of compiler is acceptable(even between debug and release)? |
It doesn't matter if you use FFI or not. If you're committing undefined behavior, there are no guarantees of any kind about what your program will do. It might corrupt everything. It might even "work fine". |
I tried this code:
Current this bug can reproduce with
wasm_bindgen
&js_sys
.The code broken in release mode since 1.80(specially nightly-05-04).
In release mode
from_js
is different fromfrom_js2
.from_js
drop memcpy(in debug mode, will do the copy) just modify the output with Mat4::onefrom_js2
mat with Mat4::one, and do memcpy to output.from_js3
is alias offrom_js
llvm-ir attached.
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: