Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: test/fuzzer/wasm-call.cc

Issue 2447643002: [wasm] Add a new fuzzer which can also test wasm function calls. (Closed)
Patch Set: sign unsigned mismatch Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/fuzzer/testcfg.py ('k') | test/fuzzer/wasm_call/foo » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #include <stddef.h>
6 #include <stdint.h>
7
8 #include "include/v8.h"
9 #include "src/isolate.h"
10 #include "src/objects.h"
11 #include "src/wasm/wasm-interpreter.h"
12 #include "src/wasm/wasm-module-builder.h"
13 #include "src/wasm/wasm-module.h"
14 #include "test/common/wasm/test-signatures.h"
15 #include "test/common/wasm/wasm-module-runner.h"
16 #include "test/fuzzer/fuzzer-support.h"
17
18 #define WASM_CODE_FUZZER_HASH_SEED 83
19 #define MAX_NUM_FUNCTIONS 3
20 #define MAX_NUM_PARAMS 3
21
22 #define FUZZER_TYPE_FLOAT32 0
23 #define FUZZER_TYPE_FLOAT64 1
24 #define FUZZER_TYPE_INT32 2
25 #define FUZZER_TYPE_INT64 3
26
27 using namespace v8::internal::wasm;
28
29 template <typename V>
30 static inline V read_value(const uint8_t** data, size_t* size, bool* ok) {
31 // The status flag {ok} checks that the decoding up until now was okay, and
32 // that a value of type V can be read without problems.
33 *ok &= (*size > sizeof(V));
34 if (!(*ok)) return 0;
35 V result = *reinterpret_cast<const V*>(*data);
titzer 2016/10/24 11:17:21 This is not going to work on platforms that don't
ahaas 2016/10/24 12:10:33 Done.
36 *data += sizeof(V);
37 *size -= sizeof(V);
38 return result;
39 }
40
41 static void add_argument(
42 v8::internal::Isolate* isolate, uint8_t type, WasmVal* interpreter_args,
43 v8::internal::Handle<v8::internal::Object>* compiled_args, int* argc,
44 const uint8_t** data, size_t* size, bool* ok) {
45 if (!(*ok)) return;
46 switch (type) {
47 case FUZZER_TYPE_FLOAT32: {
48 float value = read_value<float>(data, size, ok);
49 interpreter_args[*argc] = WasmVal(value);
50 compiled_args[*argc] =
51 isolate->factory()->NewNumber(static_cast<double>(value));
52 break;
53 }
54 case FUZZER_TYPE_FLOAT64: {
55 double value = read_value<double>(data, size, ok);
56 interpreter_args[*argc] = WasmVal(value);
57 compiled_args[*argc] = isolate->factory()->NewNumber(value);
58 break;
59 }
60 case FUZZER_TYPE_INT32: {
61 int32_t value = read_value<int32_t>(data, size, ok);
62 interpreter_args[*argc] = WasmVal(value);
63 compiled_args[*argc] =
64 isolate->factory()->NewNumber(static_cast<double>(value));
65 break;
66 }
67 default:
68 UNREACHABLE();
69 }
70 (*argc)++;
71 }
72
73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
74 v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get();
75 v8::Isolate* isolate = support->GetIsolate();
76 v8::internal::Isolate* i_isolate =
77 reinterpret_cast<v8::internal::Isolate*>(isolate);
78
79 // Clear any pending exceptions from a prior run.
80 if (i_isolate->has_pending_exception()) {
81 i_isolate->clear_pending_exception();
82 }
83
84 v8::Isolate::Scope isolate_scope(isolate);
85 v8::HandleScope handle_scope(isolate);
86 v8::Context::Scope context_scope(support->GetContext());
87 v8::TryCatch try_catch(isolate);
88
89 v8::internal::AccountingAllocator allocator;
90 v8::internal::Zone zone(&allocator, ZONE_NAME);
91
92 bool ok = true;
93 uint8_t num_functions =
94 (read_value<uint8_t>(&data, &size, &ok) % MAX_NUM_FUNCTIONS) + 1;
95
96 LocalType types[] = {kAstF32, kAstF64, kAstI32, kAstI64};
titzer 2016/10/24 11:17:21 Why not just make this global and remove WASM_FUZZ
ahaas 2016/10/24 12:10:33 I used the WASM_FUZZER_TYPE* definitions to give n
97 WasmVal interpreter_args[3];
98 v8::internal::Handle<v8::internal::Object> compiled_args[3];
99 int argc = 0;
100
101 WasmModuleBuilder builder(&zone);
102 for (int fun = 0; fun < num_functions; fun++) {
103 size_t num_params = static_cast<size_t>(
104 (read_value<uint8_t>(&data, &size, &ok) % MAX_NUM_PARAMS) + 1);
105 FunctionSig::Builder sig_builder(&zone, 1, num_params);
106 sig_builder.AddReturn(kAstI32);
107 for (size_t param = 0; param < num_params; param++) {
108 // The main function cannot handle int64 parameters.
109 uint8_t param_type = (read_value<uint8_t>(&data, &size, &ok) %
110 (arraysize(types) - (fun == 0 ? 1 : 0)));
111 sig_builder.AddParam(types[param_type]);
112 if (fun == 0) {
113 add_argument(i_isolate, param_type, interpreter_args, compiled_args,
114 &argc, &data, &size, &ok);
115 }
116 }
117 v8::internal::wasm::WasmFunctionBuilder* f =
118 builder.AddFunction(sig_builder.Build());
119 uint32_t code_size = static_cast<uint32_t>(size / num_functions);
120 f->EmitCode(data, code_size);
121 data += code_size;
122 size -= code_size;
123 if (fun == 0) {
124 f->ExportAs(v8::internal::CStrVector("main"));
125 }
126 }
127
128 ZoneBuffer buffer(&zone);
129 builder.WriteTo(buffer);
130
131 if (!ok) {
132 // The input data was too short.
133 return 0;
134 }
135
136 v8::internal::wasm::testing::SetupIsolateForWasmModule(i_isolate);
137
138 v8::internal::HandleScope scope(i_isolate);
139
140 ErrorThrower interpreter_thrower(i_isolate, "Interpreter");
141 std::unique_ptr<const WasmModule> module(testing::DecodeWasmModuleForTesting(
142 i_isolate, &interpreter_thrower, buffer.begin(), buffer.end(),
143 v8::internal::wasm::ModuleOrigin::kWasmOrigin));
144
145 if (module == nullptr) {
146 return 0;
147 }
148 int32_t result_interpreted;
149 bool possible_nondeterminism = false;
150 {
151 result_interpreted = testing::InterpretWasmModule(
152 i_isolate, &interpreter_thrower, module.get(), 0, interpreter_args,
153 &possible_nondeterminism);
154 }
155
156 ErrorThrower compiler_thrower(i_isolate, "Compiler");
157 v8::internal::Handle<v8::internal::JSObject> instance =
158 testing::InstantiateModuleForTesting(i_isolate, &compiler_thrower,
159 module.get());
160
161 if (!interpreter_thrower.error()) {
162 CHECK(!instance.is_null());
163 } else {
164 return 0;
165 }
166 int32_t result_compiled;
167 {
168 result_compiled = testing::CallWasmFunctionForTesting(
169 i_isolate, instance, &compiler_thrower, "main", argc, compiled_args,
170 v8::internal::wasm::ModuleOrigin::kWasmOrigin);
171 }
172 if (result_interpreted == 0xdeadbeef) {
173 CHECK(i_isolate->has_pending_exception());
174 i_isolate->clear_pending_exception();
175 } else {
176 // The WebAssembly spec allows the sign bit of NaN to be non-deterministic.
177 // This sign bit may cause result_interpreted to be different than
178 // result_compiled. Therefore we do not check the equality of the results
179 // if the execution may have produced a NaN at some point.
180 if (!possible_nondeterminism && (result_interpreted != result_compiled)) {
181 V8_Fatal(__FILE__, __LINE__, "WasmCodeFuzzerHash=%x",
182 v8::internal::StringHasher::HashSequentialString(
183 data, static_cast<int>(size), WASM_CODE_FUZZER_HASH_SEED));
184 }
185 }
186 return 0;
187 }
OLDNEW
« no previous file with comments | « test/fuzzer/testcfg.py ('k') | test/fuzzer/wasm_call/foo » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698