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 : #include <limits.h>
6 : #include <stddef.h>
7 : #include <stdint.h>
8 :
9 : #include "include/v8.h"
10 : #include "src/heap/factory.h"
11 : #include "src/isolate-inl.h"
12 : #include "src/objects-inl.h"
13 : #include "src/wasm/wasm-engine.h"
14 : #include "src/wasm/wasm-module.h"
15 : #include "test/common/wasm/flag-utils.h"
16 : #include "test/common/wasm/wasm-module-runner.h"
17 : #include "test/fuzzer/fuzzer-support.h"
18 : #include "test/fuzzer/wasm-fuzzer-common.h"
19 :
20 : namespace i = v8::internal;
21 :
22 1 : extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
23 : i::FlagScope<uint32_t> max_mem_flag_scope(&i::FLAG_wasm_max_mem_pages, 32);
24 : i::FlagScope<uint32_t> max_table_size_scope(&i::FLAG_wasm_max_table_size,
25 : 100);
26 1 : v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get();
27 : v8::Isolate* isolate = support->GetIsolate();
28 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
29 :
30 : // Clear any pending exceptions from a prior run.
31 1 : if (i_isolate->has_pending_exception()) {
32 0 : i_isolate->clear_pending_exception();
33 : }
34 :
35 : v8::Isolate::Scope isolate_scope(isolate);
36 2 : v8::HandleScope handle_scope(isolate);
37 1 : v8::Context::Scope context_scope(support->GetContext());
38 2 : v8::TryCatch try_catch(isolate);
39 1 : i::wasm::testing::SetupIsolateForWasmModule(i_isolate);
40 1 : i::wasm::ModuleWireBytes wire_bytes(data, data + size);
41 :
42 : i::HandleScope scope(i_isolate);
43 1 : i::wasm::ErrorThrower thrower(i_isolate, "wasm fuzzer");
44 : i::Handle<i::WasmModuleObject> module_object;
45 1 : auto enabled_features = i::wasm::WasmFeaturesFromIsolate(i_isolate);
46 : bool compiles =
47 : i_isolate->wasm_engine()
48 : ->SyncCompile(i_isolate, enabled_features, &thrower, wire_bytes)
49 2 : .ToHandle(&module_object);
50 :
51 1 : if (i::FLAG_wasm_fuzzer_gen_test) {
52 0 : i::wasm::fuzzer::GenerateTestCase(i_isolate, wire_bytes, compiles);
53 : }
54 :
55 1 : if (compiles) {
56 0 : i::wasm::fuzzer::InterpretAndExecuteModule(i_isolate, module_object);
57 : }
58 1 : return 0;
59 3 : }
|