/* * 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 #include #include #include #include #include #include #include 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 }; }