1 /* 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 17 #ifndef ANDROID_HARDWARE_PARCEL_H 18 #define ANDROID_HARDWARE_PARCEL_H 19 20 #include <string> 21 #include <vector> 22 23 #include <cutils/native_handle.h> 24 #include <utils/Errors.h> 25 #include <utils/RefBase.h> 26 #include <utils/String16.h> 27 28 #include <hwbinder/IInterface.h> 29 30 struct binder_buffer_object; 31 struct flat_binder_object; 32 33 // --------------------------------------------------------------------------- 34 namespace android { 35 namespace hardware { 36 37 #ifdef BINDER_IPC_32BIT 38 typedef unsigned int binder_size_t; 39 typedef unsigned int binder_uintptr_t; 40 #else 41 typedef unsigned long long binder_size_t; 42 typedef unsigned long long binder_uintptr_t; 43 #endif 44 45 class IBinder; 46 class IPCThreadState; 47 class ProcessState; 48 class TextOutput; 49 50 class Parcel { 51 friend class IPCThreadState; 52 public: 53 54 Parcel(); 55 ~Parcel(); 56 57 const uint8_t* data() const; 58 size_t dataSize() const; 59 size_t dataAvail() const; 60 size_t dataPosition() const; 61 size_t dataCapacity() const; 62 63 status_t setDataSize(size_t size); 64 void setDataPosition(size_t pos) const; 65 status_t setDataCapacity(size_t size); 66 67 status_t setData(const uint8_t* buffer, size_t len); 68 69 // Writes the RPC header. 70 status_t writeInterfaceToken(const char* interface); 71 72 // Parses the RPC header, returning true if the interface name 73 // in the header matches the expected interface from the caller. 74 bool enforceInterface(const char* interface) const; 75 76 void freeData(); 77 78 private: 79 const binder_size_t* objects() const; 80 81 public: 82 size_t objectsCount() const; 83 84 status_t errorCheck() const; 85 void setError(status_t err); 86 87 status_t write(const void* data, size_t len); 88 void* writeInplace(size_t len); 89 status_t writeUnpadded(const void* data, size_t len); 90 status_t writeInt8(int8_t val); 91 status_t writeUint8(uint8_t val); 92 status_t writeInt16(int16_t val); 93 status_t writeUint16(uint16_t val); 94 status_t writeInt32(int32_t val); 95 status_t writeUint32(uint32_t val); 96 status_t writeInt64(int64_t val); 97 status_t writeUint64(uint64_t val); 98 status_t writeFloat(float val); 99 status_t writeDouble(double val); 100 status_t writeCString(const char* str); 101 status_t writeString16(const String16& str); 102 status_t writeString16(const std::unique_ptr<String16>& str); 103 status_t writeString16(const char16_t* str, size_t len); 104 status_t writeStrongBinder(const sp<IBinder>& val); 105 status_t writeBool(bool val); 106 107 template<typename T> 108 status_t writeObject(const T& val); 109 110 status_t writeBuffer(const void *buffer, size_t length, size_t *handle); 111 status_t writeEmbeddedBuffer(const void *buffer, size_t length, size_t *handle, 112 size_t parent_buffer_handle, size_t parent_offset); 113 public: 114 status_t writeEmbeddedNativeHandle(const native_handle_t *handle, 115 size_t parent_buffer_handle, size_t parent_offset); 116 status_t writeNativeHandleNoDup(const native_handle* handle, bool embedded, 117 size_t parent_buffer_handle = 0, 118 size_t parent_offset = 0); 119 status_t writeNativeHandleNoDup(const native_handle* handle); 120 121 void remove(size_t start, size_t amt); 122 123 status_t read(void* outData, size_t len) const; 124 const void* readInplace(size_t len) const; 125 status_t readInt8(int8_t *pArg) const; 126 status_t readUint8(uint8_t *pArg) const; 127 status_t readInt16(int16_t *pArg) const; 128 status_t readUint16(uint16_t *pArg) const; 129 int32_t readInt32() const; 130 status_t readInt32(int32_t *pArg) const; 131 uint32_t readUint32() const; 132 status_t readUint32(uint32_t *pArg) const; 133 int64_t readInt64() const; 134 status_t readInt64(int64_t *pArg) const; 135 uint64_t readUint64() const; 136 status_t readUint64(uint64_t *pArg) const; 137 float readFloat() const; 138 status_t readFloat(float *pArg) const; 139 double readDouble() const; 140 status_t readDouble(double *pArg) const; 141 142 bool readBool() const; 143 status_t readBool(bool *pArg) const; 144 const char* readCString() const; 145 String16 readString16() const; 146 status_t readString16(String16* pArg) const; 147 status_t readString16(std::unique_ptr<String16>* pArg) const; 148 const char16_t* readString16Inplace(size_t* outLen) const; 149 sp<IBinder> readStrongBinder() const; 150 status_t readStrongBinder(sp<IBinder>* val) const; 151 status_t readNullableStrongBinder(sp<IBinder>* val) const; 152 153 template<typename T> 154 const T* readObject(size_t *objects_offset = nullptr) const; 155 156 status_t readBuffer(size_t buffer_size, size_t *buffer_handle, 157 const void **buffer_out) const; 158 status_t readNullableBuffer(size_t buffer_size, size_t *buffer_handle, 159 const void **buffer_out) const; 160 status_t readEmbeddedBuffer(size_t buffer_size, size_t *buffer_handle, 161 size_t parent_buffer_handle, size_t parent_offset, 162 const void **buffer_out) const; 163 status_t readNullableEmbeddedBuffer(size_t buffer_size, 164 size_t *buffer_handle, 165 size_t parent_buffer_handle, 166 size_t parent_offset, 167 const void **buffer_out) const; 168 169 status_t readEmbeddedNativeHandle(size_t parent_buffer_handle, 170 size_t parent_offset, const native_handle_t **handle) const; 171 status_t readNullableEmbeddedNativeHandle(size_t parent_buffer_handle, 172 size_t parent_offset, const native_handle_t **handle) const; 173 status_t readNativeHandleNoDup(const native_handle_t **handle) const; 174 status_t readNullableNativeHandleNoDup(const native_handle_t **handle) const; 175 176 // Explicitly close all file descriptors in the parcel. 177 void closeFileDescriptors(); 178 179 // Debugging: get metrics on current allocations. 180 static size_t getGlobalAllocSize(); 181 static size_t getGlobalAllocCount(); 182 183 private: 184 // Below is a cache that records some information about all actual buffers 185 // in this parcel. 186 struct BufferInfo { 187 size_t index; 188 binder_uintptr_t buffer; 189 binder_uintptr_t bufend; // buffer + length 190 }; 191 // value of mObjectSize when mBufCache is last updated. 192 mutable size_t mBufCachePos; 193 mutable std::vector<BufferInfo> mBufCache; 194 // clear mBufCachePos and mBufCache. 195 void clearCache() const; 196 // update mBufCache for all objects between mBufCachePos and mObjectsSize 197 void updateCache() const; 198 199 bool verifyBufferObject(const binder_buffer_object *buffer_obj, 200 size_t size, uint32_t flags, size_t parent, 201 size_t parentOffset) const; 202 203 status_t readBuffer(size_t buffer_size, size_t *buffer_handle, 204 uint32_t flags, size_t parent, size_t parentOffset, 205 const void **buffer_out) const; 206 207 status_t readNullableNativeHandleNoDup(const native_handle_t **handle, 208 bool embedded, 209 size_t parent_buffer_handle = 0, 210 size_t parent_offset = 0) const; 211 public: 212 213 // The following two methods attempt to find if a chunk of memory ("buffer") 214 // is written / read before (by (read|write)(Embedded)?Buffer methods. ) 215 // 1. Call findBuffer if the chunk of memory could be a small part of a larger 216 // buffer written before (for example, an element of a hidl_vec). The 217 // method will also ensure that the end address (ptr + length) is also 218 // within the buffer. 219 // 2. Call quickFindBuffer if the buffer could only be written previously 220 // by itself (for example, the mBuffer field of a hidl_vec). No lengths 221 // are checked. 222 status_t findBuffer(const void *ptr, 223 size_t length, 224 bool *found, 225 size_t *handle, 226 size_t *offset // valid if found 227 ) const; 228 status_t quickFindBuffer(const void *ptr, 229 size_t *handle // valid if found 230 ) const; 231 232 private: 233 bool validateBufferChild(size_t child_buffer_handle, 234 size_t child_offset) const; 235 bool validateBufferParent(size_t parent_buffer_handle, 236 size_t parent_offset) const; 237 238 private: 239 typedef void (*release_func)(Parcel* parcel, 240 const uint8_t* data, size_t dataSize, 241 const binder_size_t* objects, size_t objectsSize, 242 void* cookie); 243 244 uintptr_t ipcData() const; 245 size_t ipcDataSize() const; 246 uintptr_t ipcObjects() const; 247 size_t ipcObjectsCount() const; 248 size_t ipcBufferSize() const; 249 void ipcSetDataReference(const uint8_t* data, size_t dataSize, 250 const binder_size_t* objects, size_t objectsCount, 251 release_func relFunc, void* relCookie); 252 253 public: 254 void print(TextOutput& to, uint32_t flags = 0) const; 255 256 private: 257 Parcel(const Parcel& o); 258 Parcel& operator=(const Parcel& o); 259 260 status_t finishWrite(size_t len); 261 void releaseObjects(); 262 void acquireObjects(); 263 status_t growData(size_t len); 264 status_t restartWrite(size_t desired); 265 status_t continueWrite(size_t desired); 266 status_t writePointer(uintptr_t val); 267 status_t readPointer(uintptr_t *pArg) const; 268 uintptr_t readPointer() const; 269 void freeDataNoInit(); 270 void initState(); 271 void scanForFds() const; 272 273 template<class T> 274 status_t readAligned(T *pArg) const; 275 276 template<class T> T readAligned() const; 277 278 template<class T> 279 status_t writeAligned(T val); 280 281 status_t mError; 282 uint8_t* mData; 283 size_t mDataSize; 284 size_t mDataCapacity; 285 mutable size_t mDataPos; 286 binder_size_t* mObjects; 287 size_t mObjectsSize; 288 size_t mObjectsCapacity; 289 mutable size_t mNextObjectHint; 290 291 [[deprecated]] size_t mNumRef; 292 293 mutable bool mFdsKnown; 294 mutable bool mHasFds; 295 bool mAllowFds; 296 297 release_func mOwner; 298 void* mOwnerCookie; 299 }; 300 // --------------------------------------------------------------------------- 301 302 inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) 303 { 304 parcel.print(to); 305 return to; 306 } 307 308 // --------------------------------------------------------------------------- 309 310 // Generic acquire and release of objects. 311 void acquire_object(const sp<ProcessState>& proc, 312 const flat_binder_object& obj, const void* who); 313 void release_object(const sp<ProcessState>& proc, 314 const flat_binder_object& obj, const void* who); 315 316 void flatten_binder(const sp<ProcessState>& proc, 317 const sp<IBinder>& binder, flat_binder_object* out); 318 void flatten_binder(const sp<ProcessState>& proc, 319 const wp<IBinder>& binder, flat_binder_object* out); 320 status_t unflatten_binder(const sp<ProcessState>& proc, 321 const flat_binder_object& flat, sp<IBinder>* out); 322 status_t unflatten_binder(const sp<ProcessState>& proc, 323 const flat_binder_object& flat, wp<IBinder>* out); 324 325 } // namespace hardware 326 } // namespace android 327 328 // --------------------------------------------------------------------------- 329 330 #endif // ANDROID_HARDWARE_PARCEL_H 331