Skip to content

Commit dc7acaa

Browse files
authored
Fix for calling pthread_getattr_np on the main thread (emscripten-core#15540)
1 parent b347ced commit dc7acaa

7 files changed

Lines changed: 70 additions & 58 deletions

File tree

system/include/emscripten/stack.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ uintptr_t emscripten_stack_get_base(void);
2222

2323
// Returns the end address of the wasm stack. This is the address that the stack
2424
// pointer would point to when the whole stack is in use. (the address pointed
25-
// to by the end is not part of the stack itself) Note that in fastcomp, the
26-
// stack grows up, whereas in wasm backend, it grows down. So with wasm
27-
// backend, the address returned by emscripten_stack_get_end() is smaller than
25+
// to by the end is not part of the stack itself). Note that the stack grows
26+
// down so the address returned by emscripten_stack_get_end() is smaller than
2827
// emscripten_stack_get_base().
2928
uintptr_t emscripten_stack_get_end(void);
3029

system/lib/pthread/library_pthread.c

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

3333
#include <emscripten.h>
3434
#include <emscripten/threading.h>
35+
#include <emscripten/stack.h>
3536

3637
void __pthread_testcancel();
3738

@@ -836,6 +837,8 @@ void __emscripten_init_main_thread(void) {
836837
// The pthread struct has a field that points to itself - this is used as
837838
// a magic ID to detect whether the pthread_t structure is 'alive'.
838839
__main_pthread.self = &__main_pthread;
840+
__main_pthread.stack = (void*)emscripten_stack_get_base();
841+
__main_pthread.stack_size = emscripten_stack_get_base() - emscripten_stack_get_end();
839842
__main_pthread.detach_state = DT_JOINABLE;
840843
// pthread struct robust_list head should point to itself.
841844
__main_pthread.robust_list.head = &__main_pthread.robust_list.head;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#define _GNU_SOURCE
7+
8+
#include <assert.h>
9+
#include <inttypes.h>
10+
#include <pthread.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <unistd.h>
14+
15+
void TestStack() {
16+
pthread_attr_t attr;
17+
int rc;
18+
void *stbase;
19+
size_t stsize;
20+
char dummy;
21+
intptr_t result;
22+
23+
rc = pthread_attr_init(&attr);
24+
assert(rc == 0);
25+
26+
rc = pthread_getattr_np(pthread_self(), &attr);
27+
assert(rc == 0);
28+
29+
rc = pthread_attr_getstack(&attr, &stbase, &stsize);
30+
assert(rc == 0);
31+
32+
printf("size=%zu base=%p base+size=%p dummy=%p\n", stsize, stbase, (char*)stbase + stsize, &dummy);
33+
34+
assert(&dummy >= (char*)stbase);
35+
assert(&dummy <= (char*)stbase + stsize);
36+
}
37+
38+
void *ThreadMain(void *arg) {
39+
TestStack();
40+
return NULL;
41+
}
42+
43+
int main() {
44+
// Run TestStack both on the main thread and on a secondary thread
45+
TestStack();
46+
pthread_t thread;
47+
int rc;
48+
49+
rc = pthread_create(&thread, NULL, ThreadMain, NULL);
50+
assert(rc == 0);
51+
52+
rc = pthread_join(thread, NULL);
53+
assert(rc == 0);
54+
55+
printf("done\n");
56+
return 0;
57+
}

tests/pthread/test_pthread_attr_getstack.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
done

tests/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3899,7 +3899,7 @@ def test_pthread_mutex(self):
38993899

39003900
@requires_threads
39013901
def test_pthread_attr_getstack(self):
3902-
self.btest_exit(test_file('pthread/test_pthread_attr_getstack.cpp'), args=['-s', 'USE_PTHREADS', '-s', 'PTHREAD_POOL_SIZE=2'])
3902+
self.btest_exit(test_file('pthread/test_pthread_attr_getstack.c'), args=['-s', 'USE_PTHREADS', '-s', 'PTHREAD_POOL_SIZE=2'])
39033903

39043904
# Test that memory allocation is thread-safe.
39053905
@requires_threads

tests/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,12 @@ def test_pthread_setspecific_mainthread(self):
24422442
print('.. pthread_exit')
24432443
self.do_run_in_out_file_test('pthread/test_pthread_setspecific_mainthread.c')
24442444

2445+
@node_pthreads
2446+
def test_pthread_attr_getstack(self):
2447+
self.set_setting('EXIT_RUNTIME')
2448+
self.set_setting('PTHREAD_POOL_SIZE', 1)
2449+
self.do_run_in_out_file_test('pthread/test_pthread_attr_getstack.c')
2450+
24452451
@node_pthreads
24462452
@no_mac('https://github.com/emscripten-core/emscripten/issues/15014')
24472453
def test_pthread_abort(self):

0 commit comments

Comments
 (0)