Skip to content

Commit

Permalink
host_func_getptr2: use meminst, not memidx
Browse files Browse the repository at this point in the history
to prepare for other strategies to pick a memory to use.
  • Loading branch information
yamt committed Aug 7, 2024
1 parent 764f59e commit cca2805
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/cconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,20 @@ cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,

int
cconv_default_memory(struct exec_context *ctx, const struct instance *inst,
uint32_t *memidxp)
struct meminst **mip)
{
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,
memidxp);
&memidx);
if (ret != 0) {
return trap_with_id(ctx, TRAP_DEFAULT_MEMORY_NOT_FOUND,
"default memory not found");
}
*mip = VEC_ELEM(inst->mems, memidx);
return 0;
}
3 changes: 2 additions & 1 deletion lib/cconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct exec_context;
struct instance;
struct functype;
struct funcinst;
struct meminst;

__BEGIN_EXTERN_C

Expand All @@ -14,6 +15,6 @@ int cconv_deref_func_ptr(struct exec_context *ctx, const struct instance *inst,
const struct funcinst **fip);

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

__END_EXTERN_C
17 changes: 14 additions & 3 deletions lib/host_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,23 @@ int
host_func_getptr2(struct exec_context *ctx, uint32_t ptr, uint32_t offset,
uint32_t size, void **pp, bool *movedp)
{
uint32_t memidx;
int ret = cconv_default_memory(ctx, ctx->instance, &memidx);
struct meminst *meminst;
int ret = cconv_default_memory(ctx, ctx->instance, &meminst);
if (ret != 0) {
return ret;
}
return memory_getptr2(ctx, memidx, ptr, offset, size, pp, movedp);
ret = memory_instance_getptr2(meminst, ptr, offset, size, pp, movedp);
if (ret == ETOYWASMTRAP) {
ret = trap_with_id(
ctx, TRAP_OUT_OF_BOUNDS_MEMORY_ACCESS,
"host function invalid memory access at %08" PRIx32
" + %08" PRIx32 ", size %" PRIu32
", meminst size %" PRIu32 ", pagesize %" PRIu32,
ptr, offset, size, meminst->size_in_pages,
1 << memtype_page_shift(meminst->type));
assert(ret != 0);
}
return ret;
}

int
Expand Down

0 comments on commit cca2805

Please sign in to comment.