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

Use the correct memory for host func #254

Merged
merged 12 commits into from
Aug 10, 2024
23 changes: 23 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 @@ -588,6 +589,21 @@ repl_exec_init(struct repl_state *state, struct repl_module_state *mod,
return ret;
}

static void
set_memory(struct repl_state *state, struct meminst *mem)
{
#if defined(TOYWASM_ENABLE_WASI)
if (state->wasi != NULL) {
wasi_instance_set_memory(state->wasi, mem);
}
#if defined(TOYWASM_ENABLE_WASI_THREADS)
if (state->wasi_threads != NULL) {
wasi_threads_instance_set_memory(state->wasi_threads, mem);
}
#endif
#endif
}

static int
repl_load_from_buf(struct repl_state *state, const char *modname,
struct repl_module_state *mod, bool trap_ok)
Expand Down Expand Up @@ -680,6 +696,7 @@ repl_load_from_buf(struct repl_state *state, const char *modname,
report_clear(&report);
goto fail;
}
set_memory(state, cconv_memory(mod->inst));
ret = repl_exec_init(state, mod, trap_ok);
if (ret != 0) {
xlog_printf("repl_exec_init failed\n");
Expand Down Expand Up @@ -727,6 +744,12 @@ toywasm_repl_load(struct repl_state *state, const char *modname,
if (ret != 0) {
return ret;
}
set_memory(state, dyld_memory(d));
ret = dyld_execute_init_funcs(d);
if (ret != 0) {
dyld_clear(d);
return ret;
}
state->modules.lsize++;
return 0;
}
Expand Down
27 changes: 15 additions & 12 deletions examples/hostfunc/hostfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ print_backtrace(const char *func, const struct exec_context *ctx)
}

static int
load(struct exec_context *ctx, uint32_t pp, uint32_t *resultp)
load(struct exec_context *ctx, struct host_instance *hi, uint32_t pp,
uint32_t *resultp)
{
int host_ret;
uint32_t le32;
Expand All @@ -29,19 +30,21 @@ load(struct exec_context *ctx, uint32_t pp, uint32_t *resultp)
* *resultp = *(*pp)++
*/

host_ret = host_func_copyin(ctx, &le32, pp, 4, 4);
host_ret =
host_func_copyin(ctx, host_func_memory(hi), &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t p = le32_to_host(le32);
host_ret = host_func_copyin(ctx, &le32, p, 4, 4);
host_ret = host_func_copyin(ctx, host_func_memory(hi), &le32, p, 4, 4);
if (host_ret != 0) {
goto fail;
}
uint32_t result = le32_to_host(le32);
p += 4;
le32 = host_to_le32(p);
host_ret = host_func_copyout(ctx, &le32, pp, 4, 4);
host_ret =
host_func_copyout(ctx, host_func_memory(hi), &le32, pp, 4, 4);
if (host_ret != 0) {
goto fail;
}
Expand All @@ -51,18 +54,18 @@ load(struct exec_context *ctx, uint32_t pp, uint32_t *resultp)
}

static int
load_func(struct exec_context *ctx, const struct functype *ft, uint32_t pp,
const struct funcinst **fip)
load_func(struct exec_context *ctx, struct host_instance *hi,
const struct functype *ft, uint32_t pp, const struct funcinst **fip)
{
int host_ret;
uint32_t funcptr;
host_ret = load(ctx, pp, &funcptr);
host_ret = load(ctx, hi, pp, &funcptr);
if (host_ret != 0) {
goto fail;
}
const struct funcinst *func;
host_ret =
cconv_deref_func_ptr(ctx, ctx->instance, funcptr, ft, &func);
host_ret = cconv_deref_func_ptr(ctx, host_func_func_table(hi), funcptr,
ft, &func);
if (host_ret != 0) {
goto fail;
}
Expand All @@ -83,7 +86,7 @@ my_host_inst_load(struct exec_context *ctx, struct host_instance *hi,
print_backtrace(__func__, ctx);

uint32_t result;
host_ret = load(ctx, pp, &result);
host_ret = load(ctx, hi, pp, &result);
if (host_ret == 0) {
HOST_FUNC_RESULT_SET(ft, results, 0, i32, result);
}
Expand All @@ -103,7 +106,7 @@ my_host_inst_load_call(struct exec_context *ctx, struct host_instance *hi,
print_backtrace(__func__, ctx);

const struct funcinst *func;
host_ret = load_func(ctx, ft, pp, &func);
host_ret = load_func(ctx, hi, ft, pp, &func);
if (host_ret != 0) {
goto fail;
}
Expand Down Expand Up @@ -182,7 +185,7 @@ my_host_inst_load_call_add(struct exec_context *ctx, struct host_instance *hi,
* ours. (ft)
*/
const struct funcinst *func;
host_ret = load_func(ctx, ft, pp, &func);
host_ret = load_func(ctx, hi, ft, pp, &func);
if (host_ret != 0) {
goto fail;
}
Expand Down
16 changes: 14 additions & 2 deletions examples/hostfunc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <unistd.h>

#include <toywasm/host_instance.h>
#include <toywasm/instance.h>
#include <toywasm/mem.h>
#include <toywasm/xlog.h>
Expand All @@ -18,6 +19,14 @@
#include "runwasi.h"
#include "runwasi_cli_args.h"

static void
set_resources(void *v, struct meminst *mem, struct tableinst *func_table)
{
struct host_instance *hi = v;
hi->memory = mem;
hi->func_table = func_table;
}

int
main(int argc, char **argv)
{
Expand All @@ -37,15 +46,18 @@ main(int argc, char **argv)
}
struct mem_context mctx;
mem_context_init(&mctx);
struct host_instance hi;
hi.memory = NULL;
hi.func_table = NULL;
struct import_object *import_obj;
ret = import_object_create_for_my_host_inst(&mctx, NULL, &import_obj);
ret = import_object_create_for_my_host_inst(&mctx, &hi, &import_obj);
if (ret != 0) {
exit(1);
}
ret = runwasi(&mctx, a->filename, a->ndirs, a->dirs, a->nenvs,
(const char *const *)a->envs, a->argc,
(const char *const *)a->argv, stdio_fds, import_obj,
&wasi_exit_code);
set_resources, &hi, &wasi_exit_code);
import_object_destroy(&mctx, import_obj);
mem_context_clear(&mctx);
free(a->dirs);
Expand Down
2 changes: 1 addition & 1 deletion examples/log_execution/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ main(int argc, char **argv)
ret = runwasi(&mctx, a->filename, a->ndirs, a->dirs, a->nenvs,
(const char *const *)a->envs, a->argc,
(const char *const *)a->argv, stdio_fds, import_obj,
&wasi_exit_code);
NULL, NULL, &wasi_exit_code);
import_object_destroy(&mctx, import_obj);
mem_context_clear(&mctx);
free(a->dirs);
Expand Down
4 changes: 2 additions & 2 deletions examples/runwasi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ main(int argc, char **argv)
mem_context_init(&mctx);
ret = runwasi(&mctx, a->filename, a->ndirs, a->dirs, a->nenvs,
(const char *const *)a->envs, a->argc,
(const char *const *)a->argv, stdio_fds, NULL,
&wasi_exit_code);
(const char *const *)a->argv, stdio_fds, NULL, NULL,
NULL, &wasi_exit_code);
mem_context_clear(&mctx);
free(a->dirs);
free(a->envs);
Expand Down
30 changes: 23 additions & 7 deletions examples/runwasi/runwasi.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <toywasm/cconv.h>
#include <toywasm/exec_context.h>
#include <toywasm/fileio.h>
#include <toywasm/instance.h>
Expand All @@ -11,11 +12,14 @@
#include "runwasi.h"

int
runwasi_module(struct mem_context *mctx, const struct module *m,
unsigned int ndirs, char **dirs, unsigned int nenvs,
const char *const *envs, int argc, const char *const *argv,
const int stdio_fds[3], struct import_object *base_imports,
uint32_t *wasi_exit_code_p)
runwasi_module(
struct mem_context *mctx, const struct module *m, unsigned int ndirs,
char **dirs, unsigned int nenvs, const char *const *envs, int argc,
const char *const *argv, const int stdio_fds[3],
struct import_object *base_imports,
void (*set_host_instance_resources)(void *hi_arg, struct meminst *mem,
struct tableinst *func_table),
void *hi_arg, uint32_t *wasi_exit_code_p)
{
struct wasi_instance *wasi = NULL;
struct import_object *wasi_import_object = NULL;
Expand Down Expand Up @@ -93,6 +97,13 @@ runwasi_module(struct mem_context *mctx, const struct module *m,
}
report_clear(&report);

struct meminst *mem = cconv_memory(inst);
struct tableinst *func_table = cconv_func_table(inst);
wasi_instance_set_memory(wasi, mem);
if (set_host_instance_resources != NULL) {
set_host_instance_resources(hi_arg, mem, func_table);
}

/*
* execute the module
*/
Expand Down Expand Up @@ -140,7 +151,10 @@ int
runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs,
char **dirs, unsigned int nenvs, const char *const *envs, int argc,
const char *const *argv, const int stdio_fds[3],
struct import_object *base_imports, uint32_t *wasi_exit_code_p)
struct import_object *base_imports,
void (*set_host_instance_resources)(void *hi_arg, struct meminst *mem,
struct tableinst *func_table),
void *hi_arg, uint32_t *wasi_exit_code_p)
{
struct module *m = NULL;
int ret;
Expand All @@ -167,7 +181,9 @@ runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs,
load_context_clear(&lctx);

ret = runwasi_module(mctx, m, ndirs, dirs, nenvs, envs, argc, argv,
stdio_fds, base_imports, wasi_exit_code_p);
stdio_fds, base_imports,
set_host_instance_resources, hi_arg,
wasi_exit_code_p);

fail:
if (m != NULL) {
Expand Down
21 changes: 15 additions & 6 deletions examples/runwasi/runwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
struct mem_context;
struct import_object;
struct module;
struct meminst;
struct tableinst;

int runwasi_module(struct mem_context *mctx, const struct module *m,
unsigned int ndirs, char **dirs, unsigned int nenvs,
const char *const *envs, int argc, const char *const *argv,
const int stdio_fds[3], struct import_object *base_imports,
uint32_t *wasi_exit_code_p);
int runwasi_module(
struct mem_context *mctx, const struct module *m, unsigned int ndirs,
char **dirs, unsigned int nenvs, const char *const *envs, int argc,
const char *const *argv, const int stdio_fds[3],
struct import_object *base_imports,
void (*set_host_instance_resources)(void *hi_arg, struct meminst *mem,
struct tableinst *func_table),
void *hi_arg, uint32_t *wasi_exit_code_p);

int runwasi(struct mem_context *mctx, const char *filename, unsigned int ndirs,
char **dirs, unsigned int nenvs, const char *const *envs, int argc,
const char *const *argv, const int stdio_fds[3],
struct import_object *base_imports, uint32_t *wasi_exit_code_p);
struct import_object *base_imports,
void (*set_host_instance_resources)(void *hi_arg,
struct meminst *mem,
struct tableinst *func_table),
void *hi_arg, uint32_t *wasi_exit_code_p);
8 changes: 4 additions & 4 deletions examples/runwasi_cstruct/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ main(int argc, char **argv)
}
struct mem_context mctx;
mem_context_init(&mctx);
ret = runwasi_module(&mctx, &g_wasm_module, a->ndirs, a->dirs, a->nenvs,
(const char *const *)a->envs, a->argc,
(const char *const *)a->argv, stdio_fds, NULL,
&wasi_exit_code);
ret = runwasi_module(&mctx, &g_wasm_module, a->ndirs, a->dirs,
a->nenvs, (const char *const *)a->envs, a->argc,
(const char *const *)a->argv, stdio_fds, NULL,
NULL, NULL, &wasi_exit_code);
mem_context_clear(&mctx);
free(a->dirs);
free(a->envs);
Expand Down
Loading
Loading