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/op/check | |
First commit1st
Diffstat (limited to 'src/op/check')
| -rw-r--r-- | src/op/check/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | src/op/check/block.c | 34 | ||||
| -rw-r--r-- | src/op/check/br_if.c | 39 | ||||
| -rw-r--r-- | src/op/check/call.c | 39 | ||||
| -rw-r--r-- | src/op/check/end.c | 35 | ||||
| -rw-r--r-- | src/op/check/global_index.c | 39 | ||||
| -rw-r--r-- | src/op/check/i32_const.c | 32 | ||||
| -rw-r--r-- | src/op/check/i64_const.c | 32 | ||||
| -rw-r--r-- | src/op/check/local_index.c | 43 | ||||
| -rw-r--r-- | src/op/check/memory_immediate.c | 47 | ||||
| -rw-r--r-- | src/op/check/no_immediate.c | 23 |
11 files changed, 382 insertions, 0 deletions
diff --git a/src/op/check/CMakeLists.txt b/src/op/check/CMakeLists.txt new file mode 100644 index 0000000..810d3d6 --- /dev/null +++ b/src/op/check/CMakeLists.txt @@ -0,0 +1,19 @@ +# 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 + block.c + br_if.c + call.c + end.c + global_index.c + i32_const.c + i64_const.c + local_index.c + memory_immediate.c + no_immediate.c +) diff --git a/src/op/check/block.c b/src/op/check/block.c new file mode 100644 index 0000000..c58c0f6 --- /dev/null +++ b/src/op/check/block.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/io.h> +#include <nw/ops.h> + +static enum nw_state get_type(struct nw_mod *const m) +{ + nw_varint7 type; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varint7(cfg, l, &type); + + if (n) + return n; + + /* TODO: check block type. */ + c->fn.blocks++; + m->next = m->sm.code.next; + return NW_AGAIN; +} + +void nwp_op_check_block(struct nw_mod *const m) +{ + m->next = get_type; +} diff --git a/src/op/check/br_if.c b/src/op/check/br_if.c new file mode 100644 index 0000000..6e6bd10 --- /dev/null +++ b/src/op/check/br_if.c @@ -0,0 +1,39 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/ops.h> + +static enum nw_state get_relative_depth(struct nw_mod *const m) +{ + nw_varuint32 relative_depth; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &relative_depth); + + if (n) + return n; + else if (relative_depth >= c->fn.blocks) + { + LOG("%s: invalid relative depth %lu\n", __func__, + (unsigned long)relative_depth); + return NW_FATAL; + } + + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_br_if(struct nw_mod *const m) +{ + m->next = get_relative_depth; +} diff --git a/src/op/check/call.c b/src/op/check/call.c new file mode 100644 index 0000000..eaa826f --- /dev/null +++ b/src/op/check/call.c @@ -0,0 +1,39 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/ops.h> + +static enum nw_state get_index(struct nw_mod *const m) +{ + nw_varuint32 index; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &index); + + if (n) + return n; + else if (index >= c->count + m->cfg.n_imports) + { + LOG("%s: invalid function index %lu\n", __func__, + (unsigned long)index); + return NW_FATAL; + } + + m->next = m->sm.code.next; + return NW_AGAIN; +} + +void nwp_op_check_call(struct nw_mod *const m) +{ + m->next = get_index; +} diff --git a/src/op/check/end.c b/src/op/check/end.c new file mode 100644 index 0000000..7c614bb --- /dev/null +++ b/src/op/check/end.c @@ -0,0 +1,35 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/ops.h> + +static enum nw_state run(struct nw_mod *const m) +{ + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_c_fn *const fn = &c->fn; + + if (!fn->blocks) + { + LOG("%s: unexpected end in function %lu\n", __func__, + (unsigned long)c->entry_i); + return NW_FATAL; + } + + fn->blocks--; + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_end(struct nw_mod *const m) +{ + m->next = run; +} diff --git a/src/op/check/global_index.c b/src/op/check/global_index.c new file mode 100644 index 0000000..5cfceb3 --- /dev/null +++ b/src/op/check/global_index.c @@ -0,0 +1,39 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/ops.h> + +static enum nw_state get_index(struct nw_mod *const m) +{ + nw_varuint32 index; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &index); + + if (n) + return n; + else if (index >= m->global_count) + { + LOG("%s: invalid global index %lu\n", __func__, + (unsigned long)index); + return NW_FATAL; + } + + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_global_index(struct nw_mod *const m) +{ + m->next = get_index; +} diff --git a/src/op/check/i32_const.c b/src/op/check/i32_const.c new file mode 100644 index 0000000..71a90b3 --- /dev/null +++ b/src/op/check/i32_const.c @@ -0,0 +1,32 @@ +/* + * 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/io.h> +#include <nw/ops.h> + +static enum nw_state get_value(struct nw_mod *const m) +{ + nw_varuint32 value; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &value); + + if (n) + return n; + + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_i32_const(struct nw_mod *const m) +{ + m->next = get_value; +} diff --git a/src/op/check/i64_const.c b/src/op/check/i64_const.c new file mode 100644 index 0000000..4d4d066 --- /dev/null +++ b/src/op/check/i64_const.c @@ -0,0 +1,32 @@ +/* + * 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/io.h> +#include <nw/ops.h> + +static enum nw_state get_value(struct nw_mod *const m) +{ + nw_varuint64 value; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint64(cfg, l, &value); + + if (n) + return n; + + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_i64_const(struct nw_mod *const m) +{ + m->next = get_value; +} diff --git a/src/op/check/local_index.c b/src/op/check/local_index.c new file mode 100644 index 0000000..1b991b1 --- /dev/null +++ b/src/op/check/local_index.c @@ -0,0 +1,43 @@ +/* + * 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/io.h> +#include <nw/log.h> +#include <nw/ops.h> + +static enum nw_state get_index(struct nw_mod *const m) +{ + nw_varuint32 index; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &index); + + if (n) + return n; +#if 0 + else if (index >= c->fn.local_total) + { + LOG("%s: invalid local index %lu in function %lu\n", __func__, + (unsigned long)index, (unsigned long)c->entry_i); + return NW_FATAL; + } +#else +#warning TODO +#endif + + m->next = c->next; + return NW_AGAIN; +} + +void nwp_op_check_local_index(struct nw_mod *const m) +{ + m->next = get_index; +} diff --git a/src/op/check/memory_immediate.c b/src/op/check/memory_immediate.c new file mode 100644 index 0000000..2232370 --- /dev/null +++ b/src/op/check/memory_immediate.c @@ -0,0 +1,47 @@ +/* + * 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/io.h> +#include <nw/ops.h> + +static enum nw_state get_offset(struct nw_mod *const m) +{ + nw_varuint32 offset; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &offset); + + if (n) + return n; + + m->next = m->sm.code.next; + return NW_AGAIN; +} + +static enum nw_state get_flags(struct nw_mod *const m) +{ + nw_varuint32 flags; + const struct nw_io_cfg *const cfg = &m->cfg.io; + struct nw_sm_c *const c = &m->sm.code; + struct nw_sm_leb128 *const l = &c->leb128; + const enum nw_state n = nwp_varuint32(cfg, l, &flags); + + if (n) + return n; + + m->next = get_offset; + return NW_AGAIN; +} + +void nwp_op_check_memory_immediate(struct nw_mod *const m) +{ + m->next = get_flags; +} diff --git a/src/op/check/no_immediate.c b/src/op/check/no_immediate.c new file mode 100644 index 0000000..cf1dd21 --- /dev/null +++ b/src/op/check/no_immediate.c @@ -0,0 +1,23 @@ +/* + * 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/io.h> +#include <nw/ops.h> + +static enum nw_state run(struct nw_mod *const m) +{ + m->next = m->sm.code.next; + return NW_AGAIN; +} + +void nwp_op_check_no_immediate(struct nw_mod *const m) +{ + m->next = run; +} |
