Skip to content

Commit d29802c

Browse files
authored
Fix two issues to make fuzzing test quit earlier (#3471)
- Add a marco to limit the maxi allocable memory size of fuzz test to 2GB to avoid libFuzzer out-of-memory - Check global type in load_global_import and load_global_section
1 parent a2a8b32 commit d29802c

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

core/config.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,4 +663,17 @@
663663
#define WASM_MEM_ALLOC_WITH_USAGE 0
664664
#endif
665665

666+
#ifndef WASM_ENABLE_FUZZ_TEST
667+
#define WASM_ENABLE_FUZZ_TEST 0
668+
#endif
669+
670+
#ifndef WASM_MEM_ALLOC_MAX_SIZE
671+
#if WASM_ENABLE_FUZZ_TEST != 0
672+
/* In oss-fuzz, the maximum RAM is ~2.5G */
673+
#define WASM_MEM_ALLOC_MAX_SIZE (2U * 1024 * 1024 * 1024)
674+
#else
675+
#define WASM_MEM_ALLOC_MAX_SIZE UINT32_MAX
676+
#endif
677+
#endif
678+
666679
#endif /* end of _CONFIG_H_ */

core/iwasm/interpreter/wasm_loader.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
379379
{
380380
void *mem;
381381

382-
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
382+
if (size >= WASM_MEM_ALLOC_MAX_SIZE
383+
|| !(mem = wasm_runtime_malloc((uint32)size))) {
383384
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
384385
return NULL;
385386
}
@@ -3052,7 +3053,12 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
30523053

30533054
#if WASM_ENABLE_GC == 0
30543055
CHECK_BUF(p, p_end, 2);
3056+
/* global type */
30553057
declare_type = read_uint8(p);
3058+
if (!is_value_type(declare_type)) {
3059+
set_error_buf(error_buf, error_buf_size, "type mismatch");
3060+
return false;
3061+
}
30563062
declare_mutable = read_uint8(p);
30573063
#else
30583064
if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
@@ -4034,7 +4040,12 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
40344040
for (i = 0; i < global_count; i++, global++) {
40354041
#if WASM_ENABLE_GC == 0
40364042
CHECK_BUF(p, p_end, 2);
4043+
/* global type */
40374044
global->type.val_type = read_uint8(p);
4045+
if (!is_value_type(global->type.val_type)) {
4046+
set_error_buf(error_buf, error_buf_size, "type mismatch");
4047+
return false;
4048+
}
40384049
mutable = read_uint8(p);
40394050
#else
40404051
if (!resolve_value_type(&p, p_end, module, module->type_count,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ message([ceith]:REPO_ROOT_DIR, ${REPO_ROOT_DIR})
113113
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
114114
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
115115

116-
add_definitions(-DWAMR_USE_MEM_POOL=0)
116+
add_definitions(-DWAMR_USE_MEM_POOL=0 -DWASM_ENABLE_FUZZ_TEST=1)
117117

118118
# Enable fuzzer
119119
add_compile_options(-fsanitize=fuzzer)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ message([ceith]:REPO_ROOT_DIR, ${REPO_ROOT_DIR})
113113
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
114114
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
115115

116-
add_definitions(-DWAMR_USE_MEM_POOL=0)
116+
add_definitions(-DWAMR_USE_MEM_POOL=0 -DWASM_ENABLE_FUZZ_TEST=1)
117117

118118
# Enable fuzzer
119119
add_compile_options(-fsanitize=fuzzer)

0 commit comments

Comments
 (0)