Kaido Kert | f309f9a | 2021-04-30 12:09:15 -0700 | [diff] [blame] | 1 | // Copyright 2017 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/api/api.h" |
| 11 | #include "src/execution/isolate-inl.h" |
| 12 | #include "src/heap/factory.h" |
| 13 | #include "src/objects/objects-inl.h" |
| 14 | #include "src/wasm/wasm-engine.h" |
| 15 | #include "src/wasm/wasm-module.h" |
| 16 | #include "test/common/wasm/flag-utils.h" |
| 17 | #include "test/common/wasm/wasm-module-runner.h" |
| 18 | #include "test/fuzzer/fuzzer-support.h" |
| 19 | #include "test/fuzzer/wasm-fuzzer-common.h" |
| 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | class WasmModuleObject; |
| 24 | |
| 25 | namespace wasm { |
| 26 | namespace fuzzer { |
| 27 | |
| 28 | class AsyncFuzzerResolver : public i::wasm::CompilationResultResolver { |
| 29 | public: |
| 30 | AsyncFuzzerResolver(i::Isolate* isolate, bool* done) |
| 31 | : isolate_(isolate), done_(done) {} |
| 32 | |
| 33 | void OnCompilationSucceeded(i::Handle<i::WasmModuleObject> module) override { |
| 34 | *done_ = true; |
| 35 | InterpretAndExecuteModule(isolate_, module); |
| 36 | } |
| 37 | |
| 38 | void OnCompilationFailed(i::Handle<i::Object> error_reason) override { |
| 39 | *done_ = true; |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | i::Isolate* isolate_; |
| 44 | bool* done_; |
| 45 | }; |
| 46 | |
| 47 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 48 | // We explicitly enable staged WebAssembly features here to increase fuzzer |
| 49 | // coverage. For libfuzzer fuzzers it is not possible that the fuzzer enables |
| 50 | // the flag by itself. |
| 51 | OneTimeEnableStagedWasmFeatures(); |
| 52 | |
| 53 | // Set some more flags. |
| 54 | FLAG_wasm_async_compilation = true; |
| 55 | FLAG_wasm_max_mem_pages = 32; |
| 56 | FLAG_wasm_max_table_size = 100; |
| 57 | |
| 58 | v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get(); |
| 59 | v8::Isolate* isolate = support->GetIsolate(); |
| 60 | i::Isolate* i_isolate = reinterpret_cast<v8::internal::Isolate*>(isolate); |
| 61 | |
| 62 | // Clear any pending exceptions from a prior run. |
| 63 | if (i_isolate->has_pending_exception()) { |
| 64 | i_isolate->clear_pending_exception(); |
| 65 | } |
| 66 | |
| 67 | v8::Isolate::Scope isolate_scope(isolate); |
| 68 | v8::HandleScope handle_scope(isolate); |
| 69 | i::HandleScope internal_scope(i_isolate); |
| 70 | v8::Context::Scope context_scope(support->GetContext()); |
| 71 | TryCatch try_catch(isolate); |
| 72 | testing::SetupIsolateForWasmModule(i_isolate); |
| 73 | |
| 74 | bool done = false; |
| 75 | auto enabled_features = i::wasm::WasmFeatures::FromIsolate(i_isolate); |
| 76 | constexpr const char* kAPIMethodName = "WasmAsyncFuzzer.compile"; |
| 77 | i_isolate->wasm_engine()->AsyncCompile( |
| 78 | i_isolate, enabled_features, |
| 79 | std::make_shared<AsyncFuzzerResolver>(i_isolate, &done), |
| 80 | ModuleWireBytes(data, data + size), false, kAPIMethodName); |
| 81 | |
| 82 | // Wait for the promise to resolve. |
| 83 | while (!done) { |
| 84 | support->PumpMessageLoop(platform::MessageLoopBehavior::kWaitForWork); |
| 85 | isolate->PerformMicrotaskCheckpoint(); |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | } // namespace fuzzer |
| 91 | } // namespace wasm |
| 92 | } // namespace internal |
| 93 | } // namespace v8 |