1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/*
* 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 <nw/log.h>
#include <nw/ops.h>
#include <nanowasm/nw.h>
#include <nw/interp.h>
#include <nw/types.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* 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;
}
|