blob: 4035adeaa3414ee020571a76d95368388e7fd6e2 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// 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/objects.h"
11#include "src/parsing/parser.h"
12#include "src/parsing/preparser.h"
13#include "test/fuzzer/fuzzer-support.h"
14
15extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
16 v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get();
17 v8::Isolate* isolate = support->GetIsolate();
18
19 v8::Isolate::Scope isolate_scope(isolate);
20 v8::HandleScope handle_scope(isolate);
21 v8::Context::Scope context_scope(support->GetContext());
22 v8::TryCatch try_catch(isolate);
23
24 v8::internal::Isolate* i_isolate =
25 reinterpret_cast<v8::internal::Isolate*>(isolate);
26 v8::internal::Factory* factory = i_isolate->factory();
27
28 if (size > INT_MAX) return 0;
29 v8::internal::MaybeHandle<v8::internal::String> source =
30 factory->NewStringFromOneByte(
31 v8::internal::Vector<const uint8_t>(data, static_cast<int>(size)));
32 if (source.is_null()) return 0;
33
34 v8::internal::Handle<v8::internal::Script> script =
35 factory->NewScript(source.ToHandleChecked());
Ben Murdochda12d292016-06-02 14:46:10 +010036 v8::internal::Zone zone(i_isolate->allocator());
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 v8::internal::ParseInfo info(&zone, script);
38 info.set_global();
39 v8::internal::Parser parser(&info);
40 parser.Parse(&info);
Ben Murdochc5610432016-08-08 18:44:38 +010041 isolate->RequestGarbageCollectionForTesting(
42 v8::Isolate::kFullGarbageCollection);
Ben Murdoch097c5b22016-05-18 11:27:45 +010043 return 0;
44}