-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: Two-layer Option is folded and may lose information. #81
Comments
This does sound like a bug, yes. But can you explain how this is unsound? As in, is there an undefined behavior involved anywhere? |
I use
I'm don't see how just using this library can lead to safety issues. But theoretically manual impl of unsafe methods in |
Yeh, that statement from him implicitly contains „Cannot be misused in safe rust“ ‒ or at least, that's how I read the whole post and how most of the Rust folks define soundness. Unsound is something you can make to do undefined behavior without using any So, in that light (unless you can show me a code not using unsafe that not only misbehaves, but causes UB), I'll rename the bug accordingly. I'll of course look into what to do about it. Your suggestion for a fix looks a bit semver-breaking, but if we are lucky, I think I had some mechanism in place which could work. |
ArcSwapAny
I've found pretty sketchy, but working solution:
To be fair I kinda disagree with that description. David Tolnay says nothing about UB, only talking about invariants, this includes logical invariants. And this crate exposes API that can be misused in a way, that clearly breaks those logical invariants. I think it's very important to draw distinction between implementation details issues (bugs) and leaky abstractions (unsoundness).
|
That solution is interesting, I'll keep that at hand. I'm still thinking if I can actually make the nested Option work, instead of forbidding it. Not sure what it would be good for and not sure if it's possible (well, I suspect it is not, which irks me), but will give it a try first. |
I don't think this is possible. Single
I've tried to interpret
We just swap |
Actually, I'm starting to think there are 😈. The pointers that come from Arc and Rc are necessarily aligned (because the pointee holds not only the actual value, but also the ref counts and these are Another option would be, to somehow make a reserved value in the code (eg. a I need to first think it through and try it, to see if there's impact on performance and to think if this could break someone's code or not. And if I would break something inside the implementation. |
Sound scary, but doable 😈
I guess this would require |
I'm not sure how soon I'll get around to the experiments. So I'll write down few notes that could help if someone is to try this out: The unaligned addresses trick
The registration of addressActually, I don't think I need any Default or MaybeUninit. I don't need a value of the type. I need a unique address and that address could be of something completely else. AFAIK pointers can point to the wrong type (needs checking) as long as they are never used as pointers (dereferenced/for pointer arithmetic). What I worry about, though, is ‒ even if I could have some kind of |
I'm digging through the code. So far, I've come to a conclusion it is possible using the unaligned address trick and a plan how it could be done. So far I haven't come up with an exact implementation that wouldn't be somewhat ugly and entangled with the rest. The observations:
Now, there's also the compare-and-swap operation, which I haven't started taking apart yet. But considering that one is only lock-free, not wait-free (eg. it can loop and wait for the time when stuff doesn't change under its hands), it should be possible and likely even easy. Not sure when I manage to find more time to actually implement it (again, folks are welcome to give it a try, based on these notes or using their own approach). |
Because of recursive impl of
RefCnt
onOption
, it's valid to constructArcSwapAny::<Option<Option<Arc<i32>>>>
which doesn't see a difference betweenNone
andSome(None)
.playground
Least disruptive solution would be just to implement
RefCnt
onArc<T>
,Option<Arc<T>>
and mention in the docs, that to implement this trait on your ownArc
, you'll have to use customOption
.The text was updated successfully, but these errors were encountered: