c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

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

Open spernsteiner opened this issue 2 years ago • 5 comments

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)

spernsteiner avatar May 02 '23 19:05 spernsteiner

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.

fw-immunant avatar May 02 '23 19:05 fw-immunant

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).

spernsteiner avatar May 03 '23 16:05 spernsteiner

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

kkysen avatar May 03 '23 16:05 kkysen

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

spernsteiner avatar May 03 '23 16:05 spernsteiner

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)?

kkysen avatar May 03 '23 16:05 kkysen