From 976f49ad71b70bcaa5796819cd08dc659009c9f1 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Thu, 7 Nov 2024 21:21:06 +0330 Subject: [PATCH 1/4] sync with runtime once about cwd --- libc-bottom-half/sources/chdir.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index aa59881c3..a3899000a 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -17,6 +17,7 @@ void __wasilibc_cwd_unlock(void); #endif extern char *__wasilibc_cwd; static int __wasilibc_cwd_mallocd = 0; +static int __wasilibc_cwd_is_synced = 0; int chdir_legacy(const char *path) { @@ -71,6 +72,31 @@ int chdir_legacy(const char *path) } static const char *make_absolute(const char *path) { + // if the libc has not synced with the runtime yet, call into the runtime to get the cwd + if (!__wasilibc_cwd_is_synced) { + char cwd[256]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + char *new_cwd = malloc(strlen(cwd) + 1); + if (new_cwd != NULL) { + strcpy(new_cwd, cwd); + } + + __wasilibc_cwd_lock(); + char *prev_cwd = __wasilibc_cwd; + __wasilibc_cwd = new_cwd; + __wasilibc_cwd_unlock(); + + if (__wasilibc_cwd_mallocd) + free(prev_cwd); + + __wasilibc_cwd_is_synced = 1; // we synced the cwd + __wasilibc_cwd_mallocd = 1; // we allocated so next time, prev_cwd must be freed. + } else { + return NULL; + } + } + + static char *make_absolute_buf = NULL; static size_t make_absolute_len = 0; From fea3378528a73466ba8b2947ed33c87f05e46bd3 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Thu, 7 Nov 2024 21:21:22 +0330 Subject: [PATCH 2/4] always use the alloc version --- libc-bottom-half/headers/public/wasi/libc-find-relpath.h | 2 +- libc-bottom-half/sources/posix.c | 2 +- libc-bottom-half/sources/preopens.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h index 32dbb0351..df23d808d 100644 --- a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h +++ b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h @@ -70,7 +70,7 @@ int __wasilibc_find_relpath_alloc( char **relative, size_t *relative_len, int can_realloc -) __attribute__((__weak__)); +); #ifdef __cplusplus } diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index 06e9dbe05..0ab2399f1 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -19,7 +19,7 @@ static int find_relpath2( ) { // See comments in `preopens.c` for what this trick is doing. const char *abs; - if (__wasilibc_find_relpath_alloc) + if (&__wasilibc_find_relpath_alloc) return __wasilibc_find_relpath_alloc(path, &abs, relative, relative_len, 1); return __wasilibc_find_relpath(path, &abs, relative, *relative_len); } diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index a5daf5f40..33d3c751d 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -163,7 +163,7 @@ int __wasilibc_find_relpath(const char *path, // If `chdir` is linked, whose object file defines this symbol, then we // call that. Otherwise if the program can't `chdir` then `path` is // absolute (or relative to the root dir), so we delegate to `find_abspath` - if (__wasilibc_find_relpath_alloc) + if (&__wasilibc_find_relpath_alloc) return __wasilibc_find_relpath_alloc(path, abs_prefix, relative_path, &relative_path_len, 0); return __wasilibc_find_abspath(path, abs_prefix, (const char**) relative_path); } From f2690c2259be01252bf1dfddc9eb6653efd777cf Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Fri, 8 Nov 2024 03:17:39 +0330 Subject: [PATCH 3/4] improve thread safety --- libc-bottom-half/sources/chdir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libc-bottom-half/sources/chdir.c b/libc-bottom-half/sources/chdir.c index a3899000a..87c66a8bc 100644 --- a/libc-bottom-half/sources/chdir.c +++ b/libc-bottom-half/sources/chdir.c @@ -63,11 +63,12 @@ int chdir_legacy(const char *path) __wasilibc_cwd_lock(); char *prev_cwd = __wasilibc_cwd; __wasilibc_cwd = new_cwd; - __wasilibc_cwd_unlock(); if (__wasilibc_cwd_mallocd) free(prev_cwd); + __wasilibc_cwd_mallocd = 1; + __wasilibc_cwd_unlock(); return 0; } @@ -84,13 +85,13 @@ static const char *make_absolute(const char *path) { __wasilibc_cwd_lock(); char *prev_cwd = __wasilibc_cwd; __wasilibc_cwd = new_cwd; - __wasilibc_cwd_unlock(); if (__wasilibc_cwd_mallocd) free(prev_cwd); __wasilibc_cwd_is_synced = 1; // we synced the cwd __wasilibc_cwd_mallocd = 1; // we allocated so next time, prev_cwd must be freed. + __wasilibc_cwd_unlock(); } else { return NULL; } @@ -123,7 +124,6 @@ static const char *make_absolute(const char *path) { __wasilibc_cwd_lock(); size_t cwd_len = strlen(__wasilibc_cwd); size_t path_len = path ? strlen(path) : 0; - __wasilibc_cwd_unlock(); int need_slash = __wasilibc_cwd[cwd_len - 1] == '/' ? 0 : 1; size_t alloc_len = cwd_len + path_len + 1 + need_slash; if (alloc_len > make_absolute_len) { From c4dc0bc9f5d422753d9e505d957110e003bb6839 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Tue, 12 Nov 2024 23:44:50 +0330 Subject: [PATCH 4/4] revert rel_path changes --- libc-bottom-half/headers/public/wasi/libc-find-relpath.h | 2 +- libc-bottom-half/sources/posix.c | 2 +- libc-bottom-half/sources/preopens.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h index df23d808d..32dbb0351 100644 --- a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h +++ b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h @@ -70,7 +70,7 @@ int __wasilibc_find_relpath_alloc( char **relative, size_t *relative_len, int can_realloc -); +) __attribute__((__weak__)); #ifdef __cplusplus } diff --git a/libc-bottom-half/sources/posix.c b/libc-bottom-half/sources/posix.c index 0ab2399f1..06e9dbe05 100644 --- a/libc-bottom-half/sources/posix.c +++ b/libc-bottom-half/sources/posix.c @@ -19,7 +19,7 @@ static int find_relpath2( ) { // See comments in `preopens.c` for what this trick is doing. const char *abs; - if (&__wasilibc_find_relpath_alloc) + if (__wasilibc_find_relpath_alloc) return __wasilibc_find_relpath_alloc(path, &abs, relative, relative_len, 1); return __wasilibc_find_relpath(path, &abs, relative, *relative_len); } diff --git a/libc-bottom-half/sources/preopens.c b/libc-bottom-half/sources/preopens.c index 33d3c751d..a5daf5f40 100644 --- a/libc-bottom-half/sources/preopens.c +++ b/libc-bottom-half/sources/preopens.c @@ -163,7 +163,7 @@ int __wasilibc_find_relpath(const char *path, // If `chdir` is linked, whose object file defines this symbol, then we // call that. Otherwise if the program can't `chdir` then `path` is // absolute (or relative to the root dir), so we delegate to `find_abspath` - if (&__wasilibc_find_relpath_alloc) + if (__wasilibc_find_relpath_alloc) return __wasilibc_find_relpath_alloc(path, abs_prefix, relative_path, &relative_path_len, 0); return __wasilibc_find_abspath(path, abs_prefix, (const char**) relative_path); }