wasm-module-runner.h 4.61 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_WASM_MODULE_RUNNER_H_
#define V8_WASM_MODULE_RUNNER_H_

8
#include "src/execution/isolate.h"
9
#include "src/objects/objects.h"
10
#include "src/wasm/wasm-module.h"
11
#include "src/wasm/wasm-objects.h"
12 13 14 15
#include "src/wasm/wasm-result.h"

namespace v8 {
namespace internal {
marja's avatar
marja committed
16 17 18 19

template <typename T>
class Handle;

Marja Hölttä's avatar
Marja Hölttä committed
20 21 22
template <typename T>
class MaybeHandle;

23 24 25
namespace wasm {
namespace testing {

26 27
// Returns a MaybeHandle to the JsToWasm wrapper of the wasm function exported
// with the given name by the provided instance.
28 29
MaybeHandle<WasmExportedFunction> GetExportedFunction(
    Isolate* isolate, Handle<WasmInstanceObject> instance, const char* name);
30

31 32
// Call an exported wasm function by name. Returns -1 if the export does not
// exist or throws an error. Errors are cleared from the isolate before
33 34
// returning. {exception} is set to to true if an exception happened during
// execution of the wasm function.
35 36
int32_t CallWasmFunctionForTesting(Isolate* isolate,
                                   Handle<WasmInstanceObject> instance,
37
                                   const char* name, int argc,
38 39
                                   Handle<Object> argv[],
                                   bool* exception = nullptr);
40 41 42 43

// Decode, verify, and run the function labeled "main" in the
// given encoded module. The module should have no imports.
int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
44 45
                                const byte* module_end);

46 47 48 49 50
// Decode and compile the given module with no imports.
MaybeHandle<WasmModuleObject> CompileForTesting(Isolate* isolate,
                                                ErrorThrower* thrower,
                                                const ModuleWireBytes& bytes);

51 52 53 54
// Decode, compile, and instantiate the given module with no imports.
MaybeHandle<WasmInstanceObject> CompileAndInstantiateForTesting(
    Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes);

55 56
class WasmInterpretationResult {
 public:
57
  static WasmInterpretationResult Failed() { return {kFailed, 0, false}; }
58 59 60 61 62 63 64 65
  static WasmInterpretationResult Trapped(bool possible_nondeterminism) {
    return {kTrapped, 0, possible_nondeterminism};
  }
  static WasmInterpretationResult Finished(int32_t result,
                                           bool possible_nondeterminism) {
    return {kFinished, result, possible_nondeterminism};
  }

66 67 68 69
  // {failed()} captures different reasons: The module was invalid, no function
  // to call was found in the module, the function did not termine within a
  // limited number of steps, or a stack overflow happened.
  bool failed() const { return status_ == kFailed; }
70 71 72 73 74 75 76 77 78 79 80
  bool trapped() const { return status_ == kTrapped; }
  bool finished() const { return status_ == kFinished; }

  int32_t result() const {
    DCHECK_EQ(status_, kFinished);
    return result_;
  }

  bool possible_nondeterminism() const { return possible_nondeterminism_; }

 private:
81
  enum Status { kFinished, kTrapped, kFailed };
82 83 84 85 86 87 88 89 90 91 92 93

  const Status status_;
  const int32_t result_;
  const bool possible_nondeterminism_;

  WasmInterpretationResult(Status status, int32_t result,
                           bool possible_nondeterminism)
      : status_(status),
        result_(result),
        possible_nondeterminism_(possible_nondeterminism) {}
};

94 95 96
// Interprets the given module, starting at the function specified by
// {function_index}. The return type of the function has to be int32. The module
// should not have any imports or exports
97 98 99
WasmInterpretationResult InterpretWasmModule(
    Isolate* isolate, Handle<WasmInstanceObject> instance,
    int32_t function_index, WasmValue* args);
100

101 102
// Generate an array of default arguments for the given signature, to be used in
// the interpreter.
103 104
base::OwnedVector<WasmValue> MakeDefaultInterpreterArguments(
    Isolate* isolate, const FunctionSig* sig);
105 106 107 108 109

// Generate an array of default arguments for the given signature, to be used
// when calling compiled code. Make sure that the arguments match the ones
// returned by {MakeDefaultInterpreterArguments}, otherwise fuzzers will report
// differences between interpreter and compiled code.
110 111
base::OwnedVector<Handle<Object>> MakeDefaultArguments(Isolate* isolate,
                                                       const FunctionSig* sig);
112

113 114
// Install function map, module symbol for testing
void SetupIsolateForWasmModule(Isolate* isolate);
115

116 117 118 119 120 121
}  // namespace testing
}  // namespace wasm
}  // namespace internal
}  // namespace v8

#endif  // V8_WASM_MODULE_RUNNER_H_