• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // 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 
34 struct binder_buffer_object;
35 struct flat_binder_object;
36 
37 // ---------------------------------------------------------------------------
38 namespace android {
39 namespace hardware {
40 
41 #ifdef BINDER_IPC_32BIT
42 typedef unsigned int binder_size_t;
43 typedef unsigned int binder_uintptr_t;
44 #else
45 typedef unsigned long long binder_size_t;
46 typedef unsigned long long binder_uintptr_t;
47 #endif
48 
49 class IBinder;
50 class IPCThreadState;
51 class ProcessState;
52 class TextOutput;
53 
54 class Parcel {
55     friend class IPCThreadState;
56 public:
57 
58                         Parcel();
59                         ~Parcel();
60 
61     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;
66 
67     status_t            setDataSize(size_t size);
68     void                setDataPosition(size_t pos) const;
69     status_t            setDataCapacity(size_t size);
70 
71     status_t            setData(const uint8_t* buffer, size_t len);
72 
73     // 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 
80     // Writes the RPC header.
81     status_t            writeInterfaceToken(const char* interface);
82 
83     // Parses the RPC header, returning true if the interface name
84     // in the header matches the expected interface from the caller.
85     bool                enforceInterface(const char* interface) const;
86 
87     void                freeData();
88 
89 private:
90     const binder_size_t* objects() const;
91 
92 public:
93     size_t              objectsCount() const;
94 
95     status_t            errorCheck() const;
96     void                setError(status_t err);
97 
98     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);
101     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);
105     status_t            writeInt32(int32_t val);
106     status_t            writeUint32(uint32_t val);
107     status_t            writeInt64(int64_t val);
108     status_t            writeUint64(uint64_t val);
109     status_t            writeFloat(float val);
110     status_t            writeDouble(double val);
111     status_t            writeCString(const char* str);
112     status_t            writeString16(const String16& str);
113     status_t            writeString16(const std::unique_ptr<String16>& str);
114     status_t            writeString16(const char16_t* str, size_t len);
115     status_t            writeStrongBinder(const sp<IBinder>& val);
116     status_t            writeBool(bool val);
117 
118     template<typename T>
119     status_t            writeObject(const T& val);
120 
121     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,
123                             size_t parent_buffer_handle, size_t parent_offset);
124 public:
125     status_t            writeEmbeddedNativeHandle(const native_handle_t *handle,
126                             size_t parent_buffer_handle, size_t parent_offset);
127     status_t            writeNativeHandleNoDup(const native_handle* handle, bool embedded,
128                                                size_t parent_buffer_handle = 0,
129                                                size_t parent_offset = 0);
130     status_t            writeNativeHandleNoDup(const native_handle* handle);
131 
132     status_t            read(void* outData, size_t len) const;
133     const void*         readInplace(size_t len) const;
134     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;
138     int32_t             readInt32() const;
139     status_t            readInt32(int32_t *pArg) const;
140     uint32_t            readUint32() const;
141     status_t            readUint32(uint32_t *pArg) const;
142     int64_t             readInt64() const;
143     status_t            readInt64(int64_t *pArg) const;
144     uint64_t            readUint64() const;
145     status_t            readUint64(uint64_t *pArg) const;
146     float               readFloat() const;
147     status_t            readFloat(float *pArg) const;
148     double              readDouble() const;
149     status_t            readDouble(double *pArg) const;
150 
151     bool                readBool() const;
152     status_t            readBool(bool *pArg) const;
153     const char*         readCString() const;
154     String16            readString16() const;
155     status_t            readString16(String16* pArg) const;
156     status_t            readString16(std::unique_ptr<String16>* pArg) const;
157     const char16_t*     readString16Inplace(size_t* outLen) const;
158     sp<IBinder>         readStrongBinder() const;
159     status_t            readStrongBinder(sp<IBinder>* val) const;
160     status_t            readNullableStrongBinder(sp<IBinder>* val) const;
161 
162     template<typename T>
163     const T*            readObject(size_t *objects_offset = nullptr) const;
164 
165     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,
170                                            size_t parent_buffer_handle, size_t parent_offset,
171                                            const void **buffer_out) const;
172     status_t            readNullableEmbeddedBuffer(size_t buffer_size,
173                                                    size_t *buffer_handle,
174                                                    size_t parent_buffer_handle,
175                                                    size_t parent_offset,
176                                                    const void **buffer_out) const;
177 
178     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 
185     // Explicitly close all file descriptors in the parcel.
186     void                closeFileDescriptors();
187 
188     // Debugging: get metrics on current allocations.
189     static size_t       getGlobalAllocSize();
190     static size_t       getGlobalAllocCount();
191 
192 private:
193     // 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;
207 
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;
220 public:
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;
240 
241 private:
242     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 
247 private:
248     typedef void        (*release_func)(Parcel* parcel,
249                                         const uint8_t* data, size_t dataSize,
250                                         const binder_size_t* objects, size_t objectsSize,
251                                         void* cookie);
252 
253     uintptr_t           ipcData() const;
254     size_t              ipcDataSize() const;
255     uintptr_t           ipcObjects() const;
256     size_t              ipcObjectsCount() const;
257     size_t              ipcBufferSize() const;
258     void                ipcSetDataReference(const uint8_t* data, size_t dataSize,
259                                             const binder_size_t* objects, size_t objectsCount,
260                                             release_func relFunc, void* relCookie);
261 
262 public:
263     void                print(TextOutput& to, uint32_t flags = 0) const;
264 
265 private:
266                         Parcel(const Parcel& o);
267     Parcel&             operator=(const Parcel& o);
268 
269     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);
275     status_t            writePointer(uintptr_t val);
276     status_t            readPointer(uintptr_t *pArg) const;
277     uintptr_t           readPointer() const;
278     void                freeDataNoInit();
279     void                initState();
280     void                scanForFds() const;
281 
282     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 
290     status_t            mError;
291     uint8_t*            mData;
292     size_t              mDataSize;
293     size_t              mDataCapacity;
294     mutable size_t      mDataPos;
295     binder_size_t*      mObjects;
296     size_t              mObjectsSize;
297     size_t              mObjectsCapacity;
298     mutable size_t      mNextObjectHint;
299 
300     [[deprecated]] size_t mNumRef;
301 
302     mutable bool        mFdsKnown;
303     mutable bool        mHasFds;
304     bool                mAllowFds;
305 
306     // 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 
310     release_func        mOwner;
311     void*               mOwnerCookie;
312 };
313 // ---------------------------------------------------------------------------
314 
315 inline 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.
324 void acquire_object(const sp<ProcessState>& proc,
325                     const flat_binder_object& obj, const void* who);
326 void release_object(const sp<ProcessState>& proc,
327                     const flat_binder_object& obj, const void* who);
328 
329 void flatten_binder(const sp<ProcessState>& proc,
330                     const sp<IBinder>& binder, flat_binder_object* out);
331 void flatten_binder(const sp<ProcessState>& proc,
332                     const wp<IBinder>& binder, flat_binder_object* out);
333 status_t unflatten_binder(const sp<ProcessState>& proc,
334                           const flat_binder_object& flat, sp<IBinder>* out);
335 status_t unflatten_binder(const sp<ProcessState>& proc,
336                           const flat_binder_object& flat, wp<IBinder>* out);
337 
338 } // namespace hardware
339 } // namespace android
340 
341 // ---------------------------------------------------------------------------
342 
343 #endif // ANDROID_HARDWARE_PARCEL_H
344