dtolnay / linkme

Safe cross-platform linker shenanigans
Apache License 2.0
645 stars 41 forks source link

Non-ZST stop static is probably unsound #98

Open GoldsteinE opened 2 months ago

GoldsteinE commented 2 months ago

Currently linkme generates an extern static of Element type for the “stop section” symbol:

https://github.com/dtolnay/linkme/blob/0e5e28bd673014b2b8d0a7337eab9489f74d2624/impl/src/declaration.rs#L139-L143

As pointed out in https://github.com/rust-lang/rust/issues/46188#issuecomment-2295924689, statics are considered always-dereferenceable, so it’s probably unsound. As the stop static is never actually read from, I think it could be made zero-sized to remove the unsoundness?

RalfJung commented 2 months ago

Yes, this is indeed unsound and should be made zero-sized.

LINKME_START is also unsound if the array ends up being empty.

GoldsteinE commented 2 months ago

Yes, this is indeed unsound and should be made zero-sized.

Are zero-sized extern statics portable? I’m unsure because C/++ doesn’t have ZSTs, so I don’t know if LLVM portably supports it.

RalfJung commented 2 months ago

Yes they are. LLVM supports them just fine.