Skip to content

Commit 8f38f35

Browse files
authored
Support Emscripten EH/SjLj in Wasm64 (#14357)
This mostly changes some `uint32_t`s to `uintptr_t` in libraries for EH and SjLj to support wasm64. More details in case you are interested: 1. I described in https://reviews.llvm.org/D101985 how changing the first argument of `emscripten_longjmp` in turn changed `testSetjmp`'s first argument to `uintptr_t`. 2. `testSetjmp`'s first argument `id` is compared with the local variable `curr`. So it's `uintptr_t` now: [Link](https://github.com/emscripten-core/emscripten/blob/aa2bad5d6a0de5b175688fc8ead46f70a1b43a0d/system/lib/compiler-rt/emscripten_setjmp.c#L53) 3. That `curr` is set from `table[i].id`, which is `TableEntry.id`. So `id` in `TableEntry` here is now `uintptr_t`: [Link](https://github.com/emscripten-core/emscripten/blob/aa2bad5d6a0de5b175688fc8ead46f70a1b43a0d/system/lib/compiler-rt/emscripten_setjmp.c#L51) 4. Because `table[i].id` is set from `setjmpId`, `setjmpId` needs to be `uintptr_t` too: [Link](https://github.com/emscripten-core/emscripten/blob/aa2bad5d6a0de5b175688fc8ead46f70a1b43a0d/system/lib/compiler-rt/emscripten_setjmp.c#L31) 5. Because `setjmpId` is stored in `env` (first parameter) in `saveSetjmp`, `saveSetjmp`'s first argument has to change to `uintptr_t`: [Link](https://github.com/emscripten-core/emscripten/blob/aa2bad5d6a0de5b175688fc8ead46f70a1b43a0d/system/lib/compiler-rt/emscripten_setjmp.c#L28) Not sure how to add tests for this given that wasm64 support is not ready yet. Existing tests run fine.
1 parent 1647d14 commit 8f38f35

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

system/lib/compiler-rt/emscripten_exception_builtins.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
* See: https://llvm.org/doxygen/WebAssemblyLowerEmscriptenEHSjLj_8cpp.html
1010
*/
1111

12+
#include <stdint.h>
1213
#include <threads.h>
1314

14-
thread_local int __THREW__ = 0;
15+
thread_local uintptr_t __THREW__ = 0;
1516
thread_local int __threwValue = 0;
1617

17-
void setThrew(int threw, int value) {
18+
void setThrew(uintptr_t threw, int value) {
1819
if (__THREW__ == 0) {
1920
__THREW__ = threw;
2021
__threwValue = value;

system/lib/compiler-rt/emscripten_setjmp.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
#include <stdlib.h>
1010
#include <setjmp.h>
1111

12-
static uint32_t setjmpId = 0;
12+
// 0 - Nothing thrown
13+
// 1 - Exception thrown
14+
// Other values - jmpbuf pointer in the case that longjmp was thrown
15+
static uintptr_t setjmpId = 0;
1316

1417
typedef struct TableEntry {
15-
uint32_t id, label;
18+
uintptr_t id;
19+
uint32_t label;
1620
} TableEntry;
1721

1822
extern void setTempRet0(uint32_t value);
1923
extern void setThrew(uintptr_t threw, int value);
2024
extern void _emscripten_throw_longjmp(); // defined in src/library.js
2125

22-
TableEntry* saveSetjmp(uint32_t* env, uint32_t label, TableEntry* table, uint32_t size) {
26+
TableEntry* saveSetjmp(uintptr_t* env, uint32_t label, TableEntry* table, uint32_t size) {
2327
// Not particularly fast: slow table lookup of setjmpId to label. But setjmp
2428
// prevents relooping anyhow, so slowness is to be expected. And typical case
2529
// is 1 setjmp per invocation, or less.
@@ -45,10 +49,10 @@ TableEntry* saveSetjmp(uint32_t* env, uint32_t label, TableEntry* table, uint32_
4549
return table;
4650
}
4751

48-
uint32_t testSetjmp(uint32_t id, TableEntry* table, uint32_t size) {
49-
uint32_t i = 0, curr;
52+
uint32_t testSetjmp(uintptr_t id, TableEntry* table, uint32_t size) {
53+
uint32_t i = 0;
5054
while (i < size) {
51-
uint32_t curr = table[i].id;
55+
uintptr_t curr = table[i].id;
5256
if (curr == 0) break;
5357
if (curr == id) {
5458
return table[i].label;

0 commit comments

Comments
 (0)