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 status_t read(void* outData, size_t len) const; 122 const void* readInplace(size_t len) const; 123 status_t readInt8(int8_t *pArg) const; 124 status_t readUint8(uint8_t *pArg) const; 125 status_t readInt16(int16_t *pArg) const; 126 status_t readUint16(uint16_t *pArg) const; 127 int32_t readInt32() const; 128 status_t readInt32(int32_t *pArg) const; 129 uint32_t readUint32() const; 130 status_t readUint32(uint32_t *pArg) const; 131 int64_t readInt64() const; 132 status_t readInt64(int64_t *pArg) const; 133 uint64_t readUint64() const; 134 status_t readUint64(uint64_t *pArg) const; 135 float readFloat() const; 136 status_t readFloat(float *pArg) const; 137 double readDouble() const; 138 status_t readDouble(double *pArg) const; 139 140 bool readBool() const; 141 status_t readBool(bool *pArg) const; 142 const char* readCString() const; 143 String16 readString16() const; 144 status_t readString16(String16* pArg) const; 145 status_t readString16(std::unique_ptr<String16>* pArg) const; 146 const char16_t* readString16Inplace(size_t* outLen) const; 147 sp<IBinder> readStrongBinder() const; 148 status_t readStrongBinder(sp<IBinder>* val) const; 149 status_t readNullableStrongBinder(sp<IBinder>* val) const; 150 151 template<typename T> 152 const T* readObject(size_t *objects_offset = nullptr) const; 153 154 status_t readBuffer(size_t buffer_size, size_t *buffer_handle, 155 const void **buffer_out) const; 156 status_t readNullableBuffer(size_t buffer_size, size_t *buffer_handle, 157 const void **buffer_out) const; 158 status_t readEmbeddedBuffer(size_t buffer_size, size_t *buffer_handle, 159 size_t parent_buffer_handle, size_t parent_offset, 160 const void **buffer_out) const; 161 status_t readNullableEmbeddedBuffer(size_t buffer_size, 162 size_t *buffer_handle, 163 size_t parent_buffer_handle, 164 size_t parent_offset, 165 const void **buffer_out) const; 166 167 status_t readEmbeddedNativeHandle(size_t parent_buffer_handle, 168 size_t parent_offset, const native_handle_t **handle) const; 169 status_t readNullableEmbeddedNativeHandle(size_t parent_buffer_handle, 170 size_t parent_offset, const native_handle_t **handle) const; 171 status_t readNativeHandleNoDup(const native_handle_t **handle) const; 172 status_t readNullableNativeHandleNoDup(const native_handle_t **handle) const; 173 174 // Explicitly close all file descriptors in the parcel. 175 void closeFileDescriptors(); 176 177 // Debugging: get metrics on current allocations. 178 static size_t getGlobalAllocSize(); 179 static size_t getGlobalAllocCount(); 180 181 private: 182 // Below is a cache that records some information about all actual buffers 183 // in this parcel. 184 struct BufferInfo { 185 size_t index; 186 binder_uintptr_t buffer; 187 binder_uintptr_t bufend; // buffer + length 188 }; 189 // value of mObjectSize when mBufCache is last updated. 190 mutable size_t mBufCachePos; 191 mutable std::vector<BufferInfo> mBufCache; 192 // clear mBufCachePos and mBufCache. 193 void clearCache() const; 194 // update mBufCache for all objects between mBufCachePos and mObjectsSize 195 void updateCache() const; 196 197 bool verifyBufferObject(const binder_buffer_object *buffer_obj, 198 size_t size, uint32_t flags, size_t parent, 199 size_t parentOffset) const; 200 201 status_t readBuffer(size_t buffer_size, size_t *buffer_handle, 202 uint32_t flags, size_t parent, size_t parentOffset, 203 const void **buffer_out) const; 204 205 status_t readNullableNativeHandleNoDup(const native_handle_t **handle, 206 bool embedded, 207 size_t parent_buffer_handle = 0, 208 size_t parent_offset = 0) const; 209 public: 210 211 // The following two methods attempt to find if a chunk of memory ("buffer") 212 // is written / read before (by (read|write)(Embedded)?Buffer methods. ) 213 // 1. Call findBuffer if the chunk of memory could be a small part of a larger 214 // buffer written before (for example, an element of a hidl_vec). The 215 // method will also ensure that the end address (ptr + length) is also 216 // within the buffer. 217 // 2. Call quickFindBuffer if the buffer could only be written previously 218 // by itself (for example, the mBuffer field of a hidl_vec). No lengths 219 // are checked. 220 status_t findBuffer(const void *ptr, 221 size_t length, 222 bool *found, 223 size_t *handle, 224 size_t *offset // valid if found 225 ) const; 226 status_t quickFindBuffer(const void *ptr, 227 size_t *handle // valid if found 228 ) const; 229 230 private: 231 bool validateBufferChild(size_t child_buffer_handle, 232 size_t child_offset) const; 233 bool validateBufferParent(size_t parent_buffer_handle, 234 size_t parent_offset) const; 235 236 private: 237 typedef void (*release_func)(Parcel* parcel, 238 const uint8_t* data, size_t dataSize, 239 const binder_size_t* objects, size_t objectsSize, 240 void* cookie); 241 242 uintptr_t ipcData() const; 243 size_t ipcDataSize() const; 244 uintptr_t ipcObjects() const; 245 size_t ipcObjectsCount() const; 246 size_t ipcBufferSize() const; 247 void ipcSetDataReference(const uint8_t* data, size_t dataSize, 248 const binder_size_t* objects, size_t objectsCount, 249 release_func relFunc, void* relCookie); 250 251 public: 252 void print(TextOutput& to, uint32_t flags = 0) const; 253 254 private: 255 Parcel(const Parcel& o); 256 Parcel& operator=(const Parcel& o); 257 258 status_t finishWrite(size_t len); 259 void releaseObjects(); 260 void acquireObjects(); 261 status_t growData(size_t len); 262 status_t restartWrite(size_t desired); 263 status_t continueWrite(size_t desired); 264 status_t writePointer(uintptr_t val); 265 status_t readPointer(uintptr_t *pArg) const; 266 uintptr_t readPointer() const; 267 void freeDataNoInit(); 268 void initState(); 269 void scanForFds() const; 270 271 template<class T> 272 status_t readAligned(T *pArg) const; 273 274 template<class T> T readAligned() const; 275 276 template<class T> 277 status_t writeAligned(T val); 278 279 status_t mError; 280 uint8_t* mData; 281 size_t mDataSize; 282 size_t mDataCapacity; 283 mutable size_t mDataPos; 284 binder_size_t* mObjects; 285 size_t mObjectsSize; 286 size_t mObjectsCapacity; 287 mutable size_t mNextObjectHint; 288 289 [[deprecated]] size_t mNumRef; 290 291 mutable bool mFdsKnown; 292 mutable bool mHasFds; 293 bool mAllowFds; 294 295 release_func mOwner; 296 void* mOwnerCookie; 297 }; 298 // --------------------------------------------------------------------------- 299 300 inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel) 301 { 302 parcel.print(to); 303 return to; 304 } 305 306 // --------------------------------------------------------------------------- 307 308 // Generic acquire and release of objects. 309 void acquire_object(const sp<ProcessState>& proc, 310 const flat_binder_object& obj, const void* who); 311 void release_object(const sp<ProcessState>& proc, 312 const flat_binder_object& obj, const void* who); 313 314 void flatten_binder(const sp<ProcessState>& proc, 315 const sp<IBinder>& binder, flat_binder_object* out); 316 void flatten_binder(const sp<ProcessState>& proc, 317 const wp<IBinder>& binder, flat_binder_object* out); 318 status_t unflatten_binder(const sp<ProcessState>& proc, 319 const flat_binder_object& flat, sp<IBinder>* out); 320 status_t unflatten_binder(const sp<ProcessState>& proc, 321 const flat_binder_object& flat, wp<IBinder>* out); 322 323 } // namespace hardware 324 } // namespace android 325 326 // --------------------------------------------------------------------------- 327 328 #endif // ANDROID_HARDWARE_PARCEL_H 329