Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits.h> | 5 #include <limits.h> |
| 6 #include <stddef.h> | 6 #include <stddef.h> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "include/v8.h" | 9 #include "include/v8.h" |
| 10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
| 11 #include "src/objects.h" | 11 #include "src/objects.h" |
| 12 #include "src/parsing/parse-info.h" | 12 #include "src/parsing/parse-info.h" |
| 13 #include "src/parsing/parsing.h" | 13 #include "src/parsing/parsing.h" |
| 14 #include "src/parsing/preparser.h" | 14 #include "src/parsing/preparser.h" |
| 15 #include "test/fuzzer/fuzzer-support.h" | 15 #include "test/fuzzer/fuzzer-support.h" |
| 16 | 16 |
| 17 #include <list> | |
| 18 #include <cctype> | |
| 19 | |
| 20 bool IsValidInput(const uint8_t* data, size_t size) { | |
| 21 std::list<char> parentheses; | |
| 22 const char* ptr = reinterpret_cast<const char*>(data); | |
| 23 | |
| 24 for (size_t i = 0; i != size; ++i) { | |
| 25 // Check that all characters in the data are valid. | |
| 26 if (!(std::isspace(ptr[i]) || std::isprint(ptr[i]))) | |
|
marja
2017/05/17 08:56:20
Coding style nit: if the body is on the next line,
mmoroz
2017/05/17 09:59:15
Done.
| |
| 27 return false; | |
| 28 | |
| 29 // Check balance of parentheses in the data. | |
| 30 switch (ptr[i]) { | |
| 31 case '(': | |
| 32 case '[': | |
| 33 case '{': | |
| 34 parentheses.push_back(ptr[i]); | |
| 35 break; | |
| 36 case ')': | |
| 37 if (parentheses.back() != '(') | |
| 38 return false; | |
| 39 parentheses.pop_back(); | |
| 40 break; | |
| 41 case ']': | |
| 42 if (parentheses.back() != '[') | |
| 43 return false; | |
| 44 parentheses.pop_back(); | |
| 45 break; | |
| 46 case '}': | |
| 47 if (parentheses.back() != '{') | |
| 48 return false; | |
| 49 parentheses.pop_back(); | |
| 50 break; | |
| 51 default: | |
| 52 break; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 return parentheses.empty(); | |
| 57 } | |
| 58 | |
| 17 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 60 if (!IsValidInput(data, size)) | |
|
marja
2017/05/17 08:56:20
ditto
mmoroz
2017/05/17 09:59:15
Done.
| |
| 61 return 0; | |
| 62 | |
| 18 v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get(); | 63 v8_fuzzer::FuzzerSupport* support = v8_fuzzer::FuzzerSupport::Get(); |
| 19 v8::Isolate* isolate = support->GetIsolate(); | 64 v8::Isolate* isolate = support->GetIsolate(); |
| 20 | 65 |
| 21 v8::Isolate::Scope isolate_scope(isolate); | 66 v8::Isolate::Scope isolate_scope(isolate); |
| 22 v8::HandleScope handle_scope(isolate); | 67 v8::HandleScope handle_scope(isolate); |
| 23 v8::Context::Scope context_scope(support->GetContext()); | 68 v8::Context::Scope context_scope(support->GetContext()); |
| 24 v8::TryCatch try_catch(isolate); | 69 v8::TryCatch try_catch(isolate); |
| 25 | 70 |
| 26 v8::internal::Isolate* i_isolate = | 71 v8::internal::Isolate* i_isolate = |
| 27 reinterpret_cast<v8::internal::Isolate*>(isolate); | 72 reinterpret_cast<v8::internal::Isolate*>(isolate); |
| 28 v8::internal::Factory* factory = i_isolate->factory(); | 73 v8::internal::Factory* factory = i_isolate->factory(); |
| 29 | 74 |
| 30 if (size > INT_MAX) return 0; | 75 if (size > INT_MAX) return 0; |
| 31 v8::internal::MaybeHandle<v8::internal::String> source = | 76 v8::internal::MaybeHandle<v8::internal::String> source = |
| 32 factory->NewStringFromOneByte( | 77 factory->NewStringFromOneByte( |
| 33 v8::internal::Vector<const uint8_t>(data, static_cast<int>(size))); | 78 v8::internal::Vector<const uint8_t>(data, static_cast<int>(size))); |
| 34 if (source.is_null()) return 0; | 79 if (source.is_null()) return 0; |
| 35 | 80 |
| 36 v8::internal::Handle<v8::internal::Script> script = | 81 v8::internal::Handle<v8::internal::Script> script = |
| 37 factory->NewScript(source.ToHandleChecked()); | 82 factory->NewScript(source.ToHandleChecked()); |
| 38 v8::internal::ParseInfo info(script); | 83 v8::internal::ParseInfo info(script); |
| 39 if (!v8::internal::parsing::ParseProgram(&info, i_isolate)) { | 84 if (!v8::internal::parsing::ParseProgram(&info, i_isolate)) { |
| 40 i_isolate->OptionalRescheduleException(true); | 85 i_isolate->OptionalRescheduleException(true); |
| 41 } | 86 } |
| 42 isolate->RequestGarbageCollectionForTesting( | 87 isolate->RequestGarbageCollectionForTesting( |
| 43 v8::Isolate::kFullGarbageCollection); | 88 v8::Isolate::kFullGarbageCollection); |
| 44 return 0; | 89 return 0; |
| 45 } | 90 } |
| OLD | NEW |