From 4b1e60d119e6d16a7a02754897419c3b23c231f3 Mon Sep 17 00:00:00 2001 From: Shravan Narayan Date: Mon, 24 Mar 2025 20:34:18 -0500 Subject: [PATCH 1/5] wasm2c: fix TLS declaration to operate on all platforms --- wasm2c/README.md | 13 +++++++++++++ wasm2c/wasm-rt.h | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/wasm2c/README.md b/wasm2c/README.md index b7ab3bca2e..9fc620d3ce 100644 --- a/wasm2c/README.md +++ b/wasm2c/README.md @@ -720,3 +720,16 @@ void w2c_host_buf_done(w2c_host* instance, u32 ptr, u32 size) { printf("%s -> %.*s\n", instance->input, (int)size, &instance->memory.data[ptr]); } ``` +## Troubleshooting + +``` +error: No definition for WASM_RT_THREAD_LOCAL for this platform. +``` + +If you see the above error, wasm2c does not know how to define thread_local variables in your platform. You can address this in one of three ways + +1. If your compiler supports C11, pass in `-std=c11` as a compilation flag. Otherwise, you can either + +2. If you know how to define thread local variables in your compiler, define `WASM_RT_THREAD_LOCAL` macro as a compilation flag with the correct thread_local keywords. e.g., `-DWASM_RT_THREAD_LOCAL=__thread` + +3. If you can guarantee that at most one thread of your application will be running wasm2c sandboxes at any given time, you can pass the flag `-DWASM_RT_SINGLE_THREAD_ONLY` to simply define thread_local variables as global values. diff --git a/wasm2c/wasm-rt.h b/wasm2c/wasm-rt.h index d58a37b812..efb8640ad9 100644 --- a/wasm2c/wasm-rt.h +++ b/wasm2c/wasm-rt.h @@ -75,15 +75,19 @@ extern "C" { #endif +#ifndef WASM_RT_THREAD_LOCAL #ifdef WASM_RT_C11_AVAILABLE #define WASM_RT_THREAD_LOCAL _Thread_local #elif defined(_MSC_VER) #define WASM_RT_THREAD_LOCAL __declspec(thread) -#elif (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) +#elif defined(__GNUC__) || defined(__clang__) // Disabled on Apple systems due to sporadic test failures. #define WASM_RT_THREAD_LOCAL __thread -#else +#elif WASM_RT_SINGLE_THREAD_ONLY #define WASM_RT_THREAD_LOCAL +#else +#error "No definition for WASM_RT_THREAD_LOCAL for this platform." +#endif #endif /** From 8e2b1e918e1c38587d60a04826fff387e065cec9 Mon Sep 17 00:00:00 2001 From: Shravan Narayan Date: Mon, 24 Mar 2025 22:18:52 -0500 Subject: [PATCH 2/5] debugging --- test/spec-wasm2c-prefix.c | 4 ++-- wasm2c/wasm-rt-impl.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/spec-wasm2c-prefix.c b/test/spec-wasm2c-prefix.c index bc86f8fd4e..7eb590dd4d 100644 --- a/test/spec-wasm2c-prefix.c +++ b/test/spec-wasm2c-prefix.c @@ -449,7 +449,7 @@ static void posix_install_signal_handler(void) { ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; if (sigaltstack(&ss, NULL) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack specsetup failed"); abort(); } @@ -479,7 +479,7 @@ static void posix_cleanup_signal_handler(void) { stack_t ss; ss.ss_flags = SS_DISABLE; if (sigaltstack(&ss, NULL) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack specshutdown failed"); abort(); } free(ss.ss_sp); diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index e7d99cce8f..cc1c0a1b88 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -169,7 +169,7 @@ static bool os_has_altstack_installed() { /* check for altstack already in place */ stack_t ss; if (sigaltstack(NULL, &ss) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack check failed"); abort(); } @@ -199,7 +199,7 @@ static void os_allocate_and_install_altstack(void) { ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; if (sigaltstack(&ss, NULL) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack install failed"); abort(); } } @@ -212,7 +212,7 @@ static void os_disable_and_deallocate_altstack(void) { /* verify altstack was still in place */ stack_t ss; if (sigaltstack(NULL, &ss) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack dealloc check failed"); abort(); } @@ -226,7 +226,7 @@ static void os_disable_and_deallocate_altstack(void) { /* disable and free */ ss.ss_flags = SS_DISABLE; if (sigaltstack(&ss, NULL) != 0) { - perror("sigaltstack failed"); + perror("sigaltstack free failed"); abort(); } assert(!os_has_altstack_installed()); From 0e0c9c4948187f28d1b660d0a5b68ffe17ac9439 Mon Sep 17 00:00:00 2001 From: Shravan Narayan Date: Mon, 24 Mar 2025 22:35:55 -0500 Subject: [PATCH 3/5] More debugging --- test/run-tests.py | 4 ++++ test/spec-wasm2c-prefix.c | 2 ++ wasm2c/wasm-rt-impl.c | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/test/run-tests.py b/test/run-tests.py index 2eb7ebbf4a..96ba81ec28 100755 --- a/test/run-tests.py +++ b/test/run-tests.py @@ -177,6 +177,10 @@ def Decode(s): expected_lines = [Decode(line) for line in expected.splitlines() if line] actual_lines = [Decode(line) for line in actual.splitlines() if line] + + expected_lines = [line for line in expected_lines if not line.startswith("##")] + actual_lines = [line for line in actual_lines if not line.startswith("##")] + return list( difflib.unified_diff(expected_lines, actual_lines, fromfile='expected', tofile='actual', lineterm='')) diff --git a/test/spec-wasm2c-prefix.c b/test/spec-wasm2c-prefix.c index 7eb590dd4d..89583a447c 100644 --- a/test/spec-wasm2c-prefix.c +++ b/test/spec-wasm2c-prefix.c @@ -448,6 +448,7 @@ static void posix_install_signal_handler(void) { ss.ss_sp = malloc(SIGSTKSZ); ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; + printf("##sigaltstack specsetup: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack specsetup failed"); abort(); @@ -478,6 +479,7 @@ static void posix_cleanup_signal_handler(void) { /* disable and free altstack */ stack_t ss; ss.ss_flags = SS_DISABLE; + printf("##sigaltstack specshutdown: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack specshutdown failed"); abort(); diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index cc1c0a1b88..4b578ac8ef 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -168,6 +168,7 @@ static void os_cleanup_signal_handler(void) { static bool os_has_altstack_installed() { /* check for altstack already in place */ stack_t ss; + printf("##sigaltstack check: NULL, %p\n", &ss); if (sigaltstack(NULL, &ss) != 0) { perror("sigaltstack check failed"); abort(); @@ -198,6 +199,7 @@ static void os_allocate_and_install_altstack(void) { ss.ss_sp = g_alt_stack; ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; + printf("##sigaltstack install: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack install failed"); abort(); @@ -211,6 +213,7 @@ static void os_disable_and_deallocate_altstack(void) { /* verify altstack was still in place */ stack_t ss; + printf("##sigaltstack dealloc: NULL, %p\n", &ss); if (sigaltstack(NULL, &ss) != 0) { perror("sigaltstack dealloc check failed"); abort(); @@ -225,6 +228,7 @@ static void os_disable_and_deallocate_altstack(void) { /* disable and free */ ss.ss_flags = SS_DISABLE; + printf("##sigaltstack free: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack free failed"); abort(); From 69b291ad0e2968c3885260b8ae35faed51870d14 Mon Sep 17 00:00:00 2001 From: Shravan Narayan Date: Mon, 24 Mar 2025 23:09:23 -0500 Subject: [PATCH 4/5] debugging --- wasm2c/wasm-rt-impl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index 4b578ac8ef..ed66644eed 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -168,7 +168,7 @@ static void os_cleanup_signal_handler(void) { static bool os_has_altstack_installed() { /* check for altstack already in place */ stack_t ss; - printf("##sigaltstack check: NULL, %p\n", &ss); + // printf("##sigaltstack check: NULL, %p\n", &ss); if (sigaltstack(NULL, &ss) != 0) { perror("sigaltstack check failed"); abort(); @@ -199,7 +199,7 @@ static void os_allocate_and_install_altstack(void) { ss.ss_sp = g_alt_stack; ss.ss_flags = 0; ss.ss_size = SIGSTKSZ; - printf("##sigaltstack install: %p, NULL\n", &ss); + // printf("##sigaltstack install: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack install failed"); abort(); @@ -213,7 +213,7 @@ static void os_disable_and_deallocate_altstack(void) { /* verify altstack was still in place */ stack_t ss; - printf("##sigaltstack dealloc: NULL, %p\n", &ss); + // printf("##sigaltstack dealloc: NULL, %p\n", &ss); if (sigaltstack(NULL, &ss) != 0) { perror("sigaltstack dealloc check failed"); abort(); @@ -228,7 +228,7 @@ static void os_disable_and_deallocate_altstack(void) { /* disable and free */ ss.ss_flags = SS_DISABLE; - printf("##sigaltstack free: %p, NULL\n", &ss); + // printf("##sigaltstack free: %p, NULL\n", &ss); if (sigaltstack(&ss, NULL) != 0) { perror("sigaltstack free failed"); abort(); From 231e16de0cc7a0635c3a6ee0122860a2db9990ac Mon Sep 17 00:00:00 2001 From: Shravan Narayan Date: Mon, 24 Mar 2025 23:34:13 -0500 Subject: [PATCH 5/5] more debug --- wasm2c/wasm-rt-impl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index ed66644eed..8379142d01 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -221,7 +221,7 @@ static void os_disable_and_deallocate_altstack(void) { if ((!g_alt_stack) || (ss.ss_flags & SS_DISABLE) || (ss.ss_sp != g_alt_stack) || (ss.ss_size != SIGSTKSZ)) { - DEBUG_PRINTF( + printf( "wasm-rt warning: alternate stack was modified unexpectedly\n"); return; }