blob: 1008f02d8c36542064c80bfbf4a5dc2b4b842341 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/Binder.h>
33#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080034#include <binder/IPCThreadState.h>
35#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070036#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080037#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070038#include <binder/TextOutput.h>
39
Mark Salyzynabed7f72016-01-27 08:02:48 -080040#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/String8.h>
46#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047
Mathias Agopian208059f2009-05-18 15:08:03 -070048#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080049#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051#ifndef INT32_MAX
52#define INT32_MAX ((int32_t)(2147483647))
53#endif
54
55#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080057#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070059
60// ---------------------------------------------------------------------------
61
Nick Kralevichb6b14232015-04-02 09:36:02 -070062// This macro should never be used at runtime, as a too large value
63// of s could cause an integer overflow. Instead, you should always
64// use the wrapper function pad_size()
65#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
66
67static size_t pad_size(size_t s) {
68 if (s > (SIZE_T_MAX - 3)) {
69 abort();
70 }
71 return PAD_SIZE_UNSAFE(s);
72}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080075#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070077// XXX This can be made public if we want to provide
78// support for typed data.
79struct small_flat_data
80{
81 uint32_t type;
82 uint32_t data;
83};
84
85namespace android {
86
Dianne Hackborna4cff882014-11-13 17:07:40 -080087static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
88static size_t gParcelGlobalAllocSize = 0;
89static size_t gParcelGlobalAllocCount = 0;
90
Jeff Brown13b16042014-11-11 16:44:25 -080091// Maximum size of a blob to transfer in-place.
92static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
93
94enum {
95 BLOB_INPLACE = 0,
96 BLOB_ASHMEM_IMMUTABLE = 1,
97 BLOB_ASHMEM_MUTABLE = 2,
98};
99
Mark Salyzyn70f36652016-02-02 10:27:03 -0800100static dev_t ashmem_rdev()
101{
102 static dev_t __ashmem_rdev;
103 static pthread_mutex_t __ashmem_rdev_lock = PTHREAD_MUTEX_INITIALIZER;
104
105 pthread_mutex_lock(&__ashmem_rdev_lock);
106
107 dev_t rdev = __ashmem_rdev;
108 if (!rdev) {
109 int fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDONLY));
110 if (fd >= 0) {
111 struct stat st;
112
113 int ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
114 close(fd);
115 if ((ret >= 0) && S_ISCHR(st.st_mode)) {
116 rdev = __ashmem_rdev = st.st_rdev;
117 }
118 }
119 }
120
121 pthread_mutex_unlock(&__ashmem_rdev_lock);
122
123 return rdev;
124}
125
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700127 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128{
129 switch (obj.type) {
130 case BINDER_TYPE_BINDER:
131 if (obj.binder) {
132 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800133 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 }
135 return;
136 case BINDER_TYPE_WEAK_BINDER:
137 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800138 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 case BINDER_TYPE_HANDLE: {
141 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
142 if (b != NULL) {
143 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
144 b->incStrong(who);
145 }
146 return;
147 }
148 case BINDER_TYPE_WEAK_HANDLE: {
149 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
150 if (b != NULL) b.get_refs()->incWeak(who);
151 return;
152 }
153 case BINDER_TYPE_FD: {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800154 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
155 struct stat st;
156 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800157 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700158 // If we own an ashmem fd, keep track of how much memory it refers to.
159 int size = ashmem_get_size_region(obj.handle);
160 if (size > 0) {
161 *outAshmemSize += size;
162 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700163 }
164 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 }
167 }
168
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800169 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170}
171
Adrian Roos6bb31142015-10-22 16:46:12 -0700172void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 const flat_binder_object& obj, const void* who)
174{
Adrian Roos6bb31142015-10-22 16:46:12 -0700175 acquire_object(proc, obj, who, NULL);
176}
177
178static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700179 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180{
181 switch (obj.type) {
182 case BINDER_TYPE_BINDER:
183 if (obj.binder) {
184 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800185 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 }
187 return;
188 case BINDER_TYPE_WEAK_BINDER:
189 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800190 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 return;
192 case BINDER_TYPE_HANDLE: {
193 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
194 if (b != NULL) {
195 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
196 b->decStrong(who);
197 }
198 return;
199 }
200 case BINDER_TYPE_WEAK_HANDLE: {
201 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
202 if (b != NULL) b.get_refs()->decWeak(who);
203 return;
204 }
205 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800206 if (obj.cookie != 0) { // owned
207 if (outAshmemSize != NULL) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800208 struct stat st;
209 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800210 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800211 int size = ashmem_get_size_region(obj.handle);
212 if (size > 0) {
213 *outAshmemSize -= size;
214 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700215 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700216 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800217
218 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700219 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 return;
221 }
222 }
223
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800224 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225}
226
Adrian Roos6bb31142015-10-22 16:46:12 -0700227void release_object(const sp<ProcessState>& proc,
228 const flat_binder_object& obj, const void* who)
229{
230 release_object(proc, obj, who, NULL);
231}
232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800234 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235{
236 return out->writeObject(flat, false);
237}
238
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800239status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 const sp<IBinder>& binder, Parcel* out)
241{
242 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
245 if (binder != NULL) {
246 IBinder *local = binder->localBinder();
247 if (!local) {
248 BpBinder *proxy = binder->remoteBinder();
249 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 }
252 const int32_t handle = proxy ? proxy->handle() : 0;
253 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800254 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 } else {
258 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
260 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
262 } else {
263 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 obj.binder = 0;
265 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 return finish_flatten_binder(binder, obj, out);
269}
270
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800271status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 const wp<IBinder>& binder, Parcel* out)
273{
274 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
277 if (binder != NULL) {
278 sp<IBinder> real = binder.promote();
279 if (real != NULL) {
280 IBinder *local = real->localBinder();
281 if (!local) {
282 BpBinder *proxy = real->remoteBinder();
283 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000284 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285 }
286 const int32_t handle = proxy ? proxy->handle() : 0;
287 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800288 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800290 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 } else {
292 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800293 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
294 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 }
296 return finish_flatten_binder(real, obj, out);
297 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 // XXX How to deal? In order to flatten the given binder,
300 // we need to probe it for information, which requires a primary
301 // reference... but we don't have one.
302 //
303 // The OpenBinder implementation uses a dynamic_cast<> here,
304 // but we can't do that with the different reference counting
305 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000306 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800308 obj.binder = 0;
309 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 } else {
313 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800314 obj.binder = 0;
315 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 return finish_flatten_binder(NULL, obj, out);
317 }
318}
319
320inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800321 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
322 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323{
324 return NO_ERROR;
325}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327status_t unflatten_binder(const sp<ProcessState>& proc,
328 const Parcel& in, sp<IBinder>* out)
329{
330 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332 if (flat) {
333 switch (flat->type) {
334 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800335 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 return finish_unflatten_binder(NULL, *flat, in);
337 case BINDER_TYPE_HANDLE:
338 *out = proc->getStrongProxyForHandle(flat->handle);
339 return finish_unflatten_binder(
340 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700341 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 }
343 return BAD_TYPE;
344}
345
346status_t unflatten_binder(const sp<ProcessState>& proc,
347 const Parcel& in, wp<IBinder>* out)
348{
349 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700351 if (flat) {
352 switch (flat->type) {
353 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800354 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 return finish_unflatten_binder(NULL, *flat, in);
356 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800357 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700358 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800359 reinterpret_cast<IBinder*>(flat->cookie),
360 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700361 } else {
362 *out = NULL;
363 }
364 return finish_unflatten_binder(NULL, *flat, in);
365 case BINDER_TYPE_HANDLE:
366 case BINDER_TYPE_WEAK_HANDLE:
367 *out = proc->getWeakProxyForHandle(flat->handle);
368 return finish_unflatten_binder(
369 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
370 }
371 }
372 return BAD_TYPE;
373}
374
375// ---------------------------------------------------------------------------
376
377Parcel::Parcel()
378{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800379 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 initState();
381}
382
383Parcel::~Parcel()
384{
385 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800386 LOG_ALLOC("Parcel %p: destroyed", this);
387}
388
389size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800390 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
391 size_t size = gParcelGlobalAllocSize;
392 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
393 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800394}
395
396size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800397 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
398 size_t count = gParcelGlobalAllocCount;
399 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
400 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700401}
402
403const uint8_t* Parcel::data() const
404{
405 return mData;
406}
407
408size_t Parcel::dataSize() const
409{
410 return (mDataSize > mDataPos ? mDataSize : mDataPos);
411}
412
413size_t Parcel::dataAvail() const
414{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700415 size_t result = dataSize() - dataPosition();
416 if (result > INT32_MAX) {
417 abort();
418 }
419 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420}
421
422size_t Parcel::dataPosition() const
423{
424 return mDataPos;
425}
426
427size_t Parcel::dataCapacity() const
428{
429 return mDataCapacity;
430}
431
432status_t Parcel::setDataSize(size_t size)
433{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700434 if (size > INT32_MAX) {
435 // don't accept size_t values which may have come from an
436 // inadvertent conversion from a negative int.
437 return BAD_VALUE;
438 }
439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 status_t err;
441 err = continueWrite(size);
442 if (err == NO_ERROR) {
443 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700444 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 }
446 return err;
447}
448
449void Parcel::setDataPosition(size_t pos) const
450{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700451 if (pos > INT32_MAX) {
452 // don't accept size_t values which may have come from an
453 // inadvertent conversion from a negative int.
454 abort();
455 }
456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 mDataPos = pos;
458 mNextObjectHint = 0;
459}
460
461status_t Parcel::setDataCapacity(size_t size)
462{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700463 if (size > INT32_MAX) {
464 // don't accept size_t values which may have come from an
465 // inadvertent conversion from a negative int.
466 return BAD_VALUE;
467 }
468
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700469 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 return NO_ERROR;
471}
472
473status_t Parcel::setData(const uint8_t* buffer, size_t len)
474{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700475 if (len > INT32_MAX) {
476 // don't accept size_t values which may have come from an
477 // inadvertent conversion from a negative int.
478 return BAD_VALUE;
479 }
480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481 status_t err = restartWrite(len);
482 if (err == NO_ERROR) {
483 memcpy(const_cast<uint8_t*>(data()), buffer, len);
484 mDataSize = len;
485 mFdsKnown = false;
486 }
487 return err;
488}
489
Andreas Huber51faf462011-04-13 10:21:56 -0700490status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491{
492 const sp<ProcessState> proc(ProcessState::self());
493 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700494 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800495 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 size_t size = parcel->mObjectsSize;
497 int startPos = mDataPos;
498 int firstIndex = -1, lastIndex = -2;
499
500 if (len == 0) {
501 return NO_ERROR;
502 }
503
Nick Kralevichb6b14232015-04-02 09:36:02 -0700504 if (len > INT32_MAX) {
505 // don't accept size_t values which may have come from an
506 // inadvertent conversion from a negative int.
507 return BAD_VALUE;
508 }
509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 // range checks against the source parcel size
511 if ((offset > parcel->mDataSize)
512 || (len > parcel->mDataSize)
513 || (offset + len > parcel->mDataSize)) {
514 return BAD_VALUE;
515 }
516
517 // Count objects in range
518 for (int i = 0; i < (int) size; i++) {
519 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700520 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 if (firstIndex == -1) {
522 firstIndex = i;
523 }
524 lastIndex = i;
525 }
526 }
527 int numObjects = lastIndex - firstIndex + 1;
528
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700529 if ((mDataSize+len) > mDataCapacity) {
530 // grow data
531 err = growData(len);
532 if (err != NO_ERROR) {
533 return err;
534 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 }
536
537 // append data
538 memcpy(mData + mDataPos, data + offset, len);
539 mDataPos += len;
540 mDataSize += len;
541
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400542 err = NO_ERROR;
543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 if (numObjects > 0) {
545 // grow objects
546 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700547 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
548 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800549 binder_size_t *objects =
550 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
551 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 return NO_MEMORY;
553 }
554 mObjects = objects;
555 mObjectsCapacity = newSize;
556 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700557
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558 // append and acquire objects
559 int idx = mObjectsSize;
560 for (int i = firstIndex; i <= lastIndex; i++) {
561 size_t off = objects[i] - offset + startPos;
562 mObjects[idx++] = off;
563 mObjectsSize++;
564
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700565 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700566 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700567 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700570 // If this is a file descriptor, we need to dup it so the
571 // new Parcel now owns its own fd, and can declare that we
572 // officially know we have fds.
573 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800574 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700575 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400576 if (!mAllowFds) {
577 err = FDS_NOT_ALLOWED;
578 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579 }
580 }
581 }
582
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return err;
584}
585
Jeff Brown13b16042014-11-11 16:44:25 -0800586bool Parcel::allowFds() const
587{
588 return mAllowFds;
589}
590
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700591bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400592{
593 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700594 if (!allowFds) {
595 mAllowFds = false;
596 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400597 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598}
599
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700600void Parcel::restoreAllowFds(bool lastValue)
601{
602 mAllowFds = lastValue;
603}
604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605bool Parcel::hasFileDescriptors() const
606{
607 if (!mFdsKnown) {
608 scanForFds();
609 }
610 return mHasFds;
611}
612
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700613// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614status_t Parcel::writeInterfaceToken(const String16& interface)
615{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700616 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
617 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 // currently the interface identification token is just its name as a string
619 return writeString16(interface);
620}
621
Mathias Agopian83c04462009-05-22 19:00:22 -0700622bool Parcel::checkInterface(IBinder* binder) const
623{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700624 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700625}
626
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700627bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700628 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700630 int32_t strictPolicy = readInt32();
631 if (threadState == NULL) {
632 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700633 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700634 if ((threadState->getLastTransactionBinderFlags() &
635 IBinder::FLAG_ONEWAY) != 0) {
636 // For one-way calls, the callee is running entirely
637 // disconnected from the caller, so disable StrictMode entirely.
638 // Not only does disk/network usage not impact the caller, but
639 // there's no way to commuicate back any violations anyway.
640 threadState->setStrictModePolicy(0);
641 } else {
642 threadState->setStrictModePolicy(strictPolicy);
643 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700644 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645 if (str == interface) {
646 return true;
647 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700648 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 String8(interface).string(), String8(str).string());
650 return false;
651 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700652}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800654const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655{
656 return mObjects;
657}
658
659size_t Parcel::objectsCount() const
660{
661 return mObjectsSize;
662}
663
664status_t Parcel::errorCheck() const
665{
666 return mError;
667}
668
669void Parcel::setError(status_t err)
670{
671 mError = err;
672}
673
674status_t Parcel::finishWrite(size_t len)
675{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700676 if (len > INT32_MAX) {
677 // don't accept size_t values which may have come from an
678 // inadvertent conversion from a negative int.
679 return BAD_VALUE;
680 }
681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 //printf("Finish write of %d\n", len);
683 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700684 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 if (mDataPos > mDataSize) {
686 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700687 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 }
689 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
690 return NO_ERROR;
691}
692
693status_t Parcel::writeUnpadded(const void* data, size_t len)
694{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700695 if (len > INT32_MAX) {
696 // don't accept size_t values which may have come from an
697 // inadvertent conversion from a negative int.
698 return BAD_VALUE;
699 }
700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701 size_t end = mDataPos + len;
702 if (end < mDataPos) {
703 // integer overflow
704 return BAD_VALUE;
705 }
706
707 if (end <= mDataCapacity) {
708restart_write:
709 memcpy(mData+mDataPos, data, len);
710 return finishWrite(len);
711 }
712
713 status_t err = growData(len);
714 if (err == NO_ERROR) goto restart_write;
715 return err;
716}
717
718status_t Parcel::write(const void* data, size_t len)
719{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700720 if (len > INT32_MAX) {
721 // don't accept size_t values which may have come from an
722 // inadvertent conversion from a negative int.
723 return BAD_VALUE;
724 }
725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726 void* const d = writeInplace(len);
727 if (d) {
728 memcpy(d, data, len);
729 return NO_ERROR;
730 }
731 return mError;
732}
733
734void* Parcel::writeInplace(size_t len)
735{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700736 if (len > INT32_MAX) {
737 // don't accept size_t values which may have come from an
738 // inadvertent conversion from a negative int.
739 return NULL;
740 }
741
742 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743
744 // sanity check for integer overflow
745 if (mDataPos+padded < mDataPos) {
746 return NULL;
747 }
748
749 if ((mDataPos+padded) <= mDataCapacity) {
750restart_write:
751 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
752 uint8_t* const data = mData+mDataPos;
753
754 // Need to pad at end?
755 if (padded != len) {
756#if BYTE_ORDER == BIG_ENDIAN
757 static const uint32_t mask[4] = {
758 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
759 };
760#endif
761#if BYTE_ORDER == LITTLE_ENDIAN
762 static const uint32_t mask[4] = {
763 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
764 };
765#endif
766 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
767 // *reinterpret_cast<void**>(data+padded-4));
768 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
769 }
770
771 finishWrite(padded);
772 return data;
773 }
774
775 status_t err = growData(padded);
776 if (err == NO_ERROR) goto restart_write;
777 return NULL;
778}
779
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800780status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
781 const uint8_t* strData = (uint8_t*)str.data();
782 const size_t strLen= str.length();
783 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
784 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
785 return BAD_VALUE;
786 }
787
788 status_t err = writeInt32(utf16Len);
789 if (err) {
790 return err;
791 }
792
793 // Allocate enough bytes to hold our converted string and its terminating NULL.
794 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
795 if (!dst) {
796 return NO_MEMORY;
797 }
798
799 utf8_to_utf16(strData, strLen, (char16_t*)dst);
800
801 return NO_ERROR;
802}
803
804status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
805 if (!str) {
806 return writeInt32(-1);
807 }
808 return writeUtf8AsUtf16(*str);
809}
810
Casey Dahlinb9872622015-11-25 15:09:45 -0800811status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
812{
813 if (!val) {
814 return writeInt32(-1);
815 }
816
817 return writeByteVector(*val);
818}
819
Casey Dahlin451ff582015-10-19 18:12:18 -0700820status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
821{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700822 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700823 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700824 status = BAD_VALUE;
825 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700826 }
827
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700828 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700829 if (status != OK) {
830 return status;
831 }
832
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700833 void* data = writeInplace(val.size());
834 if (!data) {
835 status = BAD_VALUE;
836 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700837 }
838
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700839 memcpy(data, val.data(), val.size());
840 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700841}
842
843status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
844{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800845 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700846}
847
Casey Dahlinb9872622015-11-25 15:09:45 -0800848status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
849{
850 return writeNullableTypedVector(val, &Parcel::writeInt32);
851}
852
Casey Dahlin451ff582015-10-19 18:12:18 -0700853status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
854{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800855 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700856}
857
Casey Dahlinb9872622015-11-25 15:09:45 -0800858status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeInt64);
861}
862
Casey Dahlin451ff582015-10-19 18:12:18 -0700863status_t Parcel::writeFloatVector(const std::vector<float>& val)
864{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800865 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700866}
867
Casey Dahlinb9872622015-11-25 15:09:45 -0800868status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeFloat);
871}
872
Casey Dahlin451ff582015-10-19 18:12:18 -0700873status_t Parcel::writeDoubleVector(const std::vector<double>& val)
874{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800875 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700876}
877
Casey Dahlinb9872622015-11-25 15:09:45 -0800878status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
879{
880 return writeNullableTypedVector(val, &Parcel::writeDouble);
881}
882
Casey Dahlin451ff582015-10-19 18:12:18 -0700883status_t Parcel::writeBoolVector(const std::vector<bool>& val)
884{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800885 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700886}
887
Casey Dahlinb9872622015-11-25 15:09:45 -0800888status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
889{
890 return writeNullableTypedVector(val, &Parcel::writeBool);
891}
892
Casey Dahlin451ff582015-10-19 18:12:18 -0700893status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
894{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800895 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700896}
897
Casey Dahlinb9872622015-11-25 15:09:45 -0800898status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
899{
900 return writeNullableTypedVector(val, &Parcel::writeChar);
901}
902
Casey Dahlin451ff582015-10-19 18:12:18 -0700903status_t Parcel::writeString16Vector(const std::vector<String16>& val)
904{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800905 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700906}
907
Casey Dahlinb9872622015-11-25 15:09:45 -0800908status_t Parcel::writeString16Vector(
909 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeString16);
912}
913
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800914status_t Parcel::writeUtf8VectorAsUtf16Vector(
915 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
916 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
917}
918
919status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
920 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
921}
922
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923status_t Parcel::writeInt32(int32_t val)
924{
Andreas Huber84a6d042009-08-17 13:33:27 -0700925 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700926}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800927
928status_t Parcel::writeUint32(uint32_t val)
929{
930 return writeAligned(val);
931}
932
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700933status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700934 if (len > INT32_MAX) {
935 // don't accept size_t values which may have come from an
936 // inadvertent conversion from a negative int.
937 return BAD_VALUE;
938 }
939
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700940 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700941 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700942 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700943 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700944 if (ret == NO_ERROR) {
945 ret = write(val, len * sizeof(*val));
946 }
947 return ret;
948}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700949status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700950 if (len > INT32_MAX) {
951 // don't accept size_t values which may have come from an
952 // inadvertent conversion from a negative int.
953 return BAD_VALUE;
954 }
955
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700956 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700957 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700958 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700959 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700960 if (ret == NO_ERROR) {
961 ret = write(val, len * sizeof(*val));
962 }
963 return ret;
964}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965
Casey Dahlind6848f52015-10-15 15:44:59 -0700966status_t Parcel::writeBool(bool val)
967{
968 return writeInt32(int32_t(val));
969}
970
971status_t Parcel::writeChar(char16_t val)
972{
973 return writeInt32(int32_t(val));
974}
975
976status_t Parcel::writeByte(int8_t val)
977{
978 return writeInt32(int32_t(val));
979}
980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981status_t Parcel::writeInt64(int64_t val)
982{
Andreas Huber84a6d042009-08-17 13:33:27 -0700983 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984}
985
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700986status_t Parcel::writeUint64(uint64_t val)
987{
988 return writeAligned(val);
989}
990
Serban Constantinescuf683e012013-11-05 16:53:55 +0000991status_t Parcel::writePointer(uintptr_t val)
992{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800993 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000994}
995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996status_t Parcel::writeFloat(float val)
997{
Andreas Huber84a6d042009-08-17 13:33:27 -0700998 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999}
1000
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001001#if defined(__mips__) && defined(__mips_hard_float)
1002
1003status_t Parcel::writeDouble(double val)
1004{
1005 union {
1006 double d;
1007 unsigned long long ll;
1008 } u;
1009 u.d = val;
1010 return writeAligned(u.ll);
1011}
1012
1013#else
1014
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001015status_t Parcel::writeDouble(double val)
1016{
Andreas Huber84a6d042009-08-17 13:33:27 -07001017 return writeAligned(val);
1018}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001020#endif
1021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022status_t Parcel::writeCString(const char* str)
1023{
1024 return write(str, strlen(str)+1);
1025}
1026
1027status_t Parcel::writeString8(const String8& str)
1028{
1029 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001030 // only write string if its length is more than zero characters,
1031 // as readString8 will only read if the length field is non-zero.
1032 // this is slightly different from how writeString16 works.
1033 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034 err = write(str.string(), str.bytes()+1);
1035 }
1036 return err;
1037}
1038
Casey Dahlinb9872622015-11-25 15:09:45 -08001039status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1040{
1041 if (!str) {
1042 return writeInt32(-1);
1043 }
1044
1045 return writeString16(*str);
1046}
1047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001048status_t Parcel::writeString16(const String16& str)
1049{
1050 return writeString16(str.string(), str.size());
1051}
1052
1053status_t Parcel::writeString16(const char16_t* str, size_t len)
1054{
1055 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057 status_t err = writeInt32(len);
1058 if (err == NO_ERROR) {
1059 len *= sizeof(char16_t);
1060 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1061 if (data) {
1062 memcpy(data, str, len);
1063 *reinterpret_cast<char16_t*>(data+len) = 0;
1064 return NO_ERROR;
1065 }
1066 err = mError;
1067 }
1068 return err;
1069}
1070
1071status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1072{
1073 return flatten_binder(ProcessState::self(), val, this);
1074}
1075
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001076status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1077{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001078 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001079}
1080
Casey Dahlinb9872622015-11-25 15:09:45 -08001081status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1082{
1083 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1084}
1085
1086status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1087 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1088}
1089
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001090status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001091 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001092}
1093
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1095{
1096 return flatten_binder(ProcessState::self(), val, this);
1097}
1098
Casey Dahlinb9872622015-11-25 15:09:45 -08001099status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1100 if (!parcelable) {
1101 return writeInt32(0);
1102 }
1103
1104 return writeParcelable(*parcelable);
1105}
1106
Christopher Wiley97f048d2015-11-19 06:49:05 -08001107status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1108 status_t status = writeInt32(1); // parcelable is not null.
1109 if (status != OK) {
1110 return status;
1111 }
1112 return parcelable.writeToParcel(this);
1113}
1114
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001115status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001116{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001117 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001118 return BAD_TYPE;
1119
1120 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001121 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001122 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001124 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001125 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001127 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1128 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129
1130 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001131 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 return err;
1133 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001134 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001135 return err;
1136}
1137
Jeff Brown93ff1f92011-11-04 19:01:44 -07001138status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139{
1140 flat_binder_object obj;
1141 obj.type = BINDER_TYPE_FD;
1142 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001143 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001144 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001145 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146 return writeObject(obj, true);
1147}
1148
1149status_t Parcel::writeDupFileDescriptor(int fd)
1150{
Jeff Brownd341c712011-11-04 20:19:33 -07001151 int dupFd = dup(fd);
1152 if (dupFd < 0) {
1153 return -errno;
1154 }
1155 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001156 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001157 close(dupFd);
1158 }
1159 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160}
1161
Casey Dahlin06673e32015-11-23 13:24:23 -08001162status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1163 return writeDupFileDescriptor(fd.get());
1164}
1165
1166status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1167 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1168}
1169
Casey Dahlinb9872622015-11-25 15:09:45 -08001170status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1171 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1172}
1173
Jeff Brown13b16042014-11-11 16:44:25 -08001174status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001175{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001176 if (len > INT32_MAX) {
1177 // don't accept size_t values which may have come from an
1178 // inadvertent conversion from a negative int.
1179 return BAD_VALUE;
1180 }
1181
Jeff Brown13b16042014-11-11 16:44:25 -08001182 status_t status;
1183 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001184 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001185 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 if (status) return status;
1187
1188 void* ptr = writeInplace(len);
1189 if (!ptr) return NO_MEMORY;
1190
Jeff Brown13b16042014-11-11 16:44:25 -08001191 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001192 return NO_ERROR;
1193 }
1194
Steve Block6807e592011-10-20 11:56:00 +01001195 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196 int fd = ashmem_create_region("Parcel Blob", len);
1197 if (fd < 0) return NO_MEMORY;
1198
1199 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1200 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001201 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202 } else {
1203 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1204 if (ptr == MAP_FAILED) {
1205 status = -errno;
1206 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001207 if (!mutableCopy) {
1208 result = ashmem_set_prot_region(fd, PROT_READ);
1209 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001210 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001211 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001213 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001215 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001217 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 return NO_ERROR;
1219 }
1220 }
1221 }
1222 }
1223 ::munmap(ptr, len);
1224 }
1225 ::close(fd);
1226 return status;
1227}
1228
Jeff Brown13b16042014-11-11 16:44:25 -08001229status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1230{
1231 // Must match up with what's done in writeBlob.
1232 if (!mAllowFds) return FDS_NOT_ALLOWED;
1233 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1234 if (status) return status;
1235 return writeDupFileDescriptor(fd);
1236}
1237
Mathias Agopiane1424282013-07-29 21:24:40 -07001238status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001239{
1240 status_t err;
1241
1242 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001243 const size_t len = val.getFlattenedSize();
1244 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001245
Nick Kralevichb6b14232015-04-02 09:36:02 -07001246 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1247 // don't accept size_t values which may have come from an
1248 // inadvertent conversion from a negative int.
1249 return BAD_VALUE;
1250 }
1251
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001252 err = this->writeInt32(len);
1253 if (err) return err;
1254
1255 err = this->writeInt32(fd_count);
1256 if (err) return err;
1257
1258 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001259 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001260 if (buf == NULL)
1261 return BAD_VALUE;
1262
1263 int* fds = NULL;
1264 if (fd_count) {
1265 fds = new int[fd_count];
1266 }
1267
1268 err = val.flatten(buf, len, fds, fd_count);
1269 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1270 err = this->writeDupFileDescriptor( fds[i] );
1271 }
1272
1273 if (fd_count) {
1274 delete [] fds;
1275 }
1276
1277 return err;
1278}
1279
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001280status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1281{
1282 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1283 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1284 if (enoughData && enoughObjects) {
1285restart_write:
1286 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001287
Christopher Tate98e67d32015-06-03 18:44:15 -07001288 // remember if it's a file descriptor
1289 if (val.type == BINDER_TYPE_FD) {
1290 if (!mAllowFds) {
1291 // fail before modifying our object index
1292 return FDS_NOT_ALLOWED;
1293 }
1294 mHasFds = mFdsKnown = true;
1295 }
1296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001298 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001300 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301 mObjectsSize++;
1302 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304 return finishWrite(sizeof(flat_binder_object));
1305 }
1306
1307 if (!enoughData) {
1308 const status_t err = growData(sizeof(val));
1309 if (err != NO_ERROR) return err;
1310 }
1311 if (!enoughObjects) {
1312 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001313 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001314 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315 if (objects == NULL) return NO_MEMORY;
1316 mObjects = objects;
1317 mObjectsCapacity = newSize;
1318 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001319
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001320 goto restart_write;
1321}
1322
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001323status_t Parcel::writeNoException()
1324{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001325 binder::Status status;
1326 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001327}
1328
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001329void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330{
1331 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1332}
1333
1334status_t Parcel::read(void* outData, size_t len) const
1335{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001336 if (len > INT32_MAX) {
1337 // don't accept size_t values which may have come from an
1338 // inadvertent conversion from a negative int.
1339 return BAD_VALUE;
1340 }
1341
1342 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1343 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001345 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001346 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347 return NO_ERROR;
1348 }
1349 return NOT_ENOUGH_DATA;
1350}
1351
1352const void* Parcel::readInplace(size_t len) const
1353{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001354 if (len > INT32_MAX) {
1355 // don't accept size_t values which may have come from an
1356 // inadvertent conversion from a negative int.
1357 return NULL;
1358 }
1359
1360 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1361 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001363 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001364 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 return data;
1366 }
1367 return NULL;
1368}
1369
Andreas Huber84a6d042009-08-17 13:33:27 -07001370template<class T>
1371status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001372 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001373
1374 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001376 mDataPos += sizeof(T);
1377 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378 return NO_ERROR;
1379 } else {
1380 return NOT_ENOUGH_DATA;
1381 }
1382}
1383
Andreas Huber84a6d042009-08-17 13:33:27 -07001384template<class T>
1385T Parcel::readAligned() const {
1386 T result;
1387 if (readAligned(&result) != NO_ERROR) {
1388 result = 0;
1389 }
1390
1391 return result;
1392}
1393
1394template<class T>
1395status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001396 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001397
1398 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1399restart_write:
1400 *reinterpret_cast<T*>(mData+mDataPos) = val;
1401 return finishWrite(sizeof(val));
1402 }
1403
1404 status_t err = growData(sizeof(val));
1405 if (err == NO_ERROR) goto restart_write;
1406 return err;
1407}
1408
Casey Dahlin451ff582015-10-19 18:12:18 -07001409status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1410 val->clear();
1411
1412 int32_t size;
1413 status_t status = readInt32(&size);
1414
1415 if (status != OK) {
1416 return status;
1417 }
1418
Christopher Wiley4db672d2015-11-10 09:44:30 -08001419 if (size < 0) {
1420 status = UNEXPECTED_NULL;
1421 return status;
1422 }
1423 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001424 status = BAD_VALUE;
1425 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001426 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001427
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001428 const void* data = readInplace(size);
1429 if (!data) {
1430 status = BAD_VALUE;
1431 return status;
1432 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001433 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001434 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001435
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001436 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001437}
1438
Casey Dahlinb9872622015-11-25 15:09:45 -08001439status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1440 const int32_t start = dataPosition();
1441 int32_t size;
1442 status_t status = readInt32(&size);
1443 val->reset();
1444
1445 if (status != OK || size < 0) {
1446 return status;
1447 }
1448
1449 setDataPosition(start);
1450 val->reset(new std::vector<int8_t>());
1451
1452 status = readByteVector(val->get());
1453
1454 if (status != OK) {
1455 val->reset();
1456 }
1457
1458 return status;
1459}
1460
1461status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1462 return readNullableTypedVector(val, &Parcel::readInt32);
1463}
1464
Casey Dahlin451ff582015-10-19 18:12:18 -07001465status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001466 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001467}
1468
Casey Dahlinb9872622015-11-25 15:09:45 -08001469status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1470 return readNullableTypedVector(val, &Parcel::readInt64);
1471}
1472
Casey Dahlin451ff582015-10-19 18:12:18 -07001473status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001474 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001475}
1476
Casey Dahlinb9872622015-11-25 15:09:45 -08001477status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1478 return readNullableTypedVector(val, &Parcel::readFloat);
1479}
1480
Casey Dahlin451ff582015-10-19 18:12:18 -07001481status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001482 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001483}
1484
Casey Dahlinb9872622015-11-25 15:09:45 -08001485status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1486 return readNullableTypedVector(val, &Parcel::readDouble);
1487}
1488
Casey Dahlin451ff582015-10-19 18:12:18 -07001489status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001490 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001491}
1492
Casey Dahlinb9872622015-11-25 15:09:45 -08001493status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1494 const int32_t start = dataPosition();
1495 int32_t size;
1496 status_t status = readInt32(&size);
1497 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001498
Casey Dahlinb9872622015-11-25 15:09:45 -08001499 if (status != OK || size < 0) {
1500 return status;
1501 }
1502
1503 setDataPosition(start);
1504 val->reset(new std::vector<bool>());
1505
1506 status = readBoolVector(val->get());
1507
1508 if (status != OK) {
1509 val->reset();
1510 }
1511
1512 return status;
1513}
1514
1515status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001516 int32_t size;
1517 status_t status = readInt32(&size);
1518
1519 if (status != OK) {
1520 return status;
1521 }
1522
1523 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001524 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001525 }
1526
1527 val->resize(size);
1528
1529 /* C++ bool handling means a vector of bools isn't necessarily addressable
1530 * (we might use individual bits)
1531 */
Christopher Wiley97887982015-10-27 16:33:47 -07001532 bool data;
1533 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001534 status = readBool(&data);
1535 (*val)[i] = data;
1536
1537 if (status != OK) {
1538 return status;
1539 }
1540 }
1541
1542 return OK;
1543}
1544
Casey Dahlinb9872622015-11-25 15:09:45 -08001545status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1546 return readNullableTypedVector(val, &Parcel::readChar);
1547}
1548
Casey Dahlin451ff582015-10-19 18:12:18 -07001549status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001550 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001551}
1552
Casey Dahlinb9872622015-11-25 15:09:45 -08001553status_t Parcel::readString16Vector(
1554 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1555 return readNullableTypedVector(val, &Parcel::readString16);
1556}
1557
Casey Dahlin451ff582015-10-19 18:12:18 -07001558status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001559 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001560}
1561
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001562status_t Parcel::readUtf8VectorFromUtf16Vector(
1563 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1564 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1565}
1566
1567status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1568 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1569}
Casey Dahlin451ff582015-10-19 18:12:18 -07001570
Andreas Huber84a6d042009-08-17 13:33:27 -07001571status_t Parcel::readInt32(int32_t *pArg) const
1572{
1573 return readAligned(pArg);
1574}
1575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576int32_t Parcel::readInt32() const
1577{
Andreas Huber84a6d042009-08-17 13:33:27 -07001578 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579}
1580
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001581status_t Parcel::readUint32(uint32_t *pArg) const
1582{
1583 return readAligned(pArg);
1584}
1585
1586uint32_t Parcel::readUint32() const
1587{
1588 return readAligned<uint32_t>();
1589}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590
1591status_t Parcel::readInt64(int64_t *pArg) const
1592{
Andreas Huber84a6d042009-08-17 13:33:27 -07001593 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594}
1595
1596
1597int64_t Parcel::readInt64() const
1598{
Andreas Huber84a6d042009-08-17 13:33:27 -07001599 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001600}
1601
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001602status_t Parcel::readUint64(uint64_t *pArg) const
1603{
1604 return readAligned(pArg);
1605}
1606
1607uint64_t Parcel::readUint64() const
1608{
1609 return readAligned<uint64_t>();
1610}
1611
Serban Constantinescuf683e012013-11-05 16:53:55 +00001612status_t Parcel::readPointer(uintptr_t *pArg) const
1613{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001614 status_t ret;
1615 binder_uintptr_t ptr;
1616 ret = readAligned(&ptr);
1617 if (!ret)
1618 *pArg = ptr;
1619 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001620}
1621
1622uintptr_t Parcel::readPointer() const
1623{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001624 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001625}
1626
1627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628status_t Parcel::readFloat(float *pArg) const
1629{
Andreas Huber84a6d042009-08-17 13:33:27 -07001630 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631}
1632
1633
1634float Parcel::readFloat() const
1635{
Andreas Huber84a6d042009-08-17 13:33:27 -07001636 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637}
1638
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001639#if defined(__mips__) && defined(__mips_hard_float)
1640
1641status_t Parcel::readDouble(double *pArg) const
1642{
1643 union {
1644 double d;
1645 unsigned long long ll;
1646 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001647 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001648 status_t status;
1649 status = readAligned(&u.ll);
1650 *pArg = u.d;
1651 return status;
1652}
1653
1654double Parcel::readDouble() const
1655{
1656 union {
1657 double d;
1658 unsigned long long ll;
1659 } u;
1660 u.ll = readAligned<unsigned long long>();
1661 return u.d;
1662}
1663
1664#else
1665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666status_t Parcel::readDouble(double *pArg) const
1667{
Andreas Huber84a6d042009-08-17 13:33:27 -07001668 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669}
1670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671double Parcel::readDouble() const
1672{
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 return readAligned<double>();
1674}
1675
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001676#endif
1677
Andreas Huber84a6d042009-08-17 13:33:27 -07001678status_t Parcel::readIntPtr(intptr_t *pArg) const
1679{
1680 return readAligned(pArg);
1681}
1682
1683
1684intptr_t Parcel::readIntPtr() const
1685{
1686 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687}
1688
Casey Dahlind6848f52015-10-15 15:44:59 -07001689status_t Parcel::readBool(bool *pArg) const
1690{
1691 int32_t tmp;
1692 status_t ret = readInt32(&tmp);
1693 *pArg = (tmp != 0);
1694 return ret;
1695}
1696
1697bool Parcel::readBool() const
1698{
1699 return readInt32() != 0;
1700}
1701
1702status_t Parcel::readChar(char16_t *pArg) const
1703{
1704 int32_t tmp;
1705 status_t ret = readInt32(&tmp);
1706 *pArg = char16_t(tmp);
1707 return ret;
1708}
1709
1710char16_t Parcel::readChar() const
1711{
1712 return char16_t(readInt32());
1713}
1714
1715status_t Parcel::readByte(int8_t *pArg) const
1716{
1717 int32_t tmp;
1718 status_t ret = readInt32(&tmp);
1719 *pArg = int8_t(tmp);
1720 return ret;
1721}
1722
1723int8_t Parcel::readByte() const
1724{
1725 return int8_t(readInt32());
1726}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001728status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1729 size_t utf16Size = 0;
1730 const char16_t* src = readString16Inplace(&utf16Size);
1731 if (!src) {
1732 return UNEXPECTED_NULL;
1733 }
1734
1735 // Save ourselves the trouble, we're done.
1736 if (utf16Size == 0u) {
1737 str->clear();
1738 return NO_ERROR;
1739 }
1740
1741 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1742 if (utf8Size < 0) {
1743 return BAD_VALUE;
1744 }
1745 // Note that while it is probably safe to assume string::resize keeps a
1746 // spare byte around for the trailing null, we're going to be explicit.
1747 str->resize(utf8Size + 1);
1748 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1749 str->resize(utf8Size);
1750 return NO_ERROR;
1751}
1752
1753status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1754 const int32_t start = dataPosition();
1755 int32_t size;
1756 status_t status = readInt32(&size);
1757 str->reset();
1758
1759 if (status != OK || size < 0) {
1760 return status;
1761 }
1762
1763 setDataPosition(start);
1764 str->reset(new std::string());
1765 return readUtf8FromUtf16(str->get());
1766}
1767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001768const char* Parcel::readCString() const
1769{
1770 const size_t avail = mDataSize-mDataPos;
1771 if (avail > 0) {
1772 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1773 // is the string's trailing NUL within the parcel's valid bounds?
1774 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1775 if (eos) {
1776 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001777 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001778 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779 return str;
1780 }
1781 }
1782 return NULL;
1783}
1784
1785String8 Parcel::readString8() const
1786{
1787 int32_t size = readInt32();
1788 // watch for potential int overflow adding 1 for trailing NUL
1789 if (size > 0 && size < INT32_MAX) {
1790 const char* str = (const char*)readInplace(size+1);
1791 if (str) return String8(str, size);
1792 }
1793 return String8();
1794}
1795
1796String16 Parcel::readString16() const
1797{
1798 size_t len;
1799 const char16_t* str = readString16Inplace(&len);
1800 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001801 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802 return String16();
1803}
1804
Casey Dahlinb9872622015-11-25 15:09:45 -08001805status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1806{
1807 const int32_t start = dataPosition();
1808 int32_t size;
1809 status_t status = readInt32(&size);
1810 pArg->reset();
1811
1812 if (status != OK || size < 0) {
1813 return status;
1814 }
1815
1816 setDataPosition(start);
1817 pArg->reset(new String16());
1818
1819 status = readString16(pArg->get());
1820
1821 if (status != OK) {
1822 pArg->reset();
1823 }
1824
1825 return status;
1826}
1827
Casey Dahlin451ff582015-10-19 18:12:18 -07001828status_t Parcel::readString16(String16* pArg) const
1829{
1830 size_t len;
1831 const char16_t* str = readString16Inplace(&len);
1832 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001833 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001834 return 0;
1835 } else {
1836 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001837 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001838 }
1839}
1840
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1842{
1843 int32_t size = readInt32();
1844 // watch for potential int overflow from size+1
1845 if (size >= 0 && size < INT32_MAX) {
1846 *outLen = size;
1847 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1848 if (str != NULL) {
1849 return str;
1850 }
1851 }
1852 *outLen = 0;
1853 return NULL;
1854}
1855
Casey Dahlinf0c13772015-10-27 18:33:56 -07001856status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1857{
1858 return unflatten_binder(ProcessState::self(), *this, val);
1859}
1860
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861sp<IBinder> Parcel::readStrongBinder() const
1862{
1863 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001864 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865 return val;
1866}
1867
1868wp<IBinder> Parcel::readWeakBinder() const
1869{
1870 wp<IBinder> val;
1871 unflatten_binder(ProcessState::self(), *this, &val);
1872 return val;
1873}
1874
Christopher Wiley97f048d2015-11-19 06:49:05 -08001875status_t Parcel::readParcelable(Parcelable* parcelable) const {
1876 int32_t have_parcelable = 0;
1877 status_t status = readInt32(&have_parcelable);
1878 if (status != OK) {
1879 return status;
1880 }
1881 if (!have_parcelable) {
1882 return UNEXPECTED_NULL;
1883 }
1884 return parcelable->readFromParcel(this);
1885}
1886
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001887int32_t Parcel::readExceptionCode() const
1888{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001889 binder::Status status;
1890 status.readFromParcel(*this);
1891 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001892}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001893
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001894native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001895{
1896 int numFds, numInts;
1897 status_t err;
1898 err = readInt32(&numFds);
1899 if (err != NO_ERROR) return 0;
1900 err = readInt32(&numInts);
1901 if (err != NO_ERROR) return 0;
1902
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001903 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001904 if (!h) {
1905 return 0;
1906 }
1907
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001908 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001909 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001910 if (h->data[i] < 0) err = BAD_VALUE;
1911 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001912 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001913 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001914 native_handle_close(h);
1915 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001916 h = 0;
1917 }
1918 return h;
1919}
1920
1921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922int Parcel::readFileDescriptor() const
1923{
1924 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001925
1926 if (flat && flat->type == BINDER_TYPE_FD) {
1927 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 return BAD_TYPE;
1931}
1932
Casey Dahlin06673e32015-11-23 13:24:23 -08001933status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1934{
1935 int got = readFileDescriptor();
1936
1937 if (got == BAD_TYPE) {
1938 return BAD_TYPE;
1939 }
1940
1941 val->reset(dup(got));
1942
1943 if (val->get() < 0) {
1944 return BAD_VALUE;
1945 }
1946
1947 return OK;
1948}
1949
1950
Casey Dahlinb9872622015-11-25 15:09:45 -08001951status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1952 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1953}
1954
Casey Dahlin06673e32015-11-23 13:24:23 -08001955status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1956 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1957}
1958
Jeff Brown5707dbf2011-09-23 21:17:56 -07001959status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1960{
Jeff Brown13b16042014-11-11 16:44:25 -08001961 int32_t blobType;
1962 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001963 if (status) return status;
1964
Jeff Brown13b16042014-11-11 16:44:25 -08001965 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001966 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001967 const void* ptr = readInplace(len);
1968 if (!ptr) return BAD_VALUE;
1969
Jeff Brown13b16042014-11-11 16:44:25 -08001970 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001971 return NO_ERROR;
1972 }
1973
Steve Block6807e592011-10-20 11:56:00 +01001974 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001975 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001976 int fd = readFileDescriptor();
1977 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1978
Jeff Brown13b16042014-11-11 16:44:25 -08001979 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1980 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001981 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001982
Jeff Brown13b16042014-11-11 16:44:25 -08001983 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001984 return NO_ERROR;
1985}
1986
Mathias Agopiane1424282013-07-29 21:24:40 -07001987status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001988{
1989 // size
1990 const size_t len = this->readInt32();
1991 const size_t fd_count = this->readInt32();
1992
Nick Kralevichb6b14232015-04-02 09:36:02 -07001993 if (len > INT32_MAX) {
1994 // don't accept size_t values which may have come from an
1995 // inadvertent conversion from a negative int.
1996 return BAD_VALUE;
1997 }
1998
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001999 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002000 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002001 if (buf == NULL)
2002 return BAD_VALUE;
2003
2004 int* fds = NULL;
2005 if (fd_count) {
2006 fds = new int[fd_count];
2007 }
2008
2009 status_t err = NO_ERROR;
2010 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002011 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002012 if (fds[i] < 0) {
2013 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002014 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2015 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002016 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002017 }
2018
2019 if (err == NO_ERROR) {
2020 err = val.unflatten(buf, len, fds, fd_count);
2021 }
2022
2023 if (fd_count) {
2024 delete [] fds;
2025 }
2026
2027 return err;
2028}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2030{
2031 const size_t DPOS = mDataPos;
2032 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2033 const flat_binder_object* obj
2034 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2035 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002036 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002037 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002038 // the object list, so we don't want to check for it when
2039 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002040 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041 return obj;
2042 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002045 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 const size_t N = mObjectsSize;
2047 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002050 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 // Start at the current hint position, looking for an object at
2054 // the current data position.
2055 if (opos < N) {
2056 while (opos < (N-1) && OBJS[opos] < DPOS) {
2057 opos++;
2058 }
2059 } else {
2060 opos = N-1;
2061 }
2062 if (OBJS[opos] == DPOS) {
2063 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002064 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 this, DPOS, opos);
2066 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 return obj;
2069 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 // Look backwards for it...
2072 while (opos > 0 && OBJS[opos] > DPOS) {
2073 opos--;
2074 }
2075 if (OBJS[opos] == DPOS) {
2076 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002077 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 this, DPOS, opos);
2079 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002080 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002081 return obj;
2082 }
2083 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002084 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 this, DPOS);
2086 }
2087 return NULL;
2088}
2089
2090void Parcel::closeFileDescriptors()
2091{
2092 size_t i = mObjectsSize;
2093 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002094 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 }
2096 while (i > 0) {
2097 i--;
2098 const flat_binder_object* flat
2099 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2100 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002101 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 close(flat->handle);
2103 }
2104 }
2105}
2106
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002107uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002109 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110}
2111
2112size_t Parcel::ipcDataSize() const
2113{
2114 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2115}
2116
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002117uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002118{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002119 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120}
2121
2122size_t Parcel::ipcObjectsCount() const
2123{
2124 return mObjectsSize;
2125}
2126
2127void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002128 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002130 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131 freeDataNoInit();
2132 mError = NO_ERROR;
2133 mData = const_cast<uint8_t*>(data);
2134 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002135 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002138 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 mObjectsSize = mObjectsCapacity = objectsCount;
2140 mNextObjectHint = 0;
2141 mOwner = relFunc;
2142 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002143 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002144 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002145 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002146 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002147 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002148 mObjectsSize = 0;
2149 break;
2150 }
2151 minOffset = offset + sizeof(flat_binder_object);
2152 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 scanForFds();
2154}
2155
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002156void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157{
2158 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 if (errorCheck() != NO_ERROR) {
2161 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002162 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163 } else if (dataSize() > 0) {
2164 const uint8_t* DATA = data();
2165 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002166 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 const size_t N = objectsCount();
2168 for (size_t i=0; i<N; i++) {
2169 const flat_binder_object* flat
2170 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2171 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2172 << TypeCode(flat->type & 0x7f7f7f00)
2173 << " = " << flat->binder;
2174 }
2175 } else {
2176 to << "NULL";
2177 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002178
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 to << ")";
2180}
2181
2182void Parcel::releaseObjects()
2183{
2184 const sp<ProcessState> proc(ProcessState::self());
2185 size_t i = mObjectsSize;
2186 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002187 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 while (i > 0) {
2189 i--;
2190 const flat_binder_object* flat
2191 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002192 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 }
2194}
2195
2196void Parcel::acquireObjects()
2197{
2198 const sp<ProcessState> proc(ProcessState::self());
2199 size_t i = mObjectsSize;
2200 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002201 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 while (i > 0) {
2203 i--;
2204 const flat_binder_object* flat
2205 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002206 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002207 }
2208}
2209
2210void Parcel::freeData()
2211{
2212 freeDataNoInit();
2213 initState();
2214}
2215
2216void Parcel::freeDataNoInit()
2217{
2218 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002219 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002220 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2222 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002223 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002225 if (mData) {
2226 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002227 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002228 if (mDataCapacity <= gParcelGlobalAllocSize) {
2229 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2230 } else {
2231 gParcelGlobalAllocSize = 0;
2232 }
2233 if (gParcelGlobalAllocCount > 0) {
2234 gParcelGlobalAllocCount--;
2235 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002236 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002237 free(mData);
2238 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 if (mObjects) free(mObjects);
2240 }
2241}
2242
2243status_t Parcel::growData(size_t len)
2244{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002245 if (len > INT32_MAX) {
2246 // don't accept size_t values which may have come from an
2247 // inadvertent conversion from a negative int.
2248 return BAD_VALUE;
2249 }
2250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 size_t newSize = ((mDataSize+len)*3)/2;
2252 return (newSize <= mDataSize)
2253 ? (status_t) NO_MEMORY
2254 : continueWrite(newSize);
2255}
2256
2257status_t Parcel::restartWrite(size_t desired)
2258{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002259 if (desired > INT32_MAX) {
2260 // don't accept size_t values which may have come from an
2261 // inadvertent conversion from a negative int.
2262 return BAD_VALUE;
2263 }
2264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265 if (mOwner) {
2266 freeData();
2267 return continueWrite(desired);
2268 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 uint8_t* data = (uint8_t*)realloc(mData, desired);
2271 if (!data && desired > mDataCapacity) {
2272 mError = NO_MEMORY;
2273 return NO_MEMORY;
2274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002279 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002280 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002281 gParcelGlobalAllocSize += desired;
2282 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002283 if (!mData) {
2284 gParcelGlobalAllocCount++;
2285 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002286 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 mData = data;
2288 mDataCapacity = desired;
2289 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002292 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2293 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 free(mObjects);
2296 mObjects = NULL;
2297 mObjectsSize = mObjectsCapacity = 0;
2298 mNextObjectHint = 0;
2299 mHasFds = false;
2300 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002301 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002302
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 return NO_ERROR;
2304}
2305
2306status_t Parcel::continueWrite(size_t desired)
2307{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002308 if (desired > INT32_MAX) {
2309 // don't accept size_t values which may have come from an
2310 // inadvertent conversion from a negative int.
2311 return BAD_VALUE;
2312 }
2313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 // If shrinking, first adjust for any objects that appear
2315 // after the new data size.
2316 size_t objectsSize = mObjectsSize;
2317 if (desired < mDataSize) {
2318 if (desired == 0) {
2319 objectsSize = 0;
2320 } else {
2321 while (objectsSize > 0) {
2322 if (mObjects[objectsSize-1] < desired)
2323 break;
2324 objectsSize--;
2325 }
2326 }
2327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 if (mOwner) {
2330 // If the size is going to zero, just release the owner's data.
2331 if (desired == 0) {
2332 freeData();
2333 return NO_ERROR;
2334 }
2335
2336 // If there is a different owner, we need to take
2337 // posession.
2338 uint8_t* data = (uint8_t*)malloc(desired);
2339 if (!data) {
2340 mError = NO_MEMORY;
2341 return NO_MEMORY;
2342 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002343 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002346 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002348 free(data);
2349
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mError = NO_MEMORY;
2351 return NO_MEMORY;
2352 }
2353
2354 // Little hack to only acquire references on objects
2355 // we will be keeping.
2356 size_t oldObjectsSize = mObjectsSize;
2357 mObjectsSize = objectsSize;
2358 acquireObjects();
2359 mObjectsSize = oldObjectsSize;
2360 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 if (mData) {
2363 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2364 }
2365 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002366 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002368 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2370 mOwner = NULL;
2371
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002372 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002373 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002374 gParcelGlobalAllocSize += desired;
2375 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378 mData = data;
2379 mObjects = objects;
2380 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002381 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002382 mDataCapacity = desired;
2383 mObjectsSize = mObjectsCapacity = objectsSize;
2384 mNextObjectHint = 0;
2385
2386 } else if (mData) {
2387 if (objectsSize < mObjectsSize) {
2388 // Need to release refs on any objects we are dropping.
2389 const sp<ProcessState> proc(ProcessState::self());
2390 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2391 const flat_binder_object* flat
2392 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2393 if (flat->type == BINDER_TYPE_FD) {
2394 // will need to rescan because we may have lopped off the only FDs
2395 mFdsKnown = false;
2396 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002397 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002399 binder_size_t* objects =
2400 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 if (objects) {
2402 mObjects = objects;
2403 }
2404 mObjectsSize = objectsSize;
2405 mNextObjectHint = 0;
2406 }
2407
2408 // We own the data, so we can just do a realloc().
2409 if (desired > mDataCapacity) {
2410 uint8_t* data = (uint8_t*)realloc(mData, desired);
2411 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002412 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2413 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002414 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002415 gParcelGlobalAllocSize += desired;
2416 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002417 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 mData = data;
2419 mDataCapacity = desired;
2420 } else if (desired > mDataCapacity) {
2421 mError = NO_MEMORY;
2422 return NO_MEMORY;
2423 }
2424 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002425 if (mDataSize > desired) {
2426 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002427 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002428 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 if (mDataPos > desired) {
2430 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 }
2433 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 } else {
2436 // This is the first data. Easy!
2437 uint8_t* data = (uint8_t*)malloc(desired);
2438 if (!data) {
2439 mError = NO_MEMORY;
2440 return NO_MEMORY;
2441 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 if(!(mDataCapacity == 0 && mObjects == NULL
2444 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002445 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002448 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002449 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002450 gParcelGlobalAllocSize += desired;
2451 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002452 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 mData = data;
2455 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2457 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 mDataCapacity = desired;
2459 }
2460
2461 return NO_ERROR;
2462}
2463
2464void Parcel::initState()
2465{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002466 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 mError = NO_ERROR;
2468 mData = 0;
2469 mDataSize = 0;
2470 mDataCapacity = 0;
2471 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002472 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2473 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 mObjects = NULL;
2475 mObjectsSize = 0;
2476 mObjectsCapacity = 0;
2477 mNextObjectHint = 0;
2478 mHasFds = false;
2479 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002480 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002482 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483}
2484
2485void Parcel::scanForFds() const
2486{
2487 bool hasFds = false;
2488 for (size_t i=0; i<mObjectsSize; i++) {
2489 const flat_binder_object* flat
2490 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2491 if (flat->type == BINDER_TYPE_FD) {
2492 hasFds = true;
2493 break;
2494 }
2495 }
2496 mHasFds = hasFds;
2497 mFdsKnown = true;
2498}
2499
Dan Sandleraa5c2342015-04-10 10:08:45 -04002500size_t Parcel::getBlobAshmemSize() const
2501{
Adrian Roos6bb31142015-10-22 16:46:12 -07002502 // This used to return the size of all blobs that were written to ashmem, now we're returning
2503 // the ashmem currently referenced by this Parcel, which should be equivalent.
2504 // TODO: Remove method once ABI can be changed.
2505 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002506}
2507
Adrian Rooscbf37262015-10-22 16:12:53 -07002508size_t Parcel::getOpenAshmemSize() const
2509{
2510 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002511}
2512
2513// --- Parcel::Blob ---
2514
2515Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002516 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002517}
2518
2519Parcel::Blob::~Blob() {
2520 release();
2521}
2522
2523void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002524 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002525 ::munmap(mData, mSize);
2526 }
2527 clear();
2528}
2529
Jeff Brown13b16042014-11-11 16:44:25 -08002530void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2531 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002532 mData = data;
2533 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002534 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002535}
2536
2537void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002538 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002539 mData = NULL;
2540 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002541 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002542}
2543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544}; // namespace android