wasm-code.cc 1.7 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

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

#include "src/isolate.h"
9
#include "src/objects-inl.h"
10
#include "src/objects.h"
11
#include "src/wasm/wasm-interpreter.h"
12
#include "src/wasm/wasm-module-builder.h"
13
#include "test/common/wasm/test-signatures.h"
14
#include "test/fuzzer/wasm-fuzzer-common.h"
15

16 17 18 19
namespace v8 {
namespace internal {
namespace wasm {
namespace fuzzer {
20

21
class WasmCodeFuzzer : public WasmExecutionFuzzer {
22
  bool GenerateModule(
23
      Isolate* isolate, Zone* zone, Vector<const uint8_t> data,
24
      ZoneBuffer& buffer, int32_t& num_args,
25
      std::unique_ptr<WasmValue[]>& interpreter_args,
26 27 28 29
      std::unique_ptr<Handle<Object>[]>& compiler_args) override {
    TestSignatures sigs;
    WasmModuleBuilder builder(zone);
    WasmFunctionBuilder* f = builder.AddFunction(sigs.i_iii());
30
    f->EmitCode(data.start(), static_cast<uint32_t>(data.size()));
31 32
    uint8_t end_opcode = kExprEnd;
    f->EmitCode(&end_opcode, 1);
33
    builder.AddExport(CStrVector("main"), f);
34

35
    builder.SetMaxMemorySize(32);
36 37
    builder.WriteTo(buffer);
    num_args = 3;
38 39
    interpreter_args.reset(
        new WasmValue[3]{WasmValue(1), WasmValue(2), WasmValue(3)});
40 41

    compiler_args.reset(new Handle<Object>[3]{
42 43
        handle(Smi::FromInt(1), isolate), handle(Smi::FromInt(2), isolate),
        handle(Smi::FromInt(3), isolate)});
44 45 46
    return true;
  }
};
47

48
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
49 50
  WasmCodeFuzzer().FuzzWasmModule({data, size});
  return 0;
51
}
52 53 54 55 56

}  // namespace fuzzer
}  // namespace wasm
}  // namespace internal
}  // namespace v8