3 ms·
Yes, I do---we experienced it in QEMU and the workaround is hideous (https://patchew.org/QEMU/20211201170120.286139-1-stefanha@redhat.com/ https://patchew.org/Q
by bonzini 4y ago
Yes, I do---we experienced it in QEMU and the workaround is hideous (https://patchew.org/QEMU/20211201170120.286139-1-stefanha@redhat.com/ https://patchew.org/QEMU/20211201170120.286139-1-stefanha@re...). What happens is that errno can be compiled to
address_of_errno = &errno;
...
foo = *address_of_errno;
The compiler can assume that the address stays the same across the whole execution of the function and cache it in a register. Unfortunately, if you can change threads across yield points, you can end up reading the errno of the wrong function.
Instead, C++ stackless coroutines explicitly promise that you will read the TLS of the currently executing thread in this case.
This is what we found, but I cannot rule out that this is the only possibility.
- OskarS 4y agoThat's really interesting, I hadn't considered the case of the compiler caching the address of errno. Obviously it can't cache the value across a function call (with or without a yield point), but caching the address does make sense, and I see how that would blow up if the coroutine was scheduled on a different thread. I'd argue that the problem here is that you should obviously not use TLS with corutines, since they can be scheduled on any thread. The concept of thread-local storage just doesn't make much sense for coroutines. The issue isn't necessarily that there's anything wrong with coroutines, it's rather with APIs that use TLS as an information side-channel. errno is just a bad idea, basically. But point taken.
- bonzini 4y agoUsing TLS in coroutines may not make much sense, but using TLS in the runtime can be unavoidable. For example, the stack of running coroutines is per-thread. You can perhaps avoid that with a more complex API, but placing it in TLS is the obvious design. Unfortunately, this has happened and is not hypothetical, but at least for now we're stuck in the stackful hole we dug for ourselves. :( A few years back we had similar issues on Windows.