/* * 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 #include #include #include /* Inspired by "INT32-C. Ensure that operations on signed integers do * not result in overflow". */ static bool signed_overflow(const int32_t a, const int32_t b) { return (b > 0 && a < INT32_MIN + b) || (b < 0 && a > INT32_MAX + b); } int op_i32_sub(struct nw_interp *const i) { int32_t op1, op2; if (interp_stack_pop(i, &op2, sizeof op2)) { LOG("%s: interp_stack_pop %s failed\n", __func__, "op2"); return -1; } else if (interp_stack_pop(i, &op1, sizeof op1)) { LOG("%s: interp_stack_pop %s failed\n", __func__, "op1"); return -1; } else if (signed_overflow(op2, op1)) { LOG("%s: signed integer overflow (op1=%" PRIi32 ", op2=%" PRIi32 ")\n", __func__, op1, op2); i->exception = "signed integer overflow"; return 1; } const int32_t result = op2 - op1; if (interp_stack_push(i, &result, sizeof result)) { LOG("%s: interp_stack_push failed\n", __func__); return -1; } return 0; } int check_i32_sub(FILE *const f) { return 0; }