diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 9e65989b16..5087c30b6e 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -104,7 +104,9 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src) 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); out->num_elems = src->num_elems; return; @@ -117,7 +119,7 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src) void 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); @@ -126,7 +128,9 @@ wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data) 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); out->num_elems = size; return; @@ -435,14 +439,16 @@ wasm_valtype_vec_new(wasm_valtype_vec_t *out, size_t size, wasm_valtype_t *const data[]) { - size_t size_in_bytes = 0; + uint32 size_in_bytes = 0; bh_assert(out && data); generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *)); if (!out->data) { 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); out->num_elems = size; 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); 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 || (Wasm_Module_Bytecode == pkg_type && INTERP_MODE != current_runtime_mode()) || (Wasm_Module_AoT == pkg_type && INTERP_MODE == current_runtime_mode())) { - LOG_WARNING( - "current runtime mode %d doesn\'t support the package type " - "%d", + LOG_ERROR( + "current runtime mode %d doesn\'t support the package type %d", current_runtime_mode(), pkg_type); - goto failed; + return NULL; } 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 = 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)) { 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 */ @@ -968,7 +980,7 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) return module_ext_to_module(module_ex); failed: - LOG_DEBUG("%s failed", __FUNCTION__); + LOG_ERROR("%s failed", __FUNCTION__); wasm_module_delete_internal(module_ext_to_module(module_ex)); return NULL; } @@ -2687,7 +2699,7 @@ wasm_extern_delete(wasm_extern_t *external) wasm_externkind_t wasm_extern_kind(const wasm_extern_t *extrenal) { - return extrenal->kind; + return (wasm_externkind_t)extrenal->kind; } wasm_func_t * diff --git a/samples/wasm-c-api/src/globalexportimport.c b/samples/wasm-c-api/src/globalexportimport.c index 6fc1958a5c..076b632eb5 100644 --- a/samples/wasm-c-api/src/globalexportimport.c +++ b/samples/wasm-c-api/src/globalexportimport.c @@ -34,8 +34,8 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) { #define check(val, type, expected) \ if (val.of.type != expected) { \ - printf("> Expected reading value %f or %d \n", expected, expected); \ - printf("> Error reading value %f or %d\n", val.of.type, val.of.type); \ + printf("> Expected reading value %f or %f \n", expected, expected); \ + printf("> Error reading value %f or %f\n", val.of.type, val.of.type); \ } #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); if (fread(binary.data, file_size, 1, file) != 1) { printf("> Error loading module!\n"); - return 1; + return NULL; } // Compile. printf("Compiling module...\n"); own wasm_module_t* module = wasm_module_new(store, &binary); if (!module) { printf("> Error compiling module!\n"); - return 1; + return NULL; } wasm_byte_vec_delete(&binary); fclose(file); @@ -88,11 +88,17 @@ int main(int argc, const char* argv[]) { // Load binary. printf("Loading binary...\n"); #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 - 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 + if (!moduleimport) { + return 1; + } + // Instantiate. printf("Instantiating Import module...\n"); own wasm_instance_t* instance_import = @@ -163,4 +169,4 @@ int main(int argc, const char* argv[]) { printf("Done.\n"); return 0; -} \ No newline at end of file +}