MCPcopy Create free account
hub / github.com/WebAssembly/wabt / do_thread

Function do_thread

wasm2c/examples/threads/threads.c:61–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59}
60
61void* do_thread(void* arg) {
62 int param;
63 memcpy(&param, arg, sizeof(int));
64
65 /* Initialize the per-thread context for the Wasm runtime (in
66 practice, this allocates and installs an alternate stack for
67 catching segfaults caused by stack exhaustion or out-of-bounds
68 memory access). */
69 wasm_rt_init_thread();
70
71 /* Instantiate the Wasm module. */
72 w2c_sample inst;
73 wasm2c_sample_instantiate(&inst);
74
75 /* Expect a stack-exhaustion trap. (N.B. in a pthreads-created stack, Linux's
76 segfault for stack overflow appears the same as one for memory OOB. This is
77 similar to the behavior of macOS when exhausting a non-pthreads stack. */
78 wasm_rt_trap_t code = wasm_rt_impl_try();
79 if (code != 0) {
80 if (code == WASM_RT_TRAP_OOB || code == WASM_RT_TRAP_EXHAUSTION) {
81 /* Trap arrived as expected. Now call the "real" function. */
82 int returnval = w2c_sample_multiplyby3(&inst, param);
83 memcpy(arg, &returnval, sizeof(int));
84
85 /* Free the module instance. */
86 wasm2c_sample_free(&inst);
87
88 /* Free the per-thread runtime context. */
89 wasm_rt_free_thread();
90
91 return arg;
92 } else {
93 fprintf(stderr, "Expected OOB or exhaustion trap but got %s\n",
94 wasm_rt_strerror(code));
95 return NULL;
96 }
97 }
98
99 /* Call the stack-overflow function. Expect a trap back to above. */
100 w2c_sample_stackoverflow(&inst);
101
102 /* If no trap... */
103 fprintf(stderr, "Expected trap but did not get one\n");
104 return NULL;
105}

Callers

nothing calls this directly

Calls 3

wasm_rt_init_threadFunction · 0.85
wasm_rt_free_threadFunction · 0.85
wasm_rt_strerrorFunction · 0.85

Tested by

no test coverage detected