Summary
gccrs seems to compile pointers/pointer casts with C’s strict aliasing/type-based alias analysis (TBAA) semantics, i. e. with the assumption that a pointer to type T cannot alias a pointer to type U for T != U. This is not true in Rust.
Reproducer
I tried this code:
#![feature(no_core)]
#![no_core]
extern "C" {
fn malloc(n: u64) -> *mut u8;
}
pub fn f() -> i32 {
unsafe {
let p = malloc(4) as *mut i32;
*p = 27;
*(p as *mut i16) = 42;
*p
}
}
pub fn main() -> i32 {
f()
}
Does the code make use of any (1.49) nightly feature ?
Godbolt link
https://godbolt.org/z/889vTb4eK
Actual behavior
The current behaviour is that gccrs ignores writes to *p through *mut i16 pointers when compiling with optimisations (-O2 and above): f returns 27 (like the equivalent C code when compiled with GCC 15.2). This is incorrect in Rust.
Expected behavior
f should return 42.
GCC Version
679aad3
Summary
gccrs seems to compile pointers/pointer casts with C’s strict aliasing/type-based alias analysis (TBAA) semantics, i. e. with the assumption that a pointer to type
Tcannot alias a pointer to typeUforT != U. This is not true in Rust.Reproducer
I tried this code:
Does the code make use of any (1.49) nightly feature ?
Godbolt link
https://godbolt.org/z/889vTb4eK
Actual behavior
The current behaviour is that gccrs ignores writes to
*pthrough*mut i16pointers when compiling with optimisations (-O2and above):freturns27(like the equivalent C code when compiled with GCC 15.2). This is incorrect in Rust.Expected behavior
fshould return42.GCC Version
679aad3