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

Lazy-initialize the environment variables. #184

Merged
merged 2 commits into from
Mar 19, 2020
Merged

Conversation

sunfishcode
Copy link
Member

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. cc @alexcrichton

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.
@@ -7,6 +7,10 @@ weak_alias(dummy, __env_rm_add);

int clearenv()
{
#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init.
#else
#include "wasi/libc-environ-compat.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

sunfishcode added a commit to WebAssembly/wasi-sdk that referenced this pull request Mar 11, 2020
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.
sunfishcode added a commit to WebAssembly/wasi-sdk that referenced this pull request Mar 11, 2020
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.
@sunfishcode sunfishcode merged commit 9efc2f4 into master Mar 19, 2020
@sunfishcode sunfishcode deleted the lazy-environ branch March 19, 2020 16:32
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Mar 19, 2020
Brings in WebAssembly/wasi-libc#184 which can help standalone programs
with environment variables!
Centril added a commit to Centril/rust that referenced this pull request Mar 23, 2020
…lbini

Update the bundled wasi-libc with libstd

Brings in WebAssembly/wasi-libc#184 which can help standalone programs
with environment variables!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment variables don't work if main isn't used
2 participants