blob: f678f4c9126e71fb5f4bcb8c4169d3d0fec7beb7 [file] [log] [blame]
Steven Moreland46e0da72019-09-05 15:52:02 -07001/*
2 * Copyright (C) 2019 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#define FUZZ_LOG_TAG "binder"
17
18#include "binder.h"
19#include "util.h"
20
Steven Moreland7c7c6482019-09-30 12:32:02 -070021#include <android/os/IServiceManager.h>
22
Steven Moreland46e0da72019-09-05 15:52:02 -070023using ::android::status_t;
24
Steven Moreland7c7c6482019-09-30 12:32:02 -070025class ExampleParcelable : public android::Parcelable {
26public:
27 status_t writeToParcel(android::Parcel* /*parcel*/) const override {
28 FUZZ_LOG() << "should not reach";
29 abort();
30 }
31 status_t readFromParcel(const android::Parcel* parcel) override {
32 mExampleExtraField++;
33 return parcel->readInt64(&(this->mExampleUsedData));
34 }
35private:
36 int64_t mExampleExtraField = 0;
37 int64_t mExampleUsedData = 0;
38};
39
Steven Moreland6d813932019-09-30 15:31:35 -070040struct ExampleFlattenable : public android::Flattenable<ExampleFlattenable> {
41public:
42 size_t getFlattenedSize() const { return sizeof(mValue); }
43 size_t getFdCount() const { return 0; }
44 status_t flatten(void*& /*buffer*/, size_t& /*size*/, int*& /*fds*/, size_t& /*count*/) const {
45 FUZZ_LOG() << "should not reach";
46 abort();
47 }
48 status_t unflatten(void const*& buffer, size_t& size, int const*& /*fds*/, size_t& /*count*/) {
49 if (size < sizeof(mValue)) {
50 return android::NO_MEMORY;
51 }
52 android::FlattenableUtils::read(buffer, size, mValue);
53 return android::OK;
54 }
55private:
56 int32_t mValue = 0xFEEDBEEF;
57};
58
59struct ExampleLightFlattenable : public android::LightFlattenablePod<ExampleLightFlattenable> {
60 int32_t mValue = 0;
61};
62
Steven Moreland46e0da72019-09-05 15:52:02 -070063#define PARCEL_READ_WITH_STATUS(T, FUN) \
64 [] (const ::android::Parcel& p, uint8_t /*data*/) {\
65 FUZZ_LOG() << "about to read " #T " using " #FUN " with status";\
66 T t{};\
67 status_t status = p.FUN(&t);\
68 FUZZ_LOG() << #T " status: " << status /* << " value: " << t*/;\
69 }
70
71#define PARCEL_READ_NO_STATUS(T, FUN) \
72 [] (const ::android::Parcel& p, uint8_t /*data*/) {\
73 FUZZ_LOG() << "about to read " #T " using " #FUN " with no status";\
74 T t = p.FUN();\
75 (void) t;\
76 FUZZ_LOG() << #T " done " /* << " value: " << t*/;\
77 }
78
79#define PARCEL_READ_OPT_STATUS(T, FUN) \
80 PARCEL_READ_WITH_STATUS(T, FUN), \
81 PARCEL_READ_NO_STATUS(T, FUN)
82
83std::vector<ParcelRead<::android::Parcel>> BINDER_PARCEL_READ_FUNCTIONS {
84 PARCEL_READ_NO_STATUS(size_t, dataSize),
85 PARCEL_READ_NO_STATUS(size_t, dataAvail),
86 PARCEL_READ_NO_STATUS(size_t, dataPosition),
87 PARCEL_READ_NO_STATUS(size_t, dataCapacity),
88 [] (const ::android::Parcel& p, uint8_t pos) {
89 FUZZ_LOG() << "about to setDataPosition: " << pos;
90 p.setDataPosition(pos);
91 FUZZ_LOG() << "setDataPosition done";
92 },
93 PARCEL_READ_NO_STATUS(size_t, allowFds),
94 PARCEL_READ_NO_STATUS(size_t, hasFileDescriptors),
95 [] (const ::android::Parcel& p, uint8_t len) {
96#ifdef __ANDROID__
97 std::string interface(len, 'a');
98 FUZZ_LOG() << "about to enforceInterface: " << interface;
99 bool b = p.enforceInterface(::android::String16(interface.c_str()));
100 FUZZ_LOG() << "enforced interface: " << b;
101#else
102 FUZZ_LOG() << "skipping enforceInterface";
103 (void)p;
104 (void)len;
105#endif // __ANDROID__
106 },
107 [] (const ::android::Parcel& p, uint8_t /*len*/) {
108#ifdef __ANDROID__
109 FUZZ_LOG() << "about to checkInterface";
110 bool b = p.checkInterface(new android::BBinder());
111 FUZZ_LOG() << "checked interface: " << b;
112#else
113 FUZZ_LOG() << "skipping checkInterface";
114 (void)p;
115#endif // __ANDROID__
116 },
117 PARCEL_READ_NO_STATUS(size_t, objectsCount),
118 PARCEL_READ_NO_STATUS(status_t, errorCheck),
119 [] (const ::android::Parcel& p, uint8_t len) {
120 FUZZ_LOG() << "about to read void*";
121 std::vector<uint8_t> data(len);
122 status_t status = p.read(data.data(), len);
123 FUZZ_LOG() << "read status: " << status;
124 },
125 [] (const ::android::Parcel& p, uint8_t len) {
126 FUZZ_LOG() << "about to readInplace";
127 const void* r = p.readInplace(len);
128 FUZZ_LOG() << "readInplace done. pointer: " << r;
129 },
130 PARCEL_READ_OPT_STATUS(int32_t, readInt32),
131 PARCEL_READ_OPT_STATUS(uint32_t, readUint32),
132 PARCEL_READ_OPT_STATUS(int64_t, readInt64),
133 PARCEL_READ_OPT_STATUS(uint64_t, readUint64),
134 PARCEL_READ_OPT_STATUS(float, readFloat),
135 PARCEL_READ_OPT_STATUS(double, readDouble),
136 PARCEL_READ_OPT_STATUS(intptr_t, readIntPtr),
137 PARCEL_READ_OPT_STATUS(bool, readBool),
138 PARCEL_READ_OPT_STATUS(char16_t, readChar),
139 PARCEL_READ_OPT_STATUS(int8_t, readByte),
140
141 PARCEL_READ_WITH_STATUS(std::string, readUtf8FromUtf16),
142 PARCEL_READ_WITH_STATUS(std::unique_ptr<std::string>, readUtf8FromUtf16),
143 [] (const ::android::Parcel& p, uint8_t /*data*/) {
144 FUZZ_LOG() << "about to read c-str";
145 const char* str = p.readCString();
146 FUZZ_LOG() << "read c-str: " << (str ? str : "<empty string>");
147 },
148 PARCEL_READ_OPT_STATUS(android::String8, readString8),
149 PARCEL_READ_OPT_STATUS(android::String16, readString16),
150 PARCEL_READ_WITH_STATUS(std::unique_ptr<android::String16>, readString16),
Steven Moreland7c7c6482019-09-30 12:32:02 -0700151 [] (const ::android::Parcel& p, uint8_t /*data*/) {
152 FUZZ_LOG() << "about to readString16Inplace";
153 size_t outLen = 0;
154 const char16_t* str = p.readString16Inplace(&outLen);
155 FUZZ_LOG() << "readString16Inplace: " << (str ? "non-null" : "null") << " size: " << outLen;
156 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700157 PARCEL_READ_WITH_STATUS(android::sp<android::IBinder>, readStrongBinder),
158 PARCEL_READ_WITH_STATUS(android::sp<android::IBinder>, readNullableStrongBinder),
159
Steven Moreland7c7c6482019-09-30 12:32:02 -0700160 // only reading one parcelable type for now
161 // TODO(b/131868573): can force read of arbitrarily sized vector
162 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<ExampleParcelable>>>, readParcelableVector),
163 // PARCEL_READ_WITH_STATUS(std::vector<ExampleParcelable>, readParcelableVector),
164 PARCEL_READ_WITH_STATUS(ExampleParcelable, readParcelable),
165 PARCEL_READ_WITH_STATUS(std::unique_ptr<ExampleParcelable>, readParcelable),
166
167 // only reading one binder type for now
168 PARCEL_READ_WITH_STATUS(android::sp<android::os::IServiceManager>, readStrongBinder),
169 PARCEL_READ_WITH_STATUS(android::sp<android::os::IServiceManager>, readNullableStrongBinder),
Steven Moreland46e0da72019-09-05 15:52:02 -0700170
171 // TODO(b/131868573): can force read of arbitrarily sized vector
172 // PARCEL_READ_WITH_STATUS(::std::unique_ptr<std::vector<android::sp<android::IBinder>>>, readStrongBinderVector),
173 // PARCEL_READ_WITH_STATUS(std::vector<android::sp<android::IBinder>>, readStrongBinderVector),
174
175 // TODO(b/131868573): can force read of arbitrarily sized vector
176 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int8_t>>, readByteVector),
177 // PARCEL_READ_WITH_STATUS(std::vector<int8_t>, readByteVector),
178 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<uint8_t>>, readByteVector),
179 // PARCEL_READ_WITH_STATUS(std::vector<uint8_t>, readByteVector),
180 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int32_t>>, readInt32Vector),
181 // PARCEL_READ_WITH_STATUS(std::vector<int32_t>, readInt32Vector),
182 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<int64_t>>, readInt64Vector),
183 // PARCEL_READ_WITH_STATUS(std::vector<int64_t>, readInt64Vector),
184 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<uint64_t>>, readUint64Vector),
185 // PARCEL_READ_WITH_STATUS(std::vector<uint64_t>, readUint64Vector),
186 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<float>>, readFloatVector),
187 // PARCEL_READ_WITH_STATUS(std::vector<float>, readFloatVector),
188 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<double>>, readDoubleVector),
189 // PARCEL_READ_WITH_STATUS(std::vector<double>, readDoubleVector),
190 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<bool>>, readBoolVector),
191 // PARCEL_READ_WITH_STATUS(std::vector<bool>, readBoolVector),
192 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<char16_t>>, readCharVector),
193 // PARCEL_READ_WITH_STATUS(std::vector<char16_t>, readCharVector),
194 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<android::String16>>>, readString16Vector),
195 // PARCEL_READ_WITH_STATUS(std::vector<android::String16>, readString16Vector),
196 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<std::unique_ptr<std::string>>>, readUtf8VectorFromUtf16Vector),
197 // PARCEL_READ_WITH_STATUS(std::vector<std::string>, readUtf8VectorFromUtf16Vector),
198
Steven Moreland6d813932019-09-30 15:31:35 -0700199 [] (const android::Parcel& p, uint8_t /*len*/) {
200 FUZZ_LOG() << "about to read flattenable";
201 ExampleFlattenable f;
202 status_t status = p.read(f);
203 FUZZ_LOG() << "read flattenable: " << status;
204 },
205 [] (const android::Parcel& p, uint8_t /*len*/) {
206 FUZZ_LOG() << "about to read lite flattenable";
207 ExampleLightFlattenable f;
208 status_t status = p.read(f);
209 FUZZ_LOG() << "read lite flattenable: " << status;
210 },
Steven Moreland7c7c6482019-09-30 12:32:02 -0700211
212 // TODO(b/131868573): can force read of arbitrarily sized vector
Steven Moreland46e0da72019-09-05 15:52:02 -0700213 // TODO: resizeOutVector
214
215 PARCEL_READ_NO_STATUS(int32_t, readExceptionCode),
Steven Moreland7c7c6482019-09-30 12:32:02 -0700216 [] (const android::Parcel& p, uint8_t /*len*/) {
217 FUZZ_LOG() << "about to readNativeHandle";
218 native_handle_t* t = p.readNativeHandle();
219 FUZZ_LOG() << "readNativeHandle: " << t;
220 if (t != nullptr) {
221 FUZZ_LOG() << "about to free readNativeHandle";
222 native_handle_close(t);
223 native_handle_delete(t);
224 FUZZ_LOG() << "readNativeHandle freed";
225 }
226 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700227 PARCEL_READ_NO_STATUS(int, readFileDescriptor),
228 PARCEL_READ_NO_STATUS(int, readParcelFileDescriptor),
229 PARCEL_READ_WITH_STATUS(android::base::unique_fd, readUniqueFileDescriptor),
230
231 // TODO(b/131868573): can force read of arbitrarily sized vector
232 // PARCEL_READ_WITH_STATUS(std::unique_ptr<std::vector<android::base::unique_fd>>, readUniqueFileDescriptorVector),
233 // PARCEL_READ_WITH_STATUS(std::vector<android::base::unique_fd>, readUniqueFileDescriptorVector),
234
235 [] (const android::Parcel& p, uint8_t len) {
236 FUZZ_LOG() << "about to readBlob";
237 ::android::Parcel::ReadableBlob blob;
238 status_t status = p.readBlob(len, &blob);
239 FUZZ_LOG() << "readBlob status: " << status;
240 },
Steven Moreland7c7c6482019-09-30 12:32:02 -0700241 [] (const android::Parcel& p, uint8_t options) {
242 FUZZ_LOG() << "about to readObject";
243 bool nullMetaData = options & 0x1;
244 const void* obj = static_cast<const void*>(p.readObject(nullMetaData));
245 FUZZ_LOG() << "readObject: " << obj;
246 },
Steven Moreland46e0da72019-09-05 15:52:02 -0700247 PARCEL_READ_NO_STATUS(uid_t, readCallingWorkSourceUid),
248 PARCEL_READ_NO_STATUS(size_t, getBlobAshmemSize),
249 PARCEL_READ_NO_STATUS(size_t, getOpenAshmemSize),
250};