Skip to content

Fill correct stack info in pthread_getattr_np stub #23887

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions system/lib/pthread/library_pthread_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,6 @@ int pthread_condattr_setpshared(pthread_condattr_t *attr, int shared) {
return 0;
}

int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) {
return 0;
}

int pthread_setcancelstate(int state, int* oldstate) {
return 0;
}
Expand Down
7 changes: 7 additions & 0 deletions system/lib/pthread/pthread_self_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "pthread_impl.h"
#include <unistd.h>
#include <emscripten/stack.h>

static struct pthread __main_pthread;

Expand All @@ -24,8 +25,14 @@ pthread_t emscripten_main_runtime_thread_id() {
return &__main_pthread;
}

extern int __stack_high;
extern int __stack_low;

__attribute__((constructor))
static void init_pthread_self(void) {
__main_pthread.locale = &libc.global_locale;
__main_pthread.tid = getpid();
__main_pthread.stack = &__stack_high;
__main_pthread.stack_size = ((size_t)&__stack_high) - ((size_t)&__stack_low);
__main_pthread.guard_size = __default_guardsize;
}
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 380,
"a.js": 7030,
"a.js.gz": 3022,
"a.wasm": 9119,
"a.wasm.gz": 4701,
"total": 16701,
"total_gz": 8103
"a.wasm": 9155,
"a.wasm.gz": 4724,
"total": 16737,
"total_gz": 8126
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 12442,
"a.html.gz": 6793,
"total": 12442,
"total_gz": 6793
"a.html": 12494,
"a.html.gz": 6812,
"total": 12494,
"total_gz": 6812
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 17205,
"a.html.gz": 7516,
"total": 17205,
"total_gz": 7516
"a.html": 17262,
"a.html.gz": 7542,
"total": 17262,
"total_gz": 7542
}
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129166
129206
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like a 40 byte increase is more than I would expect from this change.. I was hoping for more like 6 bytes (2 bytes each for the 3 extra store instructions).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well we also added pthread_getattr_np.c which looks like:

int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
{
	*a = (pthread_attr_t){0};
	a->_a_detach = t->detach_state>=DT_DETACHED;
	a->_a_guardsize = t->guard_size;
	a->_a_stackaddr = (uintptr_t)t->stack;
	a->_a_stacksize = t->stack_size;
	return 0;
}

Presumably the other 34 bytes come from that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

But that file should not linking into any of those tests. It should only be linked into the (rare) programs that use it.

2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128578
128599
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170787
170827
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144542
144582
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142117
142157
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122011
122044
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
232560
232600
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131729
131769
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169089
169129
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15105
15170
18 changes: 18 additions & 0 deletions test/other/test_pthread_stub.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <stdint.h>
Expand Down Expand Up @@ -29,6 +30,7 @@ void* start_pthread(void* arg) {
#define CHECK_C11_FAIL(X) CHECK_C11(X, thrd_nomem)

void test_c11_threads() {
printf("test_c11_threads\n");
int rtn;
int res;

Expand All @@ -45,6 +47,7 @@ void test_c11_threads() {
}

void test_pthreads() {
printf("test_pthreads\n");
int rtn;
int res;

Expand All @@ -59,8 +62,23 @@ void test_pthreads() {
pthread_cleanup_pop(1);
}

void test_pthread_getattr_np() {
printf("test_pthread_getattr_np\n");
int rtn;
pthread_attr_t attr;
size_t stack_size, guard_size;
void *stack_addr;

CHECK_SUCCESS(pthread_getattr_np(pthread_self(), &attr));
CHECK_SUCCESS(pthread_attr_getguardsize(&attr, &guard_size));
CHECK_SUCCESS(pthread_attr_getstack(&attr, &stack_addr, &stack_size));
CHECK_SUCCESS(pthread_attr_destroy(&attr));
printf("stack_addr: %p, stack_size: %zu, guard_size: %zu\n", stack_addr, stack_size, guard_size);
}

int main() {
test_c11_threads();
test_pthreads();
test_pthread_getattr_np();
return 0;
}
4 changes: 4 additions & 0 deletions test/other/test_pthread_stub.out
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
test_c11_threads
test_pthreads
cleanup: 42
test_pthread_getattr_np
stack_addr: 0, stack_size: 65536, guard_size: 8192
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15105
15170
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12186
12251
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15105
15170
1 change: 1 addition & 0 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ def get_files(self):
'pthread_attr_setscope.c',
'pthread_attr_setstack.c',
'pthread_attr_setstacksize.c',
'pthread_getattr_np.c',
'pthread_getconcurrency.c',
'pthread_getcpuclockid.c',
'pthread_getschedparam.c',
Expand Down