Skip to content

Commit

Permalink
Check preopens only if needed
Browse files Browse the repository at this point in the history
  * Use `constructor` to eliminate them if unnecessary
  • Loading branch information
tyfkda committed Oct 28, 2024
1 parent c4ca0e3 commit c4c31a6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
18 changes: 0 additions & 18 deletions libsrc/_wasm/crt0/_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@

extern void __wasm_call_ctors(void);

int __max_preopen_fd = 3;

static int find_preopens(void) {
for (int fd = 3; ; ++fd) {
Prestat prestat;
int result = fd_prestat_get(fd, &prestat);
if (result != 0)
return fd;

// char buf[256];
// fd_prestat_dir_name(fd, buf, prestat.u.dir.pr_name_len); // TODO: Confirm prestat.u.dir.pr_name_len < sizeof(buf)
// buf[prestat.u.dir.pr_name_len] = '\0';
// fprintf(stderr, "preopens: %d, %s\n", fd, buf);
}
}

void _start(void) {
#define main __main_argc_argv
extern int main(int, char**);
Expand All @@ -38,8 +22,6 @@ void _start(void) {
}
argv[argc] = NULL;

__max_preopen_fd = find_preopens();

__wasm_call_ctors();

int ec = main(argc, argv);
Expand Down
20 changes: 20 additions & 0 deletions libsrc/_wasm/unistd/_detect_preopen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../wasi.h"

int __max_preopen_fd = 3;

__attribute__((constructor))
static void find_preopens(void) {
for (int fd = 3; ; ++fd) {
Prestat prestat;
int result = fd_prestat_get(fd, &prestat);
if (result != 0) {
__max_preopen_fd = fd;
return;
}

// char buf[256];
// fd_prestat_dir_name(fd, buf, prestat.u.dir.pr_name_len); // TODO: Confirm prestat.u.dir.pr_name_len < sizeof(buf)
// buf[prestat.u.dir.pr_name_len] = '\0';
// fprintf(stderr, "preopens: %d, %s\n", fd, buf);
}
}
27 changes: 27 additions & 0 deletions libsrc/_wasm/unistd/_set_stat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "sys/stat.h"
#include "../wasi.h"

void _set_stat(Filestat *fs, struct stat *st) {
mode_t mode = 0;
switch (fs->filetype) {
case FILETYPE_BLOCK_DEVICE: mode = S_IFBLK; break;
case FILETYPE_CHARACTER_DEVICE: mode = S_IFCHR; break;
case FILETYPE_DIRECTORY: mode = S_IFDIR; break;
case FILETYPE_REGULAR_FILE: mode = S_IFREG; break;
case FILETYPE_SOCKET_DGRAM: mode = S_IFSOCK; break;
case FILETYPE_SOCKET_STREAM: mode = S_IFSOCK; break;
case FILETYPE_SYMBOLIC_LINK: mode = S_IFLNK; break;
}

st->st_dev = fs->dev;
st->st_ino = fs->ino;
st->st_mode = mode;
st->st_nlink = fs->nlink;
st->st_uid = 0;
st->st_gid = 0;
st->st_rdev = 0;
st->st_size = fs->size;
st->st_blksize = 0;
st->st_blocks = 0;
// TODO: atim, mtim, ctim
}
3 changes: 2 additions & 1 deletion libsrc/_wasm/unistd/fstat.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "sys/stat.h"
#include "../wasi.h"

extern void _set_stat(Filestat *fs, struct stat *st);

int fstat(int fd, struct stat *st) {
Filestat fs;
uint32_t result = fd_filestat_get(fd, &fs);
if (result == 0) {
extern void _set_stat(Filestat *fs, struct stat *st);
_set_stat(&fs, st);
return 0;
}
Expand Down
25 changes: 1 addition & 24 deletions libsrc/_wasm/unistd/stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,7 @@

extern int __max_preopen_fd;

void _set_stat(Filestat *fs, struct stat *st) {
mode_t mode = 0;
switch (fs->filetype) {
case FILETYPE_BLOCK_DEVICE: mode = S_IFBLK; break;
case FILETYPE_CHARACTER_DEVICE: mode = S_IFCHR; break;
case FILETYPE_DIRECTORY: mode = S_IFDIR; break;
case FILETYPE_REGULAR_FILE: mode = S_IFREG; break;
case FILETYPE_SOCKET_DGRAM: mode = S_IFSOCK; break;
case FILETYPE_SOCKET_STREAM: mode = S_IFSOCK; break;
case FILETYPE_SYMBOLIC_LINK: mode = S_IFLNK; break;
}

st->st_dev = fs->dev;
st->st_ino = fs->ino;
st->st_mode = mode;
st->st_nlink = fs->nlink;
st->st_uid = 0;
st->st_gid = 0;
st->st_rdev = 0;
st->st_size = fs->size;
st->st_blksize = 0;
st->st_blocks = 0;
// TODO: atim, mtim, ctim
}
extern void _set_stat(Filestat *fs, struct stat *st);

int stat(const char *fn, struct stat *st) {
memset(st, 0, sizeof(st));
Expand Down

0 comments on commit c4c31a6

Please sign in to comment.