forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new proxying modes foo__proxy: 'abort' and foo__proxy: 'abort_deb…
…ug', which will add compile time checks to verify that given JS function is not called in pthreads or Wasm Workers.
- Loading branch information
Showing
7 changed files
with
160 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <emscripten.h> | ||
#include <pthread.h> | ||
#include <assert.h> | ||
|
||
// Test that a JS function with __proxy: 'abort' cannot be called in Wasm Workers. | ||
|
||
void proxied_js_function(void); | ||
|
||
int might_throw(void(*func)()) | ||
{ | ||
int threw = EM_ASM_INT({ | ||
// Patch over assert() so that it does not abort execution on assert failure, but instead | ||
// throws a catchable exception. | ||
assert = function(condition, text) { | ||
if (!condition) { | ||
throw 'Assertion failed' + (text ? ": " + text : ""); | ||
} | ||
}; | ||
|
||
try { | ||
getWasmTableEntry($0)(); | ||
} catch(e) { | ||
console.error('Threw an exception: ' + e); | ||
return e.toString().includes('this function has been declared to only be callable from the main browser thread'); | ||
} | ||
console.error('Function did not throw'); | ||
return 0; | ||
}, func); | ||
return threw; | ||
} | ||
|
||
void test() | ||
{ | ||
proxied_js_function(); | ||
} | ||
|
||
void *thread_main(void *arg) | ||
{ | ||
REPORT_RESULT(might_throw(test)); | ||
return 0; | ||
} | ||
|
||
char stack[1024]; | ||
|
||
int main() | ||
{ | ||
proxied_js_function(); // Should be callable from main thread | ||
|
||
pthread_t thread; | ||
pthread_attr_t attr; | ||
pthread_attr_init(&attr); | ||
pthread_create(&thread, &attr, thread_main, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <emscripten.h> | ||
#include <emscripten/wasm_worker.h> | ||
#include <assert.h> | ||
|
||
// Test that a JS function with __proxy: 'abort' cannot be called in Wasm Workers. | ||
|
||
void proxied_js_function(void); | ||
|
||
int might_throw(void(*func)()) | ||
{ | ||
int threw = EM_ASM_INT({ | ||
// Patch over assert() so that it does not abort execution on assert failure, but instead | ||
// throws a catchable exception. | ||
assert = function(condition, text) { | ||
if (!condition) { | ||
throw 'Assertion failed' + (text ? ": " + text : ""); | ||
} | ||
}; | ||
|
||
try { | ||
getWasmTableEntry($0)(); | ||
} catch(e) { | ||
console.error('Threw an exception: ' + e); | ||
return e.toString().includes('this function has been declared to only be callable from the main browser thread'); | ||
} | ||
console.error('Function did not throw'); | ||
return 0; | ||
}, func); | ||
return threw; | ||
} | ||
|
||
void test() | ||
{ | ||
proxied_js_function(); | ||
} | ||
|
||
void worker_main() | ||
{ | ||
REPORT_RESULT(might_throw(test)); | ||
} | ||
|
||
char stack[1024]; | ||
|
||
int main() | ||
{ | ||
proxied_js_function(); // Should be callable from main thread | ||
emscripten_wasm_worker_post_function_v(emscripten_malloc_wasm_worker(1024), worker_main); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
addToLibrary({ | ||
proxied_js_function__proxy: 'abort', | ||
proxied_js_function: function() { | ||
console.log('In proxied_js_function'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
addToLibrary({ | ||
proxied_js_function__proxy: 'abort_debug', | ||
proxied_js_function: function() { | ||
console.log('In proxied_js_function'); | ||
} | ||
}); |