LCOV - code coverage report
Current view: top level - test/fuzzer - fuzzer-support.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 47 47 100.0 %
Date: 2017-10-20 Functions: 8 8 100.0 %

          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 "test/fuzzer/fuzzer-support.h"
       6             : 
       7             : #include <stdio.h>
       8             : #include <stdlib.h>
       9             : #include <string.h>
      10             : 
      11             : #include "include/libplatform/libplatform.h"
      12             : 
      13             : #include "src/flags.h"
      14             : 
      15             : namespace v8_fuzzer {
      16             : 
      17             : namespace {
      18             : 
      19             : FuzzerSupport* g_fuzzer_support = nullptr;
      20             : 
      21          42 : void DeleteFuzzerSupport() {
      22          42 :   if (g_fuzzer_support) {
      23          42 :     delete g_fuzzer_support;
      24          42 :     g_fuzzer_support = nullptr;
      25             :   }
      26          42 : }
      27             : 
      28             : }  // namespace
      29             : 
      30          42 : FuzzerSupport::FuzzerSupport(int* argc, char*** argv) {
      31          42 :   v8::internal::FLAG_expose_gc = true;
      32          42 :   v8::V8::SetFlagsFromCommandLine(argc, *argv, true);
      33          42 :   v8::V8::InitializeICUDefaultLocation((*argv)[0]);
      34          42 :   v8::V8::InitializeExternalStartupData((*argv)[0]);
      35          42 :   platform_ = v8::platform::CreateDefaultPlatform();
      36          42 :   v8::V8::InitializePlatform(platform_);
      37          42 :   v8::V8::Initialize();
      38             : 
      39          42 :   allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
      40             :   v8::Isolate::CreateParams create_params;
      41          42 :   create_params.array_buffer_allocator = allocator_;
      42          42 :   isolate_ = v8::Isolate::New(create_params);
      43             : 
      44             :   {
      45             :     v8::Isolate::Scope isolate_scope(isolate_);
      46          84 :     v8::HandleScope handle_scope(isolate_);
      47          84 :     context_.Reset(isolate_, v8::Context::New(isolate_));
      48             :   }
      49             : 
      50          42 :   v8::platform::EnsureEventLoopInitialized(platform_, isolate_);
      51          42 : }
      52             : 
      53          42 : FuzzerSupport::~FuzzerSupport() {
      54             :   {
      55          42 :     v8::Isolate::Scope isolate_scope(isolate_);
      56          42 :     while (PumpMessageLoop()) {
      57             :       // empty
      58             :     }
      59             : 
      60          84 :     v8::HandleScope handle_scope(isolate_);
      61             :     context_.Reset();
      62             :   }
      63             : 
      64          42 :   isolate_->LowMemoryNotification();
      65          42 :   isolate_->Dispose();
      66          42 :   isolate_ = nullptr;
      67             : 
      68          42 :   delete allocator_;
      69          42 :   allocator_ = nullptr;
      70             : 
      71          42 :   v8::V8::Dispose();
      72          42 :   v8::V8::ShutdownPlatform();
      73             : 
      74          42 :   delete platform_;
      75          42 :   platform_ = nullptr;
      76          42 : }
      77             : 
      78             : // static
      79          42 : FuzzerSupport* FuzzerSupport::Get() { return g_fuzzer_support; }
      80             : 
      81          42 : v8::Isolate* FuzzerSupport::GetIsolate() const { return isolate_; }
      82             : 
      83          53 : v8::Local<v8::Context> FuzzerSupport::GetContext() {
      84          53 :   v8::Isolate::Scope isolate_scope(isolate_);
      85          53 :   v8::EscapableHandleScope handle_scope(isolate_);
      86             :   v8::Local<v8::Context> context =
      87          53 :       v8::Local<v8::Context>::New(isolate_, context_);
      88          53 :   return handle_scope.Escape(context);
      89             : }
      90             : 
      91          11 : bool FuzzerSupport::PumpMessageLoop(
      92             :     v8::platform::MessageLoopBehavior behavior) {
      93          53 :   return v8::platform::PumpMessageLoop(platform_, isolate_, behavior);
      94             : }
      95             : 
      96             : }  // namespace v8_fuzzer
      97             : 
      98          42 : extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
      99          42 :   v8_fuzzer::g_fuzzer_support = new v8_fuzzer::FuzzerSupport(argc, argv);
     100          42 :   atexit(&v8_fuzzer::DeleteFuzzerSupport);
     101          42 :   return 0;
     102             : }

Generated by: LCOV version 1.10