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