immunant / c2rust

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

analyze: removing `mut` from `static` introduces Sync errors #907

Open spernsteiner opened 1 year ago

spernsteiner commented 1 year ago

static mut X: *mut T = ...; is legal, but static X: *mut T = ...; is not; the latter triggers the error "*mut T cannot be shared between threads safely", since *mut T does not implement Sync.

This happens in several places in lighttpd (33 errors)

fw-immunant commented 1 year ago

If these statics are never written to (as should be the precondition for removing mut), what value of *mut type are they being initialized to? We might just be able to rewrite to static X: &'static T = ...;.

It's worth noting that *const has the same problem--we need a non-raw pointer here.

spernsteiner commented 1 year ago

what value of *mut type are they being initialized to?

Mostly null, but I see one that's an array of string literals (devices, in li_rand_device_bytes) and another that's a pointer to another static (log_errh).

kkysen commented 1 year ago

If they're not mut and initialized to null, then what use are they in the first place?

spernsteiner commented 1 year ago

I checked a few of the nulls manually and they're modified, but only in functions that we fail to analyze at the moment.

kkysen commented 1 year ago

That means we have to assume a failing function modifies all globals, right? Or something similar (maybe we could do a more cursory check to see if it's referenced at all in that function). Or is this something that might be more worthwhile to fix in the transpiler (translate static const as static not static mut)?