Skip to content

Invalid assertion in acorn-optimizer #20818

Description

@wolfpld

Version of emscripten/emsdk:
3.1.50 (047b825)

The following exception is thrown when compiling code with -O2 or higher.

/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:25
    throw new Error(text);
    ^

Error
    at assert (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:25:11)
    at /home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:430:13
    at Array.forEach (<anonymous>)
    at Object.VariableDeclarator (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:429:23)
    at c (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:85:20)
    at recursiveWalk (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:87:5)
    at /home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:83:38
    at maybeChild (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:47:7)
    at Array.forEach (<anonymous>)
    at visitChildren (/home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js:57:15)
em++: error: '/home/wolf/emsdk/node/16.20.0_64bit/bin/node /home/wolf/emsdk/upstream/emscripten/tools/acorn-optimizer.js /tmp/emscripten_temp_ctpxdg9v/x.release.jso1.js AJSDCE minifyWhitespace -o /tmp/emscripten_temp_ctpxdg9v/x.release.jso2.js' failed (returned 1)

} else if (id.type === 'ArrayPattern') {
id.elements.forEach((node) => {
assert(node.type === 'Identifier');
const name = node.name;
ensureData(scopes[scopes.length - 1], name).def = 1;
});

The node.type in the failing case is ObjectPattern.

Activity

  1. sbc100 commented on Dec 1, 2023

    @sbc100
    Collaborator

    Thanks for the report @wolfpld. Is this a new issue that just started happening for you? (i.e. did it work with the same code in previous versions of emscripten?)

    Do you happen for know what code is causing this in the input? If possible perhaps you could attach the JS file that is causing the issue? (You can use EMCC_DEBUG_SAVE=1 to have the js file saved).

  2. wolfpld commented on Dec 1, 2023

    @wolfpld
    Author

    The problem started to appear after integrating the Agora Video Streaming SDK (https://www.agora.io/en/) into the existing code. I also examined emscripten 3.1.48 and 3.1.47 and these versions also have issues in acorn-optimizer.js. There have been some fixes to the optimizer, eg #20639, so I did not look too closely at those old versions, as the code has changed anyway.

    At the moment I do not have a minimal reproducible example that would show the problem. FWIW, the emscripten-generated code is optimized without problems. It is likely that the Agora SDK javascript code uses some unusual language syntax that the minimizer has trouble parsing.

  3. wolfpld commented on Dec 4, 2023

    @wolfpld
    Author

    The issue is not present in 3.1.35.

  4. wolfpld commented on Dec 4, 2023

    @wolfpld
    Author

    To reproduce the issue create the following files:

    a.cpp:

    int main()
    {
    	return 0;
    }

    package.json:

    {
    	"devDependencies": {
    		"agora-rtc-sdk-ng" : "4.19.0",
    		"webpack": "^5.74.0",
    		"webpack-cli": "^4.10.0"
    	}
    }

    webpack.config.js:

    path = require('path');
    
    module.exports = env => {
    	return {
    		entry: path.resolve(__dirname, 'index.js'),
    		output: {
    			filename: 'all.js',
    			path: __dirname
    		},
    		mode: 'production',
    		resolve: {
    			modules: [path.resolve(__dirname, 'node_modules')],
    			extensions: ['.tsx', '.ts', '.js']
    		}
    	}
    }

    index.js:

    import AgoraRTC from 'agora-rtc-sdk-ng'

    Then run the following commands:

    npm install
    node_modules/.bin/webpack
    emcc a.cpp -O2 --pre-js all.js
  5. sbc100 commented on Dec 4, 2023

    @sbc100
    Collaborator

    Is there any chance you could attach the resulting all.js?

  6. wolfpld commented on Dec 4, 2023

    @wolfpld
    Author
  7. added a commit that references this issue on Dec 5, 2023
    fb0d986
  8. wolfpld commented on Dec 5, 2023

    @wolfpld
    Author

    I have checked with 3.1.51-git (fb0d986) and I am now getting:

    /home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:430
              if (id.type === 'ObjectPattern') {
                     ^
    
    TypeError: Cannot read properties of null (reading 'type')
        at traverse (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:430:18)
        at traverse (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:436:15)
        at Object.VariableDeclarator (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:444:9)
        at c (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:94:20)
        at recursiveWalk (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:96:5)
        at /home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:92:38
        at maybeChild (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:56:7)
        at Array.forEach (<anonymous>)
        at visitChildren (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:66:15)
        at c (/home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js:92:7)
    emcc: error: '/home/wolf/emsdk/node/16.20.0_64bit/bin/node /home/wolf/emsdk/emscripten/main/tools/acorn-optimizer.js /tmp/emscripten_temp_znpa_9gt/a.out.js JSDCE minifyWhitespace -o /tmp/emscripten_temp_znpa_9gt/a.out.jso1.js' failed (returned 1)
    

    Same repro.

  9. added a commit that references this issue on Dec 5, 2023
    95b30a8
  10. sbc100 commented on Dec 5, 2023

    @sbc100
    Collaborator

    Followup fix in #20843.

    I verified that your all.js can now run through our acorn pass.

  11. added 2 commits that reference this issue on Dec 5, 2023
    933e098
    f813f42
  12. wolfpld commented on Dec 6, 2023

    @wolfpld
    Author

    Confirming that it works now. Thanks!

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