Skip to content

Commit 5a13e1b

Browse files
authored
Fix compile warnings of wasm-c-api and add more checks (#592)
1 parent 621231a commit 5a13e1b

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

core/iwasm/common/wasm_c_api.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src)
104104
goto failed;
105105
}
106106

107-
len = src->size * src->size_of_elem;
107+
/* integer overflow has been checked in generic_vec_init_data,
108+
no need to check again */
109+
len = (uint32)(src->size * src->size_of_elem);
108110
bh_memcpy_s(out->data, len, src->data, len);
109111
out->num_elems = src->num_elems;
110112
return;
@@ -117,7 +119,7 @@ wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src)
117119
void
118120
wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data)
119121
{
120-
size_t size_in_bytes = 0;
122+
uint32 size_in_bytes = 0;
121123

122124
bh_assert(out && data);
123125

@@ -126,7 +128,9 @@ wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data)
126128
goto failed;
127129
}
128130

129-
size_in_bytes = size * sizeof(wasm_byte_t);
131+
/* integer overflow has been checked in generic_vec_init_data,
132+
no need to check again */
133+
size_in_bytes = (uint32)(size * sizeof(wasm_byte_t));
130134
bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
131135
out->num_elems = size;
132136
return;
@@ -435,14 +439,16 @@ wasm_valtype_vec_new(wasm_valtype_vec_t *out,
435439
size_t size,
436440
wasm_valtype_t *const data[])
437441
{
438-
size_t size_in_bytes = 0;
442+
uint32 size_in_bytes = 0;
439443
bh_assert(out && data);
440444
generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *));
441445
if (!out->data) {
442446
goto failed;
443447
}
444448

445-
size_in_bytes = size * sizeof(wasm_valtype_t *);
449+
/* integer overflow has been checked in generic_vec_init_data,
450+
no need to check again */
451+
size_in_bytes = (uint32)(size * sizeof(wasm_valtype_t *));
446452
bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
447453
out->num_elems = size;
448454
return;
@@ -924,17 +930,21 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
924930
check_engine_and_store(singleton_engine, store);
925931
bh_assert(binary && binary->data && binary->size);
926932

927-
pkg_type = get_package_type((uint8 *)binary->data, binary->size);
933+
if (binary->size > UINT32_MAX) {
934+
LOG_ERROR("%s failed", __FUNCTION__);
935+
return NULL;
936+
}
937+
938+
pkg_type = get_package_type((uint8 *)binary->data, (uint32)binary->size);
928939
if (Package_Type_Unknown == pkg_type
929940
|| (Wasm_Module_Bytecode == pkg_type
930941
&& INTERP_MODE != current_runtime_mode())
931942
|| (Wasm_Module_AoT == pkg_type
932943
&& INTERP_MODE == current_runtime_mode())) {
933-
LOG_WARNING(
934-
"current runtime mode %d doesn\'t support the package type "
935-
"%d",
944+
LOG_ERROR(
945+
"current runtime mode %d doesn\'t support the package type %d",
936946
current_runtime_mode(), pkg_type);
937-
goto failed;
947+
return NULL;
938948
}
939949

940950
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)
954964

955965
module_ex->module_comm_rt =
956966
wasm_runtime_load((uint8 *)module_ex->binary->data,
957-
module_ex->binary->size, error, (uint32)sizeof(error));
967+
(uint32)module_ex->binary->size,
968+
error, (uint32)sizeof(error));
958969
if (!(module_ex->module_comm_rt)) {
959970
LOG_ERROR(error);
960-
goto failed;
971+
wasm_module_delete_internal(module_ext_to_module(module_ex));
972+
return NULL;
961973
}
962974

963975
/* 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)
968980
return module_ext_to_module(module_ex);
969981

970982
failed:
971-
LOG_DEBUG("%s failed", __FUNCTION__);
983+
LOG_ERROR("%s failed", __FUNCTION__);
972984
wasm_module_delete_internal(module_ext_to_module(module_ex));
973985
return NULL;
974986
}
@@ -2687,7 +2699,7 @@ wasm_extern_delete(wasm_extern_t *external)
26872699
wasm_externkind_t
26882700
wasm_extern_kind(const wasm_extern_t *extrenal)
26892701
{
2690-
return extrenal->kind;
2702+
return (wasm_externkind_t)extrenal->kind;
26912703
}
26922704

26932705
wasm_func_t *

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
3434

3535
#define check(val, type, expected) \
3636
if (val.of.type != expected) { \
37-
printf("> Expected reading value %f or %d \n", expected, expected); \
38-
printf("> Error reading value %f or %d\n", val.of.type, val.of.type); \
37+
printf("> Expected reading value %f or %f \n", expected, expected); \
38+
printf("> Error reading value %f or %f\n", val.of.type, val.of.type); \
3939
}
4040

4141
#define check_global(global, type, expected) \
@@ -62,14 +62,14 @@ wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filena
6262
wasm_byte_vec_new_uninitialized(&binary, file_size);
6363
if (fread(binary.data, file_size, 1, file) != 1) {
6464
printf("> Error loading module!\n");
65-
return 1;
65+
return NULL;
6666
}
6767
// Compile.
6868
printf("Compiling module...\n");
6969
own wasm_module_t* module = wasm_module_new(store, &binary);
7070
if (!module) {
7171
printf("> Error compiling module!\n");
72-
return 1;
72+
return NULL;
7373
}
7474
wasm_byte_vec_delete(&binary);
7575
fclose(file);
@@ -88,11 +88,17 @@ int main(int argc, const char* argv[]) {
8888
// Load binary.
8989
printf("Loading binary...\n");
9090
#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
91-
wasm_module_t* moduleimport = create_module_from_file(store, "globalimport.aot");
91+
wasm_module_t* moduleimport =
92+
create_module_from_file(store, "globalimport.aot");
9293
#else
93-
wasm_module_t* moduleimport = create_module_from_file(store, "globalexportimport-1.wasm");
94+
wasm_module_t* moduleimport =
95+
create_module_from_file(store, "globalexportimport-1.wasm");
9496
#endif
9597

98+
if (!moduleimport) {
99+
return 1;
100+
}
101+
96102
// Instantiate.
97103
printf("Instantiating Import module...\n");
98104
own wasm_instance_t* instance_import =
@@ -163,4 +169,4 @@ int main(int argc, const char* argv[]) {
163169
printf("Done.\n");
164170
return 0;
165171

166-
}
172+
}

0 commit comments

Comments
 (0)