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

some api tweaks #256

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

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

Expand Down
15 changes: 0 additions & 15 deletions lib/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand All @@ -62,11 +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 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,
Expand Down
8 changes: 8 additions & 0 deletions lib/exec_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 31 additions & 23 deletions lib/exec_insn_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions lib/host_instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/host_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/insn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions lib/instance.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include <stdbool.h>
#include <stdint.h>

#include "platform.h"

struct module;
struct instance;
struct exec_context;
struct funcinst;
struct functype;
struct resulttype;
struct import;
struct import_object;
struct import_object_entry;
struct mem_context;
struct report;
struct name;
struct val;

__BEGIN_EXTERN_C

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion libdyld/dyld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion libdyld/dyld_dlfcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion libdyld/dyld_plt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion libwasi/wasi_abi_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string.h>

#include "endian.h"
#include "exec.h"
#include "nbio.h"
#include "wasi_impl.h"
#include "wasi_path_subr.h"
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_abi_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string.h>

#include "endian.h"
#include "exec.h"
#include "exec_context.h"
#include "restart.h"
#include "wasi_impl.h"
#include "wasi_poll_subr.h"
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_abi_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>

#include "exec.h"
#include "exec_context.h"
#include "wasi_impl.h"

#include "wasi_hostfuncs.h"
Expand Down
1 change: 0 additions & 1 deletion libwasi/wasi_abi_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#endif

#include "endian.h"
#include "exec.h"
#include "wasi_impl.h"
#include "xlog.h"

Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_poll_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <time.h>
#include <unistd.h>

#include "exec.h"
#include "exec_context.h"
#include "nbio.h"
#include "restart.h"
#include "wasi_host_subr.h"
Expand Down
1 change: 0 additions & 1 deletion libwasi/wasi_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string.h>

#include "endian.h"
#include "exec.h"
#include "host_instance.h"
#include "wasi_abi.h"
#include "wasi_subr.h"
Expand Down
2 changes: 1 addition & 1 deletion libwasi/wasi_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <inttypes.h>
#include <stdlib.h>

#include "exec.h"
#include "exec_context.h"
#include "timeutil.h"
#include "wasi_impl.h"
#include "xlog.h"
Expand Down
2 changes: 1 addition & 1 deletion libwasi_threads/wasi_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading