Skip to content

Always fixup custom __proxy attributes in library_html5_webgl.js #20135

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

Merged
merged 2 commits into from
Aug 28, 2023

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Aug 25, 2023

Fixes: #20134

@sbc100 sbc100 force-pushed the wasm_worker_proxy_error branch from 29a57f7 to 1e2ed1d Compare August 25, 2023 23:18
@sbc100 sbc100 force-pushed the wasm_worker_proxy_error branch from 1e2ed1d to ee1f4b7 Compare August 25, 2023 23:39
@sbc100 sbc100 changed the title Ignore value of __proxy in WASM_WORKERS mode. NFC Always fixup custom __proxy attributes in library_html5_webgl.js Aug 25, 2023
@sbc100
Copy link
Collaborator Author

sbc100 commented Aug 25, 2023

OK, I came up with a better solution.

Part 1: Make jsifier stricter so it always checks for invalid __proxy attributes, even if its not going to use them. This mean that the error is reproducible by any test that uses -sINCLUDE_FULL_LIBRARY
Part 2: Fix the failures by always processing the custom __proxy attributes locally.

@sbc100 sbc100 force-pushed the wasm_worker_proxy_error branch from ee1f4b7 to b5ed028 Compare August 25, 2023 23:42
@sbc100 sbc100 enabled auto-merge (squash) August 28, 2023 00:36
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm (i see auto-merge is on, so not clicking approve due to comment)

@sbc100 sbc100 disabled auto-merge August 28, 2023 19:01
@sbc100 sbc100 merged commit 7cd6360 into main Aug 28, 2023
@sbc100 sbc100 deleted the wasm_worker_proxy_error branch August 28, 2023 19:01
// In single threaded mode just delete our custom __proxy addributes, otherwise
// they will causes errors in the JS compiler.
for (var i in funcs) {
delete funcs[i + '__proxy'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awkward, no? Wouldn't all other libraries besides library_htm5_webgl.js want to run this functionality as well to ignore proxy attributes?

Ideally for Wasm Workers, the __proxy mechanism should be ignored if function is called from main thread, and in assertions builds, if a proxied JS function is called from a Wasm Worker, the code would raise an error, and in release builds simply truck through?

That is what this code

emscripten/src/jsifier.js

Lines 247 to 255 in 3a55fe1

} 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!");
${body}
}\n`);
}
attempts to do, so the above change should be redundant if the linked code works as intended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code only runs if !SHARED_MEMORY so when building with wasm workers or pthreads this code doesn't run and the attributes are not ignored.

I agree this could probably use a comment. The issue is that library_htm5_webgl.js uses custom __proxy addribute sync_on_webgl_context_handle_thread which needs to get either re-written (above), or removed (here) before the library is processed, otherwise we get an Invalid proxyingMode jsifier.

Copy link
Collaborator Author

@sbc100 sbc100 Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I did already include a comment as to why this done. Does it make sense now, given my explanation?

My initial version of this attempted to relax the __proxy checking but @kripken suggested we always remove / rewrite the custom values instead: #20135 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code only runs if !SHARED_MEMORY

I am confused. Taking a closer look, the code runs if

if (SHARED_MEMORY) {
  if (PTHREADS) {
    // ...
  } else if (WASM_WORKERS && ASSERTIONS) {
    // emit "assert(!ENVIRONMENT_IS_WASM_WORKER"
  }
}

but I think I wrote that when I still had the assumption that PTHREADS and WASM_WORKERS would always be mutually exclusive and you'd only build targeting one or the other - but turns out targeting both at the same time made sense.

So that above code should instead be

if (SHARED_MEMORY) {
  if (PTHREADS) {
    // ...
  }
  if (WASM_WORKERS && ASSERTIONS) {
    // emit "assert(!ENVIRONMENT_IS_WASM_WORKER"
  }
}

so that in PTHREADS + WASM_WORKERS builds the assertion is also added. Would that appropriately resolve the issue?

I still think no library_x.js file should need to mutate the __proxy fields depending on the build mode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I see what you are saying. We should probably not erase the __proxy attributes in the wasm workers build. I will create a followup to do that.

I agree the custom proxy stuff in library_html5_webgl.js is unusual but it looks like you were the one that created this mechanism back in #11356, and I think its OK as long as it get locally handled/erased.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I mis-remembered, I already did do that in this PR, that __proxy handling is enabled here for all SHARED_MEMORY builds.

I think updating jsifier.js in way you suggest sounds fine to me. That seems like a pre-existing issue that is somewhat unrelated to this PR and would effect all libraries, right?

Are you happy with this PR in the way it deals with library_html5_webgl.js?

Copy link
Collaborator

@juj juj Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a pre-existing issue that is somewhat unrelated to this PR and would effect all libraries, right?

Yeah, I think it would be very preferable to have that affect all libraries, so that any developer who is calling a __proxy: sync function inside a Wasm Worker would get an assertion firing on them. That is because I don't think any function with that label would work properly when called in the context of a Wasm Worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internal compiler error when using emscripten_webgl_enable_extension with WASM workers
3 participants