Repository navigation
Description
Please include the following in your bug report:
Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.50-git (9d830e22903a58a97e5aff79e31e05882112f8db)
clang version 18.0.0 (https://github.com/llvm/llvm-project e88a1ce0137bc14b754b86313228fdc48ba737f3)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/kainino/sources/emsdk/upstream/bin
Failing command line in full:
emcc main.cpp -o index.html -g -sUSE_WEBGL2
Full link command and output with -v appended:
No build failure
Repro code: https://github.com/kainino0x/emscripten-extension-bug
Repro page: https://kai.graphics/emscripten-extension-bug/
Chrome Canary (as of 121.0.6153.0) recently enabled 4 new WebGL extensions:
EXT_clip_control
EXT_depth_clamp
EXT_polygon_offset_clamp
WEBGL_polygon_mode
https://chromiumdash.appspot.com/commit/40900b5e6698955aba32af9679519104c3bad20f
Emscripten passes through the extension strings from WebGL, but it does not actually implement the new entry points glClipControlEXT and glPolygonOffsetClampEXT. These will fail to link if you try to use them directly - but they'll return NULL if you try to get them with GetProcAddress, causing a crash in existing builds if they contain code to use these extensions:
EXT_polygon_offset_clamp was available, calling glPolygonOffsetClampEXT...
Uncaught RuntimeError: null function or function signature mismatch
at index.wasm.__original_main (index.wasm:0x2dad)
at index.wasm.main (index.wasm:0x2de3)
at index.js:709:14
at callMain (index.js:5683:15)
at doRun (index.js:5733:23)
at index.js:5744:7
To reproduce this make sure you have an updated Canary and EXT_polygon_offset_clamp is available on webglreport.com.
(If on Mac, you may need to switch to the Metal backend at chrome://flags/#use-angle.)
EDIT: I am going to revert these new extensions from Chrome per my analysis below. Hopefully a repro isn't really necessary but if needed, you can use an older Chromium snapshot:
Follow the Puppeteer instructions here:
https://www.chromium.org/getting-involved/download-chromium/#chrome-for-testing
Or use these snapshots:
https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Linux_x64/1229878/ (not tried)
https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win_x64/1229878/ (not tried)
https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Mac_Arm/1229875/ (although I can't actually get this to work on my ARM mac)
https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Mac/1229878/ (nor these, though I thought they were dual-architecture binaries)
Activity
kainino0x commentedon Nov 29, 2023
cc @juj but I'm going to look into this for a bit.
kainino0x commentedon Nov 29, 2023
This is known in particular to affect Filament with EXT_clip_control:
https://github.com/google/filament/blob/e7c67d1adb45182a0b7e765a7d90564c26640da9/filament/backend/src/opengl/OpenGLContext.cpp#L281
(my example uses EXT_polygon_offset_clamp because there's a chance we'll revert EXT_clip_control to avoid breaking Filament, at least for now, but we could end up reverting both)
Possible solutions I can think of:
gl2ext.h, for example makingGL_EXT_polygon_offset_clampbe undefined. (Note we can't just#define GL_EXT_polygon_offset_clamp 0before gl2ext.h because many programs use#ifdefinstead of#if.)Options 1 and 2 will not guard us against this happening again, unless the WebGL working group adopts a policy that they'll stop reusing the upstream "EXT_" extension names and only use "WEBGL_" extension names.
Perhaps more importantly, none of these will prevent Chrome's shipment of these extensions from breaking existing content (like existing Filament-based renderers). An alternative:
WEBGL_before shipping.This is maybe technically a bit heavy-handed as a workaround for an Emscripten problem, but given the ecosystem impact I think it is the most prudent.
cc @kdashg
kainino0x commentedon Nov 29, 2023
Actually, per that analysis, I'm probably going to revert these two new extensions from Chrome. This will make the repro I provided stop working in Canary.
Anyway, that's the end of my investigation for now.
kdashg commentedon Nov 29, 2023
Emscripten should not be auto-concatenating an EXTENSIONS string from getSupportedExtensions().
Revert promotion of EXT_clip_control/EXT_polygon_offset_clamp
kdashg commentedon Dec 14, 2023
Please reopen. I don't think this is resolved until this happens.
juj commentedon Dec 14, 2023
Why?
kainino0x commentedon Dec 15, 2023
After #20802 Emscripten only constructs EXTENSIONS by concatenating getSupportedExtensions() when GetProcAddress is disabled. This should be OK because any mention of a function that Emscripten doesn't implement will cause a linker error.
When GetProcAddress is enabled, EXTENSIONS is constructed by allowlist.
kenrussell commentedon Dec 18, 2023
A couple of comments on #20802 indicate that the new build flag
GL_ENABLE_GET_PROC_ADDRESScauses problems: Emscripten apps won't build with the new SDK, and once upgraded, can't downgrade to an older SDK.Given the experience with this bug, it seems risky for Emscripten to automatically pass through support for all of the browser's supported WebGL extensions. Could Emscripten instead make the behavior of
GL_ENABLE_GET_PROC_ADDRESSthe default now? Remove the toggle, and maintain an allowlist of WebGL extensions on Emscripten's side which are known to work with Emscripten. While this will require apps to upgrade their SDK and recompile if they want to take advantage of new WebGL extensions, this seems reasonable to me - and much safer than the current situation where the browser can accidentally activate untested code paths in Emscripten.I'm taking the liberty of reopening this issue while making this comment. Not sure of the status of things right now, but perhaps #20802 could be reverted and redone in a way that no new build flag is added. The current status quo is not really workable from the WebGL WG's point of view - we don't want to have to rename every extension that's exposed to the web into the
WEBGL_namespace. Thanks.kdashg commentedon Dec 18, 2023
It is inherently risky to blindly concat the EXTENSIONS string. The blind-concat approach is a hack that mostly worked, but it's caused this backwards compat issue that is pushing your bug upstream into the working group's process for extension naming. Continuing to do so is to continue to exacerbate this issue.
I believe this is not difficult, and it's important to us (the WebGL WG, future users of webgl's extensions), it would really great to just fix this.
sbc100 commentedon Dec 19, 2023
The issue described in the PR is just how new settings work in emscripten. The solution is the pass
GL_ENABLE_GET_PROC_ADDRESS, but only if the SDK version supports that flag.In terms is backing out #20802, it sounds like your concerns would be alleviated if we simply made
GL_ENABLE_GET_PROC_ADDRESSdefault to true, at least for some period of time? I believe the #20802 already includes allowlist that are talking about, doesn't it? (Isn't that was this is:emscripten/src/library_webgl.js
Line 747 in f3c8edc
kdashg commentedon Dec 19, 2023
What is the reasoning for not always using an allowlist, even for
!GL_ENABLE_GET_PROC_ADDRESS?kainino0x commentedon Dec 19, 2023
Code size, I think, but it can also be useful for applications to use extensions (without entrypoints), before Emscripten explicitly allowlists them. On the flip side, I'm still not clear what the benefit would be of using an allowlist when GetProcAddress isn't available?
15 remaining items
juj commentedon Dec 20, 2023
glGetStringcan be used for other purposes than getting extensions, and simply inquiring the list of unfiltered extensions is not the root of the problem.However, I do get your point that there will then exist a way for groups 1 and 2 to directly target the web if they avoid
glGetString()altogether. LGTM to land by addressing https://github.com/emscripten-core/emscripten/pull/20955/files#r1433238791 .kdashg commentedon Dec 21, 2023
I can tell that you are unconvinced that returning getSupportedExtensions in EXTENSIONS unchecked could turn malignant (anymore), and I can tell that you are terrified of code size issues.
I hear you that you think this is not a problem, but I am advising that it is risky to keep doing this. An example is extensions that have no entrypoints that can be statically excluded by not using getProcAddress, but where there are for example added functionality for GLSL. There is the opportunity for the WG to believe we are making a trivial exclusion from a GLSL extension, that then comes around to bite a deployed app that expects the e.g. desktop GL version of the extension. I can tell you that there is this risk because I am one of the ones in the back room trying to give close and careful consideration to extension proposals, but also in recent months I have been pressured to reduce the care and stringency with which I review WebGL extension proposals to accelerate their roll-outs.
Put another way: I bet that, even given your mitigations, I would be able to adversarially craft a WebGL extension that would break some Emscripten apps that you believe to be immune.
Your risk is that a group that you don't participate in can (accidentally!) cause behavioral changes in apps long-deployed with your framework, which risks very unhappy deployment owners, and risks incurring reputational damage regarding Emscripten's stability and robustness in "finished" deployments.
If you are willing to risk this for the sake of these arrays of strings, that's your project's choice, but that's not the guarantee that us browsers are (capable of) giving you, and we unfortunately can't guarantee that we won't then (accidentally, unintentionally!) break your users' deployments. That is what I see as the price of cutting corners to save some ~800 bytes here.
juj commentedon Dec 21, 2023
There is absolutely no need to go into emotional characterization ad hominems here. We can surely keep discussing this issue as the issue without painting the people discussing it.
This is not an example that I could see. The thing to understand is that the fix does not apply to individual extensions at a time, but to the whole compiled codebase at a time.
So in order for a codebase to be bit by this issue via an extension that does not have entrypoints, they would have to first a) hand-write a change into their application to statically link all their other extensions that do have entry points (taking them away from the "blind port" realm), b) turn off the automatic enable extensions linker flag, then c) manually hand-write new code into their codebase to blindly enable these extensions that don't yet exist, and d) never test the code they hand-wrote to see that it doesn't work today.
Stating again, they would have to do active work to take their engine from being a "I blindly compiled an existing GL engine" port, carefully refit that engine to statically link to everything, and statically enable all the extensions, including the ones that don't exist on the web, and then decide "nope, we actually aren't going to test any of what we wrote will work" to plant their time bomb.
Please understand, for the third time maybe now, that I am not proposing to cut corners. The solutions that have already landed apply widely to all compiled code and is an opt-out to which you have to actively write code if you started from a port.
I am proposing to find a solution that can fit every Emscripten developer, without getting immediate reputational harm from other users in the groups 1-2 I mentioned earlier, had we applied naive fixes.
It would be condescending to web developers for Emscripten to mandate that "we think you can never be trusted to know what code you write, so we will always impose the allowlist to all your codebases, no matter how first-tier web experience you may think you are targeting with your code."
If the current guardrails still don't seem enough, then we can look into adding more, for example, even if a developer has a) migrated away from the "it's a port" GL realm by having refitted all their GetProcAddress spots, and they have b) refitted their code to hand-write their GL extension enables and c) have explicitly disabled the auto enable extensions mode, we could still look at d) printing a development mode warning when attempting to manually enable an extension that does not exist in WebGL (a check that gets optimized out for them in release builds), or e) adding even an extra level of opt-out field to get to the level of paranoia.
But we simply should not use a blunt tool to wholesale think there couldn't possibly exist codebases out there that know they intend what they write.
kripken commentedon Dec 21, 2023
Hey friends, the last few comments seem to be getting a little heated. I admit I do not fully understand the technical details, but I'm worried there might be some misunderstandings here. What do you think about scheduling a meeting to discuss this in more detail offline? I think that might be productive.
kdashg commentedon Dec 21, 2023
I think that's a great idea. In the new year, maybe? (Happy solstice! :))
I apologize that "terrified" felt like an ad hominem. My intent was to characterize very strong degree and not to connote emotion or any irrationality. (I would describe myself as being terrified of e.g. raw pointer member fields in c++!) Sorry that I didn't take enough care in wording here!
kripken commentedon Dec 21, 2023
The new year is probably best, yeah. I'm about to go out myself, and will mostly be out until Jan 2 or so. I'll send out an email when I'm back!
With that said, since I know the least about GL, don't wait for me if we do arrive at a compromise acceptable to everyone meanwhile. But if not, hopefully a meeting next year can help.
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed
Include GL extension fliter/allowlist if and only if its needed (#20955)