blob: 63c2800718dc1b31fd0591e364262e6461dffe03 [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 <iostream>
17
18#include "fuzzer/FuzzedDataProvider.h"
19#include "utils/String16.h"
20static constexpr int MAX_STRING_BYTES = 256;
21static constexpr uint8_t MAX_OPERATIONS = 50;
22
23std::vector<std::function<void(FuzzedDataProvider&, android::String16, android::String16)>>
24 operations = {
25
26 // Bytes and size
27 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
28 str1.string();
29 }),
30 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
31 str1.isStaticString();
32 }),
33 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
34 str1.size();
35 }),
36
37 // Casing
38 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
39 str1.makeLower();
40 }),
41
42 // Comparison
43 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
44 str1.startsWith(str2);
45 }),
46 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
47 str1.contains(str2.string());
48 }),
49 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
50 str1.compare(str2);
51 }),
52
53 // Append and format
54 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
55 str1.append(str2);
56 }),
57 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
58 android::String16 str2) -> void {
59 int pos = dataProvider.ConsumeIntegralInRange<int>(0, str1.size());
60 str1.insert(pos, str2.string());
61 }),
62
63 // Find and replace operations
64 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
65 android::String16) -> void {
66 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
67 str1.findFirst(findChar);
68 }),
69 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
70 android::String16) -> void {
71 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
72 str1.findLast(findChar);
73 }),
74 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
75 android::String16) -> void {
76 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
77 char16_t replaceChar = dataProvider.ConsumeIntegral<char16_t>();
78 str1.replaceAll(findChar, replaceChar);
79 }),
80 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
81 android::String16) -> void {
82 size_t len = dataProvider.ConsumeIntegral<size_t>();
83 size_t begin = dataProvider.ConsumeIntegral<size_t>();
84 str1.remove(len, begin);
85 }),
86};
87
88void callFunc(uint8_t index, FuzzedDataProvider& dataProvider, android::String16 str1,
89 android::String16 str2) {
90 operations[index](dataProvider, str1, str2);
91}
92
93extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
94 FuzzedDataProvider dataProvider(data, size);
95 // We're generating two char vectors.
96 // First, generate lengths.
97 const size_t kVecOneLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
98 const size_t kVecTwoLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
99
100 // Next, populate the vectors
101 std::vector<char> vec = dataProvider.ConsumeBytesWithTerminator<char>(kVecOneLen);
102 std::vector<char> vec_two = dataProvider.ConsumeBytesWithTerminator<char>(kVecTwoLen);
103
104 // Get pointers to their data
105 char* char_one = vec.data();
106 char* char_two = vec_two.data();
107
108 // Create UTF16 representations
109 android::String16 str_one_utf16 = android::String16(char_one);
110 android::String16 str_two_utf16 = android::String16(char_two);
111
112 // Run operations against strings
113 int opsRun = 0;
114 while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
115 uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
116 callFunc(op, dataProvider, str_one_utf16, str_two_utf16);
117 }
118
119 str_one_utf16.remove(0, str_one_utf16.size());
120 str_two_utf16.remove(0, str_two_utf16.size());
121 return 0;
122}