|
13 | 13 |
|
14 | 14 | using namespace std;
|
15 | 15 |
|
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) |
43 | 18 | {
|
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; |
45 | 22 | }
|
46 | 23 |
|
47 | 24 | static wasm_val_t
|
48 |
| -random_gen_val(wasm_valkind_t kind) |
| 25 | +pre_defined_val(wasm_valkind_t kind) |
49 | 26 | {
|
50 |
| - srand(1024); |
51 |
| - |
52 | 27 | 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 } }; |
55 | 29 | }
|
56 | 30 | 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 } }; |
59 | 32 | }
|
60 | 33 | 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 } }; |
63 | 35 | }
|
64 | 36 | 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 } }; |
67 | 38 | }
|
68 | 39 | else if (kind == WASM_EXTERNREF) {
|
69 | 40 | return wasm_val_t{ .kind = WASM_EXTERNREF,
|
70 |
| - .of = { .foreign = |
71 |
| - (uintptr_t)generate_random_int64() } }; |
| 41 | + .of = { .foreign = 0xabcddead } }; |
72 | 42 | }
|
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 { |
74 | 46 | return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
|
75 | 47 | }
|
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 | + } |
79 | 81 | }
|
| 82 | + std::cout << ")" << std::endl; |
80 | 83 | }
|
81 | 84 |
|
82 | 85 | static bool
|
@@ -108,53 +111,33 @@ execute_export_functions(wasm_module_t module, wasm_module_inst_t inst)
|
108 | 111 | for (unsigned p_i = 0; p_i < param_count; p_i++) {
|
109 | 112 | wasm_valkind_t param_type =
|
110 | 113 | 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); |
112 | 123 | args.push_back(arg);
|
113 | 124 | }
|
114 | 125 |
|
115 | 126 | /* build results storage */
|
116 | 127 | uint32_t result_count = wasm_func_type_get_result_count(func_type);
|
117 | 128 | std::vector<wasm_val_t> results = std::vector<wasm_val_t>(result_count);
|
118 | 129 |
|
| 130 | + print_execution_args(export_type, args, param_count); |
| 131 | + |
119 | 132 | /* execute the function */
|
120 | 133 | 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; |
154 | 137 | }
|
155 | 138 |
|
156 | 139 | 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()); |
158 | 141 | if (!ret) {
|
159 | 142 | const char *exception = wasm_runtime_get_exception(inst);
|
160 | 143 | if (!exception) {
|
|
0 commit comments