/* * 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 int store(struct nw_interp *const i, const varuint32 flags, const varuint32 offset) { enum {ALIGN = 2}; if (flags != ALIGN) { LOG("%s: expected 32-bit alignment (%d), got %lu\n", __func__, ALIGN, (unsigned long)flags); i->exception = "unaligned access"; return -1; } int32_t value, addr; if (interp_stack_pop(i, &value, sizeof value)) { LOG("%s: inter_stack_pop value failed\n", __func__); return -1; } else if (interp_stack_pop(i, &addr, sizeof addr)) { LOG("%s: inter_stack_pop addr failed\n", __func__); return -1; } else if (offset > SIZE_MAX - addr) { static const char exc[] = "offset address overflow"; LOG("%s: %s\n", __func__, exc); i->exception = exc; return -1; } const int32_t value_le = htoni32(value); if (interp_heap_store(i, offset + addr, &value_le, sizeof value_le)) { LOG("%s: interp_heap_store failed\n", __func__); return -1; } return 0; #if 0 stack_i -= sizeof addr; if (max < addr || offset >= max - addr) { LOG("%s: heap overflow\n", __func__); i->exception = "heap overflow"; return -1; } const size_t raddr = offset + addr; if (raddr % sizeof addr) { LOG("%s: unaligned memory address\n", __func__); i->exception = "unaligned access"; return -1; } else if (stack_i < sizeof (uint32_t)) { LOG("%s: stack underflow (value)\n", __func__); i->exception = "stack underflow"; return -1; } const int32_t value = htoni32(*stackptr); void *dst = &((char *)i->cfg.heap.buf)[raddr]; *(int32_t *)dst = value; stack_i -= sizeof addr; i->stack_i = stack_i; return 0; #endif } static int op(struct nw_interp *const i, FILE *const f) { varuint32 flags, offset; if (varuint32_read(f, &flags)) { LOG("%s: varint32_read flags failed\n", __func__); return -1; } else if (varuint32_read(f, &offset)) { LOG("%s: varint32_read offset failed\n", __func__); return -1; } else if (i && store(i, flags, offset)) { LOG("%s: store failed\n", __func__); return -1; } return 0; } int op_i32_store(struct nw_interp *const i) { return op(i, i->f); } int check_i32_store(FILE *const f) { return op(NULL, f); }