fuzzer_common.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2025 Intel Corporation. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. #ifndef FUZZER_COMMON_H
  4. #define FUZZER_COMMON_H
  5. #include "wasm_export.h"
  6. #include <iostream>
  7. #include <vector>
  8. // Constants for consistent buffer sizes
  9. constexpr size_t ERROR_BUF_SIZE = 128;
  10. constexpr size_t MAX_ERROR_BUF_SIZE = 120; // Used in wasm_runtime_load
  11. // Error phases for consistent reporting
  12. enum class FuzzerErrorPhase {
  13. LOADING,
  14. INSTANTIATING,
  15. COMPILING,
  16. EXECUTION,
  17. CLEANUP
  18. };
  19. // Small inline helper functions
  20. // Check if a value kind is supported by the fuzzer
  21. static inline bool
  22. is_supported_val_kind(wasm_valkind_t kind)
  23. {
  24. return kind == WASM_I32 || kind == WASM_I64 || kind == WASM_F32
  25. || kind == WASM_F64 || kind == WASM_EXTERNREF
  26. || kind == WASM_FUNCREF;
  27. }
  28. // Generate a predefined value for a given value kind
  29. static inline wasm_val_t
  30. pre_defined_val(wasm_valkind_t kind)
  31. {
  32. if (kind == WASM_I32) {
  33. return wasm_val_t{ .kind = WASM_I32, .of = { .i32 = 2025 } };
  34. }
  35. else if (kind == WASM_I64) {
  36. return wasm_val_t{ .kind = WASM_I64, .of = { .i64 = 168 } };
  37. }
  38. else if (kind == WASM_F32) {
  39. return wasm_val_t{ .kind = WASM_F32, .of = { .f32 = 3.14159f } };
  40. }
  41. else if (kind == WASM_F64) {
  42. return wasm_val_t{ .kind = WASM_F64, .of = { .f64 = 2.71828 } };
  43. }
  44. else if (kind == WASM_EXTERNREF) {
  45. return wasm_val_t{ .kind = WASM_EXTERNREF,
  46. .of = { .foreign = 0xabcddead } };
  47. }
  48. // because aft is_supported_val_kind() check, so we can safely return as
  49. // WASM_FUNCREF
  50. else {
  51. return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
  52. }
  53. }
  54. // Function declarations (implemented in fuzzer_common.cc)
  55. // Print execution arguments for debugging
  56. void
  57. print_execution_args(const wasm_export_t &export_type,
  58. const std::vector<wasm_val_t> &args, unsigned param_count);
  59. // Execute all export functions in a module
  60. bool
  61. execute_export_functions(wasm_module_t module, wasm_module_inst_t inst);
  62. // Helper for consistent error reporting
  63. void
  64. report_fuzzer_error(FuzzerErrorPhase phase, const char *message);
  65. #endif // FUZZER_COMMON_H