Skip to content

Commit 7f9e492

Browse files
authored
Enhance type checking for function types in loader and improve error handling (#4294)
Especially when GC is enabled, a valid item of `module->types` needs additional checks before casting to WASMFuncType. Also, avoid overflowing if reftype_map_count is 0. Additionally, correctly set IN_OSS_FUZZ based on CFLAGS_ENV for sanitizer configuration. Update ASan and UBSan messages for clarity in non-oss-fuzz environments.
1 parent 782c69f commit 7f9e492

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

core/iwasm/interpreter/wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ wasm_value_type_size_internal(uint8 value_type, uint8 pointer_size)
12431243
return sizeof(int16);
12441244
#endif
12451245
else {
1246-
bh_assert(0);
1246+
bh_assert(0 && "Unknown value type. It should be handled ahead.");
12471247
}
12481248
#if WASM_ENABLE_GC == 0
12491249
(void)pointer_size;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
379379
mem = mem_new; \
380380
} while (0)
381381

382-
#if WASM_ENABLE_GC != 0
383382
static bool
384383
check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
385384
char *error_buf, uint32 error_buf_size)
@@ -392,6 +391,7 @@ check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
392391
return true;
393392
}
394393

394+
#if WASM_ENABLE_GC != 0
395395
static bool
396396
check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
397397
uint32 error_buf_size)
@@ -409,6 +409,29 @@ check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
409409
}
410410
#endif
411411

412+
/*
413+
* if no GC is enabled, an valid type is always a function type.
414+
* but if GC is enabled, we need to check the type flag
415+
*/
416+
static bool
417+
check_function_type(const WASMModule *module, uint32 type_index,
418+
char *error_buf, uint32 error_buf_size)
419+
{
420+
if (!check_type_index(module, module->type_count, type_index, error_buf,
421+
error_buf_size)) {
422+
return false;
423+
}
424+
425+
#if WASM_ENABLE_GC != 0
426+
if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
427+
set_error_buf(error_buf, error_buf_size, "unknown function type");
428+
return false;
429+
}
430+
#endif
431+
432+
return true;
433+
}
434+
412435
static bool
413436
check_function_index(const WASMModule *module, uint32 function_index,
414437
char *error_buf, uint32 error_buf_size)
@@ -2479,8 +2502,8 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
24792502
read_leb_uint32(p, p_end, declare_type_index);
24802503
*p_buf = p;
24812504

2482-
if (declare_type_index >= parent_module->type_count) {
2483-
set_error_buf(error_buf, error_buf_size, "unknown type");
2505+
if (!check_function_type(parent_module, declare_type_index, error_buf,
2506+
error_buf_size)) {
24842507
return false;
24852508
}
24862509

@@ -2893,8 +2916,8 @@ load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
28932916
/* get type */
28942917
read_leb_uint32(p, p_end, declare_type_index);
28952918
/* compare against module->types */
2896-
if (declare_type_index >= parent_module->type_count) {
2897-
set_error_buf(error_buf, error_buf_size, "unknown tag type");
2919+
if (!check_function_type(parent_module, declare_type_index, error_buf,
2920+
error_buf_size)) {
28982921
goto fail;
28992922
}
29002923

@@ -3563,8 +3586,9 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
35633586
for (i = 0; i < func_count; i++) {
35643587
/* Resolve function type */
35653588
read_leb_uint32(p, p_end, type_index);
3566-
if (type_index >= module->type_count) {
3567-
set_error_buf(error_buf, error_buf_size, "unknown type");
3589+
3590+
if (!check_function_type(module, type_index, error_buf,
3591+
error_buf_size)) {
35683592
return false;
35693593
}
35703594

@@ -4970,8 +4994,8 @@ load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
49704994
/* get type */
49714995
read_leb_uint32(p, p_end, tag_type);
49724996
/* compare against module->types */
4973-
if (tag_type >= module->type_count) {
4974-
set_error_buf(error_buf, error_buf_size, "unknown type");
4997+
if (!check_function_type(module, tag_type, error_buf,
4998+
error_buf_size)) {
49754999
return false;
49765000
}
49775001

@@ -10477,7 +10501,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
1047710501
* match block type. */
1047810502
if (cur_block->is_stack_polymorphic) {
1047910503
#if WASM_ENABLE_GC != 0
10480-
int32 j = reftype_map_count - 1;
10504+
int32 j = (int32)reftype_map_count - 1;
1048110505
#endif
1048210506
for (i = (int32)arity - 1; i >= 0; i--) {
1048310507
#if WASM_ENABLE_GC != 0
@@ -10780,7 +10804,7 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
1078010804
* match block type. */
1078110805
if (block->is_stack_polymorphic) {
1078210806
#if WASM_ENABLE_GC != 0
10783-
int32 j = return_reftype_map_count - 1;
10807+
int32 j = (int32)return_reftype_map_count - 1;
1078410808
#endif
1078510809
for (i = (int32)return_count - 1; i >= 0; i--) {
1078610810
#if WASM_ENABLE_GC != 0
@@ -11549,15 +11573,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1154911573
}
1155011574
else {
1155111575
int32 type_index;
11576+
1155211577
/* Resolve the leb128 encoded type index as block type */
1155311578
p--;
1155411579
p_org = p - 1;
1155511580
pb_read_leb_int32(p, p_end, type_index);
11556-
if ((uint32)type_index >= module->type_count) {
11557-
set_error_buf(error_buf, error_buf_size,
11558-
"unknown type");
11581+
11582+
if (!check_function_type(module, type_index, error_buf,
11583+
error_buf_size)) {
1155911584
goto fail;
1156011585
}
11586+
1156111587
block_type.is_value_type = false;
1156211588
block_type.u.type =
1156311589
(WASMFuncType *)module->types[type_index];
@@ -12607,8 +12633,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1260712633
/* skip elem idx */
1260812634
POP_TBL_ELEM_IDX();
1260912635

12610-
if (type_idx >= module->type_count) {
12611-
set_error_buf(error_buf, error_buf_size, "unknown type");
12636+
if (!check_function_type(module, type_idx, error_buf,
12637+
error_buf_size)) {
1261212638
goto fail;
1261312639
}
1261412640

tests/fuzz/wasm-mutator-fuzz/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ add_link_options(-fsanitize=fuzzer -fno-sanitize=vptr)
181181

182182
# Enable sanitizers if not in oss-fuzz environment
183183
set(CFLAGS_ENV $ENV{CFLAGS})
184-
string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" IN_OSS_FUZZ)
184+
string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" FUZZ_POS)
185+
if (FUZZ_POS GREATER -1)
186+
set(IN_OSS_FUZZ 1)
187+
else()
188+
set(IN_OSS_FUZZ 0)
189+
endif()
185190

186191
add_subdirectory(aot-compiler)
187192
add_subdirectory(wasm-mutator)

tests/fuzz/wasm-mutator-fuzz/aot-compiler/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ target_link_directories(aotclib PUBLIC ${LLVM_LIBRARY_DIR})
6868
target_link_libraries(aotclib PUBLIC ${REQUIRED_LLVM_LIBS})
6969

7070
if(NOT IN_OSS_FUZZ)
71-
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
71+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment for aotclib")
7272
target_compile_options(aotclib PUBLIC
7373
-fprofile-instr-generate -fcoverage-mapping
7474
-fno-sanitize-recover=all

tests/fuzz/wasm-mutator-fuzz/wasm-mutator/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
5858
target_link_libraries(wasm_mutator_fuzz PRIVATE vmlib m)
5959

6060
if(NOT IN_OSS_FUZZ)
61-
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
61+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment for vmlib")
6262
target_compile_options(vmlib PUBLIC
6363
-fprofile-instr-generate -fcoverage-mapping
6464
-fno-sanitize-recover=all

wamr-compiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ if (WAMR_BUILD_LIB_WASI_THREADS EQUAL 1)
315315
include (${IWASM_DIR}/libraries/lib-wasi-threads/lib_wasi_threads.cmake)
316316
endif ()
317317

318+
#TODO: sync up WAMR_BUILD_SANITIZER in config_common.cmake
318319
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
319320
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
320321
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang" OR MSVC))

0 commit comments

Comments
 (0)