diff options
Diffstat (limited to 'src/interp/routines')
| -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 |
4 files changed, 316 insertions, 0 deletions
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 + }; +} |
