Skip to content

Commit

Permalink
Modify fd_prestat to hold C-str together with its length
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon authored and sunfishcode committed May 1, 2019
1 parent f7245f4 commit d6b2fae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 9 additions & 6 deletions wasmtime-wasi/sandboxed-system-primitives/src/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ static bool fd_prestats_grow(
return false;

// Mark all new file descriptors as unused.
for (size_t i = pt->size; i < size; ++i)
prestats[i].dir = NULL;
for (size_t i = pt->size; i < size; ++i) {
prestats[i].dir_name = NULL;
prestats[i].dir_name_len = 0;
}
pt->prestats = prestats;
pt->size = size;
}
Expand All @@ -295,7 +297,8 @@ bool fd_prestats_insert(
return false;
}

pt->prestats[fd].dir = strdup(dir);
pt->prestats[fd].dir_name = strdup(dir);
pt->prestats[fd].dir_name_len = strlen(dir);
rwlock_unlock(&pt->lock);
return true;
}
Expand All @@ -310,7 +313,7 @@ static __wasi_errno_t fd_prestats_get_entry(
if (fd >= pt->size)
return __WASI_EBADF;
struct fd_prestat *prestat = &pt->prestats[fd];
if (prestat->dir == NULL)
if (prestat->dir_name == NULL)
return __WASI_EBADF;

*ret = prestat;
Expand Down Expand Up @@ -664,12 +667,12 @@ __wasi_errno_t wasmtime_ssp_fd_prestat_dir_name(
rwlock_unlock(&prestats->lock);
return error;
}
if (path_len != strlen(prestat->dir)) {
if (path_len != prestat->dir_name_len) {
rwlock_unlock(&prestats->lock);
return EINVAL;
}

memcpy(path, prestat->dir, path_len);
memcpy(path, prestat->dir_name, path_len);

rwlock_unlock(&prestats->lock);

Expand Down
3 changes: 2 additions & 1 deletion wasmtime-wasi/sandboxed-system-primitives/src/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ struct fd_entry;
struct syscalls;

struct fd_prestat {
const char *dir;
const char *dir_name;
size_t dir_name_len;
};

struct fd_table {
Expand Down
7 changes: 3 additions & 4 deletions wasmtime-wasi/src/host_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn fd_prestats_get_entry(
}

let prestat = unsafe { &*pt.prestats.add(fd as usize) };
if prestat.dir == ::std::ptr::null() {
if prestat.dir_name == ::std::ptr::null() {
return None;
}

Expand Down Expand Up @@ -133,10 +133,9 @@ pub fn wasmtime_ssp_fd_prestat_get(
rwlock_rdlock!(prestats);

let ret_code = if let Some(prestat) = fd_prestats_get_entry(prestats, fd) {
(*buf).pr_type = host::__WASI_PREOPENTYPE_DIR;
buf.pr_type = host::__WASI_PREOPENTYPE_DIR;
unsafe {
let dir_name = ::std::ffi::CStr::from_ptr((*prestat).dir).to_str().unwrap();
(*buf).u.dir.pr_name_len = dir_name.len();
buf.u.dir.pr_name_len = prestat.dir_name_len;
}
host::__WASI_ESUCCESS
} else {
Expand Down

0 comments on commit d6b2fae

Please sign in to comment.