Skip to content

asm2wasm emits instructions between return and end #862

@sunfishcode

Description

@sunfishcode

When given input containing trivially unreachable code, such as this:

Module["asm"] = (function() {
    'use asm';

    // EMSCRIPTEN_START_FUNCS

    function f(i) {
        i = i | 0;
        i = i+1;
        return i|0;
        i = i+2|0;
        return i|0;
    }

    // EMSCRIPTEN_END_FUNCS

    return {f: f};
})();

asm2wasm by default emits a wasm module containing unreachable code. Adding -O1 or other options to asm2wasm does cause it to eliminate such code. However, it may be useful to make asm2wasm eliminate at least unreachable code between a br/return/br_table/unreachable and the end of the enclosing block, since the current WebAssembly spec permits a variety of behaviors in such cases (Firefox and V8 currently differ, and other variations are permitted by the spec too). Also, there's an outstanding WebAssembly design discussion on this topic happening in WebAssembly/design#894 in which one of the options is to prohibit such instructions.

I noticed this issue while investigating
https://bugzilla.mozilla.org/show_bug.cgi?id=1324032, looking for possible causes for http://files.unity3d.com/jonas/benchmarks/5.5-wasm-release containing unreachable instructions, though I don't know for sure if the example above is the cause in that case.

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