Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUnix: RwLock is incorrect #53127
Comments
This comment has been minimized.
This comment has been minimized.
|
I based my statements on http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_wrlock.html. @sfackler points out that at https://linux.die.net/man/3/pthread_rwlock_wrlock, behavior for re-acquiring the write lock is defined as "may deadlock" -- but not UB. However, we also use this implementation on non-Linux platforms, e.g. macOS. |
This comment has been minimized.
This comment has been minimized.
|
macOS does explicitly identify the behavior as undefined. |
RalfJung
referenced this issue
Aug 7, 2018
Open
RwLock and Mutex on Window theoretically allows undefined behavior in safe code #35836
This comment has been minimized.
This comment has been minimized.
|
FWIW, I think we should stop using the platform-specific implementations, and just roll our own based on thread (un)parking. |
sfackler
added
I-nominated
T-libs
labels
Aug 29, 2018
This comment has been minimized.
This comment has been minimized.
|
cc @alexcrichton - we talked about this a bit during RustConf. |
This comment has been minimized.
This comment has been minimized.
|
I agree that we're basically at the point that there's a ridiculous amount of constraints from OS primitives that the only program which doesn't have undefined behavior is correct ones, and we all know how often we write correct programs. All these discussions are spread out over a number of issues though and PRs, and it's really difficult to keep it all in my own head at least. "This is UB" is a very strong motivation for reimplementing the standard library's primitives, but I think the scale of a change such as this would probably want to go through the RFC process. Such an RFC would be a great location to centralize all the various hazards of OS primitives and how unreasonable it is for us to try to appease all possible implementations. That in turns provides excellent motivation for alternative strategies. |
This comment has been minimized.
This comment has been minimized.
|
You mean like this RFC? rust-lang/rfcs#1632 |
This comment has been minimized.
This comment has been minimized.
|
Correction: Even some correct programs are UB. Reentrancy on the read lock of an Whether reentrancy that leads to deadlocks is "correct" is open for interpretation, I guess ;) but anyway, it is safe, and hence "correct enough" for UB to be critical. |
This comment has been minimized.
This comment has been minimized.
|
I've opened an internals post to continue more long-form discussion on the topic of continuing to fix these issues. |
RalfJung commentedAug 6, 2018
•
edited
I think the
sys::unix::rwlockimplementation is incorrect in the sense that it has undefined behavior, for two reasons:The access toThis is fixed.write_lockedis not properly synchronized: Inread, we accesswrite_lockedeven ifpthread_rwlock_rdlockfailed.write. If we really want to use POSIX rwlocks, I think we have to implement a reentrancy detector. (And then maybe we also want to use that for mutex, so that we can use the static initializer and the most efficient code path?)