blob: 8f9781bfef9ba44ab26c5d924485be8d447f60e2 [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 */
Ryan Prichard999efbe2023-09-21 17:51:52 -070016#include <functional>
Dylan Katz9d5845b2020-05-11 15:44:01 -070017#include <iostream>
Ryan Prichard999efbe2023-09-21 17:51:52 -070018#include <vector>
Dylan Katz9d5845b2020-05-11 15:44:01 -070019
20#include "fuzzer/FuzzedDataProvider.h"
21#include "utils/String16.h"
22static constexpr int MAX_STRING_BYTES = 256;
23static constexpr uint8_t MAX_OPERATIONS = 50;
24
25std::vector<std::function<void(FuzzedDataProvider&, android::String16, android::String16)>>
26 operations = {
27
28 // Bytes and size
29 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000030 str1.c_str();
Dylan Katz9d5845b2020-05-11 15:44:01 -070031 }),
32 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
33 str1.isStaticString();
34 }),
35 ([](FuzzedDataProvider&, android::String16 str1, android::String16) -> void {
36 str1.size();
37 }),
38
Dylan Katz9d5845b2020-05-11 15:44:01 -070039 // Comparison
40 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
41 str1.startsWith(str2);
42 }),
43 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000044 str1.contains(str2.c_str());
Dylan Katz9d5845b2020-05-11 15:44:01 -070045 }),
46 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
47 str1.compare(str2);
48 }),
49
50 // Append and format
51 ([](FuzzedDataProvider&, android::String16 str1, android::String16 str2) -> void {
52 str1.append(str2);
53 }),
54 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
55 android::String16 str2) -> void {
56 int pos = dataProvider.ConsumeIntegralInRange<int>(0, str1.size());
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000057 str1.insert(pos, str2.c_str());
Dylan Katz9d5845b2020-05-11 15:44:01 -070058 }),
59
60 // Find and replace operations
61 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
62 android::String16) -> void {
63 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
64 str1.findFirst(findChar);
65 }),
66 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
67 android::String16) -> void {
68 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
69 str1.findLast(findChar);
70 }),
71 ([](FuzzedDataProvider& dataProvider, android::String16 str1,
72 android::String16) -> void {
73 char16_t findChar = dataProvider.ConsumeIntegral<char16_t>();
74 char16_t replaceChar = dataProvider.ConsumeIntegral<char16_t>();
75 str1.replaceAll(findChar, replaceChar);
76 }),
Dylan Katz9d5845b2020-05-11 15:44:01 -070077};
78
79void callFunc(uint8_t index, FuzzedDataProvider& dataProvider, android::String16 str1,
80 android::String16 str2) {
81 operations[index](dataProvider, str1, str2);
82}
83
84extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
85 FuzzedDataProvider dataProvider(data, size);
86 // We're generating two char vectors.
87 // First, generate lengths.
88 const size_t kVecOneLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
89 const size_t kVecTwoLen = dataProvider.ConsumeIntegralInRange<size_t>(1, MAX_STRING_BYTES);
90
91 // Next, populate the vectors
92 std::vector<char> vec = dataProvider.ConsumeBytesWithTerminator<char>(kVecOneLen);
93 std::vector<char> vec_two = dataProvider.ConsumeBytesWithTerminator<char>(kVecTwoLen);
94
95 // Get pointers to their data
96 char* char_one = vec.data();
97 char* char_two = vec_two.data();
98
99 // Create UTF16 representations
100 android::String16 str_one_utf16 = android::String16(char_one);
101 android::String16 str_two_utf16 = android::String16(char_two);
102
103 // Run operations against strings
104 int opsRun = 0;
105 while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
106 uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
107 callFunc(op, dataProvider, str_one_utf16, str_two_utf16);
108 }
109
Dylan Katz9d5845b2020-05-11 15:44:01 -0700110 return 0;
111}