Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 20 | |
| 21 | static constexpr int MAX_FILE_SIZE = 256; |
| 22 | static constexpr int MAX_STR_LEN = 2048; |
| 23 | static constexpr int MAX_OPERATIONS = 1000; |
| 24 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 25 | static const std::vector<std::function<void(FuzzedDataProvider*, android::PropertyMap&)>> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 26 | operations = { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 27 | [](FuzzedDataProvider*, android::PropertyMap& propertyMap) -> void { |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 28 | propertyMap.clear(); |
| 29 | }, |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 30 | [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void { |
| 31 | std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN); |
| 32 | std::string out; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 33 | propertyMap.tryGetProperty(key, out); |
| 34 | }, |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 35 | [](FuzzedDataProvider* dataProvider, android::PropertyMap& /*unused*/) -> void { |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 36 | TemporaryFile tf; |
| 37 | // Generate file contents |
| 38 | std::string contents = dataProvider->ConsumeRandomLengthString(MAX_FILE_SIZE); |
| 39 | // If we have string contents, dump them into the file. |
| 40 | // Otherwise, just leave it as an empty file. |
| 41 | if (contents.length() > 0) { |
| 42 | const char* bytes = contents.c_str(); |
| 43 | android::base::WriteStringToFd(bytes, tf.fd); |
| 44 | } |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 45 | android::PropertyMap::load(tf.path); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 46 | }, |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 47 | [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void { |
| 48 | std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN); |
| 49 | std::string val = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 50 | propertyMap.addProperty(key, val); |
| 51 | }, |
| 52 | }; |
| 53 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 54 | FuzzedDataProvider dataProvider(data, size); |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame^] | 55 | android::PropertyMap propertyMap; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 56 | |
| 57 | int opsRun = 0; |
| 58 | while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) { |
| 59 | uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1); |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 60 | operations[op](&dataProvider, propertyMap); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 61 | } |
| 62 | return 0; |
| 63 | } |