Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_MODULE_RUNNER_H_
6 : #define V8_WASM_MODULE_RUNNER_H_
7 :
8 : #include "src/isolate.h"
9 : #include "src/objects.h"
10 : #include "src/wasm/wasm-interpreter.h"
11 : #include "src/wasm/wasm-module.h"
12 : #include "src/wasm/wasm-objects.h"
13 : #include "src/wasm/wasm-result.h"
14 : #include "src/zone/zone.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : template <typename T>
20 : class Handle;
21 :
22 : template <typename T>
23 : class MaybeHandle;
24 :
25 : namespace wasm {
26 : namespace testing {
27 :
28 : // Decodes the given encoded module.
29 : std::shared_ptr<WasmModule> DecodeWasmModuleForTesting(
30 : Isolate* isolate, ErrorThrower* thrower, const byte* module_start,
31 : const byte* module_end, ModuleOrigin origin, bool verify_functions = false);
32 :
33 : // Returns a MaybeHandle to the JsToWasm wrapper of the wasm function exported
34 : // with the given name by the provided instance.
35 : MaybeHandle<WasmExportedFunction> GetExportedFunction(
36 : Isolate* isolate, Handle<WasmInstanceObject> instance, const char* name);
37 :
38 : // Call an exported wasm function by name. Returns -1 if the export does not
39 : // exist or throws an error. Errors are cleared from the isolate before
40 : // returning.
41 : int32_t CallWasmFunctionForTesting(Isolate* isolate,
42 : Handle<WasmInstanceObject> instance,
43 : ErrorThrower* thrower, const char* name,
44 : int argc, Handle<Object> argv[]);
45 :
46 : // Interprets an exported wasm function by name. Returns false if it was not
47 : // possible to execute the function (e.g. because it does not exist), or if the
48 : // interpretation does not finish after kMaxNumSteps. Otherwise returns true.
49 : // The arguments array is extended with default values if necessary.
50 : bool InterpretWasmModuleForTesting(Isolate* isolate,
51 : Handle<WasmInstanceObject> instance,
52 : const char* name, size_t argc,
53 : WasmValue* args);
54 :
55 : // Decode, verify, and run the function labeled "main" in the
56 : // given encoded module. The module should have no imports.
57 : int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
58 : const byte* module_end);
59 :
60 : // Decode and compile the given module with no imports.
61 : MaybeHandle<WasmModuleObject> CompileForTesting(Isolate* isolate,
62 : ErrorThrower* thrower,
63 : const ModuleWireBytes& bytes);
64 :
65 : // Decode, compile, and instantiate the given module with no imports.
66 : MaybeHandle<WasmInstanceObject> CompileAndInstantiateForTesting(
67 : Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes);
68 :
69 : class WasmInterpretationResult {
70 : public:
71 : static WasmInterpretationResult Stopped() { return {kStopped, 0, false}; }
72 : static WasmInterpretationResult Trapped(bool possible_nondeterminism) {
73 : return {kTrapped, 0, possible_nondeterminism};
74 : }
75 : static WasmInterpretationResult Finished(int32_t result,
76 : bool possible_nondeterminism) {
77 : return {kFinished, result, possible_nondeterminism};
78 : }
79 :
80 : bool stopped() const { return status_ == kStopped; }
81 1 : bool trapped() const { return status_ == kTrapped; }
82 : bool finished() const { return status_ == kFinished; }
83 :
84 : int32_t result() const {
85 : DCHECK_EQ(status_, kFinished);
86 : return result_;
87 : }
88 :
89 : bool possible_nondeterminism() const { return possible_nondeterminism_; }
90 :
91 : private:
92 : enum Status { kFinished, kTrapped, kStopped };
93 :
94 : const Status status_;
95 : const int32_t result_;
96 : const bool possible_nondeterminism_;
97 :
98 : WasmInterpretationResult(Status status, int32_t result,
99 : bool possible_nondeterminism)
100 : : status_(status),
101 : result_(result),
102 1 : possible_nondeterminism_(possible_nondeterminism) {}
103 : };
104 :
105 : // Interprets the given module, starting at the function specified by
106 : // {function_index}. The return type of the function has to be int32. The module
107 : // should not have any imports or exports
108 : WasmInterpretationResult InterpretWasmModule(
109 : Isolate* isolate, Handle<WasmInstanceObject> instance,
110 : int32_t function_index, WasmValue* args);
111 :
112 : // Runs the module instance with arguments.
113 : int32_t RunWasmModuleForTesting(Isolate* isolate,
114 : Handle<WasmInstanceObject> instance, int argc,
115 : Handle<Object> argv[]);
116 :
117 : // Install function map, module symbol for testing
118 : void SetupIsolateForWasmModule(Isolate* isolate);
119 :
120 : } // namespace testing
121 : } // namespace wasm
122 : } // namespace internal
123 : } // namespace v8
124 :
125 : #endif // V8_WASM_MODULE_RUNNER_H_
|