Lazy-initialize the environment variables.#184
Conversation
This is the first in a series of PRs to make it easier to use WASI libc in Wasm modules that don't have a `main` function. By initializing the environment on demand, we avoid depending on having `__wasm_call_ctors` run. This uses weak symbols strategically to ensure that if `environ` is used, it is initialized eagerly, but if only `getenv` and friends are used, the environment is initialized lazily. Eventually, I expect we'll have a convention for wasm modules without main functions which will allow the `__wasm_call_ctors` function to be called automatically, but this helps in simple cases for now. Fixes #180.
| { | ||
| #ifdef __wasilibc_unmodified_upstream // Lazy environment variable init. | ||
| #else | ||
| #include "wasi/libc-environ-compat.h" |
There was a problem hiding this comment.
I'd add a comment here (& in the other uses) indicating that this header is intended to be used inside a func body, and it redefines the __environ symbol. The latter seems like it may cause unintended consequences if someone performed otherwise innocent-looking code motion without understanding the details of that header.
There was a problem hiding this comment.
Comments added. The most common cases of code motion are guarded against, because if you use __environ before it's defined, you'll get errors, and if you use the redefined __environ in a function other than the one the header was included in, __wasilibc_environ won't be declared and you'll get errors. But I agree, this is particularly tricky code, so comments are good.
To support this, add a mechanism for filtering test output to filter out uninteresting diffs. This adds some coverage for the code being changed in WebAssembly/wasi-libc#184.
To support this, add a mechanism for filtering test output to filter out uninteresting diffs. This adds some coverage for the code being changed in WebAssembly/wasi-libc#184.
Brings in WebAssembly/wasi-libc#184 which can help standalone programs with environment variables!
…lbini Update the bundled wasi-libc with libstd Brings in WebAssembly/wasi-libc#184 which can help standalone programs with environment variables!
This is the first in a series of PRs to make it easier to use WASI libc
in Wasm modules that don't have a
mainfunction. By initializing theenvironment on demand, we avoid depending on having
__wasm_call_ctorsrun.
This uses weak symbols strategically to ensure that if
environisused, it is initialized eagerly, but if only
getenvand friendsare used, the environment is initialized lazily.
Eventually, I expect we'll have a convention for wasm modules without
main functions which will allow the
__wasm_call_ctorsfunction to becalled automatically, but this helps in simple cases for now.
Fixes #180. cc @alexcrichton