Skip to content

Commit

Permalink
move the "memory" reference to host_instance
Browse files Browse the repository at this point in the history
maybe it's a bit clear to do:

```
struct host_instance_using_cconv {
    struct host_instance hi;
    struct meminst *mem;
    struct tableinst *func_table;
}
```

but i didn't bother to do that right now. i might revisit later.

also, do the same for the table as well.

also, make the repl initialize them accordingly.
(only for memory, just because wasi doesn't happen to
use function pointers.)
  • Loading branch information
yamt committed Aug 10, 2024
1 parent d497e22 commit fe6ea6b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 42 deletions.
21 changes: 16 additions & 5 deletions cli/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,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 @@ -681,11 +696,7 @@ 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
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
62 changes: 33 additions & 29 deletions lib/cconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
* a part of core wasm.
*/

#include <assert.h>

#include "cconv.h"
#include "exec.h"
#include "instance.h"
#include "module.h"
#include "type.h"

static const struct name name_func_table =
NAME_FROM_CSTR_LITERAL("__indirect_function_table");

/*
* the export name "memory" is defined by wasi.
* the export name "memory" and "__indirect_function_table" are
* defined by wasi.
* https://github.com/WebAssembly/WASI/blob/main/legacy/README.md
* it's widely used for non-wasi interfaces as well.
* they are widely used for non-wasi interfaces as well.
*
* Note: some runtimes, including old versions of toywasm, assume
* the memidx 0 even for wasi. it would end up with some
* incompatibilities with multi-memory proposal.
*/

static const struct name name_func_table =
NAME_FROM_CSTR_LITERAL("__indirect_function_table");
static const struct name name_default_memory =
NAME_FROM_CSTR_LITERAL("memory");

Expand All @@ -30,30 +33,18 @@ static const struct name name_default_memory =
* raise a trap on an error.
*/
int
cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
cconv_deref_func_ptr(struct exec_context *ctx, const struct tableinst *t,
uint32_t wasmfuncptr, const struct functype *ft,
const struct funcinst **fip)
{
const struct module *m = inst->module;
uint32_t tableidx;
int ret;
/*
* XXX searching exports on each call can be too slow.
*/
ret = module_find_export(m, &name_func_table, EXTERNTYPE_TABLE,
&tableidx);
if (ret != 0) {
do_trap:
if (t == NULL) {
return trap_with_id(ctx,
TRAP_INDIRECT_FUNCTION_TABLE_NOT_FOUND,
"__indirect_function_table is not found");
}
const struct tableinst *t = VEC_ELEM(inst->tables, tableidx);
if (t->type->et != TYPE_funcref) {
goto do_trap;
}
assert(t->type->et == TYPE_funcref);
const struct funcinst *func;
ret = table_get_func(ctx, t, wasmfuncptr, ft, &func);
int ret = table_get_func(ctx, t, wasmfuncptr, ft, &func);
if (ret != 0) {
/* Note: table_get_func raises a trap inside */
return ret;
Expand All @@ -63,18 +54,31 @@ cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
}

struct meminst *
cconv_default_memory(const struct instance *inst)
cconv_memory(const struct instance *inst)
{
const struct module *m = inst->module;
uint32_t memidx;
int ret;
/*
* XXX searching exports on each call can be too slow.
*/
ret = module_find_export(m, &name_default_memory, EXTERNTYPE_MEMORY,
&memidx);
int ret = module_find_export(m, &name_default_memory,
EXTERNTYPE_MEMORY, &memidx);
if (ret != 0) {
return NULL;
}
return VEC_ELEM(inst->mems, memidx);
return VEC_ELEM(inst->mems, memidx);
}

struct tableinst *
cconv_func_table(const struct instance *inst)
{
const struct module *m = inst->module;
uint32_t tableidx;
int ret = module_find_export(m, &name_func_table, EXTERNTYPE_TABLE,
&tableidx);
if (ret != 0) {
return NULL;
}
struct tableinst *t = VEC_ELEM(inst->tables, tableidx);
if (t->type->et != TYPE_funcref) {
return NULL;
}
return t;
}
6 changes: 4 additions & 2 deletions lib/cconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ struct instance;
struct functype;
struct funcinst;
struct meminst;
struct tableinst;

__BEGIN_EXTERN_C

int cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
int cconv_deref_func_ptr(struct exec_context *ctx, const struct tableinst *t,
uint32_t wasmfuncptr, const struct functype *ft,
const struct funcinst **fip);

struct meminst *cconv_default_memory(const struct instance *inst);
struct meminst *cconv_memory(const struct instance *inst);
struct tableinst *cconv_func_table(const struct instance *inst);

__END_EXTERN_C
6 changes: 5 additions & 1 deletion lib/host_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ struct host_func {
#endif

struct host_instance {
int dummy;
struct meminst *memory;
struct tableinst *func_table;
};

#define host_func_memory(hi) (hi)->memory
#define host_func_func_table(hi) (hi)->func_table

struct host_module {
const struct name *module_name;
const struct host_func *funcs;
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ wasi_instance_create(struct mem_context *mctx,
void
wasi_instance_set_memory(struct wasi_instance *inst, struct meminst *mem)
{
inst->memory = mem;
wasi_memory(inst) = mem;
}

void
Expand Down
4 changes: 1 addition & 3 deletions libwasi/wasi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ struct wasi_instance {
struct wasi_table fdtable[WASI_NTABLES] GUARDED_VAR(
lock); /* indexed by wasi fd */

struct meminst *memory;

int argc;
const char *const *argv;
int nenvs;
Expand All @@ -74,7 +72,7 @@ uint32_t wasi_convert_errno(int host_errno);

#define wasi_copyout(c, m, h, w, l, a) host_func_copyout(c, m, h, w, l, a)
#define wasi_copyin(c, m, h, w, l, a) host_func_copyin(c, m, h, w, l, a)
#define wasi_memory(i) (i)->memory
#define wasi_memory(i) host_func_memory(&(i)->hi)

struct exec_context;
struct wasi_instance;
Expand Down
10 changes: 9 additions & 1 deletion libwasi_threads/wasi_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ wasi_threads_instance_destroy(struct wasi_threads_instance *inst)
mem_free(mctx, inst, sizeof(*inst));
}

void
wasi_threads_instance_set_memory(struct wasi_threads_instance *inst,
struct meminst *mem)
{
inst->hi.memory = mem;
}

void
wasi_threads_setup_exec_context(struct wasi_threads_instance *wasi_threads,
struct exec_context *ctx)
Expand Down Expand Up @@ -547,7 +554,8 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
le32_encode(&r.u.tid, tid);
}
HOST_FUNC_FREE_CONVERTED_PARAMS();
return host_func_copyout(ctx, &r, retp, sizeof(r), WASI_U32_ALIGN);
return host_func_copyout(ctx, host_func_memory(&wasi->hi), &r, retp,
sizeof(r), WASI_U32_ALIGN);
}

static int
Expand Down
4 changes: 4 additions & 0 deletions libwasi_threads/wasi_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

struct wasi_threads_instance;
struct import_object;
struct meminst;
struct sched;
struct exec_context;
struct trap_info;
Expand All @@ -14,6 +15,9 @@ void wasi_threads_instance_destroy(struct wasi_threads_instance *inst);
int wasi_threads_instance_create(struct mem_context *mctx,
struct wasi_threads_instance **instp);

void wasi_threads_instance_set_memory(struct wasi_threads_instance *inst,
struct meminst *mem);

/*
* wasi_threads_instance_set_thread_spawn_args: set wasi-threads parameters
*
Expand Down

0 comments on commit fe6ea6b

Please sign in to comment.