blob: 6299ca8ef95b86b7024081b4aefca616f542c8e7 [file] [log] [blame]
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -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
17#include "android-base/file.h"
18#include "fuzzer/FuzzedDataProvider.h"
19#include "input/PropertyMap.h"
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070020
21static constexpr int MAX_FILE_SIZE = 256;
22static constexpr int MAX_STR_LEN = 2048;
23static constexpr int MAX_OPERATIONS = 1000;
24
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070025static const std::vector<std::function<void(FuzzedDataProvider*, android::PropertyMap&)>>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070026 operations = {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070027 [](FuzzedDataProvider*, android::PropertyMap& propertyMap) -> void {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070028 propertyMap.clear();
29 },
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070030 [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void {
31 std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
Harry Cuttsf13161a2023-03-08 14:15:49 +000032 propertyMap.getString(key);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070033 },
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070034 [](FuzzedDataProvider* dataProvider, android::PropertyMap& /*unused*/) -> void {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070035 TemporaryFile tf;
36 // Generate file contents
37 std::string contents = dataProvider->ConsumeRandomLengthString(MAX_FILE_SIZE);
38 // If we have string contents, dump them into the file.
39 // Otherwise, just leave it as an empty file.
40 if (contents.length() > 0) {
41 const char* bytes = contents.c_str();
42 android::base::WriteStringToFd(bytes, tf.fd);
43 }
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -050044 android::PropertyMap::load(tf.path);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070045 },
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070046 [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void {
47 std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
48 std::string val = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070049 propertyMap.addProperty(key, val);
50 },
51};
52extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
53 FuzzedDataProvider dataProvider(data, size);
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070054 android::PropertyMap propertyMap;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070055
56 int opsRun = 0;
57 while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
58 uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -050059 operations[op](&dataProvider, propertyMap);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070060 }
61 return 0;
62}