blob: 40e34707acd4e39378264baad312fb5a21ac05c9 [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/factory.h"
11#include "src/objects-inl.h"
12#include "src/objects.h"
13#include "src/regexp/jsregexp.h"
14#include "test/fuzzer/fuzzer-support.h"
15
16namespace i = v8::internal;
17
18void Test(v8::Isolate* isolate, i::Handle<i::JSRegExp> regexp,
19 i::Handle<i::String> subject, i::Handle<i::JSArray> results_array) {
20 v8::TryCatch try_catch(isolate);
21 USE(i::RegExpImpl::Exec(regexp, subject, 0, results_array));
22}
23
24extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
25 v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get();
26 v8::Isolate* isolate = support->GetIsolate();
27
28 v8::Isolate::Scope isolate_scope(isolate);
29 v8::HandleScope handle_scope(isolate);
30 v8::Context::Scope context_scope(support->GetContext());
31 v8::TryCatch try_catch(isolate);
32
Ben Murdoch097c5b22016-05-18 11:27:45 +010033 i::FLAG_harmony_regexp_lookbehind = true;
34
35 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
36 i::Factory* factory = i_isolate->factory();
37
38 if (size > INT_MAX) return 0;
39 i::MaybeHandle<i::String> maybe_source = factory->NewStringFromOneByte(
40 i::Vector<const uint8_t>(data, static_cast<int>(size)));
41 i::Handle<i::String> source;
42 if (!maybe_source.ToHandle(&source)) return 0;
43
44 static const int kAllFlags = i::JSRegExp::kGlobal | i::JSRegExp::kIgnoreCase |
45 i::JSRegExp::kMultiline | i::JSRegExp::kSticky |
46 i::JSRegExp::kUnicode;
47
48 const uint8_t one_byte_array[6] = {'f', 'o', 'o', 'b', 'a', 'r'};
49 const i::uc16 two_byte_array[6] = {'f', 0xD83D, 0xDCA9, 'b', 'a', 0x2603};
50
51 i::Handle<i::JSArray> results_array = factory->NewJSArray(5);
52 i::Handle<i::String> one_byte =
53 factory->NewStringFromOneByte(i::Vector<const uint8_t>(one_byte_array, 6))
54 .ToHandleChecked();
55 i::Handle<i::String> two_byte =
56 factory->NewStringFromTwoByte(i::Vector<const i::uc16>(two_byte_array, 6))
57 .ToHandleChecked();
58
59 for (int flags = 0; flags <= kAllFlags; flags++) {
60 i::Handle<i::JSRegExp> regexp;
61 {
62 v8::TryCatch try_catch(isolate);
63 i::MaybeHandle<i::JSRegExp> maybe_regexp =
64 i::JSRegExp::New(source, static_cast<i::JSRegExp::Flags>(flags));
65 if (!maybe_regexp.ToHandle(&regexp)) continue;
66 }
67 Test(isolate, regexp, one_byte, results_array);
68 Test(isolate, regexp, two_byte, results_array);
69 Test(isolate, regexp, factory->empty_string(), results_array);
70 Test(isolate, regexp, source, results_array);
71 }
72
Ben Murdochc5610432016-08-08 18:44:38 +010073 isolate->RequestGarbageCollectionForTesting(
74 v8::Isolate::kFullGarbageCollection);
Ben Murdoch097c5b22016-05-18 11:27:45 +010075 return 0;
76}