Skip to content

Commit fc8f70c

Browse files
authored
Fix issues detected by Coverity (#1776)
- wasm_func_call always return trap if failed - in Debug, always run LEAK_TEST in samples/wasm-c-api
1 parent 1652f22 commit fc8f70c

14 files changed

Lines changed: 144 additions & 67 deletions

File tree

core/iwasm/common/wasm_c_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,7 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
21122112
memset(&module_name, 0, sizeof(wasm_val_vec_t));
21132113
memset(&name, 0, sizeof(wasm_val_vec_t));
21142114
extern_type = NULL;
2115+
import_type = NULL;
21152116

21162117
if (i < import_func_count) {
21172118
wasm_functype_t *type = NULL;

samples/wasm-c-api/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,16 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
199199

200200
if(VALGRIND)
201201
foreach(EX ${EXAMPLES})
202-
add_custom_target(${EX}_LEAK_TEST
203-
COMMAND ${VALGRIND} --tool=memcheck --leak-check=yes ./${EX}
202+
add_custom_command(
203+
OUTPUT ${EX}_leak_check.report
204204
DEPENDS ${EX} ${EX}_WASM
205-
VERBATIM
206-
COMMENT "run a leak check on ${EX}"
205+
COMMAND ${VALGRIND} --tool=memcheck --leak-check=summary -- ./${EX} > ${EX}_leak_check.report 2>&1
206+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
207+
)
208+
add_custom_target(${EX}_LEAK_TEST ALL
209+
DEPENDS ${EX}_leak_check.report
210+
COMMAND grep "All heap blocks were freed -- no leaks are possible" ${EX}_leak_check.report
211+
COMMENT "Please read ${EX}_leak_check.report when meeting Error"
207212
)
208213
endforeach()
209214
endif (VALGRIND)

samples/wasm-c-api/src/callback.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ int main(int argc, const char* argv[]) {
169169
wasm_val_t rs[1] = { WASM_INIT_VAL };
170170
wasm_val_vec_t args = WASM_ARRAY_VEC(as);
171171
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
172-
if (wasm_func_call(run_func, &args, &results)) {
173-
printf("> Error calling function!\n");
174-
return 1;
172+
wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
173+
if (trap) {
174+
printf("> Error calling function!\n");
175+
wasm_trap_delete(trap);
176+
return 1;
175177
}
176178

177179
wasm_extern_vec_delete(&exports);

samples/wasm-c-api/src/clone.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,13 @@ vm_function_set_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t byte)
231231
wasm_val_vec_t args = WASM_ARRAY_VEC(a_v);
232232
wasm_val_vec_t results = WASM_EMPTY_VEC;
233233
wasm_trap_t *trap = wasm_func_call(vm->function_list[0], &args, &results);
234-
if (trap)
234+
if (trap) {
235235
printf("call wasm_set_byte failed");
236+
wasm_trap_delete(trap);
237+
return false;
238+
}
236239

237-
return trap != NULL;
240+
return true;
238241
}
239242

240243
bool
@@ -247,6 +250,7 @@ vm_function_get_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t *byte)
247250
wasm_trap_t *trap = wasm_func_call(vm->function_list[1], &args, &results);
248251
if (trap) {
249252
printf("call wasm_get_byte failed");
253+
wasm_trap_delete(trap);
250254
return false;
251255
}
252256

samples/wasm-c-api/src/empty_imports.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,16 @@ int main(int argc, const char* argv[]) {
9797
wasm_val_t results[1] = { WASM_INIT_VAL };
9898
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
9999

100-
if (wasm_func_call(add_func, &args_vec, &results_vec)
101-
|| results_vec.data[0].of.i32 != 7) {
102-
printf("> Error calling function!\n");
103-
return 1;
100+
wasm_trap_t *trap = wasm_func_call(add_func, &args_vec, &results_vec);
101+
if (trap) {
102+
printf("> Error calling function!\n");
103+
wasm_trap_delete(trap);
104+
return 1;
105+
}
106+
107+
if (results_vec.data[0].of.i32 != 7) {
108+
printf("> Error calling function!\n");
109+
return 1;
104110
}
105111

106112
wasm_extern_vec_delete(&exports);

samples/wasm-c-api/src/global.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
3737
check(val, type, expected); \
3838
}
3939

40-
#define check_call(func, type, expected) \
41-
{ \
42-
wasm_val_t vs[1]; \
43-
wasm_val_vec_t args = WASM_EMPTY_VEC; \
44-
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
45-
wasm_func_call(func, &args, &results); \
46-
check(vs[0], type, expected); \
47-
}
48-
40+
#define check_trap(trap) \
41+
if (trap) { \
42+
printf("> Error calling function\n"); \
43+
wasm_trap_delete(trap); \
44+
exit(1); \
45+
}
46+
47+
#define check_call(func, type, expected) \
48+
{ \
49+
wasm_val_t vs[1]; \
50+
wasm_val_vec_t args = WASM_EMPTY_VEC; \
51+
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
52+
wasm_trap_t *trap = wasm_func_call(func, &args, &results); \
53+
check_trap(trap); \
54+
check(vs[0], type, expected); \
55+
}
4956

5057
int main(int argc, const char* argv[]) {
5158
// Initialize.
@@ -225,16 +232,23 @@ int main(int argc, const char* argv[]) {
225232
wasm_val_vec_t res = WASM_EMPTY_VEC;
226233
wasm_val_t vs73[] = { WASM_F32_VAL(73) };
227234
wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73);
228-
wasm_func_call(set_var_f32_import, &args73, &res);
235+
wasm_trap_t *trap = wasm_func_call(set_var_f32_import, &args73, &res);
236+
check_trap(trap);
237+
229238
wasm_val_t vs74[] = { WASM_I64_VAL(74) };
230239
wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74);
231-
wasm_func_call(set_var_i64_import, &args74, &res);
240+
trap = wasm_func_call(set_var_i64_import, &args74, &res);
241+
check_trap(trap);
242+
232243
wasm_val_t vs77[] = { WASM_F32_VAL(77) };
233244
wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77);
234-
wasm_func_call(set_var_f32_export, &args77, &res);
245+
trap = wasm_func_call(set_var_f32_export, &args77, &res);
246+
check_trap(trap);
247+
235248
wasm_val_t vs78[] = { WASM_I64_VAL(78) };
236249
wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78);
237-
wasm_func_call(set_var_i64_export, &args78, &res);
250+
trap = wasm_func_call(set_var_i64_export, &args78, &res);
251+
check_trap(trap);
238252

239253
check_global(var_f32_import, f32, 73);
240254
check_global(var_i64_import, i64, 74);

samples/wasm-c-api/src/globalexportimport.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,21 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
5050
check(val, type, expected); \
5151
}
5252

53-
#define check_call(func, type, expected) \
54-
{ \
55-
wasm_val_vec_t results; \
56-
wasm_val_vec_new_uninitialized(&results, 1); \
57-
wasm_func_call(func, NULL, &results); \
58-
check(results.data[0], type, expected); \
59-
}
53+
#define check_trap(trap) \
54+
if (trap) { \
55+
printf("> Error calling function\n"); \
56+
wasm_trap_delete(trap); \
57+
exit(1); \
58+
}
59+
60+
#define check_call(func, type, expected) \
61+
{ \
62+
wasm_val_vec_t results; \
63+
wasm_val_vec_new_uninitialized(&results, 1); \
64+
wasm_trap_t *trap = wasm_func_call(func, NULL, &results); \
65+
check_trap(trap); \
66+
check(results.data[0], type, expected); \
67+
}
6068

6169
wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filename)
6270
{
@@ -149,7 +157,9 @@ int main(int argc, const char* argv[]) {
149157
printf("Modify the variable to 77.0...\n");
150158
wasm_val_vec_t args77;
151159
wasm_val_vec_new(&args77, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 77.0}} });
152-
wasm_func_call(set_var_f32_export, &args77, NULL); //Call to module export
160+
wasm_trap_t *trap = wasm_func_call(set_var_f32_export, &args77,
161+
NULL); // Call to module export
162+
check_trap(trap);
153163
check_call(get_var_f32_export, f32, 77.0); //Call to module export
154164
check_global(var_f32_export, f32, 77.0); //Failed here, still 37
155165
check_call(get_var_f32_import, f32, 77.0); //Call to module import Failed here, still 37
@@ -158,7 +168,8 @@ int main(int argc, const char* argv[]) {
158168
printf("Modify the variable to 78.0...\n");
159169
wasm_val_vec_t args78;
160170
wasm_val_vec_new(&args78, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 78.0}} });
161-
wasm_func_call(set_var_f32_import, &args78, NULL);
171+
trap = wasm_func_call(set_var_f32_import, &args78, NULL);
172+
check_trap(trap);
162173
check_global(var_f32_export, f32, 78.0);
163174
check_call(get_var_f32_export, f32, 78.0); //Call to module export Failed here, still 77
164175
check_call(get_var_f32_import, f32, 78.0); //Call to module import

samples/wasm-c-api/src/hello.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ int main(int argc, const char* argv[]) {
118118
printf("Calling export...\n");
119119
wasm_val_vec_t args = WASM_EMPTY_VEC;
120120
wasm_val_vec_t results = WASM_EMPTY_VEC;
121-
if (wasm_func_call(run_func, &args, &results)) {
122-
printf("> Error calling function!\n");
123-
return 1;
121+
wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
122+
if (trap) {
123+
printf("> Error calling function!\n");
124+
wasm_trap_delete(trap);
125+
return 1;
124126
}
125127

126128
wasm_extern_vec_delete(&exports);

samples/wasm-c-api/src/hostref.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ own wasm_ref_t* call_v_r(const wasm_func_t* func) {
5050
wasm_val_t rs[] = { WASM_INIT_VAL };
5151
wasm_val_vec_t args = WASM_EMPTY_VEC;
5252
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
53-
if (wasm_func_call(func, &args, &results)) {
54-
printf("> Error calling function!\n");
55-
exit(1);
53+
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
54+
if (trap) {
55+
printf("> Error calling function!\n");
56+
wasm_trap_delete(trap);
57+
exit(1);
5658
}
5759
printf("okay\n");
5860
return rs[0].of.ref;
@@ -63,9 +65,11 @@ void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
6365
wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
6466
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
6567
wasm_val_vec_t results = WASM_EMPTY_VEC;
66-
if (wasm_func_call(func, &args, &results)) {
67-
printf("> Error calling function!\n");
68-
exit(1);
68+
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
69+
if (trap) {
70+
printf("> Error calling function!\n");
71+
wasm_trap_delete(trap);
72+
exit(1);
6973
}
7074
printf("okay\n");
7175
}
@@ -76,9 +80,11 @@ own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) {
7680
wasm_val_t rs[1] = { WASM_INIT_VAL };
7781
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
7882
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
79-
if (wasm_func_call(func, &args, &results)) {
80-
printf("> Error calling function!\n");
81-
exit(1);
83+
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
84+
if (trap) {
85+
printf("> Error calling function!\n");
86+
wasm_trap_delete(trap);
87+
exit(1);
8288
}
8389
printf("okay\n");
8490
return rs[0].of.ref;
@@ -89,9 +95,11 @@ void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) {
8995
wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) };
9096
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
9197
wasm_val_vec_t results = WASM_EMPTY_VEC;
92-
if (wasm_func_call(func, &args, &results)) {
93-
printf("> Error calling function!\n");
94-
exit(1);
98+
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
99+
if (trap) {
100+
printf("> Error calling function!\n");
101+
wasm_trap_delete(trap);
102+
exit(1);
95103
}
96104
printf("okay\n");
97105
}
@@ -102,9 +110,11 @@ own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) {
102110
wasm_val_t rs[1] = { WASM_INIT_VAL };
103111
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
104112
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
105-
if (wasm_func_call(func, &args, &results)) {
106-
printf("> Error calling function!\n");
107-
exit(1);
113+
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
114+
if (trap) {
115+
printf("> Error calling function!\n");
116+
wasm_trap_delete(trap);
117+
exit(1);
108118
}
109119
printf("okay\n");
110120
return rs[0].of.ref;

samples/wasm-c-api/src/memory.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) {
3636
wasm_val_t r[] = {WASM_INIT_VAL};
3737
wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
3838
wasm_val_vec_t results = WASM_ARRAY_VEC(r);
39-
if (wasm_func_call(func, &args_, &results) || r[0].of.i32 != expected) {
40-
printf("> Error on result\n");
41-
exit(1);
39+
wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
40+
if (trap) {
41+
printf("> Error on result\n");
42+
wasm_trap_delete(trap);
43+
exit(1);
44+
}
45+
46+
if (r[0].of.i32 != expected) {
47+
printf("> Error on result\n");
48+
exit(1);
4249
}
4350
}
4451

@@ -59,9 +66,11 @@ void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected
5966
void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) {
6067
wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
6168
wasm_val_vec_t results = WASM_EMPTY_VEC;
62-
if (wasm_func_call(func, &args_, &results)) {
63-
printf("> Error on result, expected empty\n");
64-
exit(1);
69+
wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
70+
if (trap) {
71+
printf("> Error on result, expected empty\n");
72+
wasm_trap_delete(trap);
73+
exit(1);
6574
}
6675
}
6776

0 commit comments

Comments
 (0)