From cca280501d846b23eb4c53f028336d3738f2783e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 8 Aug 2024 02:16:18 +0900 Subject: [PATCH] host_func_getptr2: use meminst, not memidx to prepare for other strategies to pick a memory to use. --- lib/cconv.c | 6 ++++-- lib/cconv.h | 3 ++- lib/host_instance.c | 17 ++++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/cconv.c b/lib/cconv.c index 0f24c3b6..43daef2b 100644 --- a/lib/cconv.c +++ b/lib/cconv.c @@ -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; } diff --git a/lib/cconv.h b/lib/cconv.h index ea221c3e..f22eb8d2 100644 --- a/lib/cconv.h +++ b/lib/cconv.h @@ -6,6 +6,7 @@ struct exec_context; struct instance; struct functype; struct funcinst; +struct meminst; __BEGIN_EXTERN_C @@ -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 diff --git a/lib/host_instance.c b/lib/host_instance.c index 04bef8d5..5b5bbfa4 100644 --- a/lib/host_instance.c +++ b/lib/host_instance.c @@ -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