blob: 4822fa09aa2179d199847c4d106b0f0cfce48325 [file] [log] [blame]
Mathias Agopian16475702009-05-19 19:08:10 -07001/*
2 * Copyright (C) 2005 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
Martijn Coenenf75a23d2016-08-01 11:55:17 +020017#ifndef ANDROID_HARDWARE_PARCEL_H
18#define ANDROID_HARDWARE_PARCEL_H
Mathias Agopian16475702009-05-19 19:08:10 -070019
Christopher Wileyf3cc26d2016-01-28 16:56:53 -080020#include <string>
Casey Dahlind0f31e52015-10-19 18:12:18 -070021#include <vector>
22
Mathias Agopian16475702009-05-19 19:08:10 -070023#include <cutils/native_handle.h>
24#include <utils/Errors.h>
25#include <utils/RefBase.h>
26#include <utils/String16.h>
Martijn Coenenfd51ebb2016-07-05 17:00:39 +020027
Martijn Coenen4080edc2016-05-04 14:17:02 +020028#include <hwbinder/IInterface.h>
Casey Dahlin375de4a2015-10-27 18:33:56 -070029
Steven Morelandb5f6e002021-02-26 01:51:20 +000030// WARNING: this code is part of libhwbinder, a fork of libbinder. Generally,
31// this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific
32// code should not try to use these things.
33
Martijn Coenen455e27d2017-04-18 15:53:51 -070034struct binder_buffer_object;
Steven Moreland48adadd2019-09-05 17:04:38 -070035struct flat_binder_object;
Martijn Coenen455e27d2017-04-18 15:53:51 -070036
Mathias Agopian16475702009-05-19 19:08:10 -070037// ---------------------------------------------------------------------------
38namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020039namespace hardware {
Mathias Agopian16475702009-05-19 19:08:10 -070040
Steven Moreland48adadd2019-09-05 17:04:38 -070041#ifdef BINDER_IPC_32BIT
Steven Morelandd9bdb652019-09-17 15:42:45 -070042typedef unsigned int binder_size_t;
43typedef unsigned int binder_uintptr_t;
Steven Moreland48adadd2019-09-05 17:04:38 -070044#else
Steven Morelandd9bdb652019-09-17 15:42:45 -070045typedef unsigned long long binder_size_t;
46typedef unsigned long long binder_uintptr_t;
Steven Moreland48adadd2019-09-05 17:04:38 -070047#endif
48
Mathias Agopian16475702009-05-19 19:08:10 -070049class IBinder;
Brad Fitzpatrickc1f5bf82010-07-27 09:49:11 -070050class IPCThreadState;
Mathias Agopian16475702009-05-19 19:08:10 -070051class ProcessState;
Mathias Agopian16475702009-05-19 19:08:10 -070052class TextOutput;
53
Mathias Agopian0d8e2e42013-07-29 21:24:40 -070054class Parcel {
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +000055 friend class IPCThreadState;
Mathias Agopian16475702009-05-19 19:08:10 -070056public:
Jeff Brown8a11ee52011-09-23 21:17:56 -070057
Mathias Agopian16475702009-05-19 19:08:10 -070058 Parcel();
59 ~Parcel();
Yifan Hong38d41242016-08-29 12:51:23 -070060
Mathias Agopian16475702009-05-19 19:08:10 -070061 const uint8_t* data() const;
62 size_t dataSize() const;
63 size_t dataAvail() const;
64 size_t dataPosition() const;
65 size_t dataCapacity() const;
Dianne Hackbornabc3cf42011-09-28 23:19:47 -040066
Mathias Agopian16475702009-05-19 19:08:10 -070067 status_t setDataSize(size_t size);
68 void setDataPosition(size_t pos) const;
69 status_t setDataCapacity(size_t size);
Yifan Hong38d41242016-08-29 12:51:23 -070070
Mathias Agopian16475702009-05-19 19:08:10 -070071 status_t setData(const uint8_t* buffer, size_t len);
72
Steven Morelanda80270c2020-11-19 21:08:28 +000073 // Zeros data when reallocating. Other mitigations may be added
74 // in the future.
75 //
76 // WARNING: some read methods may make additional copies of data.
77 // In order to verify this, heap dumps should be used.
78 void markSensitive() const;
79
Brad Fitzpatrick94c36342010-06-18 13:07:53 -070080 // Writes the RPC header.
Steven Moreland334305b2016-11-11 12:31:31 -080081 status_t writeInterfaceToken(const char* interface);
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -070082
Brad Fitzpatrick94c36342010-06-18 13:07:53 -070083 // Parses the RPC header, returning true if the interface name
84 // in the header matches the expected interface from the caller.
Martijn Coenen5c6fe252017-01-18 15:33:31 +010085 bool enforceInterface(const char* interface) const;
Mathias Agopiana6286c32009-05-22 19:00:22 -070086
Mathias Agopian16475702009-05-19 19:08:10 -070087 void freeData();
88
Arve Hjønnevåga5440702014-01-28 20:12:59 -080089private:
90 const binder_size_t* objects() const;
91
92public:
Mathias Agopian16475702009-05-19 19:08:10 -070093 size_t objectsCount() const;
Yifan Hong38d41242016-08-29 12:51:23 -070094
Mathias Agopian16475702009-05-19 19:08:10 -070095 status_t errorCheck() const;
96 void setError(status_t err);
Yifan Hong38d41242016-08-29 12:51:23 -070097
Mathias Agopian16475702009-05-19 19:08:10 -070098 status_t write(const void* data, size_t len);
99 void* writeInplace(size_t len);
100 status_t writeUnpadded(const void* data, size_t len);
Andreas Huber85af3212016-08-05 14:30:52 -0700101 status_t writeInt8(int8_t val);
102 status_t writeUint8(uint8_t val);
103 status_t writeInt16(int16_t val);
104 status_t writeUint16(uint16_t val);
Mathias Agopian16475702009-05-19 19:08:10 -0700105 status_t writeInt32(int32_t val);
Dan Stozaff4c1b72014-12-01 10:01:10 -0800106 status_t writeUint32(uint32_t val);
Mathias Agopian16475702009-05-19 19:08:10 -0700107 status_t writeInt64(int64_t val);
Ronghua Wu5c2eeca2015-03-16 11:11:07 -0700108 status_t writeUint64(uint64_t val);
Mathias Agopian16475702009-05-19 19:08:10 -0700109 status_t writeFloat(float val);
110 status_t writeDouble(double val);
111 status_t writeCString(const char* str);
Mathias Agopian16475702009-05-19 19:08:10 -0700112 status_t writeString16(const String16& str);
Casey Dahlin00c40682015-11-25 15:09:45 -0800113 status_t writeString16(const std::unique_ptr<String16>& str);
Mathias Agopian16475702009-05-19 19:08:10 -0700114 status_t writeString16(const char16_t* str, size_t len);
115 status_t writeStrongBinder(const sp<IBinder>& val);
Casey Dahlin458afaf2015-10-15 15:44:59 -0700116 status_t writeBool(bool val);
Jeff Brown8a11ee52011-09-23 21:17:56 -0700117
Martijn Coenen249b02b2016-07-28 16:25:10 +0200118 template<typename T>
Martijn Coenen9d679d62017-01-10 16:44:24 +0100119 status_t writeObject(const T& val);
Mathias Agopian16475702009-05-19 19:08:10 -0700120
Andreas Huber85af3212016-08-05 14:30:52 -0700121 status_t writeBuffer(const void *buffer, size_t length, size_t *handle);
122 status_t writeEmbeddedBuffer(const void *buffer, size_t length, size_t *handle,
Martijn Coenen249b02b2016-07-28 16:25:10 +0200123 size_t parent_buffer_handle, size_t parent_offset);
Yifan Hong517105d2016-10-27 13:15:22 -0700124public:
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200125 status_t writeEmbeddedNativeHandle(const native_handle_t *handle,
Martijn Coenen249b02b2016-07-28 16:25:10 +0200126 size_t parent_buffer_handle, size_t parent_offset);
Martijn Coenen455e27d2017-04-18 15:53:51 -0700127 status_t writeNativeHandleNoDup(const native_handle* handle, bool embedded,
128 size_t parent_buffer_handle = 0,
129 size_t parent_offset = 0);
Martijn Coenenfd51ebb2016-07-05 17:00:39 +0200130 status_t writeNativeHandleNoDup(const native_handle* handle);
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -0700131
Mathias Agopian16475702009-05-19 19:08:10 -0700132 status_t read(void* outData, size_t len) const;
133 const void* readInplace(size_t len) const;
Andreas Huber85af3212016-08-05 14:30:52 -0700134 status_t readInt8(int8_t *pArg) const;
135 status_t readUint8(uint8_t *pArg) const;
136 status_t readInt16(int16_t *pArg) const;
137 status_t readUint16(uint16_t *pArg) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700138 int32_t readInt32() const;
139 status_t readInt32(int32_t *pArg) const;
Dan Stozaff4c1b72014-12-01 10:01:10 -0800140 uint32_t readUint32() const;
141 status_t readUint32(uint32_t *pArg) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700142 int64_t readInt64() const;
143 status_t readInt64(int64_t *pArg) const;
Ronghua Wu5c2eeca2015-03-16 11:11:07 -0700144 uint64_t readUint64() const;
145 status_t readUint64(uint64_t *pArg) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700146 float readFloat() const;
147 status_t readFloat(float *pArg) const;
148 double readDouble() const;
149 status_t readDouble(double *pArg) const;
Martijn Coenen5c6fe252017-01-18 15:33:31 +0100150
Casey Dahlin458afaf2015-10-15 15:44:59 -0700151 bool readBool() const;
152 status_t readBool(bool *pArg) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700153 const char* readCString() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700154 String16 readString16() const;
Casey Dahlind0f31e52015-10-19 18:12:18 -0700155 status_t readString16(String16* pArg) const;
Casey Dahlin00c40682015-11-25 15:09:45 -0800156 status_t readString16(std::unique_ptr<String16>* pArg) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700157 const char16_t* readString16Inplace(size_t* outLen) const;
158 sp<IBinder> readStrongBinder() const;
Casey Dahlin375de4a2015-10-27 18:33:56 -0700159 status_t readStrongBinder(sp<IBinder>* val) const;
Christopher Wiley32fc37f2016-03-08 10:49:51 -0800160 status_t readNullableStrongBinder(sp<IBinder>* val) const;
Mathias Agopian0d8e2e42013-07-29 21:24:40 -0700161
Martijn Coenen249b02b2016-07-28 16:25:10 +0200162 template<typename T>
Martijn Coenen455e27d2017-04-18 15:53:51 -0700163 const T* readObject(size_t *objects_offset = nullptr) const;
Mathias Agopian16475702009-05-19 19:08:10 -0700164
Martijn Coenen455e27d2017-04-18 15:53:51 -0700165 status_t readBuffer(size_t buffer_size, size_t *buffer_handle,
166 const void **buffer_out) const;
167 status_t readNullableBuffer(size_t buffer_size, size_t *buffer_handle,
168 const void **buffer_out) const;
169 status_t readEmbeddedBuffer(size_t buffer_size, size_t *buffer_handle,
Martijn Coenen9d679d62017-01-10 16:44:24 +0100170 size_t parent_buffer_handle, size_t parent_offset,
171 const void **buffer_out) const;
Martijn Coenen455e27d2017-04-18 15:53:51 -0700172 status_t readNullableEmbeddedBuffer(size_t buffer_size,
173 size_t *buffer_handle,
Martijn Coenen9d679d62017-01-10 16:44:24 +0100174 size_t parent_buffer_handle,
175 size_t parent_offset,
176 const void **buffer_out) const;
177
Martijn Coenen9d679d62017-01-10 16:44:24 +0100178 status_t readEmbeddedNativeHandle(size_t parent_buffer_handle,
179 size_t parent_offset, const native_handle_t **handle) const;
180 status_t readNullableEmbeddedNativeHandle(size_t parent_buffer_handle,
181 size_t parent_offset, const native_handle_t **handle) const;
182 status_t readNativeHandleNoDup(const native_handle_t **handle) const;
183 status_t readNullableNativeHandleNoDup(const native_handle_t **handle) const;
184
Mathias Agopian16475702009-05-19 19:08:10 -0700185 // Explicitly close all file descriptors in the parcel.
186 void closeFileDescriptors();
Dianne Hackborn492acff2014-11-11 12:22:53 -0800187
188 // Debugging: get metrics on current allocations.
189 static size_t getGlobalAllocSize();
190 static size_t getGlobalAllocCount();
191
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800192private:
Yifan Hong38d41242016-08-29 12:51:23 -0700193 // Below is a cache that records some information about all actual buffers
194 // in this parcel.
195 struct BufferInfo {
196 size_t index;
197 binder_uintptr_t buffer;
198 binder_uintptr_t bufend; // buffer + length
199 };
200 // value of mObjectSize when mBufCache is last updated.
201 mutable size_t mBufCachePos;
202 mutable std::vector<BufferInfo> mBufCache;
203 // clear mBufCachePos and mBufCache.
204 void clearCache() const;
205 // update mBufCache for all objects between mBufCachePos and mObjectsSize
206 void updateCache() const;
Martijn Coenen455e27d2017-04-18 15:53:51 -0700207
208 bool verifyBufferObject(const binder_buffer_object *buffer_obj,
209 size_t size, uint32_t flags, size_t parent,
210 size_t parentOffset) const;
211
212 status_t readBuffer(size_t buffer_size, size_t *buffer_handle,
213 uint32_t flags, size_t parent, size_t parentOffset,
214 const void **buffer_out) const;
215
216 status_t readNullableNativeHandleNoDup(const native_handle_t **handle,
217 bool embedded,
218 size_t parent_buffer_handle = 0,
219 size_t parent_offset = 0) const;
Yifan Hong38d41242016-08-29 12:51:23 -0700220public:
221
222 // The following two methods attempt to find if a chunk of memory ("buffer")
223 // is written / read before (by (read|write)(Embedded)?Buffer methods. )
224 // 1. Call findBuffer if the chunk of memory could be a small part of a larger
225 // buffer written before (for example, an element of a hidl_vec). The
226 // method will also ensure that the end address (ptr + length) is also
227 // within the buffer.
228 // 2. Call quickFindBuffer if the buffer could only be written previously
229 // by itself (for example, the mBuffer field of a hidl_vec). No lengths
230 // are checked.
231 status_t findBuffer(const void *ptr,
232 size_t length,
233 bool *found,
234 size_t *handle,
235 size_t *offset // valid if found
236 ) const;
237 status_t quickFindBuffer(const void *ptr,
238 size_t *handle // valid if found
239 ) const;
Yifan Hong1a3127d2016-09-27 15:46:52 -0700240
241private:
Yifan Hong38d41242016-08-29 12:51:23 -0700242 bool validateBufferChild(size_t child_buffer_handle,
243 size_t child_offset) const;
244 bool validateBufferParent(size_t parent_buffer_handle,
245 size_t parent_offset) const;
246
247private:
Mathias Agopian16475702009-05-19 19:08:10 -0700248 typedef void (*release_func)(Parcel* parcel,
249 const uint8_t* data, size_t dataSize,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800250 const binder_size_t* objects, size_t objectsSize,
Mathias Agopian16475702009-05-19 19:08:10 -0700251 void* cookie);
Yifan Hong38d41242016-08-29 12:51:23 -0700252
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800253 uintptr_t ipcData() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700254 size_t ipcDataSize() const;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800255 uintptr_t ipcObjects() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700256 size_t ipcObjectsCount() const;
Martijn Coenend39a1682016-06-03 21:27:28 +0200257 size_t ipcBufferSize() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700258 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800259 const binder_size_t* objects, size_t objectsCount,
Mathias Agopian16475702009-05-19 19:08:10 -0700260 release_func relFunc, void* relCookie);
Yifan Hong38d41242016-08-29 12:51:23 -0700261
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800262public:
Mathias Agopian16475702009-05-19 19:08:10 -0700263 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown8a11ee52011-09-23 21:17:56 -0700264
Mathias Agopian16475702009-05-19 19:08:10 -0700265private:
266 Parcel(const Parcel& o);
267 Parcel& operator=(const Parcel& o);
Yifan Hong38d41242016-08-29 12:51:23 -0700268
Mathias Agopian16475702009-05-19 19:08:10 -0700269 status_t finishWrite(size_t len);
270 void releaseObjects();
271 void acquireObjects();
272 status_t growData(size_t len);
273 status_t restartWrite(size_t desired);
274 status_t continueWrite(size_t desired);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000275 status_t writePointer(uintptr_t val);
276 status_t readPointer(uintptr_t *pArg) const;
277 uintptr_t readPointer() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700278 void freeDataNoInit();
279 void initState();
280 void scanForFds() const;
Yifan Hong38d41242016-08-29 12:51:23 -0700281
Andreas Huberf79b77e2009-08-17 13:33:27 -0700282 template<class T>
283 status_t readAligned(T *pArg) const;
284
285 template<class T> T readAligned() const;
286
287 template<class T>
288 status_t writeAligned(T val);
289
Mathias Agopian16475702009-05-19 19:08:10 -0700290 status_t mError;
291 uint8_t* mData;
292 size_t mDataSize;
293 size_t mDataCapacity;
294 mutable size_t mDataPos;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800295 binder_size_t* mObjects;
Mathias Agopian16475702009-05-19 19:08:10 -0700296 size_t mObjectsSize;
297 size_t mObjectsCapacity;
298 mutable size_t mNextObjectHint;
Steven Morelandd6ec2e82019-05-28 17:28:50 -0700299
300 [[deprecated]] size_t mNumRef;
Mathias Agopian16475702009-05-19 19:08:10 -0700301
302 mutable bool mFdsKnown;
303 mutable bool mHasFds;
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400304 bool mAllowFds;
Christopher Tate8d245682016-03-24 16:03:44 -0700305
Steven Morelanda80270c2020-11-19 21:08:28 +0000306 // if this parcelable is involved in a secure transaction, force the
307 // data to be overridden with zero when deallocated
308 mutable bool mDeallocZero;
309
Mathias Agopian16475702009-05-19 19:08:10 -0700310 release_func mOwner;
311 void* mOwnerCookie;
Mathias Agopian16475702009-05-19 19:08:10 -0700312};
Mathias Agopian0a84ac82012-08-12 19:37:16 -0700313// ---------------------------------------------------------------------------
314
Mathias Agopian16475702009-05-19 19:08:10 -0700315inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
316{
317 parcel.print(to);
318 return to;
319}
320
321// ---------------------------------------------------------------------------
322
323// Generic acquire and release of objects.
324void acquire_object(const sp<ProcessState>& proc,
Ian Pedowitz2a3fccc2015-10-22 22:08:10 +0000325 const flat_binder_object& obj, const void* who);
Mathias Agopian16475702009-05-19 19:08:10 -0700326void release_object(const sp<ProcessState>& proc,
Ian Pedowitz2a3fccc2015-10-22 22:08:10 +0000327 const flat_binder_object& obj, const void* who);
Mathias Agopian16475702009-05-19 19:08:10 -0700328
329void flatten_binder(const sp<ProcessState>& proc,
330 const sp<IBinder>& binder, flat_binder_object* out);
331void flatten_binder(const sp<ProcessState>& proc,
332 const wp<IBinder>& binder, flat_binder_object* out);
333status_t unflatten_binder(const sp<ProcessState>& proc,
334 const flat_binder_object& flat, sp<IBinder>* out);
335status_t unflatten_binder(const sp<ProcessState>& proc,
336 const flat_binder_object& flat, wp<IBinder>* out);
337
Steven Moreland7173a4c2019-09-26 15:55:02 -0700338} // namespace hardware
339} // namespace android
Mathias Agopian16475702009-05-19 19:08:10 -0700340
341// ---------------------------------------------------------------------------
342
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200343#endif // ANDROID_HARDWARE_PARCEL_H