You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we execute this function repeatedly, then we will see the program can load env-vars correctly; but the program memory usage keeps increasing. (I've also tested other wasm implementations as well, e.g. WasmEdge crate)
I found the __wasilibc_initialize_environ function might be the root cause of the memory leak, and after changing some lines like the following, the program could run normally.
wujunzhuo@wujunzhuo-macbook wasi-libc % git diff
diff --git a/libc-bottom-half/sources/__wasilibc_initialize_environ.c b/libc-bottom-half/sources/__wasilibc_initialize_environ.c
index 2d31c5d..65738d1 100644
--- a/libc-bottom-half/sources/__wasilibc_initialize_environ.c+++ b/libc-bottom-half/sources/__wasilibc_initialize_environ.c@@ -15,7 +15,7 @@ weak char **__wasilibc_environ = (char **)-1;
// See the comments in libc-environ.h.
void __wasilibc_ensure_environ(void) {
- if (__wasilibc_environ == (char **)-1) {+ if (__wasilibc_environ == (char **)-1 || __wasilibc_environ == NULL) {
__wasilibc_initialize_environ();
}
}
diff --git a/libc-bottom-half/sources/environ.c b/libc-bottom-half/sources/environ.c
index 50d60de..fedfc83 100644
--- a/libc-bottom-half/sources/environ.c+++ b/libc-bottom-half/sources/environ.c@@ -20,12 +20,12 @@ weak_alias(__wasilibc_environ, environ);
// reserved things to go before or after.
__attribute__((constructor(50)))
static void __wasilibc_initialize_environ_eagerly(void) {
- __wasilibc_initialize_environ();+ __wasilibc_ensure_environ();
}
// See the comments in libc-environ.h.
void __wasilibc_maybe_reinitialize_environ_eagerly(void) {
// This translation unit is linked in if `environ` is used, meaning we need
// to eagerly reinitialize the environment variables.
- __wasilibc_initialize_environ();+ __wasilibc_ensure_environ();
}
However, I can't figure out why __wasilibc_environ might be initialized to NULL: since if we only compare it with -1, the wasm function couldn't load any environment variable:
This is a very simple Rust guest function, which just prints out an environment variable
USER:If we execute this function repeatedly, then we will see the program can load env-vars correctly; but the program memory usage keeps increasing. (I've also tested other wasm implementations as well, e.g. WasmEdge crate)
I found the
__wasilibc_initialize_environfunction might be the root cause of the memory leak, and after changing some lines like the following, the program could run normally.However, I can't figure out why
__wasilibc_environmight be initialized toNULL: since if we only compare it with-1, the wasm function couldn't load any environment variable: