-
Notifications
You must be signed in to change notification settings - Fork 206
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
+124
−17
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef __wasi_libc_environ_h | ||
#define __wasi_libc_environ_h | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/// Initialize the global environment variable state. Only needs to be | ||
/// called once; most users should call `__wasilibc_ensure_environ` instead. | ||
void __wasilibc_initialize_environ(void); | ||
|
||
/// If `__wasilibc_initialize_environ` has not yet been called, call it. | ||
void __wasilibc_ensure_environ(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <sysexits.h> | ||
#include <wasi/api.h> | ||
#include <wasi/libc.h> | ||
#include <wasi/libc-environ.h> | ||
|
||
// If the program does use `environ`, it'll get this version of | ||
// `__wasilibc_environ`, which is initialized with a constructor function, so | ||
// that it's initialized whenever user code might want to access it. | ||
char **__wasilibc_environ; | ||
extern __typeof(__wasilibc_environ) _environ | ||
__attribute__((weak, alias("__wasilibc_environ"))); | ||
extern __typeof(__wasilibc_environ) environ | ||
__attribute__((weak, alias("__wasilibc_environ"))); | ||
|
||
// We define this function here in the same source file as | ||
// `__wasilibc_environ`, so that this function is called in iff environment | ||
// variable support is used. | ||
// Concerning the 50 -- levels up to 100 are reserved for the implementation, | ||
// so we an arbitrary number in the middle of the range to allow other | ||
// reserved things to go before or after. | ||
__attribute__((constructor(50))) | ||
static void __wasilibc_initialize_environ_eagerly(void) { | ||
__wasilibc_initialize_environ(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// This header file is meant to be included withinin the body of a function | ||
// which uses `__environ`. Code using `__environ` expects it will be initialized | ||
// eagerly. `__wasilibc_environ` is initialized lazily. Provide `__environ` as | ||
// an alias and arrange for the lazy initialization to be performed. | ||
|
||
extern char **__wasilibc_environ; | ||
|
||
__wasilibc_ensure_environ(); | ||
|
||
#ifndef __wasilibc_environ | ||
#define __environ __wasilibc_environ | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.