| Dylan Katz | 9d5845b | 2020-05-11 15:44:01 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2020 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #include <functional> | 
|  | 17 | #include <iostream> | 
|  | 18 |  | 
|  | 19 | #include "fuzzer/FuzzedDataProvider.h" | 
|  | 20 | #include "utils/String8.h" | 
|  | 21 |  | 
|  | 22 | static constexpr int MAX_STRING_BYTES = 256; | 
|  | 23 | static constexpr uint8_t MAX_OPERATIONS = 50; | 
|  | 24 |  | 
|  | 25 | std::vector<std::function<void(FuzzedDataProvider&, android::String8, android::String8)>> | 
|  | 26 | operations = { | 
|  | 27 |  | 
|  | 28 | // Bytes and size | 
|  | 29 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 30 | str1.bytes(); | 
|  | 31 | }, | 
|  | 32 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 33 | str1.isEmpty(); | 
|  | 34 | }, | 
|  | 35 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 36 | str1.length(); | 
|  | 37 | }, | 
|  | 38 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 39 | str1.size(); | 
|  | 40 | }, | 
|  | 41 |  | 
|  | 42 | // Casing | 
|  | 43 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 44 | str1.toUpper(); | 
|  | 45 | }, | 
|  | 46 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 47 | str1.toLower(); | 
|  | 48 | }, | 
|  | 49 |  | 
|  | 50 | [](FuzzedDataProvider&, android::String8 str1, android::String8 str2) -> void { | 
|  | 51 | str1.removeAll(str2.c_str()); | 
|  | 52 | }, | 
|  | 53 | [](FuzzedDataProvider&, android::String8 str1, android::String8 str2) -> void { | 
|  | 54 | str1.compare(str2); | 
|  | 55 | }, | 
|  | 56 |  | 
|  | 57 | // Append and format | 
|  | 58 | [](FuzzedDataProvider&, android::String8 str1, android::String8 str2) -> void { | 
|  | 59 | str1.append(str2); | 
|  | 60 | }, | 
|  | 61 | [](FuzzedDataProvider&, android::String8 str1, android::String8 str2) -> void { | 
|  | 62 | str1.appendFormat(str1.c_str(), str2.c_str()); | 
|  | 63 | }, | 
|  | 64 | [](FuzzedDataProvider&, android::String8 str1, android::String8 str2) -> void { | 
|  | 65 | str1.format(str1.c_str(), str2.c_str()); | 
|  | 66 | }, | 
|  | 67 |  | 
|  | 68 | // Find operation | 
|  | 69 | [](FuzzedDataProvider& dataProvider, android::String8 str1, | 
|  | 70 | android::String8) -> void { | 
|  | 71 | // We need to get a value from our fuzzer here. | 
|  | 72 | int start_index = dataProvider.ConsumeIntegralInRange<int>(0, str1.size()); | 
|  | 73 | str1.find(str1.c_str(), start_index); | 
|  | 74 | }, | 
|  | 75 |  | 
|  | 76 | // Path handling | 
|  | 77 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 78 | str1.getBasePath(); | 
|  | 79 | }, | 
|  | 80 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 81 | str1.getPathExtension(); | 
|  | 82 | }, | 
|  | 83 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 84 | str1.getPathLeaf(); | 
|  | 85 | }, | 
|  | 86 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 87 | str1.getPathDir(); | 
|  | 88 | }, | 
|  | 89 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 90 | str1.convertToResPath(); | 
|  | 91 | }, | 
|  | 92 | [](FuzzedDataProvider&, android::String8 str1, android::String8) -> void { | 
|  | 93 | android::String8 path_out_str = android::String8(); | 
|  | 94 | str1.walkPath(&path_out_str); | 
|  | 95 | path_out_str.clear(); | 
|  | 96 | }, | 
|  | 97 | [](FuzzedDataProvider& dataProvider, android::String8 str1, | 
|  | 98 | android::String8) -> void { | 
|  | 99 | str1.setPathName(dataProvider.ConsumeBytesWithTerminator<char>(5).data()); | 
|  | 100 | }, | 
|  | 101 | [](FuzzedDataProvider& dataProvider, android::String8 str1, | 
|  | 102 | android::String8) -> void { | 
|  | 103 | str1.appendPath(dataProvider.ConsumeBytesWithTerminator<char>(5).data()); | 
|  | 104 | }, | 
|  | 105 | }; | 
|  | 106 |  | 
|  | 107 | void callFunc(uint8_t index, FuzzedDataProvider& dataProvider, android::String8 str1, | 
|  | 108 | android::String8 str2) { | 
|  | 109 | operations[index](dataProvider, str1, str2); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 
|  | 113 | FuzzedDataProvider dataProvider(data, size); | 
|  | 114 | // Generate vector lengths | 
|  | 115 | const size_t kVecOneLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES); | 
|  | 116 | const size_t kVecTwoLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES); | 
|  | 117 | // Populate vectors | 
|  | 118 | std::vector<char> vec = dataProvider.ConsumeBytesWithTerminator<char>(kVecOneLen); | 
|  | 119 | std::vector<char> vec_two = dataProvider.ConsumeBytesWithTerminator<char>(kVecTwoLen); | 
|  | 120 | // Create UTF-8 pointers | 
|  | 121 | android::String8 str_one_utf8 = android::String8(vec.data()); | 
|  | 122 | android::String8 str_two_utf8 = android::String8(vec_two.data()); | 
|  | 123 |  | 
|  | 124 | // Run operations against strings | 
|  | 125 | int opsRun = 0; | 
|  | 126 | while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) { | 
|  | 127 | uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1); | 
|  | 128 | callFunc(op, dataProvider, str_one_utf8, str_two_utf8); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | // Just to be extra sure these can be freed, we're going to explicitly clear | 
|  | 132 | // them | 
|  | 133 | str_one_utf8.clear(); | 
|  | 134 | str_two_utf8.clear(); | 
|  | 135 | return 0; | 
|  | 136 | } |