Skip to content

Commit

Permalink
Allow wasi-libc to initialize its environment variables lazily.
Browse files Browse the repository at this point in the history
Use `__wasilibc_get_environ()` to read the environment variable list
from wasi-libc instead of using `environ`. `environ` is a global
variable which effectively requires wasi-libc to initialize the
environment variables eagerly, and `__wasilibc_get_environ()` is
specifically designed to be an alternative that lets wasi-libc
intiailize its environment variables lazily.

This should have the side effect of fixing at least some of the cases
of #107635.
  • Loading branch information
sunfishcode committed Feb 10, 2023
1 parent 8996ea9 commit 4b11575
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion library/std/src/sys/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod libc {
extern "C" {
pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char;
pub fn chdir(dir: *const c_char) -> c_int;
pub fn __wasilibc_get_environ() -> *mut *mut c_char;
}
}

Expand Down Expand Up @@ -161,7 +162,12 @@ impl Iterator for Env {
pub fn env() -> Env {
unsafe {
let _guard = env_read_lock();
let mut environ = libc::environ;

// Use `__wasilibc_get_environ` instead of `environ` here so that we
// don't require wasi-libc to eagerly initialize the environment
// variables.
let mut environ = libc::__wasilibc_get_environ();

let mut result = Vec::new();
if !environ.is_null() {
while !(*environ).is_null() {
Expand Down

0 comments on commit 4b11575

Please sign in to comment.