/* * 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_content_type(struct nw_mod *); static int push(const void *const src, const size_t n, void *const user) { struct nw_mod *const m = user; struct nw_sm_gl *const gl = &m->sm.global; if (n > sizeof gl->value) { #ifdef NW_LOG nwp_log("stack overflow\n"); #endif return -1; } memcpy(&gl->value, src, n); return n; } static enum nw_state loop(struct nw_mod *const m) { struct nw_sm_gl *const gl = &m->sm.global; const enum nw_state n = nwp_interp_run(&gl->interp); if (n) return n; m->next = ++gl->entry_i >= m->global_count ? nwp_section_exit : get_content_type; return NW_AGAIN; } static enum nw_state get_global(struct nw_mod *const m) { struct nw_sm_gl *const gl = &m->sm.global; struct nw_interp_cfg cfg = {0}; cfg.m = m; cfg.io = m->cfg.io; cfg.user = m; cfg.stack.push = push; if (nwp_interp_start(&gl->interp, &cfg, &nwp_interp_initexpr_set)) { #ifdef NW_LOG nwp_log("nw_interp_start failed\n"); #endif return NW_FATAL; } m->next = loop; return NW_AGAIN; } static enum nw_state get_mutability(struct nw_mod *const m) { nw_varuint1 mutability; const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_gl *const gl = &m->sm.global; struct nw_sm_leb128 *const l = &gl->leb128; const enum nw_state n = nwp_varuint1(cfg, l, &mutability, cfg->user); if (n) return n; m->next = get_global; return NW_AGAIN; } static enum nw_state get_content_type(struct nw_mod *const m) { nw_varint7 content_type; const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_gl *const gl = &m->sm.global; struct nw_sm_leb128 *const l = &gl->leb128; const enum nw_state n = nwp_varint7(cfg, l, &content_type, cfg->user); enum nw_type type; /* TODO: do not ignore type */ if (n) return n; else if (nwp_get_type(content_type, &type)) { #ifdef NW_LOG nwp_log("nwp_get_type failed\n"); #endif return NW_FATAL; } m->next = get_mutability; return NW_AGAIN; } static enum nw_state get_count(struct nw_mod *const m) { const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_gl *const gl = &m->sm.global; struct nw_sm_leb128 *const l = &gl->leb128; enum nw_state n; if (!m->sections[NW_SECTION_IMPORT]) { #ifdef NW_LOG nwp_log("global section found before import section\n"); #endif return NW_FATAL; } else if ((n = nwp_varuint32(cfg, l, &m->global_count, cfg->user))) return n; else if (m->global_count > ULONG_MAX / sizeof (struct nw_global)) { #ifdef NW_LOG nwp_log("global_count too large: %lu\n", (unsigned long)m->global_count); #endif return NW_FATAL; } m->out.global = m->global_count * sizeof (struct nw_global); m->next = m->global_count ? get_content_type : nwp_section_exit; return NW_AGAIN; } void nwp_section_global(struct nw_mod *const m) { const struct nw_sm_gl g = {0}; m->sm.global = g; m->next = get_count; }