-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add new proxying modes foo__proxy: 'abort' and foo__proxy: 'abort_debug' #22648
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
Changes from 1 commit
d30d432
b2bac1e
bd6efe7
5f402cd
e753f21
382c420
1891f48
e6d7eb6
9f59913
1f2992a
5564586
725615c
5f0a9ef
5838805
bb7a5fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -319,39 +319,40 @@ function(${args}) { | |
|
|
||
| const proxyingMode = LibraryManager.library[symbol + '__proxy']; | ||
| if (proxyingMode) { | ||
| if (proxyingMode !== 'sync' && proxyingMode !== 'async' && proxyingMode !== 'none') { | ||
| throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified!`); | ||
| const possibleProxyingModes = ['sync', 'async', 'none', 'abort', 'abort_debug']; | ||
| if (!possibleProxyingModes.includes(proxyingMode)) { | ||
| throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified! Possible modes: ${possibleProxyingModes.join(',')}`); | ||
| } | ||
| if (SHARED_MEMORY) { | ||
| const sync = proxyingMode === 'sync'; | ||
| if (PTHREADS) { | ||
| snippet = modifyJSFunction(snippet, (args, body, async_, oneliner) => { | ||
| if (oneliner) { | ||
| body = `return ${body}`; | ||
| snippet = modifyJSFunction(snippet, (args, body, async_, oneliner) => { | ||
| if (oneliner) { | ||
| body = `return ${body}`; | ||
| } | ||
| const rtnType = sig && sig.length ? sig[0] : null; | ||
| const proxyFunc = | ||
| MEMORY64 && rtnType == 'p' ? 'proxyToMainThreadPtr' : 'proxyToMainThread'; | ||
| var prefix = ''; | ||
|
|
||
| if (proxyingMode == 'sync' || proxyingMode == 'async') { | ||
| if (PTHREADS) { | ||
| deps.push('$' + proxyFunc); | ||
| prefix = `if (ENVIRONMENT_IS_PTHREAD) return ${proxyFunc}(${proxiedFunctionTable.length}, 0, ${+(proxyingMode === 'sync')}${args ? ', ' : ''}${args});`; | ||
| } | ||
| const rtnType = sig && sig.length ? sig[0] : null; | ||
| const proxyFunc = | ||
| MEMORY64 && rtnType == 'p' ? 'proxyToMainThreadPtr' : 'proxyToMainThread'; | ||
| deps.push('$' + proxyFunc); | ||
| return ` | ||
| function(${args}) { | ||
| if (ENVIRONMENT_IS_PTHREAD) | ||
| return ${proxyFunc}(${proxiedFunctionTable.length}, 0, ${+sync}${args ? ', ' : ''}${args}); | ||
| if (WASM_WORKERS && ASSERTIONS) { | ||
| prefix += `\nassert(!ENVIRONMENT_IS_WASM_WORKER, "Attempted to call proxied function '${mangled}' in a Wasm Worker, but in Wasm Worker enabled builds, proxied function architecture is not available!");`; | ||
| } | ||
| } else if (proxyingMode == 'abort' || (proxyingMode == 'abort_debug' && ASSERTIONS)) { | ||
| const insideWorker = (PTHREADS && WASM_WORKERS) | ||
| ? '(ENVIRONMENT_IS_PTHREAD || ENVIRONMENT_IS_WASM_WORKER)' | ||
| : (PTHREADS ? 'ENVIRONMENT_IS_PTHREAD' : 'ENVIRONMENT_IS_WASM_WORKER'); | ||
sbc100 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| prefix = `assert(!${insideWorker}, "Attempted to call function '${mangled}' inside a pthread/Wasm Worker, but this function has been declared to only be callable from the main browser thread");`; | ||
|
||
| } | ||
|
|
||
| return `function(${args}) { | ||
| ${prefix} | ||
| ${body} | ||
| }\n`; | ||
| }); | ||
| } else if (WASM_WORKERS && ASSERTIONS) { | ||
| // In ASSERTIONS builds add runtime checks that proxied functions are not attempted to be called in Wasm Workers | ||
| // (since there is no automatic proxying architecture available) | ||
| snippet = modifyJSFunction( | ||
| snippet, | ||
| (args, body) => ` | ||
| function(${args}) { | ||
| assert(!ENVIRONMENT_IS_WASM_WORKER, "Attempted to call proxied function '${mangled}' in a Wasm Worker, but in Wasm Worker enabled builds, proxied function architecture is not available!"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come the fdwrite syscall wasn't hitting this assertion already? |
||
| ${body} | ||
| }\n`, | ||
| ); | ||
| } | ||
| }); | ||
| proxiedFunctionTable.push(mangled); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <emscripten.h> | ||
| #include <pthread.h> | ||
| #include <assert.h> | ||
|
|
||
| // Tests behavior of __proxy: 'abort' and __proxy: 'abort_debug' in pthreads. | ||
|
|
||
| void proxied_js_function(void); | ||
|
|
||
| int might_throw(void(*func)()) | ||
| { | ||
juj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| 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> | ||
|
|
||
| // Tests behavior of __proxy: 'abort' and __proxy: 'abort_debug' in Wasm Workers. | ||
|
|
||
| void proxied_js_function(void); | ||
|
|
||
| int might_throw(void(*func)()) | ||
| { | ||
juj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| 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'); | ||
| } | ||
| }); |
| 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'); | ||
| } | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.