Skip to content

Commit 4360748

Browse files
committed
Refactor wasm mutator fuzz tests to use simpler random number generation and ensure termination in WASM shape
1 parent 670f154 commit 4360748

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

tests/fuzz/wasm-mutator-fuzz/smith_wasm.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function try_generate_wasm()
4141
printf -- "-- output ${GENERATED_WASM_NAME} in %d retries\n" $try_i
4242
}
4343

44-
WASM_SHAPE=" --allow-invalid-funcs true \
44+
WASM_SHAPE=" --ensure-termination \
4545
--export-everything true \
4646
--generate-custom-sections true \
4747
--min-funcs 5 \

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

+46-17
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,66 @@
1010
#include <string.h>
1111
#include <iostream>
1212
#include <vector>
13-
#include <random>
13+
#include <bits/stdc++.h>
1414

1515
using namespace std;
1616

17-
/* use std generation */
17+
/*
18+
* there is a unsigned integer overflow in
19+
* /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/random.tcc:416
20+
*
21+
* use srand() and rand() instead
22+
*/
23+
24+
static int32_t
25+
generate_random_int32()
26+
{
27+
return (int32_t)rand();
28+
}
29+
30+
static int64_t
31+
generate_random_int64()
32+
{
33+
return ((int64_t)rand() << 32) | rand();
34+
}
35+
36+
static float
37+
generate_random_float()
38+
{
39+
return (float)rand() / (float)RAND_MAX;
40+
}
41+
42+
static double
43+
generate_random_double()
44+
{
45+
return (double)rand() / (double)RAND_MAX;
46+
}
47+
1848
static wasm_val_t
1949
random_gen_val(wasm_valkind_t kind)
2050
{
21-
static std::random_device rd;
22-
static std::mt19937 gen(rd());
51+
srand(1024);
52+
2353
if (kind == WASM_I32) {
24-
std::uniform_int_distribution<int32_t> dis;
25-
return wasm_val_t{ .kind = WASM_I32, .of = { .i32 = dis(gen) } };
54+
return wasm_val_t{ .kind = WASM_I32,
55+
.of = { .i32 = generate_random_int32() } };
2656
}
2757
else if (kind == WASM_I64) {
28-
std::uniform_int_distribution<int64_t> dis;
29-
return wasm_val_t{ .kind = WASM_I64, .of = { .i64 = dis(gen) } };
58+
return wasm_val_t{ .kind = WASM_I64,
59+
.of = { .i64 = generate_random_int64() } };
3060
}
3161
else if (kind == WASM_F32) {
32-
std::uniform_real_distribution<float> dis;
33-
return wasm_val_t{ .kind = WASM_F32, .of = { .f32 = dis(gen) } };
62+
return wasm_val_t{ .kind = WASM_F32,
63+
.of = { .f32 = generate_random_float() } };
3464
}
3565
else if (kind == WASM_F64) {
36-
std::uniform_real_distribution<double> dis;
37-
return wasm_val_t{ .kind = WASM_F64, .of = { .f64 = dis(gen) } };
66+
return wasm_val_t{ .kind = WASM_F64,
67+
.of = { .f64 = generate_random_double() } };
3868
}
3969
else if (kind == WASM_EXTERNREF) {
40-
std::uniform_int_distribution<uintptr_t> dis;
4170
return wasm_val_t{ .kind = WASM_EXTERNREF,
42-
.of = { .foreign = dis(gen) } };
71+
.of = { .foreign =
72+
(uintptr_t)generate_random_int64() } };
4373
}
4474
else if (kind == WASM_FUNCREF) {
4575
return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
@@ -124,9 +154,8 @@ execute_export_functions(wasm_module_t module, wasm_module_inst_t inst)
124154
std::cout << ")";
125155
}
126156

127-
bool ret =
128-
wasm_runtime_call_wasm_a(exec_env, func, result_count,
129-
results.data(), param_count, args.data());
157+
bool ret = wasm_runtime_call_wasm_a(exec_env, func, result_count,
158+
&results[0], param_count, &args[0]);
130159
if (!ret) {
131160
const char *exception = wasm_runtime_get_exception(inst);
132161
if (!exception) {

0 commit comments

Comments
 (0)