Skip to content

WebGL: extension string is available even though Emscripten doesn't implement it #20798

Description

@kainino0x

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

  1. kainino0x commented on Nov 29, 2023

    @kainino0x
    CollaboratorAuthor

    cc @juj but I'm going to look into this for a bit.

  2. kainino0x commented on Nov 29, 2023

    @kainino0x
    CollaboratorAuthor

    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:

    1. Implement these new extensions now.
    2. Filter out these new extensions from the list Emscripten exposes, until they're implemented. Has code size impact.
    3. Filter out everything except known extensions from the list. Has code size impact.
    4. Remove unimplemented extensions from gl2ext.h, for example making GL_EXT_polygon_offset_clamp be undefined. (Note we can't just #define GL_EXT_polygon_offset_clamp 0 before gl2ext.h because many programs use #ifdef instead 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:

    1. Don't ship these extensions in Chrome, get the WG to rename them to 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

  3. kainino0x commented on Nov 29, 2023

    @kainino0x
    CollaboratorAuthor

    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.

  4. kdashg commented on Nov 29, 2023

    @kdashg
    Contributor

    Emscripten should not be auto-concatenating an EXTENSIONS string from getSupportedExtensions().

  5. kdashg commented on Dec 14, 2023

    @kdashg
    Contributor

    Emscripten should not be auto-concatenating an EXTENSIONS string from getSupportedExtensions().

    Please reopen. I don't think this is resolved until this happens.

  6. juj commented on Dec 14, 2023

    @juj
    Collaborator

    Why?

  7. kainino0x commented on Dec 15, 2023

    @kainino0x
    CollaboratorAuthor

    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.

  8. kenrussell commented on Dec 18, 2023

    @kenrussell
    Collaborator

    A couple of comments on #20802 indicate that the new build flag GL_ENABLE_GET_PROC_ADDRESS causes 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_ADDRESS the 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.

  9. kdashg commented on Dec 18, 2023

    @kdashg
    Contributor

    Why?

    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.

  10. sbc100 commented on Dec 19, 2023

    @sbc100
    Collaborator

    A couple of comments on #20802 indicate that the new build flag GL_ENABLE_GET_PROC_ADDRESS causes problems: Emscripten apps won't build with the new SDK, and once upgraded, can't downgrade to an older SDK.

    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_ADDRESS default 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:

    var supportedExtensionsForGetProcAddress = [
    )

  11. kdashg commented on Dec 19, 2023

    @kdashg
    Contributor

    What is the reasoning for not always using an allowlist, even for !GL_ENABLE_GET_PROC_ADDRESS?

  12. kainino0x commented on Dec 19, 2023

    @kainino0x
    CollaboratorAuthor

    What is the reasoning for not always using an allowlist, even for !GL_ENABLE_GET_PROC_ADDRESS?

    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?

  13. 15 remaining items

  14. juj commented on Dec 20, 2023

    @juj
    Collaborator

    or via glGetString

    glGetString can 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 .

  15. kdashg commented on Dec 21, 2023

    @kdashg
    Contributor

    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.

    A couple of times now I have read above a comment that Emscripten returning all extensions in glGet is a defect, but that by itself is not a problem, and after the PRs that have already landed, I argue, cannot lead to future problems.

    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.

  16. juj commented on Dec 21, 2023

    @juj
    Collaborator

    you are terrified

    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.

    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

    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.

    That is what I see as the price of cutting corners to save some ~800 bytes here.

    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.

  17. kripken commented on Dec 21, 2023

    @kripken
    Member

    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.

  18. kdashg commented on Dec 21, 2023

    @kdashg
    Contributor

    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!

  19. kripken commented on Dec 21, 2023

    @kripken
    Member

    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.

  20. added 4 commits that reference this issue on Dec 21, 2023
    ca8fd5b
    2b2dedc
    33bb5d9
    65f964c
  21. added 3 commits that reference this issue on Jan 5, 2024
    304c744
    9e3c841
    ce7fe78
  22. added a commit that references this issue on Jan 9, 2024
    90696df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions