Skip to content

Commit

Permalink
Fixes to support WASI on Zephyr (#3948)
Browse files Browse the repository at this point in the history
Fixed issues in os_renameat and added os_is_* methods for stdin/stdout/stderr.
ps. #3311.

Signed-off-by: Stephen Berard <[email protected]>
  • Loading branch information
srberard authored Jan 15, 2025
1 parent 87ffe3f commit e3b0244
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions core/shared/platform/zephyr/zephyr_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
* represent a zephyr file descriptor and hold useful informations.
* We also created a file descriptor table to keep track of all the file
* descriptors.
*
* To pass the file descriptor reference to the higher level abstraction, we
* pass the index of the fd table to an `os_file_handle` struct.
* Then in the WASI implementation layer we can retrieve the file descriptor
* reference.
*
* We also fake the stdin, stdout and stderr file descriptors.
* We redirect the write operation on stdin, stdout and stderr to `os_printf`.
* We do not handle write on stdin and read on stdin, stdout and stderr.
Expand All @@ -43,7 +45,7 @@

// We will take the maximum number of open files
// from the Zephyr POSIX configuration
#define CONFIG_WASI_MAX_OPEN_FILES 16
#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_POSIX_MAX_FDS

// Macro to retrieve a file system descriptor and check it's validity.
#define GET_FILE_SYSTEM_DESCRIPTOR(fd, ptr) \
Expand Down Expand Up @@ -711,22 +713,24 @@ os_renameat(os_file_handle old_handle, const char *old_path,
char abs_old_path[MAX_FILE_NAME + 1];
char abs_new_path[MAX_FILE_NAME + 1];

GET_FILE_SYSTEM_DESCRIPTOR(old_handle->fd, ptr);

char *path = strdup(new_path);
if (path == NULL) {
return __WASI_ENOMEM;
}

snprintf(abs_old_path, MAX_FILE_NAME, "%s/%s", prestat_dir, old_path);
snprintf(abs_new_path, MAX_FILE_NAME, "%s/%s", prestat_dir, new_path);

int rc = fs_rename(abs_old_path, abs_new_path);
if (rc < 0) {
free(path);
return convert_errno(-rc);
}

GET_FILE_SYSTEM_DESCRIPTOR(old_handle->fd, ptr);

ptr->path = strdup(new_path);
if (ptr->path == NULL) {
ptr->path = old_path;
return __WASI_ENOMEM;
}

free(ptr->path);
ptr->path = path;
return __WASI_ESUCCESS;
}

Expand Down Expand Up @@ -983,4 +987,22 @@ bool
os_compare_file_handle(os_file_handle handle1, os_file_handle handle2)
{
return handle1->fd == handle2->fd && handle1->is_sock == handle2->is_sock;
}

bool
os_is_stdin_handle(os_file_handle fd)
{
return fd == stdin;
}

bool
os_is_stdout_handle(os_file_handle fd)
{
return fd == stdout;
}

bool
os_is_stderr_handle(os_file_handle fd)
{
return fd == stderr;
}
Binary file removed samples/socket-api/file.wasm
Binary file not shown.

0 comments on commit e3b0244

Please sign in to comment.