Skip to content

Patches from emscripten 3.1.57 #10

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 1 commit into from
Mar 26, 2024
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
59 changes: 52 additions & 7 deletions compiler-rt/emscripten_setjmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* found in the LICENSE file.
*/

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <setjmp.h>
Expand Down Expand Up @@ -67,27 +68,71 @@ uint32_t testSetjmp(uintptr_t id, TableEntry* table, uint32_t size) {
#include "emscripten_internal.h"

void emscripten_longjmp(uintptr_t env, int val) {
// C standard:
// The longjmp function cannot cause the setjmp macro to return
// the value 0; if val is 0, the setjmp macro returns the value 1.
if (val == 0) {
val = 1;
}
setThrew(env, val);
_emscripten_throw_longjmp();
}
#endif

#ifdef __USING_WASM_SJLJ__

struct __WasmLongjmpArgs {
void *env;
int val;
};

thread_local struct __WasmLongjmpArgs __wasm_longjmp_args;
// llvm uses `1` for the __c_longjmp tag.
// See https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/CodeGen/WasmEHFuncInfo.h
#define C_LONGJMP 1
#endif

// jmp_buf should have large enough size and alignment to contain
// this structure.
struct jmp_buf_impl {
void* func_invocation_id;
uint32_t label;
#ifdef __USING_WASM_SJLJ__
struct __WasmLongjmpArgs arg;
#endif
};

void __wasm_setjmp(void* env, uint32_t label, void* func_invocation_id) {
struct jmp_buf_impl* jb = env;
assert(label != 0); // ABI contract
assert(func_invocation_id != NULL); // sanity check
jb->func_invocation_id = func_invocation_id;
jb->label = label;
}

uint32_t __wasm_setjmp_test(void* env, void* func_invocation_id) {
struct jmp_buf_impl* jb = env;
assert(jb->label != 0); // ABI contract
assert(func_invocation_id != NULL); // sanity check
if (jb->func_invocation_id == func_invocation_id) {
return jb->label;
}
return 0;
}

#ifdef __USING_WASM_SJLJ__
// Wasm EH allows us to throw and catch multiple values, but that requires
// multivalue support in the toolchain, whch is not reliable at the time.
// TODO Consider switching to throwing two values at the same time later.
void __wasm_longjmp(void *env, int val) {
__wasm_longjmp_args.env = env;
__wasm_longjmp_args.val = val;
__builtin_wasm_throw(1, &__wasm_longjmp_args);
void __wasm_longjmp(void* env, int val) {
struct jmp_buf_impl* jb = env;
struct __WasmLongjmpArgs* arg = &jb->arg;
// C standard says:
// The longjmp function cannot cause the setjmp macro to return
// the value 0; if val is 0, the setjmp macro returns the value 1.
if (val == 0) {
val = 1;
}
arg->env = env;
arg->val = val;
__builtin_wasm_throw(C_LONGJMP, arg);
}

#endif
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ u64 MonotonicNanoTime() {
return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
}

void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}

} // namespace __sanitizer

#endif
19 changes: 6 additions & 13 deletions compiler-rt/stack_ops.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.globl stackSave
.globl stackRestore
.globl stackAlloc
.globl _emscripten_stack_restore
.globl _emscripten_stack_alloc
.globl emscripten_stack_get_current

#ifdef __wasm64__
Expand All @@ -13,19 +12,14 @@

.globaltype __stack_pointer, PTR

stackSave:
.functype stackSave() -> (PTR)
global.get __stack_pointer
end_function

stackRestore:
.functype stackRestore(PTR) -> ()
_emscripten_stack_restore:
.functype _emscripten_stack_restore(PTR) -> ()
local.get 0
global.set __stack_pointer
end_function

stackAlloc:
.functype stackAlloc(PTR) -> (PTR)
_emscripten_stack_alloc:
.functype _emscripten_stack_alloc(PTR) -> (PTR)
.local PTR, PTR
global.get __stack_pointer
# Get arg 0 -> number of bytes to allocate
Expand All @@ -44,4 +38,3 @@ emscripten_stack_get_current:
.functype emscripten_stack_get_current () -> (PTR)
global.get __stack_pointer
end_function