Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing default memory existance checks for host functions #247

Merged
merged 7 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/exec_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ enum trapid {
TRAP_UNCAUGHT_EXCEPTION,
TRAP_THROW_REF_NULL,
TRAP_UNRESOLVED_IMPORTED_FUNC,
TRAP_INVALID_MEMORY,
};

enum exec_event {
Expand Down
1 change: 1 addition & 0 deletions lib/exec_insn_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr,
uint32_t offset, uint32_t size, void **pp, bool *movedp)
{
const struct instance *inst = ctx->instance;
assert(memidx < inst->module->nmems + inst->module->nimportedmems);
struct meminst *meminst = VEC_ELEM(inst->mems, memidx);
assert(meminst->allocated <=
(uint64_t)meminst->size_in_pages
Expand Down
36 changes: 36 additions & 0 deletions lib/host_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ host_func_copyout(struct exec_context *ctx, const void *hostaddr,
return 0;
}

static int
check_memidx(struct exec_context *ctx, uint32_t memidx)
{
const struct module *m = ctx->instance->module;
if (memidx < m->nmems + m->nimportedmems) {
return 0;
}
return trap_with_id(
ctx, TRAP_INVALID_MEMORY,
"access to invalid memidx %" PRIx32 " in a host call", memidx);
}

int
host_func_memory_getptr(struct exec_context *ctx, uint32_t memidx,
uint32_t ptr, uint32_t offset, uint32_t size,
void **pp)
{
int ret = check_memidx(ctx, memidx);
if (ret != 0) {
return ret;
}
return memory_getptr(ctx, memidx, ptr, offset, size, pp);
}

int
host_func_memory_getptr2(struct exec_context *ctx, uint32_t memidx,
uint32_t ptr, uint32_t offset, uint32_t size,
void **pp, bool *movedp)
{
int ret = check_memidx(ctx, memidx);
if (ret != 0) {
return ret;
}
return memory_getptr2(ctx, memidx, ptr, offset, size, pp, movedp);
}

int
schedule_call_from_hostfunc(struct exec_context *ctx,
struct restart_info *restart,
Expand Down
6 changes: 6 additions & 0 deletions lib/host_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ int host_func_copyout(struct exec_context *ctx, const void *hostaddr,
uint32_t wasmaddr, size_t len, size_t align);
int host_func_copyin(struct exec_context *ctx, void *hostaddr,
uint32_t wasmaddr, size_t len, size_t align);
int host_func_memory_getptr(struct exec_context *ctx, uint32_t memidx,
uint32_t ptr, uint32_t offset, uint32_t size,
void **pp);
int host_func_memory_getptr2(struct exec_context *ctx, uint32_t memidx,
uint32_t ptr, uint32_t offset, uint32_t size,
void **pp, bool *movedp);
struct restart_info;
int schedule_call_from_hostfunc(struct exec_context *ctx,
struct restart_info *restart,
Expand Down
4 changes: 2 additions & 2 deletions libdyld/dyld_dlfcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ dyld_dlfcn_load_object(struct exec_context *ctx, struct host_instance *hi,
}

void *vp;
ret = memory_getptr(ctx, 0, namep, 0, namelen, &vp);
ret = host_func_memory_getptr(ctx, 0, namep, 0, namelen, &vp);
if (ret != 0) {
user_ret = 1;
goto fail;
Expand Down Expand Up @@ -169,7 +169,7 @@ dyld_dlfcn_resolve_symbol(struct exec_context *ctx, struct host_instance *hi,
const struct dyld_dynamic_object *dobj = &VEC_ELEM(d->dynobjs, idx);

void *vp;
ret = memory_getptr(ctx, 0, namep, 0, namelen, &vp);
ret = host_func_memory_getptr(ctx, 0, namep, 0, namelen, &vp);
if (ret != 0) {
user_ret = 1;
goto fail;
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_abi_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ wasi_path_readlink(struct exec_context *ctx, struct host_instance *hi,
* https://github.com/bytecodealliance/wasmtime/commit/24b607cf751930c51f2b6449cdfbf2e81dce1c31
*/
void *p;
host_ret = memory_getptr(ctx, 0, buf, 0, buflen, &p);
host_ret = host_func_memory_getptr(ctx, 0, buf, 0, buflen, &p);
if (host_ret != 0) {
goto fail;
}
Expand Down
5 changes: 3 additions & 2 deletions libwasi/wasi_abi_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ wasi_poll_oneoff(struct exec_context *ctx, struct host_instance *hi,
if (host_ret != 0) {
goto fail;
}
host_ret = memory_getptr(ctx, 0, in, 0, insize, &p);
host_ret = host_func_memory_getptr(ctx, 0, in, 0, insize, &p);
if (host_ret != 0) {
goto fail;
}
Expand All @@ -62,7 +62,8 @@ wasi_poll_oneoff(struct exec_context *ctx, struct host_instance *hi,
if (host_ret != 0) {
goto fail;
}
host_ret = memory_getptr2(ctx, 0, out, 0, outsize, &p, &moved);
host_ret =
host_func_memory_getptr2(ctx, 0, out, 0, outsize, &p, &moved);
if (host_ret != 0) {
goto fail;
}
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_abi_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ wasi_random_get(struct exec_context *ctx, struct host_instance *hi,
uint32_t buflen = HOST_FUNC_PARAM(ft, params, 1, i32);
int ret = 0;
void *p;
int host_ret = memory_getptr(ctx, 0, buf, 0, buflen, &p);
int host_ret = host_func_memory_getptr(ctx, 0, buf, 0, buflen, &p);
if (host_ret != 0) {
goto fail;
}
Expand Down
8 changes: 4 additions & 4 deletions libwasi/wasi_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ wasi_copyin_iovec(struct exec_context *ctx, uint32_t iov_uaddr,
if (host_ret != 0) {
goto fail;
}
host_ret = memory_getptr(ctx, 0, iov_uaddr, 0,
iov_count * sizeof(struct wasi_iov), &p);
host_ret = host_func_memory_getptr(
ctx, 0, iov_uaddr, 0, iov_count * sizeof(struct wasi_iov), &p);
if (host_ret != 0) {
goto fail;
}
Expand All @@ -94,8 +94,8 @@ wasi_copyin_iovec(struct exec_context *ctx, uint32_t iov_uaddr,
uint32_t iov_len = le32_decode(&iov_in_module[i].iov_len);
xlog_trace("iov [%" PRIu32 "] base %" PRIx32 " len %" PRIu32,
i, iov_base, iov_len);
host_ret = memory_getptr2(ctx, 0, iov_base, 0, iov_len, &p,
&moved);
host_ret = host_func_memory_getptr2(ctx, 0, iov_base, 0,
iov_len, &p, &moved);
if (host_ret != 0) {
goto fail;
}
Expand Down
Loading