Skip to content

Commit db24677

Browse files
authored
fix: use copy of input for wasm_runtime_load to fix overwrites-const-input in fuzz (#4869)
Signed-off-by: zhenweijin <zhenwei.jin@intel.com>
1 parent 26aa924 commit db24677

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

tests/fuzz/wasm-mutator-fuzz/aot-compiler/aot_compiler_fuzz.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
3939
wasm_module_t aot_module = NULL;
4040
wasm_module_inst_t inst = NULL;
4141

42-
/* libfuzzer don't allow to modify the given Data, but get_package_type and
43-
* wasm_runtime_load only read the data, so we can safely use const_cast */
42+
/* wasm_runtime_load may modify the input buffer in-place,
43+
* so we must work on a copy to avoid overwriting libFuzzer's const input */
44+
std::vector<uint8_t> data_copy(Data, Data + Size);
45+
4446
if (Size >= 4
45-
&& get_package_type(const_cast<uint8_t *>(Data), Size)
47+
&& get_package_type(data_copy.data(), Size)
4648
!= Wasm_Module_Bytecode) {
4749
printf("Invalid wasm file: magic header not detected\n");
4850
return 0;
4951
}
5052

5153
wasm_runtime_init();
5254

53-
module = wasm_runtime_load(const_cast<uint8_t *>(Data), Size, error_buf,
55+
module = wasm_runtime_load(data_copy.data(), Size, error_buf,
5456
MAX_ERROR_BUF_SIZE);
5557
if (!module) {
5658
std::cout << "[LOADING] " << error_buf << std::endl;

tests/fuzz/wasm-mutator-fuzz/wasm-mutator/wasm_mutator_fuzz.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ using namespace std;
1515
extern "C" int
1616
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
1717
{
18-
/* libfuzzer don't allow us to modify the given Data, but wasm_runtime_load
19-
* only reads the data, so we can safely use const_cast */
18+
/* wasm_runtime_load may modify the input buffer in-place,
19+
* so we must work on a copy to avoid overwriting libFuzzer's const input */
20+
std::vector<uint8_t> data_copy(Data, Data + Size);
21+
2022
/* init runtime environment */
2123
wasm_runtime_init();
2224

2325
char error_buf[ERROR_BUF_SIZE] = { 0 };
24-
wasm_module_t module = wasm_runtime_load(const_cast<uint8_t *>(Data), Size,
26+
wasm_module_t module = wasm_runtime_load(data_copy.data(), Size,
2527
error_buf, MAX_ERROR_BUF_SIZE);
2628
if (!module) {
2729
std::cout << "[LOADING] " << error_buf << std::endl;

0 commit comments

Comments
 (0)