wasm-fuzzer-common.h 1.96 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef WASM_FUZZER_COMMON_H_
#define WASM_FUZZER_COMMON_H_
7 8 9

#include <stddef.h>
#include <stdint.h>
10

11
#include <memory>
12 13 14

#include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-module-builder.h"
15
#include "test/common/wasm/wasm-interpreter.h"
16 17 18 19 20 21

namespace v8 {
namespace internal {
namespace wasm {
namespace fuzzer {

22 23 24 25
// First instantiates and interprets the "main" function within module_object if
// possible. If the interpretation finishes within kMaxSteps steps,
// module_object is instantiated again and the compiled "main" function is
// executed.
26 27 28 29
void InterpretAndExecuteModule(
    Isolate* isolate, Handle<WasmModuleObject> module_object,
    Handle<WasmModuleObject> module_ref = Handle<WasmModuleObject>::null(),
    int32_t* max_steps = nullptr, int32_t* nondeterminism = nullptr);
30

31 32 33
void GenerateTestCase(Isolate* isolate, ModuleWireBytes wire_bytes,
                      bool compiles);

34 35 36 37
// On the first call, enables all staged wasm features. All subsequent calls are
// no-ops. This avoids race conditions with threads reading the flags. Fuzzers
// are executed in their own process anyway, so this should not interfere with
// anything.
38
void OneTimeEnableStagedWasmFeatures(v8::Isolate* isolate);
39

40 41
class WasmExecutionFuzzer {
 public:
42
  virtual ~WasmExecutionFuzzer() = default;
43 44
  void FuzzWasmModule(base::Vector<const uint8_t> data,
                      bool require_valid = false);
45

46 47
  virtual size_t max_input_size() const { return 512; }

48
 protected:
49 50
  virtual bool GenerateModule(Isolate* isolate, Zone* zone,
                              base::Vector<const uint8_t> data,
51 52
                              ZoneBuffer* buffer,
                              bool liftoff_as_reference) = 0;
53 54 55 56 57 58
};

}  // namespace fuzzer
}  // namespace wasm
}  // namespace internal
}  // namespace v8
59
#endif  // WASM_FUZZER_COMMON_H_