diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-09-07 00:04:38 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-10-05 08:17:21 +0200 |
| commit | 144b7fe1415ff97497fa4ea3b95e8dd6b95d069e (patch) | |
| tree | 68391f6ec2dba2879ca8255ee25ce39ebaa7d4ad /src/interp | |
First commit1st
Diffstat (limited to 'src/interp')
| -rw-r--r-- | src/interp/CMakeLists.txt | 21 | ||||
| -rw-r--r-- | src/interp/check_opcode.c | 59 | ||||
| -rw-r--r-- | src/interp/global_pop.c | 34 | ||||
| -rw-r--r-- | src/interp/globalptr.c | 17 | ||||
| -rw-r--r-- | src/interp/initexpr_set.c | 28 | ||||
| -rw-r--r-- | src/interp/interp.c | 411 | ||||
| -rw-r--r-- | src/interp/ops.c | 53 | ||||
| -rw-r--r-- | src/interp/resume.c | 20 | ||||
| -rw-r--r-- | src/interp/routines/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | src/interp/routines/find_export.c | 202 | ||||
| -rw-r--r-- | src/interp/routines/full.c | 48 | ||||
| -rw-r--r-- | src/interp/routines/limited.c | 54 | ||||
| -rw-r--r-- | src/interp/run.c | 16 | ||||
| -rw-r--r-- | src/interp/stack_pop.c | 31 | ||||
| -rw-r--r-- | src/interp/stack_push.c | 34 | ||||
| -rw-r--r-- | src/interp/stackptr.c | 16 | ||||
| -rw-r--r-- | src/interp/start.c | 26 |
17 files changed, 1082 insertions, 0 deletions
diff --git a/src/interp/CMakeLists.txt b/src/interp/CMakeLists.txt new file mode 100644 index 0000000..c3bb726 --- /dev/null +++ b/src/interp/CMakeLists.txt @@ -0,0 +1,21 @@ +# nanowasm, a tiny WebAssembly/Wasm interpreter +# Copyright (C) 2023-2024 Xavier Del Campo Romero +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +target_sources(${PROJECT_NAME} PRIVATE + global_pop.c + globalptr.c + initexpr_set.c + ops.c + run.c + stack_pop.c + stackptr.c + stack_push.c + resume.c + start.c +) + +add_subdirectory(routines) diff --git a/src/interp/check_opcode.c b/src/interp/check_opcode.c new file mode 100644 index 0000000..b10b927 --- /dev/null +++ b/src/interp/check_opcode.c @@ -0,0 +1,59 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/io.h> +#include <nw/log.h> +#include <nw/opcodes.h> + +int nwp_interp_check_opcode(const uint8_t op, FILE *const f) +{ + static int (*const checks[])(FILE *) = + { + [OP_UNREACHABLE] = check_unreachable, + [OP_NOP] = check_nop, + [OP_BLOCK] = check_block, + [OP_LOOP] = check_loop, + [OP_IF] = check_if, + [OP_ELSE] = check_else, + [OP_END] = check_end, + [OP_BR] = check_br, + [OP_BR_IF] = check_br_if, + [OP_BR_TABLE] = check_br_table, + [OP_RETURN] = check_return, + [OP_CALL] = check_call, + [OP_CALL_INDIRECT] = check_call_indirect, + [OP_GET_LOCAL] = check_get_local, + [OP_SET_LOCAL] = check_set_local, + [OP_TEE_LOCAL] = check_tee_local, + [OP_GET_GLOBAL] = check_get_global, + [OP_SET_GLOBAL] = check_set_global, + [OP_I32_LOAD] = check_i32_load, + [OP_I32_STORE] = check_i32_store, + [OP_I32_CONST] = check_i32_const, + [OP_I64_CONST] = check_i64_const, + [OP_F32_CONST] = check_f32_const, + [OP_F64_CONST] = check_f64_const, + [OP_I32_SUB] = check_i32_sub + }; + + if (op >= sizeof checks / sizeof *checks) + { + LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op); + return 1; + } + else if (!checks[op]) + { + LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op); + return 1; + } + + return checks[op](f); +} diff --git a/src/interp/global_pop.c b/src/interp/global_pop.c new file mode 100644 index 0000000..4066d63 --- /dev/null +++ b/src/interp/global_pop.c @@ -0,0 +1,34 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/log.h> +#include <nw/types.h> +#include <stddef.h> +#include <string.h> + +int interp_global_pop(struct nw_interp *const i, void *const dst, + const size_t n) +{ + const void *const src = (const char *)i->cfg.global.buf + i->global_i; + + if (i->global_i < n) + { + static const char exc[] = "global memory underflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + i->global_i -= n; + memcpy(dst, src, n); + return 0; +} diff --git a/src/interp/globalptr.c b/src/interp/globalptr.c new file mode 100644 index 0000000..486f5ba --- /dev/null +++ b/src/interp/globalptr.c @@ -0,0 +1,17 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/types.h> + +void *interp_globalptr(const struct nw_interp *const i) +{ + return (char *)i->cfg.global.buf + i->global_i; +} diff --git a/src/interp/initexpr_set.c b/src/interp/initexpr_set.c new file mode 100644 index 0000000..57601be --- /dev/null +++ b/src/interp/initexpr_set.c @@ -0,0 +1,28 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/opcodes.h> + +static const enum opcode opcodes[] = +{ + OP_NOP, + OP_END, + OP_I32_CONST, + OP_I64_CONST, + OP_F32_CONST, + OP_F64_CONST +}; + +const struct nwp_interp_set nwp_interp_initexpr_set = +{ + .opcodes = opcodes, + .n = sizeof opcodes / sizeof *opcodes +}; diff --git a/src/interp/interp.c b/src/interp/interp.c new file mode 100644 index 0000000..dbeb126 --- /dev/null +++ b/src/interp/interp.c @@ -0,0 +1,411 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/log.h> +#include <nw/interp.h> +#include <nw/opcodes.h> +#include <nw/ops.h> +#include <nw/sections.h> +#include <nw/types.h> +#include <errno.h> +#include <limits.h> +#include <inttypes.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static const enum opcode initexpr_opcodes[] = +{ + OP_NOP, + OP_END, + OP_I32_CONST, + OP_I64_CONST, + OP_F32_CONST, + OP_F64_CONST +}; + +const struct interp_set nwp_interp_initexpr_set = +{ + .opcodes = initexpr_opcodes, + .n = sizeof initexpr_opcodes / sizeof *initexpr_opcodes +}; + +static int (*const ops[])(struct nw_interp *) = +{ + [OP_UNREACHABLE] = op_unreachable, + [OP_NOP] = op_nop, + [OP_BLOCK] = op_block, + [OP_LOOP] = op_loop, + [OP_IF] = op_if, + [OP_ELSE] = op_else, + [OP_END] = op_end, + [OP_BR] = op_br, + [OP_BR_IF] = op_br_if, + [OP_BR_TABLE] = op_br_table, + [OP_RETURN] = op_return, + [OP_CALL] = op_call, + [OP_CALL_INDIRECT] = op_call_indirect, + [OP_GET_LOCAL] = op_get_local, + [OP_SET_LOCAL] = op_set_local, + [OP_TEE_LOCAL] = op_tee_local, + [OP_GET_GLOBAL] = op_get_global, + [OP_SET_GLOBAL] = op_set_global, + [OP_I32_LOAD] = op_i32_load, + [OP_I32_STORE] = op_i32_store, + [OP_I32_CONST] = op_i32_const, + [OP_I64_CONST] = op_i64_const, + [OP_F32_CONST] = op_f32_const, + [OP_F64_CONST] = op_f64_const, + [OP_I32_SUB] = op_i32_sub +}; + +int interp_run(struct nw_interp *const i) +{ + uint8_t op; + + if (!fread(&op, sizeof op, 1, i->f)) + { + LOG("%s: fread(3) failed, feof=%d, ferror=%d\n", __func__, feof(i->f), + ferror(i->f)); + i->exception = "I/O error"; + return -1; + } + else if (op >= sizeof ops / sizeof *ops) + { + LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op); + i->exception = "invalid opcode"; + return -1; + } + else if (!ops[op]) + { + LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op); + i->exception = "invalid opcode"; + return -1; + } + + return ops[op](i); +} + +static int run_opcode_limited(struct nw_interp *const in, + const struct interp_set *const set) +{ + uint8_t op; + + if (!fread(&op, sizeof op, 1, in->f)) + { + LOG("%s: fread(3) failed, feof=%d, ferror=%d\n", + __func__, feof(in->f), ferror(in->f)); + in->exception = "I/O error"; + return -1; + } + + for (size_t i = 0; i < set->n; i++) + if (op == set->opcodes[i]) + return ops[op](in); + + LOG("%s: unexpected opcode %#" PRIx8 "\n", __func__, op); + in->exception = "invalid opcode"; + return -1; +} + +int interp_run_limited(struct nw_interp *const i, + const struct interp_set *const set) +{ + while (!i->exit) + if (run_opcode_limited(i, set)) + { + LOG("%s: run_opcode_limited failed\n", __func__); + return -1; + } + + return 0; +} + +int interp_check_opcode(const uint8_t op, FILE *const f) +{ + static int (*const checks[])(FILE *) = + { + [OP_UNREACHABLE] = check_unreachable, + [OP_NOP] = check_nop, + [OP_BLOCK] = check_block, + [OP_LOOP] = check_loop, + [OP_IF] = check_if, + [OP_ELSE] = check_else, + [OP_END] = check_end, + [OP_BR] = check_br, + [OP_BR_IF] = check_br_if, + [OP_BR_TABLE] = check_br_table, + [OP_RETURN] = check_return, + [OP_CALL] = check_call, + [OP_CALL_INDIRECT] = check_call_indirect, + [OP_GET_LOCAL] = check_get_local, + [OP_SET_LOCAL] = check_set_local, + [OP_TEE_LOCAL] = check_tee_local, + [OP_GET_GLOBAL] = check_get_global, + [OP_SET_GLOBAL] = check_set_global, + [OP_I32_LOAD] = check_i32_load, + [OP_I32_STORE] = check_i32_store, + [OP_I32_CONST] = check_i32_const, + [OP_I64_CONST] = check_i64_const, + [OP_F32_CONST] = check_f32_const, + [OP_F64_CONST] = check_f64_const, + [OP_I32_SUB] = check_i32_sub + }; + + if (op >= sizeof checks / sizeof *checks) + { + LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op); + return 1; + } + else if (!checks[op]) + { + LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op); + return 1; + } + + return checks[op](f); +} + +int interp_start(const struct nw_interp_cfg *const cfg, FILE *const f, + struct nw_interp *const i) +{ + *i = (const struct nw_interp) + { + .f = f, + .cfg = *cfg + }; + + return 0; +} + +void *nwp_interp_stackptr(const struct nw_interp *const i) +{ + return (char *)i->cfg.stack.buf + i->stack_i; +} + +struct retval_priv +{ + enum value_type type; + + union + { + int32_t i32; + int64_t i64; + float f32; + double f64; + } u; +}; + +static int get_i32(struct nw_interp *const i, struct retval_priv *const rp) +{ + return interp_stack_pop(i, &rp->u.i32, sizeof rp->u.i32); +} + +static int get_i64(struct nw_interp *const i, struct retval_priv *const rp) +{ + return interp_stack_pop(i, &rp->u.i64, sizeof rp->u.i64); +} + +static int get_f32(struct nw_interp *const i, struct retval_priv *const rp) +{ + return interp_stack_pop(i, &rp->u.f32, sizeof rp->u.f32); +} + +static int get_f64(struct nw_interp *const i, struct retval_priv *const rp) +{ + return interp_stack_pop(i, &rp->u.f64, sizeof rp->u.f64); +} + +static int get_retval(struct nw_interp *const i, struct retval_priv *const rp) +{ + const struct retval *const r = &i->fp->retval; + const enum value_type v = r->type; + static int (*const f[])(struct nw_interp *, struct retval_priv *) = + { + [VALUE_TYPE_I32] = get_i32, + [VALUE_TYPE_I64] = get_i64, + [VALUE_TYPE_F32] = get_f32, + [VALUE_TYPE_F64] = get_f64 + }; + + if (f[r->type](i, rp)) + { + LOG("%s: callback for type %s failed\n", __func__, + value_type_tostr(v)); + return -1; + } + + rp->type = r->type; + return 0; +} + +int interp_pop(struct nw_interp *const i) +{ + if (!i->fp) + { + static const char exc[] = "no frame pointer"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + const struct retval *const r = &i->fp->retval; + struct retval_priv rp; + + if (r->returns && get_retval(i, &rp)) + { + LOG("%s: get_retval failed\n", __func__); + return -1; + } + +#if 0 +#error TODO: do Wasm modules really have an exit status? +#endif + + if (!(i->fp = i->fp->prev)) + { + /* Entry point is always assumed to return int, which is + * defined as an i32 variable according to Wasm. */ + if (rp.type != VALUE_TYPE_I32) + { + LOG("%s: expected i32 return type, got %s\n", __func__, + value_type_tostr(rp.type)); + i->exception = "unexpected return type"; + return -1; + } + + i->retval = rp.u.i32; + i->exit = true; + } + + return 0; +} + +int interp_stack_push(struct nw_interp *const i, const void *const src, + const size_t n) +{ + const size_t max = i->cfg.stack.n; + void *const dst = nwp_interp_stackptr(i); + + if (max < n || i->stack_i > max - n) + { + static const char exc[] = "stack overflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + memcpy(dst, src, n); + i->stack_i += n; + return 0; +} + +int interp_stack_pop(struct nw_interp *const i, void *const dst, + const size_t n) +{ + if (i->stack_i < n) + { + static const char exc[] = "stack underflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + i->stack_i -= n; + memcpy(dst, nwp_interp_stackptr(i), n); + return 0; +} + +int interp_heap_store(struct nw_interp *const i, const size_t addr, + const void *const src, const size_t n) +{ + if (i->cfg.heap.n < n || addr > i->cfg.heap.n - n) + { + static const char exc[] = "heap overflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + void *const dst = (char *)i->cfg.heap.buf + addr; + + memcpy(dst, src, n); + return 0; +} + +int interp_heap_load(struct nw_interp *const i, const size_t addr, + void *const dst, const size_t n) +{ + const size_t max = i->cfg.heap.n; + + if (max < n || addr > max - n) + { + static const char exc[] = "heap out-of-bounds access"; + + LOG("%s: %s (%lu, max %zu)\n", __func__, exc, (unsigned long)addr, max); + i->exception = exc; + return -1; + } + + const void *const src = (const char *)i->cfg.heap.buf + addr; + + memcpy(dst, src, n); + return 0; +} + +int interp_global_push(struct nw_interp *const i, const void *const src, + const size_t n) +{ + const size_t max = i->cfg.global.n; + void *const dst = (char *)i->cfg.global.buf + i->global_i; + + if (max < n || i->global_i > max - n) + { + static const char exc[] = "global memory overflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + memcpy(dst, src, n); + i->global_i += n; + return 0; +} + +int interp_global_pop(struct nw_interp *const i, void *const dst, + const size_t n) +{ + const void *const src = (const char *)i->cfg.global.buf + i->global_i; + + if (i->global_i < n) + { + static const char exc[] = "global memory underflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + i->global_i -= n; + memcpy(dst, src, n); + return 0; +} + +void *interp_globalptr(const struct nw_interp *const i) +{ + return (char *)i->cfg.global.buf + i->global_i; +} diff --git a/src/interp/ops.c b/src/interp/ops.c new file mode 100644 index 0000000..ea7c2b9 --- /dev/null +++ b/src/interp/ops.c @@ -0,0 +1,53 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/opcodes.h> +#include <nw/ops.h> +#include <nw/interp.h> + +static void (*const ops[])(struct nw_interp *) = +{ + [OP_UNREACHABLE] = nwp_op_unreachable, + [OP_NOP] = nwp_op_nop, + [OP_I32_LOAD] = nwp_op_i32_load, + [OP_I32_CONST] = nwp_op_i32_const, + [OP_END] = nwp_op_end, +#if 0 + [OP_BLOCK] = nwp_op_block, + [OP_LOOP] = nwp_op_loop, + [OP_IF] = nwp_op_if, + [OP_ELSE] = nwp_op_else, + + [OP_BR] = nwp_op_br, + [OP_BR_IF] = nwp_op_br_if, + [OP_BR_TABLE] = nwp_op_br_table, + [OP_RETURN] = nwp_op_return, + [OP_CALL] = nwp_op_call, + [OP_CALL_INDIRECT] = nwp_op_call_indirect, + [OP_GET_LOCAL] = nwp_op_get_local, + [OP_SET_LOCAL] = nwp_op_set_local, + [OP_TEE_LOCAL] = nwp_op_tee_local, + [OP_GET_GLOBAL] = nwp_op_get_global, + [OP_SET_GLOBAL] = nwp_op_set_global, + + [OP_I32_STORE] = nwp_op_i32_store, + + [OP_I64_CONST] = nwp_op_i64_const, + [OP_F32_CONST] = nwp_op_f32_const, + [OP_F64_CONST] = nwp_op_f64_const, + [OP_I32_SUB] = nwp_op_i32_sub +#endif +}; + +const struct nwp_ops nwp_ops = +{ + .ops = ops, + .n = sizeof ops / sizeof *ops +}; diff --git a/src/interp/resume.c b/src/interp/resume.c new file mode 100644 index 0000000..e808889 --- /dev/null +++ b/src/interp/resume.c @@ -0,0 +1,20 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nanowasm/types.h> +#include <nw/interp.h> + +void nwp_interp_resume(struct nw_interp *const i) +{ + if (i->set) + nwp_interp_limited(i); + else + nwp_interp_full(i); +} diff --git a/src/interp/routines/CMakeLists.txt b/src/interp/routines/CMakeLists.txt new file mode 100644 index 0000000..eb76ed3 --- /dev/null +++ b/src/interp/routines/CMakeLists.txt @@ -0,0 +1,12 @@ +# nanowasm, a tiny WebAssembly/Wasm interpreter +# Copyright (C) 2023-2024 Xavier Del Campo Romero +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +target_sources(${PROJECT_NAME} PRIVATE + find_export.c + full.c + limited.c +) diff --git a/src/interp/routines/find_export.c b/src/interp/routines/find_export.c new file mode 100644 index 0000000..f82e5f9 --- /dev/null +++ b/src/interp/routines/find_export.c @@ -0,0 +1,202 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nanowasm/types.h> +#include <nw/interp.h> +#include <nw/io.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <inttypes.h> +#include <string.h> + +static enum nw_state entry_loop(struct nw_interp *i); + +static enum nw_state get_index(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_exp *const e = &i->sm.export; + struct nw_sm_leb128 *const l = &e->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &e->index); + + if (n) + return n; + + i->next = e->next; + return NW_AGAIN; +} + +static enum nw_state get_kind(struct nw_interp *const i) +{ + uint8_t kind; + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_sm_io io = {.buf = &kind, .n = sizeof kind}; + const enum nw_state n = nwp_io_read(cfg, &io); + + if (n) + return n; + else if (kind >= NW_KINDS) + { + static const char exc[] = "invalid export kind"; + + i->exception = exc; + LOG("%s: %s: %" PRIu8 "\n", __func__, exc, kind); + return NW_FATAL; + } + + struct nw_i_sm_exp *const e = &i->sm.export; + + e->kind = kind; + i->next = get_index; + return NW_AGAIN; +} + +static enum nw_state skip_index(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_exp *const e = &i->sm.export; + struct nw_sm_leb128 *const l = &e->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &(nw_varuint32){0}); + + if (n) + return n; + + e->entry_i++; + i->next = entry_loop; + return NW_AGAIN; +} + +static enum nw_state skip_kind(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_sm_io io = {.buf = &(uint8_t){0}, .n = sizeof (uint8_t)}; + const enum nw_state n = nwp_io_read(cfg, &io); + + if (n) + return n; + + i->next = skip_index; + return NW_AGAIN; +} + +static enum nw_state skip_field_str(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + const struct nw_i_sm_exp *const e = &i->sm.export; + const long offset = e->len - e->len_i; + const enum nw_state n = cfg->seek(offset, NW_SEEK_CUR, cfg->user); + + if (n) + return n; + + i->next = skip_kind; + return NW_AGAIN; +} + +static enum nw_state compare(struct nw_interp *const i) +{ + uint8_t byte; + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_exp *const e = &i->sm.export; + struct nw_sm_io io = {.buf = &byte, .n = sizeof byte}; + const enum nw_state n = nwp_io_read(cfg, &io); + + if (n) + return n; + else if (byte != e->sym[e->len_i++]) + i->next = skip_field_str; + else if (e->len_i >= e->len) + i->next = get_kind; + + return NW_AGAIN; +} + +static enum nw_state get_len(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_exp *const e = &i->sm.export; + struct nw_sm_leb128 *const l = &e->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &e->len); + + if (n) + return n; + else if (e->len != strlen(e->sym)) + i->next = skip_field_str; + else + i->next = compare; + + return NW_AGAIN; +} + +static enum nw_state entry_loop(struct nw_interp *const i) +{ + struct nw_i_sm_exp *const e = &i->sm.export; + + if (e->entry_i >= e->count) + { + static const char exc[] = "failed to find symbol"; + + i->exception = exc; + LOG("%s: %s: %s\n", __func__, exc, e->sym); + return NW_FATAL; + } + + e->len_i = 0; + i->next = get_len; + return NW_AGAIN; +} + +static enum nw_state get_count(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_exp *const e = &i->sm.export; + struct nw_sm_leb128 *const l = &e->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &e->count); + + if (n) + return n; + + i->next = entry_loop; + return NW_AGAIN; +} + +static enum nw_state seek(struct nw_interp *const i) +{ + const struct nw_mod *const m = i->cfg.m; + const struct nw_io_cfg *const cfg = &i->cfg.io; + + if (!m->sections[NW_SECTION_EXPORT]) + { + static const char exc[] = "section not found"; + + i->exception = exc; + LOG("%s: %s: %s\n", __func__, exc, "export"); + return NW_FATAL; + } + + const enum nw_state n = cfg->seek(m->sections[NW_SECTION_EXPORT], + NW_SEEK_SET, cfg->user); + + if (n) + return n; + + i->next = get_count; + return NW_AGAIN; +} + +void nwp_find_export(struct nw_interp *i, const char *sym, + enum nw_state (*const next)(struct nw_interp *)) +{ + i->next = seek; + i->sm.export = (const struct nw_i_sm_exp) + { + .sym = sym, + .next = next + }; +} diff --git a/src/interp/routines/full.c b/src/interp/routines/full.c new file mode 100644 index 0000000..c60432d --- /dev/null +++ b/src/interp/routines/full.c @@ -0,0 +1,48 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nw/interp.h> +#include <nw/io.h> +#include <nw/log.h> +#include <nw/opcodes.h> +#include <nw/ops.h> +#include <nw/routines.h> +#include <inttypes.h> + +static enum nw_state get_bytecode(struct nw_interp *const i) +{ + const struct nw_io_cfg *const cfg = &i->cfg.io; + struct nw_i_sm_b *const b = &i->sm.bytecode; + const enum nw_state n = nwp_io_read(cfg, &b->io); + + if (n) + return n; + else if (b->op >= nwp_ops.n) + { + static const char exc[] = "invalid opcode"; + + LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op); + i->exception = exc; + } + + nwp_ops.ops[b->op](i); + return NW_AGAIN; +} + +void nwp_interp_full(struct nw_interp *const i) +{ + struct nw_i_sm_b *const b = &i->sm.bytecode; + + i->next = get_bytecode; + b->io = (const struct nw_sm_io) + { + .buf = &b->op, + .n = sizeof b->op + }; +} diff --git a/src/interp/routines/limited.c b/src/interp/routines/limited.c new file mode 100644 index 0000000..766b2d3 --- /dev/null +++ b/src/interp/routines/limited.c @@ -0,0 +1,54 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nanowasm/types.h> +#include <nw/interp.h> +#include <nw/io.h> +#include <nw/log.h> +#include <nw/routines.h> +#include <stddef.h> +#include <inttypes.h> + +static enum nw_state get_bytecode(struct nw_interp *const in) +{ + const struct nw_io_cfg *const cfg = &in->cfg.io; + struct nw_i_sm_b *const b = &in->sm.bytecode; + const enum nw_state n = nwp_io_read(cfg, &b->io); + + if (n) + return n; + + const struct nwp_interp_set *const set = in->set; + + for (size_t i = 0; i < set->n; i++) + if (b->op == set->opcodes[i]) + { + nwp_ops.ops[b->op](in); + return NW_AGAIN; + } + + static const char exc[] = "invalid opcode"; + + LOG("%s: %s: %#" PRIx8 "\n", __func__, exc, b->op); + in->exception = exc; + return NW_FATAL; +} + +void nwp_interp_limited(struct nw_interp *const i) +{ + struct nw_i_sm_b *const b = &i->sm.bytecode; + + i->next = get_bytecode; + b->io = (const struct nw_sm_io) + { + .buf = &b->op, + .n = sizeof b->op + }; +} diff --git a/src/interp/run.c b/src/interp/run.c new file mode 100644 index 0000000..ef7f614 --- /dev/null +++ b/src/interp/run.c @@ -0,0 +1,16 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> + +enum nw_state nwp_interp_run(struct nw_interp *const i) +{ + return i->next(i); +} diff --git a/src/interp/stack_pop.c b/src/interp/stack_pop.c new file mode 100644 index 0000000..8650be1 --- /dev/null +++ b/src/interp/stack_pop.c @@ -0,0 +1,31 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/log.h> +#include <stddef.h> +#include <string.h> + +int interp_stack_pop(struct nw_interp *const i, void *const dst, + const size_t n) +{ + if (i->stack_i < n) + { + static const char exc[] = "stack underflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + i->stack_i -= n; + memcpy(dst, nwp_interp_stackptr(i), n); + return 0; +} diff --git a/src/interp/stack_push.c b/src/interp/stack_push.c new file mode 100644 index 0000000..2145423 --- /dev/null +++ b/src/interp/stack_push.c @@ -0,0 +1,34 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> +#include <nw/log.h> +#include <stddef.h> +#include <string.h> + +int nwp_interp_stack_push(struct nw_interp *const i, const void *const src, + const size_t n) +{ + const size_t max = i->cfg.stack.n; + void *const dst = nwp_interp_stackptr(i); + + if (max < n || i->stack_i > max - n) + { + static const char exc[] = "stack overflow"; + + LOG("%s: %s\n", __func__, exc); + i->exception = exc; + return -1; + } + + memcpy(dst, src, n); + i->stack_i += n; + return 0; +} diff --git a/src/interp/stackptr.c b/src/interp/stackptr.c new file mode 100644 index 0000000..6b95e1d --- /dev/null +++ b/src/interp/stackptr.c @@ -0,0 +1,16 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/interp.h> + +void *nwp_interp_stackptr(const struct nw_interp *const i) +{ + return (char *)i->cfg.stack.buf + i->stack_i; +} diff --git a/src/interp/start.c b/src/interp/start.c new file mode 100644 index 0000000..9d98ccb --- /dev/null +++ b/src/interp/start.c @@ -0,0 +1,26 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nanowasm/types.h> +#include <nw/interp.h> + +int nwp_interp_start(const struct nw_interp_cfg *const cfg, + const struct nwp_interp_set *const set, + struct nw_interp *const i) +{ + *i = (const struct nw_interp) + { + .cfg = *cfg, + .set = set + }; + + nwp_interp_resume(i); + return 0; +} |
