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
This commit is contained in:
liang.he 2022-12-01 22:03:09 +08:00 committed by GitHub
parent 1652f22a77
commit fc8f70cfa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 143 additions and 66 deletions

View File

@ -2112,6 +2112,7 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
memset(&module_name, 0, sizeof(wasm_val_vec_t));
memset(&name, 0, sizeof(wasm_val_vec_t));
extern_type = NULL;
import_type = NULL;
if (i < import_func_count) {
wasm_functype_t *type = NULL;

View File

@ -199,11 +199,16 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
if(VALGRIND)
foreach(EX ${EXAMPLES})
add_custom_target(${EX}_LEAK_TEST
COMMAND ${VALGRIND} --tool=memcheck --leak-check=yes ./${EX}
add_custom_command(
OUTPUT ${EX}_leak_check.report
DEPENDS ${EX} ${EX}_WASM
VERBATIM
COMMENT "run a leak check on ${EX}"
COMMAND ${VALGRIND} --tool=memcheck --leak-check=summary -- ./${EX} > ${EX}_leak_check.report 2>&1
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(${EX}_LEAK_TEST ALL
DEPENDS ${EX}_leak_check.report
COMMAND grep "All heap blocks were freed -- no leaks are possible" ${EX}_leak_check.report
COMMENT "Please read ${EX}_leak_check.report when meeting Error"
)
endforeach()
endif (VALGRIND)

View File

@ -169,9 +169,11 @@ int main(int argc, const char* argv[]) {
wasm_val_t rs[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(as);
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return 1;
}
wasm_extern_vec_delete(&exports);

View File

@ -231,10 +231,13 @@ vm_function_set_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t byte)
wasm_val_vec_t args = WASM_ARRAY_VEC(a_v);
wasm_val_vec_t results = WASM_EMPTY_VEC;
wasm_trap_t *trap = wasm_func_call(vm->function_list[0], &args, &results);
if (trap)
if (trap) {
printf("call wasm_set_byte failed");
wasm_trap_delete(trap);
return false;
}
return trap != NULL;
return true;
}
bool
@ -247,6 +250,7 @@ vm_function_get_byte(const wasm_vm_t *vm, uint32_t offset, uint8_t *byte)
wasm_trap_t *trap = wasm_func_call(vm->function_list[1], &args, &results);
if (trap) {
printf("call wasm_get_byte failed");
wasm_trap_delete(trap);
return false;
}

View File

@ -97,10 +97,16 @@ int main(int argc, const char* argv[]) {
wasm_val_t results[1] = { WASM_INIT_VAL };
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
if (wasm_func_call(add_func, &args_vec, &results_vec)
|| results_vec.data[0].of.i32 != 7) {
printf("> Error calling function!\n");
return 1;
wasm_trap_t *trap = wasm_func_call(add_func, &args_vec, &results_vec);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return 1;
}
if (results_vec.data[0].of.i32 != 7) {
printf("> Error calling function!\n");
return 1;
}
wasm_extern_vec_delete(&exports);

View File

@ -37,15 +37,22 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
check(val, type, expected); \
}
#define check_call(func, type, expected) \
{ \
wasm_val_t vs[1]; \
wasm_val_vec_t args = WASM_EMPTY_VEC; \
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
wasm_func_call(func, &args, &results); \
check(vs[0], type, expected); \
}
#define check_trap(trap) \
if (trap) { \
printf("> Error calling function\n"); \
wasm_trap_delete(trap); \
exit(1); \
}
#define check_call(func, type, expected) \
{ \
wasm_val_t vs[1]; \
wasm_val_vec_t args = WASM_EMPTY_VEC; \
wasm_val_vec_t results = WASM_ARRAY_VEC(vs); \
wasm_trap_t *trap = wasm_func_call(func, &args, &results); \
check_trap(trap); \
check(vs[0], type, expected); \
}
int main(int argc, const char* argv[]) {
// Initialize.
@ -225,16 +232,23 @@ int main(int argc, const char* argv[]) {
wasm_val_vec_t res = WASM_EMPTY_VEC;
wasm_val_t vs73[] = { WASM_F32_VAL(73) };
wasm_val_vec_t args73 = WASM_ARRAY_VEC(vs73);
wasm_func_call(set_var_f32_import, &args73, &res);
wasm_trap_t *trap = wasm_func_call(set_var_f32_import, &args73, &res);
check_trap(trap);
wasm_val_t vs74[] = { WASM_I64_VAL(74) };
wasm_val_vec_t args74 = WASM_ARRAY_VEC(vs74);
wasm_func_call(set_var_i64_import, &args74, &res);
trap = wasm_func_call(set_var_i64_import, &args74, &res);
check_trap(trap);
wasm_val_t vs77[] = { WASM_F32_VAL(77) };
wasm_val_vec_t args77 = WASM_ARRAY_VEC(vs77);
wasm_func_call(set_var_f32_export, &args77, &res);
trap = wasm_func_call(set_var_f32_export, &args77, &res);
check_trap(trap);
wasm_val_t vs78[] = { WASM_I64_VAL(78) };
wasm_val_vec_t args78 = WASM_ARRAY_VEC(vs78);
wasm_func_call(set_var_i64_export, &args78, &res);
trap = wasm_func_call(set_var_i64_export, &args78, &res);
check_trap(trap);
check_global(var_f32_import, f32, 73);
check_global(var_i64_import, i64, 74);

View File

@ -50,13 +50,21 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
check(val, type, expected); \
}
#define check_call(func, type, expected) \
{ \
wasm_val_vec_t results; \
wasm_val_vec_new_uninitialized(&results, 1); \
wasm_func_call(func, NULL, &results); \
check(results.data[0], type, expected); \
}
#define check_trap(trap) \
if (trap) { \
printf("> Error calling function\n"); \
wasm_trap_delete(trap); \
exit(1); \
}
#define check_call(func, type, expected) \
{ \
wasm_val_vec_t results; \
wasm_val_vec_new_uninitialized(&results, 1); \
wasm_trap_t *trap = wasm_func_call(func, NULL, &results); \
check_trap(trap); \
check(results.data[0], type, expected); \
}
wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filename)
{
@ -149,7 +157,9 @@ int main(int argc, const char* argv[]) {
printf("Modify the variable to 77.0...\n");
wasm_val_vec_t args77;
wasm_val_vec_new(&args77, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 77.0}} });
wasm_func_call(set_var_f32_export, &args77, NULL); //Call to module export
wasm_trap_t *trap = wasm_func_call(set_var_f32_export, &args77,
NULL); // Call to module export
check_trap(trap);
check_call(get_var_f32_export, f32, 77.0); //Call to module export
check_global(var_f32_export, f32, 77.0); //Failed here, still 37
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[]) {
printf("Modify the variable to 78.0...\n");
wasm_val_vec_t args78;
wasm_val_vec_new(&args78, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 78.0}} });
wasm_func_call(set_var_f32_import, &args78, NULL);
trap = wasm_func_call(set_var_f32_import, &args78, NULL);
check_trap(trap);
check_global(var_f32_export, f32, 78.0);
check_call(get_var_f32_export, f32, 78.0); //Call to module export Failed here, still 77
check_call(get_var_f32_import, f32, 78.0); //Call to module import

View File

@ -118,9 +118,11 @@ int main(int argc, const char* argv[]) {
printf("Calling export...\n");
wasm_val_vec_t args = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return 1;
}
wasm_extern_vec_delete(&exports);

View File

@ -50,9 +50,11 @@ own wasm_ref_t* call_v_r(const wasm_func_t* func) {
wasm_val_t rs[] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
if (wasm_func_call(func, &args, &results)) {
printf("> Error calling function!\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
exit(1);
}
printf("okay\n");
return rs[0].of.ref;
@ -63,9 +65,11 @@ void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
wasm_val_vec_t results = WASM_EMPTY_VEC;
if (wasm_func_call(func, &args, &results)) {
printf("> Error calling function!\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
exit(1);
}
printf("okay\n");
}
@ -76,9 +80,11 @@ own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) {
wasm_val_t rs[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
if (wasm_func_call(func, &args, &results)) {
printf("> Error calling function!\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
exit(1);
}
printf("okay\n");
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) {
wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) };
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
wasm_val_vec_t results = WASM_EMPTY_VEC;
if (wasm_func_call(func, &args, &results)) {
printf("> Error calling function!\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
exit(1);
}
printf("okay\n");
}
@ -102,9 +110,11 @@ own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) {
wasm_val_t rs[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
wasm_val_vec_t results = WASM_ARRAY_VEC(rs);
if (wasm_func_call(func, &args, &results)) {
printf("> Error calling function!\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
exit(1);
}
printf("okay\n");
return rs[0].of.ref;

View File

@ -36,9 +36,16 @@ void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) {
wasm_val_t r[] = {WASM_INIT_VAL};
wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
wasm_val_vec_t results = WASM_ARRAY_VEC(r);
if (wasm_func_call(func, &args_, &results) || r[0].of.i32 != expected) {
printf("> Error on result\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
if (trap) {
printf("> Error on result\n");
wasm_trap_delete(trap);
exit(1);
}
if (r[0].of.i32 != expected) {
printf("> Error on result\n");
exit(1);
}
}
@ -59,9 +66,11 @@ void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected
void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) {
wasm_val_vec_t args_ = {i, args, i, sizeof(wasm_val_t), NULL};
wasm_val_vec_t results = WASM_EMPTY_VEC;
if (wasm_func_call(func, &args_, &results)) {
printf("> Error on result, expected empty\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args_, &results);
if (trap) {
printf("> Error on result, expected empty\n");
wasm_trap_delete(trap);
exit(1);
}
}

View File

@ -133,9 +133,11 @@ int main(int argc, const char* argv[]) {
};
wasm_val_vec_t args = WASM_ARRAY_VEC(vals);
wasm_val_vec_t results = WASM_ARRAY_VEC(res);
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
wasm_trap_t *trap = wasm_func_call(run_func, &args, &results);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return 1;
}
wasm_extern_vec_delete(&exports);

View File

@ -111,8 +111,10 @@ main(int argc, const char *argv[])
// Call.
printf("Calling export...\n");
wasm_val_vec_t empty = WASM_EMPTY_VEC;
if (wasm_func_call(run_func, &empty, &empty)) {
wasm_trap_t *trap = wasm_func_call(run_func, &empty, &empty);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return 1;
}

View File

@ -53,9 +53,16 @@ void check_call(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected)
wasm_val_t r[1] = { WASM_INIT_VAL };
wasm_val_vec_t args = WASM_ARRAY_VEC(vs);
wasm_val_vec_t results = WASM_ARRAY_VEC(r);
if (wasm_func_call(func, &args, &results) || r[0].of.i32 != expected) {
printf("> Error on result\n");
exit(1);
wasm_trap_t *trap = wasm_func_call(func, &args, &results);
if (trap) {
printf("> Error on calling\n");
wasm_trap_delete(trap);
exit(1);
}
if (r[0].of.i32 != expected) {
printf("> Error on result\n");
exit(1);
}
}

View File

@ -85,8 +85,10 @@ run(void *args_abs)
// Call.
wasm_val_vec_t empty = WASM_EMPTY_VEC;
if (wasm_func_call(run_func, &empty, &empty)) {
wasm_trap_t *trap = wasm_func_call(run_func, &empty, &empty);
if (trap) {
printf("> Error calling function!\n");
wasm_trap_delete(trap);
return NULL;
}