linkme icon indicating copy to clipboard operation
linkme copied to clipboard

Non-ZST stop static is probably unsound

Open GoldsteinE opened this issue 1 year ago • 3 comments

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?

GoldsteinE avatar Aug 19 '24 08:08 GoldsteinE

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

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

RalfJung avatar Aug 19 '24 09:08 RalfJung

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.

GoldsteinE avatar Aug 19 '24 09:08 GoldsteinE

Yes they are. LLVM supports them just fine.

RalfJung avatar Aug 19 '24 09:08 RalfJung