Skip to content

Commit

Permalink
[EH/SjLj] Add tests for mixing exceptions and sjlj (#14732)
Browse files Browse the repository at this point in the history
This adds three tests for mixing exceptions and sjlj. We currently pass
`test_exceptions_longjmp1`, and other two tests will pass after
llvm/llvm-project@c285a11.

- `test_exceptions_longjmp1`
  In a function with a `setjmp` call, `invoke` exists in LLVM IR. We
  have to check both for EH and sjlj when a function throws.
- `test_exceptions_longjmp2`
  In a function without a `setjmp` call, `invoke` exists in LLVM IR. We
  only have to handle exceptions, but when longjmps occur, we shouldn't
  swallow them.
- `test_exceptions_longjmp3`
  In a function with a `setjmp` call, there's no `invoke` in LLVM IR. We
  only have to handle longjmps, but w hen an exceptions occcur, we
  shouldn't swallow them.

Each of these tests corresponds with tests in
test/CodeGen/WebAssembly/lower-em-sjlj.ll in
llvm/llvm-project@c285a11.
  • Loading branch information
aheejin committed Jul 27, 2021
1 parent a1a2ec1 commit 6f9a294
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/core/test_exceptions_longjmp1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

void foo() {
longjmp(buf, 1);
}

int main() {
int jmpval = setjmp(buf);
if (jmpval != 0) {
printf("setjmp returned for the second time\n");
return 0;
}
try {
foo();
} catch (...) {
}
return 0;
}
1 change: 1 addition & 0 deletions tests/core/test_exceptions_longjmp1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setjmp returned for the second time
25 changes: 25 additions & 0 deletions tests/core/test_exceptions_longjmp2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

void foo() {
longjmp(buf, 1);
}

void test() {
try {
foo();
} catch (...) {
}
}

int main() {
int jmpval = setjmp(buf);
if (jmpval != 0) {
printf("setjmp returned for second time\n");
return 0;
}
test();
return 0;
}
1 change: 1 addition & 0 deletions tests/core/test_exceptions_longjmp2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setjmp returned for second time
26 changes: 26 additions & 0 deletions tests/core/test_exceptions_longjmp3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

void foo() {
throw 3;
}

void test() {
int jmpval = setjmp(buf);
if (jmpval != 0) {
printf("setjmp returned for 2nd time\n");
return;
}
foo();
}

int main() {
try {
test();
} catch (int n) {
printf("caught: %d\n", n);
}
return 0;
}
1 change: 1 addition & 0 deletions tests/core/test_exceptions_longjmp3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
caught: 3
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,21 @@ def test_iostream_ctors(self):
}
''', 'bugfree code')

def test_exceptions_longjmp1(self):
self.set_setting('SUPPORT_LONGJMP')
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
self.do_core_test('test_exceptions_longjmp1.cpp')

def test_exceptions_longjmp2(self):
self.set_setting('SUPPORT_LONGJMP')
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
self.do_core_test('test_exceptions_longjmp2.cpp')

def test_exceptions_longjmp3(self):
self.set_setting('SUPPORT_LONGJMP')
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
self.do_core_test('test_exceptions_longjmp3.cpp')

# Marked as impure since the WASI reactor modules (modules without main)
# are not yet suppored by the wasm engines we test against.
@also_with_standalone_wasm(impure=True)
Expand Down

0 comments on commit 6f9a294

Please sign in to comment.