rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
893 stars 61 forks source link

Make sure the GCC backend does not assume "pointer lifetime end zap" rules #517

Open RalfJung opened 1 month ago

RalfJung commented 1 month ago

C has some very peculiar UB. For instance, when an allocation gets freed, then all pointers to that allocation become indeterminate values. This is sometimes called "pointer lifetime end zap". This means the following code has UB in C:

int *x = malloc(100);
free(x);
// Looking at x again is UB!
if ((uintptr_t)x == 1024) { printf("Hello!\n"); }

Obviously, the equivalent code in Rust is well-defined. Therefore, it would be good to make sure that whatever parts of GCC the backend is using are aware that Rust does not have pointer-lifetime-end-zap. I am somewhat concerned that GCC is modeled closely after C semantics and hence implicitly applies rules like that -- but I don't know how one would even begin to figure this out. Is there something like a LangRef for the GCC IR(s)?