/* * nanowasm, a tiny WebAssembly/Wasm interpreter * Copyright (C) 2023-2025 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 #include #include #include #include #include #include #include static enum nw_state get_param_type(struct nw_interp *); static enum nw_state seek_pc(struct nw_interp *const i) { const struct nw_i_sm_type *const t = &i->sm.type; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = cfg->seek(t->pc, cfg->user); if (n) return n; i->next = t->next; return NW_AGAIN; } static enum nw_state get_return_type(struct nw_interp *const i) { nw_varint7 type; struct nw_i_sm_type *const t = &i->sm.type; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = nwp_varint7(cfg, &t->leb128, &type, cfg->user); if (n) return n; else if (nwp_get_type(type, &t->out.ret.type)) { static const char *const exc = "invalid type"; i->exception = exc; #ifdef NW_LOG nwp_log("%s: %#x\n", exc, (unsigned)type); #endif return NW_FATAL; } i->next = seek_pc; return NW_AGAIN; } static enum nw_state get_return_count(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; struct nw_sm_leb128 *const l = &t->leb128; nw_varuint1 *const return_count = &t->out.ret.count; const enum nw_state n = nwp_varuint1(cfg, l, return_count, cfg->user); if (n) return n; else if (*return_count) i->next = get_return_type; else t->next(i); return NW_AGAIN; } static enum nw_state get_param_type(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; nw_varint7 type; enum nw_type param_type; const enum nw_state n = nwp_varint7(cfg, &t->leb128, &type, cfg->user); if (n) return n; else if (nwp_get_type(type, ¶m_type)) { i->exception = "invalid type"; return NW_FATAL; } else if (++t->param_i >= t->out.param_count) i->next = get_return_count; return NW_AGAIN; } static enum nw_state tell_param_types(struct nw_interp *const i) { struct nw_i_sm_type *const t = &i->sm.type; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = cfg->tell(&t->out.param_types, cfg->user); if (n) return n; i->next = get_param_type; return NW_AGAIN; } static enum nw_state get_param_count(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; struct nw_sm_leb128 *const l = &t->leb128; nw_varuint32 *const count = &t->out.param_count; const enum nw_state n = nwp_varuint32(cfg, l, count, cfg->user); if (n) return n; i->next = *count ? tell_param_types : get_return_count; return NW_AGAIN; } static enum nw_state get_form(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; struct nw_sm_leb128 *const l = &t->leb128; nw_varint7 form; const enum nw_state n = nwp_varint7(cfg, l, &form, cfg->user); if (n) return n; else if (form != 0x60) { static const char *const exc = "type index not a function"; i->exception = exc; #ifdef NW_LOG nwp_log("%s: %#x\n", exc, (unsigned)form); #endif return NW_FATAL; } i->next = get_param_count; return NW_AGAIN; } static enum nw_state seek_type(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; const struct nw_i_sm_type *const t = &i->sm.type; const unsigned long to = nwp_leuint32(&t->to); const enum nw_state n = cfg->seek(to, cfg->user); if (n) return n; i->next = get_form; return NW_AGAIN; } static enum nw_state get_to(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; const enum nw_state n = nwp_io_read(cfg, &t->io, cfg->user); if (n) return n; i->next = seek_type; return NW_AGAIN; } static enum nw_state seek_to(struct nw_interp *const i) { const struct nw_interp_cfg *const icfg = &i->cfg; struct nw_i_sm_type *const t = &i->sm.type; const struct nw_mod *const m = icfg->m; long offset = m->c_sections[NW_CUSTOM_TO]; const unsigned long fti = nwp_leuint32(&t->fti); const struct nw_io_cfg *const cfg = &icfg->io; enum nw_state n; if (fti >= m->type_count) { static const char *const exc = "invalid type index"; i->exception = exc; #ifdef NW_LOG nwp_log("%s: %lu\n", exc, fti); #endif return NW_FATAL; } else if (!offset) { static const char *const exc = "nw_to section not found"; i->exception = exc; #ifdef NW_LOG nwp_log("%s\n", exc); #endif return NW_FATAL; } offset += sizeof t->to * fti; if ((n = cfg->seek(offset, cfg->user))) return n; else { struct nw_sm_io io = {0}; io.buf = &t->to; io.n = sizeof t->to; t->io = io; i->next = get_to; } return NW_AGAIN; } static enum nw_state get_fti(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_i_sm_type *const t = &i->sm.type; const enum nw_state n = nwp_io_read(cfg, &t->io, cfg->user); if (n) return n; i->next = seek_to; return NW_AGAIN; } static enum nw_state seek_fti(struct nw_interp *const i) { const struct nw_interp_cfg *const icfg = &i->cfg; const struct nw_io_cfg *const cfg = &icfg->io; struct nw_i_sm_type *const t = &i->sm.type; long offset = icfg->m->c_sections[NW_CUSTOM_FTI]; enum nw_state n; if (!offset) { static const char *const exc = "nw_fti section not found"; #ifdef NW_LOG nwp_log("%s: %s\n", exc, "function type index"); #endif i->exception = exc; return NW_FATAL; } offset += sizeof t->fti * t->fn_index; if ((n = cfg->seek(offset, cfg->user))) return n; else { struct nw_sm_io io = {0}; io.buf = &t->fti; io.n = sizeof t->fti; t->io = io; i->next = get_fti; } return NW_AGAIN; } static enum nw_state get_index(struct nw_interp *const i) { struct nw_i_sm_type *const t = &i->sm.type; const struct nw_get_import_type_out *const out = &t->git.out; struct nw_sm_leb128 *const l = &t->leb128; nw_varuint32 index; const struct nw_io_cfg *const cfg = &i->cfg.io; enum nw_state n; if (out->kind != NW_KIND_FUNCTION) { static const char *const exc = "import type not a function"; i->exception = exc; #ifdef NW_LOG nwp_log("%s: %#x\n", exc, (unsigned)out->kind); #endif return NW_FATAL; } else if ((n = nwp_varuint32(cfg, l, &index, cfg->user))) return n; nwp_toleuint32(index, &t->fti); i->next = seek_to; return NW_AGAIN; } static enum nw_state tell(struct nw_interp *const i) { struct nw_i_sm_type *const t = &i->sm.type; const struct nw_io_cfg *const cfg = &i->cfg.io; const nw_varuint32 icnt = i->cfg.m->import_count, index = t->out.index; const enum nw_state n = cfg->tell(&t->pc, cfg->user); if (n) return n; else if (index >= icnt) { t->fn_index = index - icnt; i->next = seek_fti; } else nwp_get_import_type(i, &t->git, index, get_index); return NW_AGAIN; } void nwp_get_function_type(struct nw_interp *const i, const nw_varuint32 index, enum nw_state (*const next)(struct nw_interp *)) { const struct nw_i_sm_type t = {0}; struct nw_i_sm_type *const pt = &i->sm.type; *pt = t; pt->out.index = index; pt->next = next; i->next = tell; }