xref: /aosp_15_r20/frameworks/native/libs/input/PropertyMap_fuzz.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include "android-base/file.h"
18*38e8c45fSAndroid Build Coastguard Worker #include "fuzzer/FuzzedDataProvider.h"
19*38e8c45fSAndroid Build Coastguard Worker #include "input/PropertyMap.h"
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker static constexpr int MAX_FILE_SIZE = 256;
22*38e8c45fSAndroid Build Coastguard Worker static constexpr int MAX_STR_LEN = 2048;
23*38e8c45fSAndroid Build Coastguard Worker static constexpr int MAX_OPERATIONS = 1000;
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker static const std::vector<std::function<void(FuzzedDataProvider*, android::PropertyMap&)>>
26*38e8c45fSAndroid Build Coastguard Worker         operations = {
__anonbc24cc150102() 27*38e8c45fSAndroid Build Coastguard Worker                 [](FuzzedDataProvider*, android::PropertyMap& propertyMap) -> void {
28*38e8c45fSAndroid Build Coastguard Worker                     propertyMap.clear();
29*38e8c45fSAndroid Build Coastguard Worker                 },
__anonbc24cc150202() 30*38e8c45fSAndroid Build Coastguard Worker                 [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void {
31*38e8c45fSAndroid Build Coastguard Worker                     std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
32*38e8c45fSAndroid Build Coastguard Worker                     propertyMap.getString(key);
33*38e8c45fSAndroid Build Coastguard Worker                 },
__anonbc24cc150302() 34*38e8c45fSAndroid Build Coastguard Worker                 [](FuzzedDataProvider* dataProvider, android::PropertyMap& /*unused*/) -> void {
35*38e8c45fSAndroid Build Coastguard Worker                     TemporaryFile tf;
36*38e8c45fSAndroid Build Coastguard Worker                     // Generate file contents
37*38e8c45fSAndroid Build Coastguard Worker                     std::string contents = dataProvider->ConsumeRandomLengthString(MAX_FILE_SIZE);
38*38e8c45fSAndroid Build Coastguard Worker                     // If we have string contents, dump them into the file.
39*38e8c45fSAndroid Build Coastguard Worker                     // Otherwise, just leave it as an empty file.
40*38e8c45fSAndroid Build Coastguard Worker                     if (contents.length() > 0) {
41*38e8c45fSAndroid Build Coastguard Worker                         const char* bytes = contents.c_str();
42*38e8c45fSAndroid Build Coastguard Worker                         android::base::WriteStringToFd(bytes, tf.fd);
43*38e8c45fSAndroid Build Coastguard Worker                     }
44*38e8c45fSAndroid Build Coastguard Worker                     android::PropertyMap::load(tf.path);
45*38e8c45fSAndroid Build Coastguard Worker                 },
__anonbc24cc150402() 46*38e8c45fSAndroid Build Coastguard Worker                 [](FuzzedDataProvider* dataProvider, android::PropertyMap& propertyMap) -> void {
47*38e8c45fSAndroid Build Coastguard Worker                     std::string key = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
48*38e8c45fSAndroid Build Coastguard Worker                     std::string val = dataProvider->ConsumeRandomLengthString(MAX_STR_LEN);
49*38e8c45fSAndroid Build Coastguard Worker                     propertyMap.addProperty(key, val);
50*38e8c45fSAndroid Build Coastguard Worker                 },
51*38e8c45fSAndroid Build Coastguard Worker };
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52*38e8c45fSAndroid Build Coastguard Worker extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
53*38e8c45fSAndroid Build Coastguard Worker     FuzzedDataProvider dataProvider(data, size);
54*38e8c45fSAndroid Build Coastguard Worker     android::PropertyMap propertyMap;
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker     int opsRun = 0;
57*38e8c45fSAndroid Build Coastguard Worker     while (dataProvider.remaining_bytes() > 0 && opsRun++ < MAX_OPERATIONS) {
58*38e8c45fSAndroid Build Coastguard Worker         uint8_t op = dataProvider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1);
59*38e8c45fSAndroid Build Coastguard Worker         operations[op](&dataProvider, propertyMap);
60*38e8c45fSAndroid Build Coastguard Worker     }
61*38e8c45fSAndroid Build Coastguard Worker     return 0;
62*38e8c45fSAndroid Build Coastguard Worker }
63