wasm-micro-runtime/tests/fuzz/wasm-mutator-fuzz/common/fuzzer_common.h
liang.he 9e4aa9c850
fuzzing: reject non-wasm files quickly and execute aot after compilation (#4780)
* fix: disable unsigned integer overflow sanitization in build configurations

FYI: from https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

`-fsanitize=unsigned-integer-overflow`: Unsigned integer overflow, where the result of an unsigned integer computation cannot be represented in its type. Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional. This sanitizer does not check for lossy implicit conversions performed before such a computation.

It brings a more common question: which is better, pre-additional-check or post-additional-check to fix a potential unsigned integer overflow? A pre-additional-check involves using a check to prevent integer overflow from the very beginning. A post-additional-check involves using a check after addition to see if there is an overflow.

In this project, post-additional-checking is widely used. let's follow the routine.

for performance sensitive logic, use __builtin_add_overflow etc. provide something like 9a5622791e/lib/platform.h (L176-L191) and encourage the use of them.

ref. https://github.com/bytecodealliance/wasm-micro-runtime/pull/4549#issuecomment-3218687294

* fix: update AOT compiler configuration and enhance error handling in fuzz tests
2026-03-09 18:24:43 -07:00

77 lines
2.2 KiB
C++

// Copyright (C) 2025 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef FUZZER_COMMON_H
#define FUZZER_COMMON_H
#include "wasm_export.h"
#include <iostream>
#include <vector>
// Constants for consistent buffer sizes
constexpr size_t ERROR_BUF_SIZE = 128;
constexpr size_t MAX_ERROR_BUF_SIZE = 120; // Used in wasm_runtime_load
// Error phases for consistent reporting
enum class FuzzerErrorPhase {
LOADING,
INSTANTIATING,
COMPILING,
EXECUTION,
CLEANUP
};
// Small inline helper functions
// Check if a value kind is supported by the fuzzer
static inline bool
is_supported_val_kind(wasm_valkind_t kind)
{
return kind == WASM_I32 || kind == WASM_I64 || kind == WASM_F32
|| kind == WASM_F64 || kind == WASM_EXTERNREF
|| kind == WASM_FUNCREF;
}
// Generate a predefined value for a given value kind
static inline wasm_val_t
pre_defined_val(wasm_valkind_t kind)
{
if (kind == WASM_I32) {
return wasm_val_t{ .kind = WASM_I32, .of = { .i32 = 2025 } };
}
else if (kind == WASM_I64) {
return wasm_val_t{ .kind = WASM_I64, .of = { .i64 = 168 } };
}
else if (kind == WASM_F32) {
return wasm_val_t{ .kind = WASM_F32, .of = { .f32 = 3.14159f } };
}
else if (kind == WASM_F64) {
return wasm_val_t{ .kind = WASM_F64, .of = { .f64 = 2.71828 } };
}
else if (kind == WASM_EXTERNREF) {
return wasm_val_t{ .kind = WASM_EXTERNREF,
.of = { .foreign = 0xabcddead } };
}
// because aft is_supported_val_kind() check, so we can safely return as
// WASM_FUNCREF
else {
return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
}
}
// Function declarations (implemented in fuzzer_common.cc)
// Print execution arguments for debugging
void
print_execution_args(const wasm_export_t &export_type,
const std::vector<wasm_val_t> &args, unsigned param_count);
// Execute all export functions in a module
bool
execute_export_functions(wasm_module_t module, wasm_module_inst_t inst);
// Helper for consistent error reporting
void
report_fuzzer_error(FuzzerErrorPhase phase, const char *message);
#endif // FUZZER_COMMON_H