/* * 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 static enum nw_state get_index(struct nw_mod *); static enum nw_state skip_bytes(struct nw_mod *const m) { struct nw_sm_d *const d = &m->sm.data; const struct nw_io_cfg *const cfg = &m->cfg.io; const long offset = d->offset + d->size; const enum nw_state n = cfg->seek(offset, cfg->user); if (n) return n; m->next = ++d->entry_i >= m->data_count ? nwp_section_exit : get_index; return NW_AGAIN; } static enum nw_state tell(struct nw_mod *const m) { const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_d *const d = &m->sm.data; const enum nw_state n = cfg->tell(&d->offset, cfg->user); if (n) return n; m->next = skip_bytes; return NW_AGAIN; } static enum nw_state get_size(struct nw_mod *const m) { const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_d *const d = &m->sm.data; struct nw_sm_leb128 *const l = &d->leb128; const enum nw_state n = nwp_varuint32(cfg, l, &d->size, cfg->user); if (n) return n; else if (!d->size) { #ifdef NW_LOG nwp_log("unexpected zero size for data entry %lu\n", (unsigned long)d->entry_i); #endif return NW_FATAL; } m->next = tell; return NW_AGAIN; } static int push(const void *const src, const size_t n, void *const user) { struct nw_mod *const m = user; struct nw_sm_d *const d = &m->sm.data; if (n > sizeof d->value) { #ifdef NW_LOG nwp_log("stack overflow\n"); #endif return -1; } memcpy(&d->value, src, n); return n; } static enum nw_state loop(struct nw_mod *const m) { struct nw_sm_d *const d = &m->sm.data; const enum nw_state n = nwp_interp_run(&d->interp); if (n) return n; m->next = get_size; return NW_AGAIN; } static enum nw_state setup_initexpr(struct nw_mod *const m) { struct nw_interp_cfg icfg = {0}; struct nw_sm_d *const d = &m->sm.data; icfg.m = m; icfg.io = m->cfg.io; icfg.user = m; icfg.stack.push = push; if (nwp_interp_start(&d->interp, &icfg, &nwp_interp_data_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_index(struct nw_mod *const m) { const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_d *const d = &m->sm.data; struct nw_sm_leb128 *const l = &d->leb128; nw_varuint32 *const index = &d->index; const enum nw_state n = nwp_varuint32(cfg, l, index, cfg->user); if (n) return n; else if (*index) { #ifdef NW_LOG nwp_log("expected memory index 0 in data entry %lu, got %lu\n", (unsigned long)d->entry_i, *index); #endif return NW_FATAL; } return setup_initexpr(m); } static enum nw_state get_count(struct nw_mod *const m) { const struct nw_io_cfg *const cfg = &m->cfg.io; const long dc = m->sections[NW_SECTION_DATA_COUNT]; struct nw_sm_d *const d = &m->sm.data; struct nw_sm_leb128 *const l = &d->leb128; nw_varuint32 count, *const pcount = dc ? &count : &m->data_count; enum nw_state n; if (!m->sections[NW_SECTION_MEMORY]) { #ifdef NW_LOG nwp_log("data section found before memory section\n"); #endif return NW_FATAL; } else if ((n = nwp_varuint32(cfg, l, pcount, cfg->user))) return n; else if (dc && count != m->data_count) { #ifdef NW_LOG nwp_log("data count mismatch, expected %lu, got %lu\n", (unsigned long)m->data_count, (unsigned long)count); #endif return NW_FATAL; } m->next = m->data_count ? get_index : nwp_section_exit; return NW_AGAIN; } void nwp_section_data(struct nw_mod *const m) { const struct nw_sm_d d = {0}; m->sm.data = d; m->next = get_count; }