Use no_std-friendly spin::Mutex to avoid unsafe for static mut.#3834
Use no_std-friendly spin::Mutex to avoid unsafe for static mut.#3834anforowicz wants to merge 1 commit into
no_std-friendly spin::Mutex to avoid unsafe for static mut.#3834Conversation
|
So I think this PR makes sense in general, thanks! The issue I see is that spinlocks introduce overhead. Since this locks on deserialize, finding a single new testcase will hit the registry of all nodes that will unpack this. This may add up? Especially if this introduces some sort of numa caching stuff, etc... I just don't know. Since LibAFL runs single-threaded (multi-process) by design, for our use case this might be a net negative? It would be interesting to either benchmark this, or simply add a safe variant for other crates that demand this. Note that the deserializer (after the registry is done) should technically not need a full exclusive lock, but a RwLock, so that multiple threads (that we don't have, but anyway) can use it concurrently... I'll leave this to smarter people like @addisoncrump |
Description
When reviewing the crate when importing it into my project, I've noticed that some of the
unsafeusage may be removed by using aMutex<RefCell<...>>. Sincecrates/serde_anymap/src/lib.rsdeclares itself as#![no_std], we can't usestd::sync::Mutex, but it seems thatspin::Mutexis ano_std-friendly, relatively-widely-used alternative.Things were okay before this PR, but removing
unsafestill seems desirable:create_register!macro had a safety comment explaining how "registercall will always run at startup and never in parallel". After this PR maintainers and auditors don't have to think about it anymore (i.e. to reason how exactly the "always run at startup" guarantee is upheld).RegistryBuilder::registermay have to be called manually, requiring the user of LibAFL to uphold the concurrency guarantees. After this PR the user of LibAFL still has to ensure that registration is done before serialization, but failure to synchronize can't cause undefined behavior (I.e. this PR removes one reason why users of LibAFL may have had to useunsafeblocks in the past).Checklist
./scripts/precommit.shand addressed all comments