Skip to content

Commit 9e8c542

Browse files
authored
add __wasilibc_reset_preopens (#489)
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>
1 parent d038294 commit 9e8c542

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

expected/wasm32-wasip1-threads/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ __wasilibc_pthread_self
395395
__wasilibc_register_preopened_fd
396396
__wasilibc_rename_newat
397397
__wasilibc_rename_oldat
398+
__wasilibc_reset_preopens
398399
__wasilibc_rmdirat
399400
__wasilibc_stat
400401
__wasilibc_tell

expected/wasm32-wasip1/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ __wasilibc_populate_preopens
333333
__wasilibc_register_preopened_fd
334334
__wasilibc_rename_newat
335335
__wasilibc_rename_oldat
336+
__wasilibc_reset_preopens
336337
__wasilibc_rmdirat
337338
__wasilibc_stat
338339
__wasilibc_tell

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ __wasilibc_populate_preopens
348348
__wasilibc_register_preopened_fd
349349
__wasilibc_rename_newat
350350
__wasilibc_rename_oldat
351+
__wasilibc_reset_preopens
351352
__wasilibc_rmdirat
352353
__wasilibc_stat
353354
__wasilibc_tell

libc-bottom-half/sources/preopens.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void __wasilibc_populate_preopens(void) {
260260
if (prefix == NULL)
261261
goto software;
262262

263-
// TODO: Remove the cast on `path` once the witx is updated with
263+
// TODO: Remove the cast on `prefix` once the witx is updated with
264264
// char8 support.
265265
ret = __wasi_fd_prestat_dir_name(fd, (uint8_t *)prefix,
266266
prestat.u.dir.pr_name_len);
@@ -290,3 +290,23 @@ void __wasilibc_populate_preopens(void) {
290290
software:
291291
_Exit(EX_SOFTWARE);
292292
}
293+
294+
void __wasilibc_reset_preopens(void) {
295+
LOCK(lock);
296+
297+
if (num_preopens) {
298+
for (int i = 0; i < num_preopens; ++i) {
299+
free((void*) preopens[i].prefix);
300+
}
301+
free(preopens);
302+
}
303+
304+
preopens_populated = false;
305+
preopens = NULL;
306+
num_preopens = 0;
307+
preopen_capacity = 0;
308+
309+
assert_invariants();
310+
311+
UNLOCK(lock);
312+
}

0 commit comments

Comments
 (0)