From f114a9772875fe41a37427388dc2ec77347baa10 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 8 Aug 2024 00:09:40 +0900 Subject: [PATCH 1/4] add host_func_trap --- lib/exec.h | 2 ++ lib/host_instance.c | 11 +++++++++++ lib/host_instance.h | 2 ++ 3 files changed, 15 insertions(+) diff --git a/lib/exec.h b/lib/exec.h index cb87a79b..15967585 100644 --- a/lib/exec.h +++ b/lib/exec.h @@ -65,6 +65,8 @@ int invoke(struct funcinst *finst, const struct resulttype *paramtype, int check_interrupt(struct exec_context *ctx); int check_interrupt_interval_ms(struct exec_context *ctx); +int vtrap(struct exec_context *ctx, enum trapid id, const char *fmt, + va_list ap); int trap_with_id(struct exec_context *ctx, enum trapid id, const char *fmt, ...) __attribute__((__format__(__printf__, 3, 4))); int memory_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, diff --git a/lib/host_instance.c b/lib/host_instance.c index 7f230f78..2dac7536 100644 --- a/lib/host_instance.c +++ b/lib/host_instance.c @@ -227,6 +227,17 @@ host_func_memory_getptr2(struct exec_context *ctx, uint32_t memidx, return memory_getptr2(ctx, memidx, ptr, offset, size, pp, movedp); } +int +host_func_trap(struct exec_context *ctx, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vtrap(ctx, TRAP_MISC, fmt, ap); + va_end(ap); + return ret; +} + int schedule_call_from_hostfunc(struct exec_context *ctx, struct restart_info *restart, diff --git a/lib/host_instance.h b/lib/host_instance.h index 0b35246c..15e7ab3b 100644 --- a/lib/host_instance.h +++ b/lib/host_instance.h @@ -95,6 +95,8 @@ int host_func_memory_getptr(struct exec_context *ctx, uint32_t memidx, int host_func_memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, 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; int schedule_call_from_hostfunc(struct exec_context *ctx, struct restart_info *restart, From c5a9d155d449f4b28bb7b8e7b6e37f94778c8e90 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 8 Aug 2024 00:21:19 +0900 Subject: [PATCH 2/4] add memory_instance_getptr2 --- lib/exec.h | 3 +++ lib/exec_insn_subr.c | 54 +++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/lib/exec.h b/lib/exec.h index 15967585..ff5112e6 100644 --- a/lib/exec.h +++ b/lib/exec.h @@ -73,6 +73,9 @@ int memory_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp); int memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp, bool *movedp); +int memory_instance_getptr2(struct meminst *meminst, uint32_t ptr, + uint32_t offset, uint32_t size, void **pp, + bool *movedp); struct toywasm_mutex; int memory_atomic_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, diff --git a/lib/exec_insn_subr.c b/lib/exec_insn_subr.c index e3a6c772..54ea1624 100644 --- a/lib/exec_insn_subr.c +++ b/lib/exec_insn_subr.c @@ -41,12 +41,9 @@ trap_with_id(struct exec_context *ctx, enum trapid id, const char *fmt, ...) } int -memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, - uint32_t offset, uint32_t size, void **pp, bool *movedp) +memory_instance_getptr2(struct meminst *meminst, uint32_t ptr, uint32_t offset, + uint32_t size, void **pp, bool *movedp) { - const struct instance *inst = ctx->instance; - assert(memidx < inst->module->nmems + inst->module->nimportedmems); - struct meminst *meminst = VEC_ELEM(inst->mems, memidx); assert(meminst->allocated <= (uint64_t)meminst->size_in_pages << memtype_page_shift(meminst->type)); @@ -79,19 +76,8 @@ memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, const uint32_t page_shift = memtype_page_shift(meminst->type); uint32_t need_in_pages = (last_byte >> page_shift) + 1; if (need_in_pages > meminst->size_in_pages) { - int ret; do_trap: - ret = trap_with_id( - ctx, TRAP_OUT_OF_BOUNDS_MEMORY_ACCESS, - "invalid memory access at %04" PRIx32 - " %08" PRIx32 " + %08" PRIx32 ", size %" PRIu32 - ", meminst size %" PRIu32 - ", pagesize %" PRIu32, - memidx, ptr, offset, size, - meminst->size_in_pages, - 1 << memtype_page_shift(meminst->type)); - assert(ret != 0); /* appease clang-tidy */ - return ret; + return ETOYWASMTRAP; } /* * Note: shared memories do never come here because @@ -111,8 +97,8 @@ memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, return ENOMEM; } meminst->data = np; - xlog_trace_insn("extend memory %" PRIu32 " from %zu to %zu", - memidx, meminst->allocated, need); + xlog_trace_insn("extend memory from %zu to %zu", + meminst->allocated, need); if (movedp != NULL) { *movedp = true; } @@ -121,14 +107,36 @@ memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, meminst->allocated = need; } success: - xlog_trace_insn("memory access: at %04" PRIx32 " %08" PRIx32 - " + %08" PRIx32 ", size %" PRIu32 - ", meminst size %" PRIu32, - memidx, ptr, offset, size, meminst->size_in_pages); + xlog_trace_insn("memory access: at %08" PRIx32 " + %08" PRIx32 + ", size %" PRIu32 ", meminst size %" PRIu32, + ptr, offset, size, meminst->size_in_pages); *pp = meminst->data + ea; return 0; } +int +memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, + uint32_t offset, uint32_t size, void **pp, bool *movedp) +{ + const struct instance *inst = ctx->instance; + assert(memidx < inst->module->nmems + inst->module->nimportedmems); + struct meminst *meminst = VEC_ELEM(inst->mems, memidx); + int ret = memory_instance_getptr2(meminst, ptr, offset, size, pp, + movedp); + + if (ret == ETOYWASMTRAP) { + ret = trap_with_id( + ctx, TRAP_OUT_OF_BOUNDS_MEMORY_ACCESS, + "invalid memory access at %04" PRIx32 " %08" PRIx32 + " + %08" PRIx32 ", size %" PRIu32 + ", meminst size %" PRIu32 ", pagesize %" PRIu32, + memidx, ptr, offset, size, meminst->size_in_pages, + 1 << memtype_page_shift(meminst->type)); + assert(ret != 0); + } + return ret; +} + int memory_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp) From 1bf7f40bb97c72fb291b3d35d0750cd2e9b3f0ca Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 8 Aug 2024 00:38:04 +0900 Subject: [PATCH 3/4] move some instance resource functions from exec.h to instance.h note: exec.h is a lib internal header. --- lib/cconv.c | 1 + lib/exec.h | 13 ------------- lib/insn.c | 1 + lib/instance.h | 17 +++++++++++++++++ libdyld/dyld.c | 2 +- libdyld/dyld_dlfcn.c | 2 +- libdyld/dyld_plt.c | 3 ++- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/cconv.c b/lib/cconv.c index 3e793014..2be557ff 100644 --- a/lib/cconv.c +++ b/lib/cconv.c @@ -4,6 +4,7 @@ */ #include "exec.h" +#include "instance.h" #include "module.h" #include "type.h" diff --git a/lib/exec.h b/lib/exec.h index ff5112e6..bc0bb8f8 100644 --- a/lib/exec.h +++ b/lib/exec.h @@ -29,7 +29,6 @@ int exec_const_expr(const struct expr *expr, enum valtype type, int memory_init(struct exec_context *ctx, uint32_t memidx, uint32_t dataidx, uint32_t d, uint32_t s, uint32_t n); -uint32_t memory_grow(struct meminst *mi, uint32_t sz); uint32_t memory_grow2(struct exec_context *ctx, uint32_t memidx, uint32_t sz); int memory_notify(struct exec_context *ctx, uint32_t memidx, uint32_t addr, @@ -42,15 +41,6 @@ int table_init(struct exec_context *ctx, uint32_t tableidx, uint32_t elemidx, uint32_t d, uint32_t s, uint32_t n); int table_access(struct exec_context *ectx, uint32_t tableidx, uint32_t offset, uint32_t n); -void table_set(struct tableinst *tinst, uint32_t elemidx, - const struct val *val); -void table_get(struct tableinst *tinst, uint32_t elemidx, struct val *val); -int table_get_func(struct exec_context *ectx, const struct tableinst *t, - uint32_t i, const struct functype *ft, - const struct funcinst **fip); -int table_grow(struct tableinst *tinst, const struct val *val, uint32_t n); -void global_set(struct globalinst *ginst, const struct val *val); -void global_get(struct globalinst *ginst, struct val *val); void data_drop(struct exec_context *ectx, uint32_t dataidx); void elem_drop(struct exec_context *ectx, uint32_t elemidx); @@ -73,9 +63,6 @@ int memory_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp); int memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp, bool *movedp); -int memory_instance_getptr2(struct meminst *meminst, uint32_t ptr, - uint32_t offset, uint32_t size, void **pp, - bool *movedp); struct toywasm_mutex; int memory_atomic_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, diff --git a/lib/insn.c b/lib/insn.c index 9a63969b..32128ded 100644 --- a/lib/insn.c +++ b/lib/insn.c @@ -18,6 +18,7 @@ #include "insn_macros.h" #include "insn_op.h" #include "insn_op_helpers.h" +#include "instance.h" #include "leb128.h" #include "mem.h" #include "platform.h" diff --git a/lib/instance.h b/lib/instance.h index b5181916..f40d42f0 100644 --- a/lib/instance.h +++ b/lib/instance.h @@ -1,3 +1,4 @@ +#include #include #include "platform.h" @@ -5,6 +6,8 @@ struct module; struct instance; struct exec_context; +struct funcinst; +struct functype; struct resulttype; struct import; struct import_object; @@ -12,6 +15,7 @@ struct import_object_entry; struct mem_context; struct report; struct name; +struct val; __BEGIN_EXTERN_C @@ -130,18 +134,31 @@ struct memtype; int memory_instance_create(struct mem_context *mctx, struct meminst **mip, const struct memtype *mt); void memory_instance_destroy(struct mem_context *mctx, struct meminst *mi); +uint32_t memory_grow(struct meminst *mi, uint32_t sz); +int memory_instance_getptr2(struct meminst *meminst, uint32_t ptr, + uint32_t offset, uint32_t size, void **pp, + bool *movedp); struct globalinst; struct globaltype; int global_instance_create(struct mem_context *mctx, struct globalinst **gip, const struct globaltype *gt); void global_instance_destroy(struct mem_context *mctx, struct globalinst *gi); +void global_set(struct globalinst *ginst, const struct val *val); +void global_get(struct globalinst *ginst, struct val *val); struct tableinst; struct tabletype; int table_instance_create(struct mem_context *mctx, struct tableinst **tip, const struct tabletype *tt); void table_instance_destroy(struct mem_context *mctx, struct tableinst *ti); +void table_set(struct tableinst *tinst, uint32_t elemidx, + const struct val *val); +void table_get(struct tableinst *tinst, uint32_t elemidx, struct val *val); +int table_grow(struct tableinst *tinst, const struct val *val, uint32_t n); +int table_get_func(struct exec_context *ectx, const struct tableinst *t, + uint32_t i, const struct functype *ft, + const struct funcinst **fip); /* * create_satisfying_shared_memories: diff --git a/libdyld/dyld.c b/libdyld/dyld.c index 3efaa497..f5123801 100644 --- a/libdyld/dyld.c +++ b/libdyld/dyld.c @@ -23,7 +23,7 @@ #include "dyld_impl.h" #include "dyld_plt.h" #include "dylink_type.h" -#include "exec.h" +#include "exec_context.h" #include "fileio.h" #include "instance.h" #include "list.h" diff --git a/libdyld/dyld_dlfcn.c b/libdyld/dyld_dlfcn.c index d914a395..cf29bcb5 100644 --- a/libdyld/dyld_dlfcn.c +++ b/libdyld/dyld_dlfcn.c @@ -16,7 +16,7 @@ #include "dyld_dlfcn_abi.h" #include "dyld_impl.h" #include "endian.h" -#include "exec.h" +#include "exec_context.h" #include "host_instance.h" #include "mem.h" #include "xlog.h" diff --git a/libdyld/dyld_plt.c b/libdyld/dyld_plt.c index a9b8d761..cd1f6daa 100644 --- a/libdyld/dyld_plt.c +++ b/libdyld/dyld_plt.c @@ -4,7 +4,8 @@ #include "dyld.h" #include "dyld_impl.h" #include "dyld_plt.h" -#include "exec.h" +#include "exec_context.h" +#include "instance.h" #include "xlog.h" int From a4e6a667a53908397b2b161606a6398bd23d6076 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 8 Aug 2024 00:46:07 +0900 Subject: [PATCH 4/4] move a few functions from exec.h to exec_context.h --- lib/exec.h | 7 ------- lib/exec_context.h | 8 ++++++++ libwasi/wasi_abi_path.c | 1 - libwasi/wasi_abi_poll.c | 2 +- libwasi/wasi_abi_proc.c | 2 +- libwasi/wasi_abi_random.c | 1 - libwasi/wasi_poll_subr.c | 2 +- libwasi/wasi_subr.c | 1 - libwasi/wasi_table.c | 2 +- libwasi_threads/wasi_threads.c | 2 +- 10 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/exec.h b/lib/exec.h index bc0bb8f8..9237adeb 100644 --- a/lib/exec.h +++ b/lib/exec.h @@ -52,13 +52,6 @@ void rewind_stack(struct exec_context *ctx, uint32_t height, uint32_t arity); int invoke(struct funcinst *finst, const struct resulttype *paramtype, const struct resulttype *resulttype, struct exec_context *ctx); -int check_interrupt(struct exec_context *ctx); -int check_interrupt_interval_ms(struct exec_context *ctx); - -int vtrap(struct exec_context *ctx, enum trapid id, const char *fmt, - va_list ap); -int trap_with_id(struct exec_context *ctx, enum trapid id, const char *fmt, - ...) __attribute__((__format__(__printf__, 3, 4))); int memory_getptr(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, uint32_t offset, uint32_t size, void **pp); int memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr, diff --git a/lib/exec_context.h b/lib/exec_context.h index 94f4553c..6d841ef2 100644 --- a/lib/exec_context.h +++ b/lib/exec_context.h @@ -346,4 +346,12 @@ int exec_push_vals(struct exec_context *ctx, const struct resulttype *rt, void exec_pop_vals(struct exec_context *ctx, const struct resulttype *rt, struct val *results); +int check_interrupt(struct exec_context *ctx); +int check_interrupt_interval_ms(struct exec_context *ctx); + +int vtrap(struct exec_context *ctx, enum trapid id, const char *fmt, + va_list ap); +int trap_with_id(struct exec_context *ctx, enum trapid id, const char *fmt, + ...) __attribute__((__format__(__printf__, 3, 4))); + __END_EXTERN_C diff --git a/libwasi/wasi_abi_path.c b/libwasi/wasi_abi_path.c index 9c59d799..540ea804 100644 --- a/libwasi/wasi_abi_path.c +++ b/libwasi/wasi_abi_path.c @@ -5,7 +5,6 @@ #include #include "endian.h" -#include "exec.h" #include "nbio.h" #include "wasi_impl.h" #include "wasi_path_subr.h" diff --git a/libwasi/wasi_abi_poll.c b/libwasi/wasi_abi_poll.c index ee83273a..c80c35e1 100644 --- a/libwasi/wasi_abi_poll.c +++ b/libwasi/wasi_abi_poll.c @@ -5,7 +5,7 @@ #include #include "endian.h" -#include "exec.h" +#include "exec_context.h" #include "restart.h" #include "wasi_impl.h" #include "wasi_poll_subr.h" diff --git a/libwasi/wasi_abi_proc.c b/libwasi/wasi_abi_proc.c index ffc5f957..27942809 100644 --- a/libwasi/wasi_abi_proc.c +++ b/libwasi/wasi_abi_proc.c @@ -3,7 +3,7 @@ #include #include -#include "exec.h" +#include "exec_context.h" #include "wasi_impl.h" #include "wasi_hostfuncs.h" diff --git a/libwasi/wasi_abi_random.c b/libwasi/wasi_abi_random.c index 112a56ef..5894afd5 100644 --- a/libwasi/wasi_abi_random.c +++ b/libwasi/wasi_abi_random.c @@ -13,7 +13,6 @@ #endif #include "endian.h" -#include "exec.h" #include "wasi_impl.h" #include "xlog.h" diff --git a/libwasi/wasi_poll_subr.c b/libwasi/wasi_poll_subr.c index 9dcfbfb1..578df173 100644 --- a/libwasi/wasi_poll_subr.c +++ b/libwasi/wasi_poll_subr.c @@ -4,7 +4,7 @@ #include #include -#include "exec.h" +#include "exec_context.h" #include "nbio.h" #include "restart.h" #include "wasi_host_subr.h" diff --git a/libwasi/wasi_subr.c b/libwasi/wasi_subr.c index 41fc4136..bb80ecdc 100644 --- a/libwasi/wasi_subr.c +++ b/libwasi/wasi_subr.c @@ -8,7 +8,6 @@ #include #include "endian.h" -#include "exec.h" #include "host_instance.h" #include "wasi_abi.h" #include "wasi_subr.h" diff --git a/libwasi/wasi_table.c b/libwasi/wasi_table.c index 8f1f67c1..f0a5cbd9 100644 --- a/libwasi/wasi_table.c +++ b/libwasi/wasi_table.c @@ -3,7 +3,7 @@ #include #include -#include "exec.h" +#include "exec_context.h" #include "timeutil.h" #include "wasi_impl.h" #include "xlog.h" diff --git a/libwasi_threads/wasi_threads.c b/libwasi_threads/wasi_threads.c index 6eb6bbfd..a3bacc36 100644 --- a/libwasi_threads/wasi_threads.c +++ b/libwasi_threads/wasi_threads.c @@ -13,7 +13,7 @@ #include "cluster.h" #include "endian.h" -#include "exec.h" +#include "exec_context.h" #include "host_instance.h" #include "idalloc.h" #include "instance.h"