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