immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.81k stars 220 forks source link

analyze: rewriting sometimes produces non-Sync, non-mut statics #953

Closed spernsteiner closed 1 year ago

spernsteiner commented 1 year ago

Input:

static mut FOO: *mut u8 = std::ptr::null_mut();

We currently remove the mut, producing the following output:

static FOO: *mut u8 = std::ptr::null_mut();

However, this results in a compile error: *mut u8 is not Sync, so it can't be used as the type of a non-mut static. Note that mutable statics are exempt from this check, so the input code does not trigger a compile error.

fw-immunant commented 1 year ago

We do this if we see no writes to the static in the program. Types that don't admit writes are Sync, so it would be preferable (and sufficient to enable compilation) to change the type and rewrite use sites to match:

static FOO: Option<&mut u8> = None;

But we don't at present rewrite pointer types for statics, which seems like the right way to fix this.

aneksteind commented 1 year ago

@spernsteiner would one temporary solution be to mark these FIXED to address other more fundamental forms of rewritten-code compilation errors, and then come back to this?

aneksteind commented 1 year ago

is this a duplicate of #907?

spernsteiner commented 1 year ago

Closing as a duplicate of #907