Fix compile warnings of wasm-c-api and add more checks (#592)

This commit is contained in:
Wenyong Huang 2021-03-24 21:13:34 -05:00 committed by GitHub
parent 621231a48b
commit 5a13e1bbbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 21 deletions

View File

@ -104,7 +104,9 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src)
goto failed; goto failed;
} }
len = src->size * src->size_of_elem; /* integer overflow has been checked in generic_vec_init_data,
no need to check again */
len = (uint32)(src->size * src->size_of_elem);
bh_memcpy_s(out->data, len, src->data, len); bh_memcpy_s(out->data, len, src->data, len);
out->num_elems = src->num_elems; out->num_elems = src->num_elems;
return; return;
@ -117,7 +119,7 @@ failed:
void void
wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data) wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data)
{ {
size_t size_in_bytes = 0; uint32 size_in_bytes = 0;
bh_assert(out && data); bh_assert(out && data);
@ -126,7 +128,9 @@ wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data)
goto failed; goto failed;
} }
size_in_bytes = size * sizeof(wasm_byte_t); /* integer overflow has been checked in generic_vec_init_data,
no need to check again */
size_in_bytes = (uint32)(size * sizeof(wasm_byte_t));
bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
out->num_elems = size; out->num_elems = size;
return; return;
@ -435,14 +439,16 @@ wasm_valtype_vec_new(wasm_valtype_vec_t *out,
size_t size, size_t size,
wasm_valtype_t *const data[]) wasm_valtype_t *const data[])
{ {
size_t size_in_bytes = 0; uint32 size_in_bytes = 0;
bh_assert(out && data); bh_assert(out && data);
generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *)); generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *));
if (!out->data) { if (!out->data) {
goto failed; goto failed;
} }
size_in_bytes = size * sizeof(wasm_valtype_t *); /* integer overflow has been checked in generic_vec_init_data,
no need to check again */
size_in_bytes = (uint32)(size * sizeof(wasm_valtype_t *));
bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
out->num_elems = size; out->num_elems = size;
return; return;
@ -924,17 +930,21 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
check_engine_and_store(singleton_engine, store); check_engine_and_store(singleton_engine, store);
bh_assert(binary && binary->data && binary->size); bh_assert(binary && binary->data && binary->size);
pkg_type = get_package_type((uint8 *)binary->data, binary->size); if (binary->size > UINT32_MAX) {
LOG_ERROR("%s failed", __FUNCTION__);
return NULL;
}
pkg_type = get_package_type((uint8 *)binary->data, (uint32)binary->size);
if (Package_Type_Unknown == pkg_type if (Package_Type_Unknown == pkg_type
|| (Wasm_Module_Bytecode == pkg_type || (Wasm_Module_Bytecode == pkg_type
&& INTERP_MODE != current_runtime_mode()) && INTERP_MODE != current_runtime_mode())
|| (Wasm_Module_AoT == pkg_type || (Wasm_Module_AoT == pkg_type
&& INTERP_MODE == current_runtime_mode())) { && INTERP_MODE == current_runtime_mode())) {
LOG_WARNING( LOG_ERROR(
"current runtime mode %d doesn\'t support the package type " "current runtime mode %d doesn\'t support the package type %d",
"%d",
current_runtime_mode(), pkg_type); current_runtime_mode(), pkg_type);
goto failed; return NULL;
} }
module_ex = malloc_internal(sizeof(wasm_module_ex_t)); module_ex = malloc_internal(sizeof(wasm_module_ex_t));
@ -954,10 +964,12 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
module_ex->module_comm_rt = module_ex->module_comm_rt =
wasm_runtime_load((uint8 *)module_ex->binary->data, wasm_runtime_load((uint8 *)module_ex->binary->data,
module_ex->binary->size, error, (uint32)sizeof(error)); (uint32)module_ex->binary->size,
error, (uint32)sizeof(error));
if (!(module_ex->module_comm_rt)) { if (!(module_ex->module_comm_rt)) {
LOG_ERROR(error); LOG_ERROR(error);
goto failed; wasm_module_delete_internal(module_ext_to_module(module_ex));
return NULL;
} }
/* add it to a watching list in store */ /* add it to a watching list in store */
@ -968,7 +980,7 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
return module_ext_to_module(module_ex); return module_ext_to_module(module_ex);
failed: failed:
LOG_DEBUG("%s failed", __FUNCTION__); LOG_ERROR("%s failed", __FUNCTION__);
wasm_module_delete_internal(module_ext_to_module(module_ex)); wasm_module_delete_internal(module_ext_to_module(module_ex));
return NULL; return NULL;
} }
@ -2687,7 +2699,7 @@ wasm_extern_delete(wasm_extern_t *external)
wasm_externkind_t wasm_externkind_t
wasm_extern_kind(const wasm_extern_t *extrenal) wasm_extern_kind(const wasm_extern_t *extrenal)
{ {
return extrenal->kind; return (wasm_externkind_t)extrenal->kind;
} }
wasm_func_t * wasm_func_t *

View File

@ -34,8 +34,8 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
#define check(val, type, expected) \ #define check(val, type, expected) \
if (val.of.type != expected) { \ if (val.of.type != expected) { \
printf("> Expected reading value %f or %d \n", expected, expected); \ printf("> Expected reading value %f or %f \n", expected, expected); \
printf("> Error reading value %f or %d\n", val.of.type, val.of.type); \ printf("> Error reading value %f or %f\n", val.of.type, val.of.type); \
} }
#define check_global(global, type, expected) \ #define check_global(global, type, expected) \
@ -62,14 +62,14 @@ wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filena
wasm_byte_vec_new_uninitialized(&binary, file_size); wasm_byte_vec_new_uninitialized(&binary, file_size);
if (fread(binary.data, file_size, 1, file) != 1) { if (fread(binary.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n"); printf("> Error loading module!\n");
return 1; return NULL;
} }
// Compile. // Compile.
printf("Compiling module...\n"); printf("Compiling module...\n");
own wasm_module_t* module = wasm_module_new(store, &binary); own wasm_module_t* module = wasm_module_new(store, &binary);
if (!module) { if (!module) {
printf("> Error compiling module!\n"); printf("> Error compiling module!\n");
return 1; return NULL;
} }
wasm_byte_vec_delete(&binary); wasm_byte_vec_delete(&binary);
fclose(file); fclose(file);
@ -88,11 +88,17 @@ int main(int argc, const char* argv[]) {
// Load binary. // Load binary.
printf("Loading binary...\n"); printf("Loading binary...\n");
#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0 #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
wasm_module_t* moduleimport = create_module_from_file(store, "globalimport.aot"); wasm_module_t* moduleimport =
create_module_from_file(store, "globalimport.aot");
#else #else
wasm_module_t* moduleimport = create_module_from_file(store, "globalexportimport-1.wasm"); wasm_module_t* moduleimport =
create_module_from_file(store, "globalexportimport-1.wasm");
#endif #endif
if (!moduleimport) {
return 1;
}
// Instantiate. // Instantiate.
printf("Instantiating Import module...\n"); printf("Instantiating Import module...\n");
own wasm_instance_t* instance_import = own wasm_instance_t* instance_import =
@ -163,4 +169,4 @@ int main(int argc, const char* argv[]) {
printf("Done.\n"); printf("Done.\n");
return 0; return 0;
} }