-
Notifications
You must be signed in to change notification settings - Fork 3.4k
brk() implementation is racy #10006
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
Comments
Good point, we made sbrk threadsafe but not brk, it looks like. Worth fixing, but in most cases brk is not called directly, and both malloc and sbrk are safe, so low-risk. But I opened #10008 to show a clear error in a pthreads build. |
Shouldn't brk now be considered thread-safe considering that it only invokes sbrk which has been made thread-safe since commit d15f2 ? Thus, the only modification needed is removing the #if directive on condition EMSCRIPTEN_PTHREADS . |
@Albirair I think it is still not thread-safe. |
emscripten-core#10008) See emscripten-core#10006 - this will at least get us a clear error from a user that hits this, until we fix the issue properly.
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant. |
Emscripten moved to implement
brk()
in C instead of JS, with implementationHowever the implementation can race in multithreaded setting and produce an incorrect sbrk bump.
Suppose heap size is 2MB, and thread A is calling
brk(4MB)
.last
will come out at2MB
, so thread A is about to callsbrk(4MB-2MB)
to increase the heap by two megabytes, but let's suppose it pauses there before the call.Meanwhile, suppose thread B is also calling
brk(4MB)
to set the heap size to the same size. It computes that it should bump the size by 2MB, so it should also callsbrk(4MB-2MB)
. Now both thread A and thread B will be calling to bump break limit up by 2 megabytes. They both succeed, and heap will grow from 2MB -> 4MB -> 6MB. But both threads A and B had initially issued a callbrk(4MB)
to set the heap limit to the same 4MB ceiling!I am not running into this issue in practice, but just noticed this when glancing over the implemented C code.
The text was updated successfully, but these errors were encountered: