Skip to content
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

Fix for longjmp(.., 0) under wasm EH #21493

Merged
merged 1 commit into from
Mar 8, 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
8 changes: 8 additions & 0 deletions system/lib/compiler-rt/emscripten_setjmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ thread_local struct __WasmLongjmpArgs __wasm_longjmp_args;
// TODO Consider switching to throwing two values at the same time later.
void __wasm_longjmp(void *env, int val) {
__wasm_longjmp_args.env = env;
/*
 * 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;
}
__wasm_longjmp_args.val = val;
__builtin_wasm_throw(C_LONGJMP, &__wasm_longjmp_args);
}
Expand Down
28 changes: 28 additions & 0 deletions test/core/test_longjmp_zero.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>
#include <setjmp.h>

int main() {
printf("start\n");
jmp_buf b1;
int val = setjmp(b1);
if (val) {
printf("success\n");
return 0;
}
/*
* 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.
*/
printf("longjmp\n");
longjmp(b1, 0);
__builtin_trap();
return 0;
}
3 changes: 3 additions & 0 deletions test/core/test_longjmp_zero.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
start
longjmp
success
4 changes: 4 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,10 @@ def test_longjmp_standalone(self):
def test_longjmp(self):
self.do_core_test('test_longjmp.c')

@with_both_sjlj
def test_longjmp_zero(self):
self.do_core_test('test_longjmp_zero.c')

def test_longjmp_with_and_without_exceptions(self):
# Emscripten SjLj with and without Emscripten EH support
self.set_setting('SUPPORT_LONGJMP', 'emscripten')
Expand Down
Loading