Skip to content

Commit d6ebc74

Browse files
authored
Fill correct stack info in pthread_getattr_np stub (#23887)
See use case here: https://github.com/python/cpython/pull/130398/files#diff-c22186367cbe20233e843261998dc027ae5f1f8c0d2e778abfa454ae74cc59deR365-R380 I guess for stack protection, this only enables introspecting whether we run out of the spill stack. I'm not sure whether to expect running out of spill stack or running out of true stack to be more common. But at least this unbreaks the code.
1 parent f4fb3cd commit d6ebc74

21 files changed

+55
-29
lines changed

system/lib/pthread/library_pthread_stub.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,6 @@ int pthread_condattr_setpshared(pthread_condattr_t *attr, int shared) {
276276
return 0;
277277
}
278278

279-
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) {
280-
return 0;
281-
}
282-
283279
int pthread_setcancelstate(int state, int* oldstate) {
284280
return 0;
285281
}

system/lib/pthread/pthread_self_stub.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "pthread_impl.h"
99
#include <unistd.h>
10+
#include <emscripten/stack.h>
1011

1112
static struct pthread __main_pthread;
1213

@@ -24,8 +25,14 @@ pthread_t emscripten_main_runtime_thread_id() {
2425
return &__main_pthread;
2526
}
2627

28+
extern int __stack_high;
29+
extern int __stack_low;
30+
2731
__attribute__((constructor))
2832
static void init_pthread_self(void) {
2933
__main_pthread.locale = &libc.global_locale;
3034
__main_pthread.tid = getpid();
35+
__main_pthread.stack = &__stack_high;
36+
__main_pthread.stack_size = ((size_t)&__stack_high) - ((size_t)&__stack_low);
37+
__main_pthread.guard_size = __default_guardsize;
3138
}

test/code_size/embind_val_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 380,
44
"a.js": 7030,
55
"a.js.gz": 3022,
6-
"a.wasm": 9119,
7-
"a.wasm.gz": 4701,
8-
"total": 16701,
9-
"total_gz": 8103
6+
"a.wasm": 9155,
7+
"a.wasm.gz": 4724,
8+
"total": 16737,
9+
"total_gz": 8126
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 12442,
3-
"a.html.gz": 6793,
4-
"total": 12442,
5-
"total_gz": 6793
2+
"a.html": 12494,
3+
"a.html.gz": 6812,
4+
"total": 12494,
5+
"total_gz": 6812
66
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 17205,
3-
"a.html.gz": 7516,
4-
"total": 17205,
5-
"total_gz": 7516
2+
"a.html": 17262,
3+
"a.html.gz": 7542,
4+
"total": 17262,
5+
"total_gz": 7542
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
129166
1+
129206
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
128578
1+
128599
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
170787
1+
170827
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
144542
1+
144582
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
142117
1+
142157
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
122011
1+
122044
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
232560
1+
232600
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
131729
1+
131769
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
169089
1+
169129
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15105
1+
15170

test/other/test_pthread_stub.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define _GNU_SOURCE
12
#include <assert.h>
23
#include <errno.h>
34
#include <stdint.h>
@@ -29,6 +30,7 @@ void* start_pthread(void* arg) {
2930
#define CHECK_C11_FAIL(X) CHECK_C11(X, thrd_nomem)
3031

3132
void test_c11_threads() {
33+
printf("test_c11_threads\n");
3234
int rtn;
3335
int res;
3436

@@ -45,6 +47,7 @@ void test_c11_threads() {
4547
}
4648

4749
void test_pthreads() {
50+
printf("test_pthreads\n");
4851
int rtn;
4952
int res;
5053

@@ -59,8 +62,23 @@ void test_pthreads() {
5962
pthread_cleanup_pop(1);
6063
}
6164

65+
void test_pthread_getattr_np() {
66+
printf("test_pthread_getattr_np\n");
67+
int rtn;
68+
pthread_attr_t attr;
69+
size_t stack_size, guard_size;
70+
void *stack_addr;
71+
72+
CHECK_SUCCESS(pthread_getattr_np(pthread_self(), &attr));
73+
CHECK_SUCCESS(pthread_attr_getguardsize(&attr, &guard_size));
74+
CHECK_SUCCESS(pthread_attr_getstack(&attr, &stack_addr, &stack_size));
75+
CHECK_SUCCESS(pthread_attr_destroy(&attr));
76+
printf("stack_addr: %p, stack_size: %zu, guard_size: %zu\n", stack_addr, stack_size, guard_size);
77+
}
78+
6279
int main() {
6380
test_c11_threads();
6481
test_pthreads();
82+
test_pthread_getattr_np();
6583
return 0;
6684
}

test/other/test_pthread_stub.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
test_c11_threads
2+
test_pthreads
13
cleanup: 42
4+
test_pthread_getattr_np
5+
stack_addr: 0, stack_size: 65536, guard_size: 8192
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15105
1+
15170
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12186
1+
12251
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15105
1+
15170

tools/system_libs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ def get_files(self):
11681168
'pthread_attr_setscope.c',
11691169
'pthread_attr_setstack.c',
11701170
'pthread_attr_setstacksize.c',
1171+
'pthread_getattr_np.c',
11711172
'pthread_getconcurrency.c',
11721173
'pthread_getcpuclockid.c',
11731174
'pthread_getschedparam.c',

0 commit comments

Comments
 (0)