Skip to content

Halt on calling brk() in a pthreads build, as it is not threadsafe yet - #10008

Merged
kripken merged 2 commits into
incomingfrom
brk
Dec 12, 2019
Merged

kripken merged 2 commits into
incomingfrom
brk

Conversation

@kripken

@kripken kripken commented Dec 11, 2019

Copy link
Copy Markdown
Member

See #10006

@kripken
kripken requested a review from dschuff December 12, 2019 00:28
Comment thread system/lib/sbrk.c
}

int brk(intptr_t ptr) {
#if __EMSCRIPTEN_PTHREADS__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could even make this work only on the main thread (but not background threads) if we wanted.

@kripken
kripken merged commit d15f2c5 into incoming Dec 12, 2019
@kripken
kripken deleted the brk branch December 12, 2019 00:49
@VirtualTim

VirtualTim commented Dec 12, 2019 •

Copy link
Copy Markdown
Collaborator

I don't really like this change.
If brk is called from malloc this will abort, even though it's safe to do so.
Also my understanding of the impact of the racy-ness is that it's quite low. It it is hit the impact is that the heap is increased by more than it should have been, but nothing will really be broken.

What do you think about putting this behind ASSERTIONS? Or am I underestimating the impact?

Edit: I was mistaken, see below comments.

Comment thread system/lib/sbrk.c
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These newly added headers should probably also be behind an #if __EMSCRIPTEN_PTHREADS__.

@dschuff

dschuff commented Dec 12, 2019

Copy link
Copy Markdown
Member

brk is not actually called by malloc though, it's sbrk. IIUC any calls of brk would be in user code, which means that the user would have particular expectations (i.e. they'd expect it to work, which it might not).

@kripken

kripken commented Dec 12, 2019

Copy link
Copy Markdown
Member Author

Correct, our default malloc doesn't call brk, only sbrk.

@VirtualTim This patch just avoids users having silent memory races or other issues, if they ship their own allocation code that does use brk, until we fix this properly.

@kripken

kripken commented Dec 12, 2019

Copy link
Copy Markdown
Member Author

It is true that if the user ships a malloc that is threadsafe (like dlmalloc), and that does use brk, then this PR would make it fail. But we can't tell the custom allocator is safe.

kripken added a commit that referenced this pull request Dec 12, 2019
@VirtualTim

Copy link
Copy Markdown
Collaborator

OK, I guess I misunderstood the impact. If this only affects brk called from user code then this change is good.

belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants