Skip to content

Is it possible avoid binding js to use unsafe-eval syntax? #20994

Closed

Description

@yunsii

I have tried to migrate https://github.com/yunsii/fasttext.wasm.js to used in background script of browser extension, after wasm file loaded successful, the binding js encounter the below error:

image
image
image

Activity

  1. brendandahl commented on Jan 3, 2024

    @brendandahl
    Collaborator

    -sDYNAMIC_EXECUTION=0

    emscripten/src/settings.js

    Lines 1302 to 1333 in 25308a1

    // When set to 0, we do not emit eval() and new Function(), which disables some
    // functionality (causing runtime errors if attempted to be used), but allows
    // the emitted code to be acceptable in places that disallow dynamic code
    // execution (chrome packaged app, privileged firefox app, etc.). Pass this flag
    // when developing an Emscripten application that is targeting a privileged or a
    // certified execution environment, see Firefox Content Security Policy (CSP)
    // webpage for details:
    // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
    // in particular the 'unsafe-eval' and 'wasm-unsafe-eval' policies.
    //
    // When this flag is set, the following features (linker flags) are unavailable:
    //
    // - RELOCATABLE: the function loadDynamicLibrary would need to eval().
    //
    // and some features may fall back to slower code paths when they need to:
    // Embind: uses eval() to jit functions for speed.
    //
    // Additionally, the following Emscripten runtime functions are unavailable when
    // DYNAMIC_EXECUTION=0 is set, and an attempt to call them will throw an exception:
    //
    // - emscripten_run_script(),
    // - emscripten_run_script_int(),
    // - emscripten_run_script_string(),
    // - dlopen(),
    // - the functions ccall() and cwrap() are still available, but they are
    // restricted to only being able to call functions that have been exported in
    // the Module object in advance.
    //
    // When -sDYNAMIC_EXECUTION=2 is set, attempts to call to eval() are demoted to
    // warnings instead of throwing an exception.
    // [link]
    var DYNAMIC_EXECUTION = 1;

  2. yunsii commented on Jan 4, 2024

    @yunsii
    Author

    @brendandahl Thanks for your quick response, I will have a try today and the settings.js I will read carefully.

  3. yunsii commented on Jan 5, 2024

    @yunsii
    Author
  4. brendandahl commented on Jan 5, 2024

    @brendandahl
    Collaborator

    I'd note that the bindings will be much slower without eval. Whether that matters is how frequently the JS->C++ code path is used, but there's a new option -sEMBIND_AOT to generate the binding glue code during compilation and keep the speed of the old bindings.

  5. yunsii commented on Jan 6, 2024

    @yunsii
    Author

    bindings will be much slower without eval

    Yes, I've noticed this problem, but -sDYNAMIC_EXECUTION comment:

    emscripten/src/settings.js

    Lines 1316 to 1317 in 249cf8c

    // and some features may fall back to slower code paths when they need to:
    // Embind: uses eval() to jit functions for speed.
    do not provide the -sEMBIND_AOT infomation.

    I will try to use -sEMBIND_AOT today.

  6. yunsii commented on Jan 6, 2024

    @yunsii
    Author

    After add -sEMBIND_AOT option, encounter runtime error:

    image

  7. reopened this on Jan 6, 2024
  8. brendandahl commented on Jan 8, 2024

    @brendandahl
    Collaborator

    Can you set a breakpoint or log what humanName is in craftInvokerFunction and also share the c++ function and what the binding is for it?

  9. added 6 commits that reference this issue on Jan 25, 2024
    7d117c9
    3741aad
    d6d2e79
    4d2c647
    1daa248
    6ae900e
  10. added a commit that references this issue on Apr 12, 2024
    baa07c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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