Skip to content

Commit

Permalink
add __wasilibc_reset_preopens (#489)
Browse files Browse the repository at this point in the history
This resets the preopens table to an uninitialized state, forcing it to be
reinitialized next time it is needed.  This is useful when pre-initializing
using e.g. `wizer` or `component-init`.  Such tools are capable of taking a
snapshot of a running application which may later be resumed on an unrelated
runtime (which may have its own, unrelated preopens).

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Apr 11, 2024
1 parent d038294 commit 9e8c542
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions expected/wasm32-wasip1-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ __wasilibc_pthread_self
__wasilibc_register_preopened_fd
__wasilibc_rename_newat
__wasilibc_rename_oldat
__wasilibc_reset_preopens
__wasilibc_rmdirat
__wasilibc_stat
__wasilibc_tell
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip1/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ __wasilibc_populate_preopens
__wasilibc_register_preopened_fd
__wasilibc_rename_newat
__wasilibc_rename_oldat
__wasilibc_reset_preopens
__wasilibc_rmdirat
__wasilibc_stat
__wasilibc_tell
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ __wasilibc_populate_preopens
__wasilibc_register_preopened_fd
__wasilibc_rename_newat
__wasilibc_rename_oldat
__wasilibc_reset_preopens
__wasilibc_rmdirat
__wasilibc_stat
__wasilibc_tell
Expand Down
22 changes: 21 additions & 1 deletion libc-bottom-half/sources/preopens.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void __wasilibc_populate_preopens(void) {
if (prefix == NULL)
goto software;

// TODO: Remove the cast on `path` once the witx is updated with
// TODO: Remove the cast on `prefix` once the witx is updated with
// char8 support.
ret = __wasi_fd_prestat_dir_name(fd, (uint8_t *)prefix,
prestat.u.dir.pr_name_len);
Expand Down Expand Up @@ -290,3 +290,23 @@ void __wasilibc_populate_preopens(void) {
software:
_Exit(EX_SOFTWARE);
}

void __wasilibc_reset_preopens(void) {
LOCK(lock);

if (num_preopens) {
for (int i = 0; i < num_preopens; ++i) {
free((void*) preopens[i].prefix);
}
free(preopens);
}

preopens_populated = false;
preopens = NULL;
num_preopens = 0;
preopen_capacity = 0;

assert_invariants();

UNLOCK(lock);
}

0 comments on commit 9e8c542

Please sign in to comment.