Skip to content

Commit

Permalink
host_instance.c: take a meminst explicity
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Aug 10, 2024
1 parent 91a1974 commit d497e22
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 106 deletions.
6 changes: 6 additions & 0 deletions cli/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <stdlib.h>
#include <string.h>

#include "cconv.h"
#include "endian.h"
#include "exec_context.h"
#include "exec_debug.h"
Expand Down Expand Up @@ -680,6 +681,11 @@ repl_load_from_buf(struct repl_state *state, const char *modname,
report_clear(&report);
goto fail;
}
#if defined(TOYWASM_ENABLE_WASI)
if (state->wasi != NULL) {
wasi_instance_set_memory(state->wasi, cconv_default_memory(mod->inst));
}
#endif
ret = repl_exec_init(state, mod, trap_ok);
if (ret != 0) {
xlog_printf("repl_exec_init failed\n");
Expand Down
11 changes: 4 additions & 7 deletions lib/cconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
return 0;
}

int
cconv_default_memory(struct exec_context *ctx, const struct instance *inst,
struct meminst **mip)
struct meminst *
cconv_default_memory(const struct instance *inst)
{
const struct module *m = inst->module;
uint32_t memidx;
Expand All @@ -75,9 +74,7 @@ cconv_default_memory(struct exec_context *ctx, const struct instance *inst,
ret = module_find_export(m, &name_default_memory, EXTERNTYPE_MEMORY,
&memidx);
if (ret != 0) {
return trap_with_id(ctx, TRAP_DEFAULT_MEMORY_NOT_FOUND,
"default memory not found");
return NULL;
}
*mip = VEC_ELEM(inst->mems, memidx);
return 0;
return VEC_ELEM(inst->mems, memidx);
}
3 changes: 1 addition & 2 deletions lib/cconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ int cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
uint32_t wasmfuncptr, const struct functype *ft,
const struct funcinst **fip);

int cconv_default_memory(struct exec_context *ctx, const struct instance *inst,
struct meminst **mip);
struct meminst *cconv_default_memory(const struct instance *inst);

__END_EXTERN_C
42 changes: 22 additions & 20 deletions lib/host_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ host_func_check_align(struct exec_context *ctx, uint32_t wasmaddr,
}

int
host_func_copyin(struct exec_context *ctx, void *hostaddr, uint32_t wasmaddr,
size_t len, size_t align)
host_func_copyin(struct exec_context *ctx, struct meminst *mem, void *hostaddr,
uint32_t wasmaddr, size_t len, size_t align)
{
void *p;
int ret;
ret = host_func_check_align(ctx, wasmaddr, align);
if (ret != 0) {
return ret;
}
ret = host_func_getptr(ctx, wasmaddr, len, &p);
ret = host_func_getptr(ctx, mem, wasmaddr, len, &p);
if (ret != 0) {
return ret;
}
Expand All @@ -175,16 +175,17 @@ host_func_copyin(struct exec_context *ctx, void *hostaddr, uint32_t wasmaddr,
}

int
host_func_copyout(struct exec_context *ctx, const void *hostaddr,
uint32_t wasmaddr, size_t len, size_t align)
host_func_copyout(struct exec_context *ctx, struct meminst *mem,
const void *hostaddr, uint32_t wasmaddr, size_t len,
size_t align)
{
void *p;
int ret;
ret = host_func_check_align(ctx, wasmaddr, align);
if (ret != 0) {
return ret;
}
ret = host_func_getptr(ctx, wasmaddr, len, &p);
ret = host_func_getptr(ctx, mem, wasmaddr, len, &p);
if (ret != 0) {
return ret;
}
Expand All @@ -193,31 +194,32 @@ host_func_copyout(struct exec_context *ctx, const void *hostaddr,
}

int
host_func_getptr(struct exec_context *ctx, uint32_t ptr, uint32_t size,
void **pp)
host_func_getptr(struct exec_context *ctx, struct meminst *mem, uint32_t ptr,
uint32_t size, void **pp)
{
return host_func_getptr2(ctx, ptr, size, pp, NULL);
return host_func_getptr2(ctx, mem, ptr, size, pp, NULL);
}

int
host_func_getptr2(struct exec_context *ctx, uint32_t ptr, uint32_t size,
void **pp, bool *movedp)
host_func_getptr2(struct exec_context *ctx, struct meminst *mem, uint32_t ptr,
uint32_t size, void **pp, bool *movedp)
{
struct meminst *meminst;
int ret = cconv_default_memory(ctx, ctx->instance, &meminst);
if (ret != 0) {
return ret;
if (mem == NULL) {
return trap_with_id(
ctx, TRAP_OUT_OF_BOUNDS_MEMORY_ACCESS,
"host function invalid memory access at %08" PRIx32
", size %" PRIu32 ", no memory",
ptr, size);
}
ret = memory_instance_getptr2(meminst, ptr, 0, size, pp, movedp);
int ret = memory_instance_getptr2(mem, ptr, 0, size, pp, movedp);
if (ret == ETOYWASMTRAP) {
ret = trap_with_id(
return trap_with_id(
ctx, TRAP_OUT_OF_BOUNDS_MEMORY_ACCESS,
"host function invalid memory access at %08" PRIx32
", size %" PRIu32 ", meminst size %" PRIu32
", pagesize %" PRIu32,
ptr, size, meminst->size_in_pages,
1 << memtype_page_shift(meminst->type));
assert(ret != 0);
ptr, size, mem->size_in_pages,
1 << memtype_page_shift(mem->type));
}
return ret;
}
Expand Down
20 changes: 11 additions & 9 deletions lib/host_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct host_func {
#endif

struct host_instance {
struct mem_context *mctx;
int dummy;
};

struct host_module {
Expand All @@ -85,14 +85,16 @@ void host_func_dump_params(const struct functype *ft,
const struct cell *params);
int host_func_check_align(struct exec_context *ctx, uint32_t wasmaddr,
size_t align);
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_getptr(struct exec_context *ctx, uint32_t ptr, uint32_t size,
void **pp);
int host_func_getptr2(struct exec_context *ctx, uint32_t ptr, uint32_t size,
void **pp, bool *movedp);
int host_func_copyout(struct exec_context *ctx, struct meminst *mem,
const void *hostaddr, uint32_t wasmaddr, size_t len,
size_t align);
int host_func_copyin(struct exec_context *ctx, struct meminst *mem,
void *hostaddr, uint32_t wasmaddr, size_t len,
size_t align);
int host_func_getptr(struct exec_context *ctx, struct meminst *mem,
uint32_t ptr, uint32_t size, void **pp);
int host_func_getptr2(struct exec_context *ctx, struct meminst *mem,
uint32_t ptr, uint32_t size, void **pp, bool *movedp);
int host_func_trap(struct exec_context *ctx, const char *fmt, ...)
__attribute__((__format__(__printf__, 2, 3)));
struct restart_info;
Expand Down
6 changes: 6 additions & 0 deletions libwasi/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ wasi_instance_create(struct mem_context *mctx,
return 0;
}

void
wasi_instance_set_memory(struct wasi_instance *inst, struct meminst *mem)
{
inst->memory = mem;
}

void
wasi_instance_set_args(struct wasi_instance *inst, int argc,
const char *const *argv)
Expand Down
2 changes: 2 additions & 0 deletions libwasi/wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
struct wasi_instance;
struct import_object;
struct mem_context;
struct meminst;

__BEGIN_EXTERN_C

Expand All @@ -18,6 +19,7 @@ __BEGIN_EXTERN_C

int wasi_instance_create(struct mem_context *mctx,
struct wasi_instance **instp);
void wasi_instance_set_memory(struct wasi_instance *inst, struct meminst *mem);
void wasi_instance_set_args(struct wasi_instance *inst, int argc,
const char *const *argv);
void wasi_instance_set_environ(struct wasi_instance *inst, int nenvs,
Expand Down
10 changes: 6 additions & 4 deletions libwasi/wasi_abi_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ wasi_clock_res_get(struct exec_context *ctx, struct host_instance *hi,
struct cell *results)
{
WASI_TRACE;
struct wasi_instance *wasi = (void *)hi;
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t clockid = HOST_FUNC_PARAM(ft, params, 0, i32);
uint32_t retp = HOST_FUNC_PARAM(ft, params, 1, i32);
Expand All @@ -35,8 +36,8 @@ wasi_clock_res_get(struct exec_context *ctx, struct host_instance *hi,
goto fail;
}
uint64_t result = host_to_le64(timespec_to_ns(&ts));
host_ret = wasi_copyout(ctx, &result, retp, sizeof(result),
WASI_U64_ALIGN);
host_ret = wasi_copyout(ctx, wasi_memory(wasi), &result, retp,
sizeof(result), WASI_U64_ALIGN);
fail:
if (host_ret == 0) {
HOST_FUNC_RESULT_SET(ft, results, 0, i32,
Expand All @@ -52,6 +53,7 @@ wasi_clock_time_get(struct exec_context *ctx, struct host_instance *hi,
struct cell *results)
{
WASI_TRACE;
struct wasi_instance *wasi = (void *)hi;
HOST_FUNC_CONVERT_PARAMS(ft, params);
uint32_t clockid = HOST_FUNC_PARAM(ft, params, 0, i32);
#if 0 /* REVISIT what to do with the precision? */
Expand All @@ -73,8 +75,8 @@ wasi_clock_time_get(struct exec_context *ctx, struct host_instance *hi,
goto fail;
}
uint64_t result = host_to_le64(timespec_to_ns(&ts));
host_ret = wasi_copyout(ctx, &result, retp, sizeof(result),
WASI_U64_ALIGN);
host_ret = wasi_copyout(ctx, wasi_memory(wasi), &result, retp,
sizeof(result), WASI_U64_ALIGN);
fail:
if (host_ret == 0) {
HOST_FUNC_RESULT_SET(ft, results, 0, i32,
Expand Down
12 changes: 6 additions & 6 deletions libwasi/wasi_abi_environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ args_environ_sizes_get(struct exec_context *ctx, struct wasi_instance *wasi,
uint32_t argv_buf_sizep = HOST_FUNC_PARAM(ft, params, 1, i32);
int host_ret;
uint32_t argc_le32 = host_to_le32(argc);
host_ret = wasi_copyout(ctx, &argc_le32, argcp, sizeof(argc_le32),
WASI_U32_ALIGN);
host_ret = wasi_copyout(ctx, wasi_memory(wasi), &argc_le32, argcp,
sizeof(argc_le32), WASI_U32_ALIGN);
if (host_ret != 0) {
goto fail;
}
Expand All @@ -30,8 +30,8 @@ args_environ_sizes_get(struct exec_context *ctx, struct wasi_instance *wasi,
argv_buf_size += strlen(argv[i]) + 1;
}
argv_buf_size = host_to_le32(argv_buf_size);
host_ret = wasi_copyout(ctx, &argv_buf_size, argv_buf_sizep,
sizeof(argv_buf_size), 1);
host_ret = wasi_copyout(ctx, wasi_memory(wasi), &argv_buf_size,
argv_buf_sizep, sizeof(argv_buf_size), 1);
fail:
if (host_ret == 0) {
int ret = 0; /* never fail */
Expand Down Expand Up @@ -69,13 +69,13 @@ args_environ_get(struct exec_context *ctx, struct wasi_instance *wasi,
}
for (i = 0; i < argc; i++) {
size_t sz = strlen(argv[i]) + 1;
host_ret = wasi_copyout(ctx, argv[i],
host_ret = wasi_copyout(ctx, wasi_memory(wasi), argv[i],
le32_to_host(wasm_argv[i]), sz, 1);
if (host_ret != 0) {
goto fail;
}
}
host_ret = wasi_copyout(ctx, wasm_argv, argvp,
host_ret = wasi_copyout(ctx, wasi_memory(wasi), wasm_argv, argvp,
argc * sizeof(*wasm_argv), WASI_U32_ALIGN);
fail:
free(wasm_argv);
Expand Down
Loading

0 comments on commit d497e22

Please sign in to comment.