Skip to content

[pthread] Honor guard size in pthread_create - #27268

Merged
sbc100 merged 2 commits into
emscripten-core:mainfrom
sbc100:pthread-guard-size
Jul 10, 2026
Merged

[pthread] Honor guard size in pthread_create#27268
sbc100 merged 2 commits into
emscripten-core:mainfrom
sbc100:pthread-guard-size

Conversation

@sbc100

@sbc100 sbc100 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Honor requested/default guard size in pthread_create by reserving guard_size bytes between thread control data/TSD/TLS and the usable stack.

Although we cannot set memory protection on this region like a native platform this can still be useful for catching and reporting overflows that are smaller than the guard region itself. Without this change -sSTACK_OVERFLOW_CHECK=1 is mostly useless for pthreads because any kind of overflow is likely to corrupt the stack before being reported.

Add test cases covering guard size queries via pthread_getattr_np and stack overflow detection within guard region.

Fixes: #27266

@sbc100
sbc100 requested a review from kleisauke July 7, 2026 19:46
@sbc100
sbc100 force-pushed the pthread-guard-size branch from 9316c51 to 1b52448 Compare July 7, 2026 20:21
@sbc100
sbc100 requested a review from kripken July 7, 2026 20:22
@sbc100
sbc100 force-pushed the pthread-guard-size branch from 1b52448 to c2c362f Compare July 7, 2026 22:01
@sbc100

sbc100 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

ping @kripken, should be a quick one

@kripken

kripken commented Jul 9, 2026

Copy link
Copy Markdown
Member

😆 Not sure how this can be quick, I need to page back in all the stuff about how memory is managed here. Do we have that documented somewhere?

@sbc100

sbc100 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

😆 Not sure how this can be quick, I need to page back in all the stuff about how memory is managed here. Do we have that documented somewhere?

The public documenation for this is https://man7.org/linux/man-pages/man3/pthread_attr_setguardsize.3.html.

There are not really docs on how its implemented internally. The basic idea is fairly simple though: Each pthread has a guard region at the end of its stack (to detect and catch overflows). The size of the region is even controllable on a per-thread bases. Prior to this change we simply ignored this. Now we honor it. Its 8k by default:

#ifdef __EMSCRIPTEN__
// Keep in sync with DEFAULT_PTHREAD_STACK_SIZE in settings.js
#define DEFAULT_STACK_SIZE (64*1024)
#else
#define DEFAULT_STACK_SIZE 131072
#endif
#define DEFAULT_GUARD_SIZE 8192

This means per-thread overhead now defaults to 64k+8k rather than just 64k, but all of that is contollable at pthread-creation, these are just the default.

Since we don't have memory protection you could argue that maybe we only want to enable this in debug builds (where we have the basic stack checking enabled). But I'm not sure its worth the difference.

@sbc100

sbc100 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

Perhaps @stevenwdv can help review too?

Comment thread system/lib/pthread/pthread_create.c Outdated
Comment thread system/lib/pthread/pthread_create.c Outdated
@sbc100
sbc100 requested a review from kleisauke July 10, 2026 16:02
@sbc100
sbc100 force-pushed the pthread-guard-size branch from c2c362f to e4da702 Compare July 10, 2026 16:02
@sbc100

sbc100 commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator Author

@kripken are you ok with @kleisauke's review here, or would you like to also approve?

@kripken

kripken commented Jul 10, 2026

Copy link
Copy Markdown
Member

Feel free to land, @kleisauke knows this code much better than me!

@sbc100
sbc100 force-pushed the pthread-guard-size branch from e4da702 to 8ed2e0b Compare July 10, 2026 18:10
@sbc100
sbc100 enabled auto-merge (squash) July 10, 2026 18:10
sbc100 added 2 commits July 10, 2026 13:38
Honor requested/default guard size in pthread_create by reserving
guard_size bytes between thread control data/TSD/TLS and the usable
stack.

Although we cannot set memory protection on this region like a native
platform this can still be useful for catching and reporting overflows
that are smaller than the guard region itself.  Without this change
`-sSTACK_OVERFLOW_CHECK=1` is mostly useless for pthreads because
any kind of overflow is likely to corrupt the stack before being
reported.

Add test cases covering guard size queries via pthread_getattr_np and
stack overflow detection within guard region.

Fixes: emscripten-core#27266
@sbc100
sbc100 force-pushed the pthread-guard-size branch from 8ed2e0b to 6aab1f1 Compare July 10, 2026 20:42
@sbc100
sbc100 disabled auto-merge July 10, 2026 21:28
@sbc100
sbc100 merged commit 17b8584 into emscripten-core:main Jul 10, 2026
33 of 39 checks passed
@sbc100
sbc100 deleted the pthread-guard-size branch July 10, 2026 21:28
@stevenwdv

Copy link
Copy Markdown
Contributor

Perhaps @stevenwdv can help review too?

Sorry for not responding, I was working on other stuff. In any case, I don't think my review would be of much use, since I don't know all the Pthread inners... I'm glad this is implemented now, though!

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.

STACK_OVERFLOW_CHECK does not work with pthreads

4 participants