blob: 23ead0e4690f6150c6a28c10aca542ae6e370293 [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"
20#include "utils/String8.h"
21
22static constexpr int MAX_FILE_SIZE = 256;
23static constexpr int MAX_STR_LEN = 2048;
24static constexpr int MAX_OPERATIONS = 1000;
25
26static const std::vector<std::function<void(FuzzedDataProvider*, android::PropertyMap)>>
27 operations = {
28 [](FuzzedDataProvider*, android::PropertyMap propertyMap) -> void {
29 propertyMap.getProperties();
30 },
31 [](FuzzedDataProvider*, android::PropertyMap propertyMap) -> void {
32 propertyMap.clear();
33 },
34 [](FuzzedDataProvider* dataProvider, android::PropertyMap propertyMap) -> void {
35 std::string keyStr = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
36 android::String8 key = android::String8(keyStr.c_str());
37 propertyMap.hasProperty(key);
38 },
39 [](FuzzedDataProvider* dataProvider, android::PropertyMap propertyMap) -> void {
40 std::string keyStr = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
41 android::String8 key = android::String8(keyStr.c_str());
42 android::String8 out;
43 propertyMap.tryGetProperty(key, out);
44 },
45 [](FuzzedDataProvider* dataProvider, android::PropertyMap propertyMap) -> void {
46 TemporaryFile tf;
47 // Generate file contents
48 std::string contents = dataProvider->ConsumeRandomLengthString(MAX_FILE_SIZE);
49 // If we have string contents, dump them into the file.
50 // Otherwise, just leave it as an empty file.
51 if (contents.length() > 0) {
52 const char* bytes = contents.c_str();
53 android::base::WriteStringToFd(bytes, tf.fd);
54 }
55 android::PropertyMap* mapPtr = &propertyMap;
56 android::PropertyMap::load(android::String8(tf.path), &mapPtr);
57 },
58 [](FuzzedDataProvider* dataProvider, android::PropertyMap propertyMap) -> void {
59 std::string keyStr = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
60 std::string valStr = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
61 android::String8 key = android::String8(keyStr.c_str());
62 android::String8 val = android::String8(valStr.c_str());
63 propertyMap.addProperty(key, val);
64 },
65};
66extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
67 FuzzedDataProvider dataProvider(data, size);
68 android::PropertyMap proprtyMap = android::PropertyMap();
69
70 int opsRun = 0;
71 while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
72 uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
73 operations[op](&dataProvider, proprtyMap);
74 }
75 return 0;
76}