blob: 6f7a54f9f31043a7ba74b10e2925b6e213c48ff2 [file] [log] [blame]
Dylan Katz9d5845b2020-05-11 15:44:01 -07001/*
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>
Dylan Katz98e14de2020-09-25 16:22:38 -070018#include <memory>
Dylan Katz9d5845b2020-05-11 15:44:01 -070019
Dylan Katz98e14de2020-09-25 16:22:38 -070020#include "FuzzFormatTypes.h"
Dylan Katz9d5845b2020-05-11 15:44:01 -070021#include "fuzzer/FuzzedDataProvider.h"
22#include "utils/String8.h"
23
24static constexpr int MAX_STRING_BYTES = 256;
25static constexpr uint8_t MAX_OPERATIONS = 50;
Dylan Katz98e14de2020-09-25 16:22:38 -070026// Interestingly, 2147483614 (INT32_MAX - 33) seems to be the max value that is handled for format
27// flags. Unfortunately we need to use a smaller value so we avoid consuming too much memory.
Dylan Katz9d5845b2020-05-11 15:44:01 -070028
Dylan Katz98e14de2020-09-25 16:22:38 -070029void fuzzFormat(FuzzedDataProvider* dataProvider, android::String8* str1, bool shouldAppend);
30std::vector<std::function<void(FuzzedDataProvider*, android::String8*, android::String8*)>>
Dylan Katz9d5845b2020-05-11 15:44:01 -070031 operations = {
Dylan Katz9d5845b2020-05-11 15:44:01 -070032 // Bytes and size
Dylan Katz98e14de2020-09-25 16:22:38 -070033 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
34 str1->bytes();
Dylan Katz9d5845b2020-05-11 15:44:01 -070035 },
Dylan Katz98e14de2020-09-25 16:22:38 -070036 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +000037 str1->empty();
Dylan Katz9d5845b2020-05-11 15:44:01 -070038 },
Dylan Katz98e14de2020-09-25 16:22:38 -070039 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
40 str1->length();
Dylan Katz9d5845b2020-05-11 15:44:01 -070041 },
42
43 // Casing
Dylan Katz98e14de2020-09-25 16:22:38 -070044 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
Dylan Katz98e14de2020-09-25 16:22:38 -070045 str1->toLower();
Dylan Katz9d5845b2020-05-11 15:44:01 -070046 },
Dylan Katz98e14de2020-09-25 16:22:38 -070047 [](FuzzedDataProvider*, android::String8* str1, android::String8* str2) -> void {
Steven Moreland749becf2023-07-13 21:19:50 +000048 if (str2->size() == 0) return;
49
Dylan Katz98e14de2020-09-25 16:22:38 -070050 str1->removeAll(str2->c_str());
Dylan Katz9d5845b2020-05-11 15:44:01 -070051 },
Dylan Katz98e14de2020-09-25 16:22:38 -070052 [](FuzzedDataProvider*, android::String8* str1, android::String8* str2) -> void {
53 const android::String8& constRef(*str2);
54 str1->compare(constRef);
Dylan Katz9d5845b2020-05-11 15:44:01 -070055 },
56
57 // Append and format
Dylan Katz98e14de2020-09-25 16:22:38 -070058 [](FuzzedDataProvider*, android::String8* str1, android::String8* str2) -> void {
59 str1->append(str2->c_str());
Dylan Katz9d5845b2020-05-11 15:44:01 -070060 },
Dylan Katz98e14de2020-09-25 16:22:38 -070061 [](FuzzedDataProvider* dataProvider, android::String8* str1, android::String8*)
62 -> void { fuzzFormat(dataProvider, str1, dataProvider->ConsumeBool()); },
Dylan Katz9d5845b2020-05-11 15:44:01 -070063
64 // Find operation
Dylan Katz98e14de2020-09-25 16:22:38 -070065 [](FuzzedDataProvider* dataProvider, android::String8* str1,
66 android::String8* str2) -> void {
Dylan Katz9d5845b2020-05-11 15:44:01 -070067 // We need to get a value from our fuzzer here.
Dylan Katz98e14de2020-09-25 16:22:38 -070068 int start_index = dataProvider->ConsumeIntegralInRange<int>(0, str1->size());
69 str1->find(str2->c_str(), start_index);
Dylan Katz9d5845b2020-05-11 15:44:01 -070070 },
Tomasz Wasilczykcff2e402023-09-08 17:08:39 +000071
72 // Path handling
73 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
74 str1->getBasePath();
75 },
76 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
77 str1->getPathExtension();
78 },
79 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
80 str1->getPathLeaf();
81 },
82 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
83 str1->getPathDir();
84 },
85 [](FuzzedDataProvider*, android::String8* str1, android::String8*) -> void {
86 std::shared_ptr<android::String8> path_out_str =
87 std::make_shared<android::String8>();
88 str1->walkPath(path_out_str.get());
89 path_out_str->clear();
90 },
91 [](FuzzedDataProvider* dataProvider, android::String8* str1,
92 android::String8*) -> void {
93 str1->appendPath(dataProvider->ConsumeBytesWithTerminator<char>(5).data());
94 },
Dylan Katz9d5845b2020-05-11 15:44:01 -070095};
96
Dylan Katz98e14de2020-09-25 16:22:38 -070097void fuzzFormat(FuzzedDataProvider* dataProvider, android::String8* str1, bool shouldAppend) {
98 FormatChar formatType = dataProvider->ConsumeEnum<FormatChar>();
99
100 std::string formatString("%");
101 // Width specifier
102 if (dataProvider->ConsumeBool()) {
103 // Left pad with zeroes
104 if (dataProvider->ConsumeBool()) {
105 formatString.push_back('0');
106 }
107 // Right justify (or left justify if negative)
108 int32_t justify = dataProvider->ConsumeIntegralInRange<int32_t>(-kMaxFormatFlagValue,
109 kMaxFormatFlagValue);
110 formatString += std::to_string(justify);
111 }
112
113 // The # specifier only works with o, x, X, a, A, e, E, f, F, g, and G
114 if (canApplyFlag(formatType, '#') && dataProvider->ConsumeBool()) {
115 formatString.push_back('#');
116 }
117
118 // Precision specifier
119 if (canApplyFlag(formatType, '.') && dataProvider->ConsumeBool()) {
120 formatString.push_back('.');
121 formatString +=
122 std::to_string(dataProvider->ConsumeIntegralInRange<int>(0, kMaxFormatFlagValue));
123 }
124
125 formatString.push_back(kFormatChars.at(static_cast<uint8_t>(formatType)));
126
127 switch (formatType) {
128 case SIGNED_DECIMAL: {
129 int val = dataProvider->ConsumeIntegral<int>();
130 if (shouldAppend) {
131 str1->appendFormat(formatString.c_str(), val);
132 } else {
133 str1->format(formatString.c_str(), dataProvider->ConsumeIntegral<int>());
134 }
135 break;
136 }
137
138 case UNSIGNED_DECIMAL:
139 case UNSIGNED_OCTAL:
140 case UNSIGNED_HEX_LOWER:
141 case UNSIGNED_HEX_UPPER: {
142 // Unsigned integers for u, o, x, and X
143 uint val = dataProvider->ConsumeIntegral<uint>();
144 if (shouldAppend) {
145 str1->appendFormat(formatString.c_str(), val);
146 } else {
147 str1->format(formatString.c_str(), val);
148 }
149 break;
150 }
151
152 case FLOAT_LOWER:
153 case FLOAT_UPPER:
154 case EXPONENT_LOWER:
155 case EXPONENT_UPPER:
156 case SHORT_EXP_LOWER:
157 case SHORT_EXP_UPPER:
158 case HEX_FLOAT_LOWER:
159 case HEX_FLOAT_UPPER: {
160 // Floating points for f, F, e, E, g, G, a, and A
161 float val = dataProvider->ConsumeFloatingPoint<float>();
162 if (shouldAppend) {
163 str1->appendFormat(formatString.c_str(), val);
164 } else {
165 str1->format(formatString.c_str(), val);
166 }
167 break;
168 }
169
170 case CHAR: {
171 char val = dataProvider->ConsumeIntegral<char>();
172 if (shouldAppend) {
173 str1->appendFormat(formatString.c_str(), val);
174 } else {
175 str1->format(formatString.c_str(), val);
176 }
177 break;
178 }
179
180 case STRING: {
181 std::string val = dataProvider->ConsumeRandomLengthString(MAX_STRING_BYTES);
182 if (shouldAppend) {
183 str1->appendFormat(formatString.c_str(), val.c_str());
184 } else {
185 str1->format(formatString.c_str(), val.c_str());
186 }
187 break;
188 }
189 case POINTER: {
190 uintptr_t val = dataProvider->ConsumeIntegral<uintptr_t>();
191 if (shouldAppend) {
192 str1->appendFormat(formatString.c_str(), val);
193 } else {
194 str1->format(formatString.c_str(), val);
195 }
196 break;
197 }
198 }
199}
200
201void callFunc(uint8_t index, FuzzedDataProvider* dataProvider, android::String8* str1,
202 android::String8* str2) {
Dylan Katz9d5845b2020-05-11 15:44:01 -0700203 operations[index](dataProvider, str1, str2);
204}
205
206extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
207 FuzzedDataProvider dataProvider(data, size);
208 // Generate vector lengths
209 const size_t kVecOneLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
210 const size_t kVecTwoLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
211 // Populate vectors
212 std::vector<char> vec = dataProvider.ConsumeBytesWithTerminator<char>(kVecOneLen);
213 std::vector<char> vec_two = dataProvider.ConsumeBytesWithTerminator<char>(kVecTwoLen);
214 // Create UTF-8 pointers
215 android::String8 str_one_utf8 = android::String8(vec.data());
216 android::String8 str_two_utf8 = android::String8(vec_two.data());
Dylan Katz9d5845b2020-05-11 15:44:01 -0700217 // Run operations against strings
218 int opsRun = 0;
219 while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
220 uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
Dylan Katz98e14de2020-09-25 16:22:38 -0700221 operations[op](&dataProvider, &str_one_utf8, &str_two_utf8);
Dylan Katz9d5845b2020-05-11 15:44:01 -0700222 }
Dylan Katz9d5845b2020-05-11 15:44:01 -0700223 // Just to be extra sure these can be freed, we're going to explicitly clear
224 // them
225 str_one_utf8.clear();
226 str_two_utf8.clear();
227 return 0;
228}