Skip to content

Commit a4c64b9

Browse files
authored
Rollup merge of #107866 - sunfishcode:sunfishcode/wasi-lazy-environ, r=workingjubilee
Allow wasi-libc to initialize its environment variables lazily. 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.
2 parents d8e4d99 + 4b11575 commit a4c64b9

File tree

1 file changed

+7
-1
lines changed
  • library/std/src/sys/wasi

1 file changed

+7
-1
lines changed

library/std/src/sys/wasi/os.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod libc {
2121
extern "C" {
2222
pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char;
2323
pub fn chdir(dir: *const c_char) -> c_int;
24+
pub fn __wasilibc_get_environ() -> *mut *mut c_char;
2425
}
2526
}
2627

@@ -161,7 +162,12 @@ impl Iterator for Env {
161162
pub fn env() -> Env {
162163
unsafe {
163164
let _guard = env_read_lock();
164-
let mut environ = libc::environ;
165+
166+
// Use `__wasilibc_get_environ` instead of `environ` here so that we
167+
// don't require wasi-libc to eagerly initialize the environment
168+
// variables.
169+
let mut environ = libc::__wasilibc_get_environ();
170+
165171
let mut result = Vec::new();
166172
if !environ.is_null() {
167173
while !(*environ).is_null() {

0 commit comments

Comments
 (0)