blob: 0ba3abebd4ba3c88c91fd68b3aa07f514aea848a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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_PARCEL_H
18#define ANDROID_PARCEL_H
19
20#include <cutils/native_handle.h>
21#include <utils/Errors.h>
22#include <utils/RefBase.h>
23#include <utils/String16.h>
24#include <utils/Vector.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070025#include <utils/Flattenable.h>
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080026#include <linux/binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28// ---------------------------------------------------------------------------
29namespace android {
30
Mathias Agopiane1424282013-07-29 21:24:40 -070031template <typename T> class Flattenable;
Mathias Agopian8683fca2012-08-12 19:37:16 -070032template <typename T> class LightFlattenable;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033class IBinder;
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070034class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035class ProcessState;
36class String8;
37class TextOutput;
38
Mathias Agopiane1424282013-07-29 21:24:40 -070039class Parcel {
Serban Constantinescuf683e012013-11-05 16:53:55 +000040 friend class IPCThreadState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041public:
Jeff Brown5707dbf2011-09-23 21:17:56 -070042 class ReadableBlob;
43 class WritableBlob;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 Parcel();
46 ~Parcel();
47
48 const uint8_t* data() const;
49 size_t dataSize() const;
50 size_t dataAvail() const;
51 size_t dataPosition() const;
52 size_t dataCapacity() const;
Dianne Hackborn8938ed22011-09-28 23:19:47 -040053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 status_t setDataSize(size_t size);
55 void setDataPosition(size_t pos) const;
56 status_t setDataCapacity(size_t size);
57
58 status_t setData(const uint8_t* buffer, size_t len);
59
Andreas Huber51faf462011-04-13 10:21:56 -070060 status_t appendFrom(const Parcel *parcel,
61 size_t start, size_t len);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Dianne Hackborn7746cc32011-10-03 21:09:35 -070063 bool pushAllowFds(bool allowFds);
64 void restoreAllowFds(bool lastValue);
Dianne Hackborn8938ed22011-09-28 23:19:47 -040065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 bool hasFileDescriptors() const;
67
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070068 // Writes the RPC header.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 status_t writeInterfaceToken(const String16& interface);
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070071 // Parses the RPC header, returning true if the interface name
72 // in the header matches the expected interface from the caller.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070073 //
74 // Additionally, enforceInterface does part of the work of
75 // propagating the StrictMode policy mask, populating the current
76 // IPCThreadState, which as an optimization may optionally be
77 // passed in.
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078 bool enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -070079 IPCThreadState* threadState = NULL) const;
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070080 bool checkInterface(IBinder*) const;
Mathias Agopian83c04462009-05-22 19:00:22 -070081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 void freeData();
83
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -080084private:
85 const binder_size_t* objects() const;
86
87public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 size_t objectsCount() const;
89
90 status_t errorCheck() const;
91 void setError(status_t err);
92
93 status_t write(const void* data, size_t len);
94 void* writeInplace(size_t len);
95 status_t writeUnpadded(const void* data, size_t len);
96 status_t writeInt32(int32_t val);
Dan Stoza41a0f2f2014-12-01 10:01:10 -080097 status_t writeUint32(uint32_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 status_t writeInt64(int64_t val);
Ronghua Wu2d13afd2015-03-16 11:11:07 -070099 status_t writeUint64(uint64_t val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 status_t writeFloat(float val);
101 status_t writeDouble(double val);
102 status_t writeCString(const char* str);
103 status_t writeString8(const String8& str);
104 status_t writeString16(const String16& str);
105 status_t writeString16(const char16_t* str, size_t len);
106 status_t writeStrongBinder(const sp<IBinder>& val);
107 status_t writeWeakBinder(const wp<IBinder>& val);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700108 status_t writeInt32Array(size_t len, const int32_t *val);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700109 status_t writeByteArray(size_t len, const uint8_t *val);
Mathias Agopiane1424282013-07-29 21:24:40 -0700110
111 template<typename T>
112 status_t write(const Flattenable<T>& val);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113
Mathias Agopian8683fca2012-08-12 19:37:16 -0700114 template<typename T>
115 status_t write(const LightFlattenable<T>& val);
116
117
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700118 // Place a native_handle into the parcel (the native_handle's file-
119 // descriptors are dup'ed, so it is safe to delete the native_handle
120 // when this function returns).
121 // Doesn't take ownership of the native_handle.
122 status_t writeNativeHandle(const native_handle* handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
124 // Place a file descriptor into the parcel. The given fd must remain
125 // valid for the lifetime of the parcel.
Jeff Brown93ff1f92011-11-04 19:01:44 -0700126 // The Parcel does not take ownership of the given fd unless you ask it to.
127 status_t writeFileDescriptor(int fd, bool takeOwnership = false);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128
129 // Place a file descriptor into the parcel. A dup of the fd is made, which
130 // will be closed once the parcel is destroyed.
131 status_t writeDupFileDescriptor(int fd);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700132
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700133 // Writes a raw fd and optional comm channel fd to the parcel as a ParcelFileDescriptor.
134 // A dup's of the fds are made, which will be closed once the parcel is destroyed.
135 // Null values are passed as -1.
136 status_t writeParcelFileDescriptor(int fd, int commChannel = -1);
137
Jeff Brown5707dbf2011-09-23 21:17:56 -0700138 // Writes a blob to the parcel.
139 // If the blob is small, then it is stored in-place, otherwise it is
140 // transferred by way of an anonymous shared memory region.
141 // The caller should call release() on the blob after writing its contents.
142 status_t writeBlob(size_t len, WritableBlob* outBlob);
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 status_t writeObject(const flat_binder_object& val, bool nullMetaData);
145
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700146 // Like Parcel.java's writeNoException(). Just writes a zero int32.
147 // Currently the native implementation doesn't do any of the StrictMode
148 // stack gathering and serialization that the Java implementation does.
149 status_t writeNoException();
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 void remove(size_t start, size_t amt);
152
153 status_t read(void* outData, size_t len) const;
154 const void* readInplace(size_t len) const;
155 int32_t readInt32() const;
156 status_t readInt32(int32_t *pArg) const;
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800157 uint32_t readUint32() const;
158 status_t readUint32(uint32_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 int64_t readInt64() const;
160 status_t readInt64(int64_t *pArg) const;
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700161 uint64_t readUint64() const;
162 status_t readUint64(uint64_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 float readFloat() const;
164 status_t readFloat(float *pArg) const;
165 double readDouble() const;
166 status_t readDouble(double *pArg) const;
Andreas Huber84a6d042009-08-17 13:33:27 -0700167 intptr_t readIntPtr() const;
168 status_t readIntPtr(intptr_t *pArg) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
170 const char* readCString() const;
171 String8 readString8() const;
172 String16 readString16() const;
173 const char16_t* readString16Inplace(size_t* outLen) const;
174 sp<IBinder> readStrongBinder() const;
175 wp<IBinder> readWeakBinder() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700176
177 template<typename T>
178 status_t read(Flattenable<T>& val) const;
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700179
Mathias Agopian8683fca2012-08-12 19:37:16 -0700180 template<typename T>
181 status_t read(LightFlattenable<T>& val) const;
182
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700183 // Like Parcel.java's readExceptionCode(). Reads the first int32
184 // off of a Parcel's header, returning 0 or the negative error
185 // code on exceptions, but also deals with skipping over rich
186 // response headers. Callers should use this to read & parse the
187 // response headers rather than doing it by hand.
188 int32_t readExceptionCode() const;
189
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700190 // Retrieve native_handle from the parcel. This returns a copy of the
191 // parcel's native_handle (the caller takes ownership). The caller
192 // must free the native_handle with native_handle_close() and
193 // native_handle_delete().
194 native_handle* readNativeHandle() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195
196
197 // Retrieve a file descriptor from the parcel. This returns the raw fd
198 // in the parcel, which you do not own -- use dup() to get your own copy.
199 int readFileDescriptor() const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700200
Mike Lockwoodcbe36fe2013-09-05 08:41:06 -0700201 // Reads a ParcelFileDescriptor from the parcel. Returns the raw fd as
202 // the result, and the optional comm channel fd in outCommChannel.
203 // Null values are returned as -1.
204 int readParcelFileDescriptor(int& outCommChannel) const;
205
Jeff Brown5707dbf2011-09-23 21:17:56 -0700206 // Reads a blob from the parcel.
207 // The caller should call release() on the blob after reading its contents.
208 status_t readBlob(size_t len, ReadableBlob* outBlob) const;
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 const flat_binder_object* readObject(bool nullMetaData) const;
211
212 // Explicitly close all file descriptors in the parcel.
213 void closeFileDescriptors();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800214
215 // Debugging: get metrics on current allocations.
216 static size_t getGlobalAllocSize();
217 static size_t getGlobalAllocCount();
218
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800219private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 typedef void (*release_func)(Parcel* parcel,
221 const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 const binder_size_t* objects, size_t objectsSize,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 void* cookie);
224
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 uintptr_t ipcData() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 size_t ipcDataSize() const;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800227 uintptr_t ipcObjects() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 size_t ipcObjectsCount() const;
229 void ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 const binder_size_t* objects, size_t objectsCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 release_func relFunc, void* relCookie);
232
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 void print(TextOutput& to, uint32_t flags = 0) const;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236private:
237 Parcel(const Parcel& o);
238 Parcel& operator=(const Parcel& o);
239
240 status_t finishWrite(size_t len);
241 void releaseObjects();
242 void acquireObjects();
243 status_t growData(size_t len);
244 status_t restartWrite(size_t desired);
245 status_t continueWrite(size_t desired);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000246 status_t writePointer(uintptr_t val);
247 status_t readPointer(uintptr_t *pArg) const;
248 uintptr_t readPointer() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 void freeDataNoInit();
250 void initState();
251 void scanForFds() const;
252
Andreas Huber84a6d042009-08-17 13:33:27 -0700253 template<class T>
254 status_t readAligned(T *pArg) const;
255
256 template<class T> T readAligned() const;
257
258 template<class T>
259 status_t writeAligned(T val);
260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 status_t mError;
262 uint8_t* mData;
263 size_t mDataSize;
264 size_t mDataCapacity;
265 mutable size_t mDataPos;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800266 binder_size_t* mObjects;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 size_t mObjectsSize;
268 size_t mObjectsCapacity;
269 mutable size_t mNextObjectHint;
270
271 mutable bool mFdsKnown;
272 mutable bool mHasFds;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400273 bool mAllowFds;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
275 release_func mOwner;
276 void* mOwnerCookie;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700277
278 class Blob {
279 public:
280 Blob();
281 ~Blob();
282
283 void release();
284 inline size_t size() const { return mSize; }
285
286 protected:
287 void init(bool mapped, void* data, size_t size);
288 void clear();
289
290 bool mMapped;
291 void* mData;
292 size_t mSize;
293 };
294
Mathias Agopiane1424282013-07-29 21:24:40 -0700295 class FlattenableHelperInterface {
296 protected:
297 ~FlattenableHelperInterface() { }
298 public:
299 virtual size_t getFlattenedSize() const = 0;
300 virtual size_t getFdCount() const = 0;
301 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const = 0;
302 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) = 0;
303 };
304
305 template<typename T>
306 class FlattenableHelper : public FlattenableHelperInterface {
307 friend class Parcel;
308 const Flattenable<T>& val;
309 explicit FlattenableHelper(const Flattenable<T>& val) : val(val) { }
310
311 public:
312 virtual size_t getFlattenedSize() const {
313 return val.getFlattenedSize();
314 }
315 virtual size_t getFdCount() const {
316 return val.getFdCount();
317 }
318 virtual status_t flatten(void* buffer, size_t size, int* fds, size_t count) const {
319 return val.flatten(buffer, size, fds, count);
320 }
321 virtual status_t unflatten(void const* buffer, size_t size, int const* fds, size_t count) {
322 return const_cast<Flattenable<T>&>(val).unflatten(buffer, size, fds, count);
323 }
324 };
325 status_t write(const FlattenableHelperInterface& val);
326 status_t read(FlattenableHelperInterface& val) const;
327
Jeff Brown5707dbf2011-09-23 21:17:56 -0700328public:
329 class ReadableBlob : public Blob {
330 friend class Parcel;
331 public:
332 inline const void* data() const { return mData; }
333 };
334
335 class WritableBlob : public Blob {
336 friend class Parcel;
337 public:
338 inline void* data() { return mData; }
339 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340};
341
342// ---------------------------------------------------------------------------
343
Mathias Agopian8683fca2012-08-12 19:37:16 -0700344template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700345status_t Parcel::write(const Flattenable<T>& val) {
346 const FlattenableHelper<T> helper(val);
347 return write(helper);
348}
349
350template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700351status_t Parcel::write(const LightFlattenable<T>& val) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700352 size_t size(val.getFlattenedSize());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700353 if (!val.isFixedSize()) {
354 status_t err = writeInt32(size);
355 if (err != NO_ERROR) {
356 return err;
357 }
358 }
Mathias Agopian20985172012-08-31 14:25:22 -0700359 if (size) {
360 void* buffer = writeInplace(size);
Mathias Agopiane1424282013-07-29 21:24:40 -0700361 if (buffer == NULL)
362 return NO_MEMORY;
363 return val.flatten(buffer, size);
Mathias Agopian20985172012-08-31 14:25:22 -0700364 }
365 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700366}
367
368template<typename T>
Mathias Agopiane1424282013-07-29 21:24:40 -0700369status_t Parcel::read(Flattenable<T>& val) const {
370 FlattenableHelper<T> helper(val);
371 return read(helper);
372}
373
374template<typename T>
Mathias Agopian8683fca2012-08-12 19:37:16 -0700375status_t Parcel::read(LightFlattenable<T>& val) const {
376 size_t size;
377 if (val.isFixedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700378 size = val.getFlattenedSize();
Mathias Agopian8683fca2012-08-12 19:37:16 -0700379 } else {
380 int32_t s;
381 status_t err = readInt32(&s);
382 if (err != NO_ERROR) {
383 return err;
384 }
385 size = s;
386 }
Mathias Agopian20985172012-08-31 14:25:22 -0700387 if (size) {
388 void const* buffer = readInplace(size);
389 return buffer == NULL ? NO_MEMORY :
390 val.unflatten(buffer, size);
391 }
392 return NO_ERROR;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700393}
394
395// ---------------------------------------------------------------------------
396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
398{
399 parcel.print(to);
400 return to;
401}
402
403// ---------------------------------------------------------------------------
404
405// Generic acquire and release of objects.
406void acquire_object(const sp<ProcessState>& proc,
407 const flat_binder_object& obj, const void* who);
408void release_object(const sp<ProcessState>& proc,
409 const flat_binder_object& obj, const void* who);
410
411void flatten_binder(const sp<ProcessState>& proc,
412 const sp<IBinder>& binder, flat_binder_object* out);
413void flatten_binder(const sp<ProcessState>& proc,
414 const wp<IBinder>& binder, flat_binder_object* out);
415status_t unflatten_binder(const sp<ProcessState>& proc,
416 const flat_binder_object& flat, sp<IBinder>* out);
417status_t unflatten_binder(const sp<ProcessState>& proc,
418 const flat_binder_object& flat, wp<IBinder>* out);
419
420}; // namespace android
421
422// ---------------------------------------------------------------------------
423
424#endif // ANDROID_PARCEL_H