• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <binder/Parcel.h>
20 #include <binder/Parcelable.h>
21 #include <binder/PersistableBundle.h>
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include <utils/String16.h>
24 #include <utils/StrongPointer.h>
25 #include <map>
26 #include <set>
27 #include <vector>
28 
29 namespace android {
30 
31 /* This is a vector of lambda functions the fuzzer will pull from.
32  *  This is done so new functions can be added to the fuzzer easily
33  *  without requiring modifications to the main fuzzer file. This also
34  *  allows multiple fuzzers to include this file, if functionality is needed.
35  */
36 static const std::vector<std::function<
37         void(FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const&, String16*)>>
38         gPersistableBundleOperations =
39                 {[](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
40                     String16*) -> void { p_bundle->empty(); },
41                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
42                     String16*) -> void {
43                      Parcel parcel;
44                      p_bundle->writeToParcel(&parcel);
45                  },
46                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
47                     String16*) -> void {
48                      Parcel parcel;
49                      std::vector<uint8_t> buf = fdp->ConsumeBytes<uint8_t>(
50                              fdp->ConsumeIntegralInRange<size_t>(0, fdp->remaining_bytes() - 1));
51                      parcel.write(buf.data(), buf.size());
52                      p_bundle->readFromParcel(&parcel);
53                  },
54                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
55                     String16*) -> void { p_bundle->size(); },
56                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
57                     String16* key) -> void { p_bundle->erase(*key); },
58                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
59                     String16* key) -> void {
60                      bool value = fdp->ConsumeBool();
61                      p_bundle->putBoolean(*key, value);
62                  },
63                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
64                     String16* key) -> void {
65                      int32_t value = fdp->ConsumeIntegral<int32_t>();
66                      p_bundle->putInt(*key, value);
67                  },
68                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
69                     String16* key) -> void {
70                      os::PersistableBundle value = os::PersistableBundle();
71                      p_bundle->putPersistableBundle(*key, value);
72                  },
73                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
74                     String16* key) -> void {
75                      std::vector<String16> value;
76                      p_bundle->putStringVector(*key, value);
77                  },
78                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
79                     String16* key) -> void {
80                      std::vector<double> value;
81                      p_bundle->putDoubleVector(*key, value);
82                  },
83                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
84                     String16* key) -> void {
85                      std::vector<int64_t> value;
86                      p_bundle->putLongVector(*key, value);
87                  },
88                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
89                     String16* key) -> void {
90                      std::vector<int32_t> value;
91                      p_bundle->putIntVector(*key, value);
92                  },
93                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
94                     String16* key) -> void {
95                      std::vector<bool> value;
96                      p_bundle->putBooleanVector(*key, value);
97                  },
98                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
99                     String16* key) -> void {
100                      String16 value(fdp->ConsumeRandomLengthString(fdp->remaining_bytes()).c_str());
101                      p_bundle->putString(*key, value);
102                  },
103                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
104                     String16* key) -> void {
105                      int64_t value = fdp->ConsumeIntegral<int64_t>();
106                      p_bundle->putLong(*key, value);
107                  },
108                  [](FuzzedDataProvider* fdp, std::shared_ptr<os::PersistableBundle> const& p_bundle,
109                     String16* key) -> void {
110                      double value = fdp->ConsumeFloatingPoint<double>();
111                      p_bundle->putDouble(*key, value);
112                  },
113                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
114                     String16* key) -> void {
115                      bool out;
116                      p_bundle->getBoolean(*key, &out);
117                  },
118                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
119                     String16* key) -> void {
120                      os::PersistableBundle out;
121                      p_bundle->getPersistableBundle(*key, &out);
122                  },
123                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
124                     String16* key) -> void {
125                      std::vector<String16> out;
126                      p_bundle->getStringVector(*key, &out);
127                  },
128                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
129                     String16* key) -> void {
130                      std::vector<double> out;
131                      p_bundle->getDoubleVector(*key, &out);
132                  },
133                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
134                     String16* key) -> void {
135                      std::vector<int64_t> out;
136                      p_bundle->getLongVector(*key, &out);
137                  },
138                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
139                     String16* key) -> void {
140                      std::vector<int32_t> out;
141                      p_bundle->getIntVector(*key, &out);
142                  },
143                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
144                     String16* key) -> void {
145                      std::vector<bool> out;
146                      p_bundle->getBooleanVector(*key, &out);
147                  },
148                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
149                     String16* key) -> void {
150                      String16 out;
151                      p_bundle->getString(*key, &out);
152                  },
153                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
154                     String16* key) -> void {
155                      double out;
156                      p_bundle->getDouble(*key, &out);
157                  },
158                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
159                     String16* key) -> void {
160                      int64_t out;
161                      p_bundle->getLong(*key, &out);
162                  },
163                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
164                     String16* key) -> void {
165                      int32_t out;
166                      p_bundle->getInt(*key, &out);
167                  },
168                  [](FuzzedDataProvider*, std::shared_ptr<os::PersistableBundle> const& p_bundle,
169                     String16*) -> void {
170                      p_bundle->getBooleanKeys();
171                      p_bundle->getIntKeys();
172                      p_bundle->getLongKeys();
173                      p_bundle->getDoubleKeys();
174                      p_bundle->getStringKeys();
175                      p_bundle->getBooleanVectorKeys();
176                      p_bundle->getIntVectorKeys();
177                      p_bundle->getLongVectorKeys();
178                      p_bundle->getDoubleVectorKeys();
179                      p_bundle->getStringVectorKeys();
180                      p_bundle->getPersistableBundleKeys();
181                  }};
182 
183 } // namespace android
184