Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a toolchain-independent ABI document, and propose _initialize #203

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Commits on Mar 6, 2023

  1. Add a toolchain-independent ABI document, and propose _initialize

    The Wasm ecosystem is currently not consistent in how "constructors" such
    as C++ static initializers and similar features in other languages are
    implemented, and the result is users reporting constructs running multiple
    times, and other users reporting constructors not getting run when they
    should.
    
    WASI has [defined a convention] using an exported function named
    `_initialize`, however not all users are using WASI conventions. In
    particular, users of what is sometimes called "wasm32-unknown-unknown"
    are not expecting to follow WASI conventions. However, they still have a
    need for constructors working in a reliable way.
    
    To address this, I propose moving this out of WASI and defining this as
    a toolchain-independent ABI, here in tool-conventions. This would
    recognize the `_initialize` function as the toolchain-independent way
    to ensure that constructors are properly called before other exports are
    accessed.
    
    Related activities
    ------------------
    
    In the component model, there is a proposal to add a
    [second initialization phase]. If that's done, then component-model
    toolchains could arrange for this `_initialize` function to be called
    automatically by this second initialization mechanism.
    
    Considered alternatives
    -----------------------
    
    It is tempting to use the [Wasm start function] for C++ constructors;
    this has been [extensively discussed], and the short answer is, the Wasm
    start function is often called at a time when the outside environment
    can't access the module's exports, and C++ constructors can run
    arbitrary user code which may generate calls to things that need to
    access the module's exports.
    
    It's also tempting to propose defining a second initialization phase in
    core Wasm. I'm not opposed to this, but it is more complex at the core
    Wasm level than at the component-model level. For example, in Emscripten,
    Wasm modules depend on JS code being able to run after the exports are
    available but before the initialization function is called, which
    wouldn't be possible if we simply call the initilaization function as
    part of the linking step.
    
    Wasm-ld has a [`__wasm_call_ctors` function], and in theory we could use
    that name instead of `_initialize`, but wasm-ld already does insert some
    initialization in addition to just constructors, so I think it makes
    sense to use `_initialize` as the exported function, which may call
    `__wasm_call_ctors` in its body.
    
    Process
    -------
    
    We don't have a formal process defined for tool-convention proposals,
    but because this is proposal has potentially wide-ranging impacts, I
    propose to follow the following process:
    
     - I'm starting by posting this PR here, and people can comment on it.
       If a better alternative emerges, I'll close this PR.
    
     - After discussion here settles, if a better alternative hasn't emerged,
       I plan to request a CG meeting agenda item to present this topic to the
       CG, and seek feedback there, to ensure that it has CG-level visibility.
    
     - If the CG is in favor of it, then I'd propose we merge this PR.
    
    [defined a convention]: https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md
    [second initialization phase]: WebAssembly/component-model#146
    [Wasm start function]: https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start
    [extensively discussed]: WebAssembly/design#1160
    [`__wasm_call_ctors` function]: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md#start-section
    sunfishcode committed Mar 6, 2023
    Configuration menu
    Copy the full SHA
    19a35b8 View commit details
    Browse the repository at this point in the history

Commits on Mar 7, 2023

  1. Configuration menu
    Copy the full SHA
    710ec66 View commit details
    Browse the repository at this point in the history
  2. Update BasicModuleABI.md

    Co-authored-by: Derek Schuff <dschuff@chromium.org>
    sunfishcode and dschuff authored Mar 7, 2023
    Configuration menu
    Copy the full SHA
    875879d View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    8e07360 View commit details
    Browse the repository at this point in the history