Skip to content

Commit 59b1581

Browse files
committed
Refactor wasm interpreter to use predefined values and enhance argument logging in execution
1 parent bb52418 commit 59b1581

File tree

2 files changed

+67
-83
lines changed

2 files changed

+67
-83
lines changed

core/iwasm/interpreter/wasm_interp_fast.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
16701670
{
16711671
uint32 ret_idx;
16721672
WASMFuncType *func_type;
1673-
uint32 off, ret_offset;
1673+
int32 off;
1674+
uint32 ret_offset;
16741675
uint8 *ret_types;
16751676
if (cur_func->is_import_func)
16761677
func_type = cur_func->u.func_import->func_type;
@@ -1682,9 +1683,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
16821683
ret_offset = prev_frame->ret_offset;
16831684

16841685
for (ret_idx = 0,
1685-
off = sizeof(int16) * (func_type->result_count - 1);
1686+
off = (int32)sizeof(int16) * (func_type->result_count - 1);
16861687
ret_idx < func_type->result_count;
1687-
ret_idx++, off -= sizeof(int16)) {
1688+
ret_idx++, off -= (int32)sizeof(int16)) {
16881689
if (ret_types[ret_idx] == VALUE_TYPE_I64
16891690
|| ret_types[ret_idx] == VALUE_TYPE_F64) {
16901691
PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,

tests/fuzz/wasm-mutator-fuzz/wasm_mutator_fuzz.cc

+63-80
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,73 @@
1313

1414
using namespace std;
1515

16-
/*
17-
* there is a unsigned integer overflow in
18-
* /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/random.tcc:416
19-
*
20-
* use srand() and rand() instead
21-
*/
22-
23-
static int32_t
24-
generate_random_int32()
25-
{
26-
return (int32_t)rand();
27-
}
28-
29-
static int64_t
30-
generate_random_int64()
31-
{
32-
return ((int64_t)rand() << 32) | rand();
33-
}
34-
35-
static float
36-
generate_random_float()
37-
{
38-
return (float)rand() / (float)RAND_MAX;
39-
}
40-
41-
static double
42-
generate_random_double()
16+
static bool
17+
is_supported_val_kind(wasm_valkind_t kind)
4318
{
44-
return (double)rand() / (double)RAND_MAX;
19+
return kind == WASM_I32 || kind == WASM_I64 || kind == WASM_F32
20+
|| kind == WASM_F64 || kind == WASM_EXTERNREF
21+
|| kind == WASM_FUNCREF;
4522
}
4623

4724
static wasm_val_t
48-
random_gen_val(wasm_valkind_t kind)
25+
pre_defined_val(wasm_valkind_t kind)
4926
{
50-
srand(1024);
51-
5227
if (kind == WASM_I32) {
53-
return wasm_val_t{ .kind = WASM_I32,
54-
.of = { .i32 = generate_random_int32() } };
28+
return wasm_val_t{ .kind = WASM_I32, .of = { .i32 = 2025 } };
5529
}
5630
else if (kind == WASM_I64) {
57-
return wasm_val_t{ .kind = WASM_I64,
58-
.of = { .i64 = generate_random_int64() } };
31+
return wasm_val_t{ .kind = WASM_I64, .of = { .i64 = 168 } };
5932
}
6033
else if (kind == WASM_F32) {
61-
return wasm_val_t{ .kind = WASM_F32,
62-
.of = { .f32 = generate_random_float() } };
34+
return wasm_val_t{ .kind = WASM_F32, .of = { .f32 = 3.14159f } };
6335
}
6436
else if (kind == WASM_F64) {
65-
return wasm_val_t{ .kind = WASM_F64,
66-
.of = { .f64 = generate_random_double() } };
37+
return wasm_val_t{ .kind = WASM_F64, .of = { .f64 = 2.71828 } };
6738
}
6839
else if (kind == WASM_EXTERNREF) {
6940
return wasm_val_t{ .kind = WASM_EXTERNREF,
70-
.of = { .foreign =
71-
(uintptr_t)generate_random_int64() } };
41+
.of = { .foreign = 0xabcddead } };
7242
}
73-
else if (kind == WASM_FUNCREF) {
43+
// because aft is_supported_val_kind() check, so we can safely return as
44+
// WASM_FUNCREF
45+
else {
7446
return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
7547
}
76-
// TODO:v128
77-
else {
78-
assert(0 && "unsupported value kind");
48+
}
49+
void
50+
print_execution_args(const wasm_export_t &export_type,
51+
const std::vector<wasm_val_t> &args, unsigned param_count)
52+
{
53+
std::cout << "[EXECUTION] " << export_type.name << "(";
54+
for (unsigned p_i = 0; p_i < param_count; p_i++) {
55+
if (p_i != 0) {
56+
std::cout << ", ";
57+
}
58+
59+
switch (args[p_i].kind) {
60+
case WASM_I32:
61+
std::cout << "i32:" << args[p_i].of.i32;
62+
break;
63+
case WASM_I64:
64+
std::cout << "i64:" << args[p_i].of.i64;
65+
break;
66+
case WASM_F32:
67+
std::cout << "f32:" << args[p_i].of.f32;
68+
break;
69+
case WASM_F64:
70+
std::cout << "f64:" << args[p_i].of.f64;
71+
break;
72+
case WASM_EXTERNREF:
73+
std::cout << "externref:" << args[p_i].of.foreign;
74+
break;
75+
default:
76+
// because aft is_supported_val_kind() check, so we can safely
77+
// return as WASM_FUNCREF
78+
std::cout << "funcref:" << args[p_i].of.ref;
79+
break;
80+
}
7981
}
82+
std::cout << ")" << std::endl;
8083
}
8184

8285
static bool
@@ -108,53 +111,33 @@ execute_export_functions(wasm_module_t module, wasm_module_inst_t inst)
108111
for (unsigned p_i = 0; p_i < param_count; p_i++) {
109112
wasm_valkind_t param_type =
110113
wasm_func_type_get_param_valkind(func_type, p_i);
111-
wasm_val_t arg = random_gen_val(param_type);
114+
115+
if (!is_supported_val_kind(param_type)) {
116+
std::cout
117+
<< "Bypass execution because of unsupported value kind: "
118+
<< param_type << std::endl;
119+
return true;
120+
}
121+
122+
wasm_val_t arg = pre_defined_val(param_type);
112123
args.push_back(arg);
113124
}
114125

115126
/* build results storage */
116127
uint32_t result_count = wasm_func_type_get_result_count(func_type);
117128
std::vector<wasm_val_t> results = std::vector<wasm_val_t>(result_count);
118129

130+
print_execution_args(export_type, args, param_count);
131+
119132
/* execute the function */
120133
wasm_exec_env_t exec_env = wasm_runtime_get_exec_env_singleton(inst);
121-
122-
{
123-
std::cout << "[EXECUTION] " << export_type.name << "(";
124-
for (unsigned p_i = 0; p_i < param_count; p_i++) {
125-
if (p_i != 0) {
126-
std::cout << ", ";
127-
}
128-
129-
if (args[p_i].kind == WASM_I32) {
130-
std::cout << "i32:" << args[p_i].of.i32;
131-
}
132-
else if (args[p_i].kind == WASM_I64) {
133-
std::cout << "i64:" << args[p_i].of.i64;
134-
}
135-
else if (args[p_i].kind == WASM_F32) {
136-
std::cout << "f32:" << args[p_i].of.f32;
137-
}
138-
else if (args[p_i].kind == WASM_F64) {
139-
std::cout << "f64:" << args[p_i].of.f64;
140-
}
141-
else if (args[p_i].kind == WASM_EXTERNREF) {
142-
std::cout << "externref:" << args[p_i].of.foreign;
143-
}
144-
else if (args[p_i].kind == WASM_FUNCREF) {
145-
std::cout << "funcref:" << args[p_i].of.ref;
146-
}
147-
// TODO:v128
148-
else {
149-
assert(0 && "unsupported value kind");
150-
}
151-
}
152-
153-
std::cout << ")" << std::endl;
134+
if (!exec_env) {
135+
std::cout << "Failed to get exec env" << std::endl;
136+
return false;
154137
}
155138

156139
bool ret = wasm_runtime_call_wasm_a(exec_env, func, result_count,
157-
&results[0], param_count, &args[0]);
140+
results.data(), param_count, args.data());
158141
if (!ret) {
159142
const char *exception = wasm_runtime_get_exception(inst);
160143
if (!exception) {

0 commit comments

Comments
 (0)