Skip to content
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

Fix CWD sync #45

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc-bottom-half/headers/public/wasi/libc-find-relpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int __wasilibc_find_relpath_alloc(
char **relative,
size_t *relative_len,
int can_realloc
) __attribute__((__weak__));
);

#ifdef __cplusplus
}
Expand Down
30 changes: 28 additions & 2 deletions libc-bottom-half/sources/chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -62,15 +63,41 @@ 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;
}

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;

if (__wasilibc_cwd_mallocd)
free(prev_cwd);
theduke marked this conversation as resolved.
Show resolved Hide resolved

__wasilibc_cwd_is_synced = 1; // we synced the cwd
Arshia001 marked this conversation as resolved.
Show resolved Hide resolved
__wasilibc_cwd_mallocd = 1; // we allocated so next time, prev_cwd must be freed.
__wasilibc_cwd_unlock();
} else {
return NULL;
}
}


static char *make_absolute_buf = NULL;
static size_t make_absolute_len = 0;

Expand All @@ -97,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) {
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/sources/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
return __wasilibc_find_relpath_alloc(path, &abs, relative, relative_len, 1);
return __wasilibc_find_relpath(path, &abs, relative, *relative_len);
}
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/sources/preopens.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
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);
}
Expand Down
Loading