blob: 4a6bce8f115e1f6519f1d592d1246b12849d734a [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{
415 // TODO: decide what to do about the possibility that this can
416 // report an available-data size that exceeds a Java int's max
417 // positive value, causing havoc. Fortunately this will only
418 // happen if someone constructs a Parcel containing more than two
419 // gigabytes of data, which on typical phone hardware is simply
420 // not possible.
421 return dataSize() - dataPosition();
422}
423
424size_t Parcel::dataPosition() const
425{
426 return mDataPos;
427}
428
429size_t Parcel::dataCapacity() const
430{
431 return mDataCapacity;
432}
433
434status_t Parcel::setDataSize(size_t size)
435{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700436 if (size > INT32_MAX) {
437 // don't accept size_t values which may have come from an
438 // inadvertent conversion from a negative int.
439 return BAD_VALUE;
440 }
441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 status_t err;
443 err = continueWrite(size);
444 if (err == NO_ERROR) {
445 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700446 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700447 }
448 return err;
449}
450
451void Parcel::setDataPosition(size_t pos) const
452{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700453 if (pos > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 abort();
457 }
458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 mDataPos = pos;
460 mNextObjectHint = 0;
461}
462
463status_t Parcel::setDataCapacity(size_t size)
464{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700465 if (size > INT32_MAX) {
466 // don't accept size_t values which may have come from an
467 // inadvertent conversion from a negative int.
468 return BAD_VALUE;
469 }
470
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700471 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 return NO_ERROR;
473}
474
475status_t Parcel::setData(const uint8_t* buffer, size_t len)
476{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700477 if (len > INT32_MAX) {
478 // don't accept size_t values which may have come from an
479 // inadvertent conversion from a negative int.
480 return BAD_VALUE;
481 }
482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700483 status_t err = restartWrite(len);
484 if (err == NO_ERROR) {
485 memcpy(const_cast<uint8_t*>(data()), buffer, len);
486 mDataSize = len;
487 mFdsKnown = false;
488 }
489 return err;
490}
491
Andreas Huber51faf462011-04-13 10:21:56 -0700492status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493{
494 const sp<ProcessState> proc(ProcessState::self());
495 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700496 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800497 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 size_t size = parcel->mObjectsSize;
499 int startPos = mDataPos;
500 int firstIndex = -1, lastIndex = -2;
501
502 if (len == 0) {
503 return NO_ERROR;
504 }
505
Nick Kralevichb6b14232015-04-02 09:36:02 -0700506 if (len > INT32_MAX) {
507 // don't accept size_t values which may have come from an
508 // inadvertent conversion from a negative int.
509 return BAD_VALUE;
510 }
511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512 // range checks against the source parcel size
513 if ((offset > parcel->mDataSize)
514 || (len > parcel->mDataSize)
515 || (offset + len > parcel->mDataSize)) {
516 return BAD_VALUE;
517 }
518
519 // Count objects in range
520 for (int i = 0; i < (int) size; i++) {
521 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700522 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 if (firstIndex == -1) {
524 firstIndex = i;
525 }
526 lastIndex = i;
527 }
528 }
529 int numObjects = lastIndex - firstIndex + 1;
530
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700531 if ((mDataSize+len) > mDataCapacity) {
532 // grow data
533 err = growData(len);
534 if (err != NO_ERROR) {
535 return err;
536 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 }
538
539 // append data
540 memcpy(mData + mDataPos, data + offset, len);
541 mDataPos += len;
542 mDataSize += len;
543
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400544 err = NO_ERROR;
545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 if (numObjects > 0) {
547 // grow objects
548 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700549 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
550 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800551 binder_size_t *objects =
552 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
553 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700554 return NO_MEMORY;
555 }
556 mObjects = objects;
557 mObjectsCapacity = newSize;
558 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700559
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700560 // append and acquire objects
561 int idx = mObjectsSize;
562 for (int i = firstIndex; i <= lastIndex; i++) {
563 size_t off = objects[i] - offset + startPos;
564 mObjects[idx++] = off;
565 mObjectsSize++;
566
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700567 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700569 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700572 // If this is a file descriptor, we need to dup it so the
573 // new Parcel now owns its own fd, and can declare that we
574 // officially know we have fds.
575 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800576 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578 if (!mAllowFds) {
579 err = FDS_NOT_ALLOWED;
580 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581 }
582 }
583 }
584
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400585 return err;
586}
587
Jeff Brown13b16042014-11-11 16:44:25 -0800588bool Parcel::allowFds() const
589{
590 return mAllowFds;
591}
592
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700593bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400594{
595 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700596 if (!allowFds) {
597 mAllowFds = false;
598 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400599 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600}
601
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700602void Parcel::restoreAllowFds(bool lastValue)
603{
604 mAllowFds = lastValue;
605}
606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607bool Parcel::hasFileDescriptors() const
608{
609 if (!mFdsKnown) {
610 scanForFds();
611 }
612 return mHasFds;
613}
614
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700615// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616status_t Parcel::writeInterfaceToken(const String16& interface)
617{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700618 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
619 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 // currently the interface identification token is just its name as a string
621 return writeString16(interface);
622}
623
Mathias Agopian83c04462009-05-22 19:00:22 -0700624bool Parcel::checkInterface(IBinder* binder) const
625{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700626 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700627}
628
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700629bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700630 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700632 int32_t strictPolicy = readInt32();
633 if (threadState == NULL) {
634 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700635 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700636 if ((threadState->getLastTransactionBinderFlags() &
637 IBinder::FLAG_ONEWAY) != 0) {
638 // For one-way calls, the callee is running entirely
639 // disconnected from the caller, so disable StrictMode entirely.
640 // Not only does disk/network usage not impact the caller, but
641 // there's no way to commuicate back any violations anyway.
642 threadState->setStrictModePolicy(0);
643 } else {
644 threadState->setStrictModePolicy(strictPolicy);
645 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700646 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 if (str == interface) {
648 return true;
649 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700650 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 String8(interface).string(), String8(str).string());
652 return false;
653 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700654}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800656const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657{
658 return mObjects;
659}
660
661size_t Parcel::objectsCount() const
662{
663 return mObjectsSize;
664}
665
666status_t Parcel::errorCheck() const
667{
668 return mError;
669}
670
671void Parcel::setError(status_t err)
672{
673 mError = err;
674}
675
676status_t Parcel::finishWrite(size_t len)
677{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700678 if (len > INT32_MAX) {
679 // don't accept size_t values which may have come from an
680 // inadvertent conversion from a negative int.
681 return BAD_VALUE;
682 }
683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700684 //printf("Finish write of %d\n", len);
685 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700686 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687 if (mDataPos > mDataSize) {
688 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700689 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690 }
691 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
692 return NO_ERROR;
693}
694
695status_t Parcel::writeUnpadded(const void* data, size_t len)
696{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700697 if (len > INT32_MAX) {
698 // don't accept size_t values which may have come from an
699 // inadvertent conversion from a negative int.
700 return BAD_VALUE;
701 }
702
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700703 size_t end = mDataPos + len;
704 if (end < mDataPos) {
705 // integer overflow
706 return BAD_VALUE;
707 }
708
709 if (end <= mDataCapacity) {
710restart_write:
711 memcpy(mData+mDataPos, data, len);
712 return finishWrite(len);
713 }
714
715 status_t err = growData(len);
716 if (err == NO_ERROR) goto restart_write;
717 return err;
718}
719
720status_t Parcel::write(const void* data, size_t len)
721{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700722 if (len > INT32_MAX) {
723 // don't accept size_t values which may have come from an
724 // inadvertent conversion from a negative int.
725 return BAD_VALUE;
726 }
727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728 void* const d = writeInplace(len);
729 if (d) {
730 memcpy(d, data, len);
731 return NO_ERROR;
732 }
733 return mError;
734}
735
736void* Parcel::writeInplace(size_t len)
737{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700738 if (len > INT32_MAX) {
739 // don't accept size_t values which may have come from an
740 // inadvertent conversion from a negative int.
741 return NULL;
742 }
743
744 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700745
746 // sanity check for integer overflow
747 if (mDataPos+padded < mDataPos) {
748 return NULL;
749 }
750
751 if ((mDataPos+padded) <= mDataCapacity) {
752restart_write:
753 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
754 uint8_t* const data = mData+mDataPos;
755
756 // Need to pad at end?
757 if (padded != len) {
758#if BYTE_ORDER == BIG_ENDIAN
759 static const uint32_t mask[4] = {
760 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
761 };
762#endif
763#if BYTE_ORDER == LITTLE_ENDIAN
764 static const uint32_t mask[4] = {
765 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
766 };
767#endif
768 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
769 // *reinterpret_cast<void**>(data+padded-4));
770 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
771 }
772
773 finishWrite(padded);
774 return data;
775 }
776
777 status_t err = growData(padded);
778 if (err == NO_ERROR) goto restart_write;
779 return NULL;
780}
781
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800782status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
783 const uint8_t* strData = (uint8_t*)str.data();
784 const size_t strLen= str.length();
785 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
786 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
787 return BAD_VALUE;
788 }
789
790 status_t err = writeInt32(utf16Len);
791 if (err) {
792 return err;
793 }
794
795 // Allocate enough bytes to hold our converted string and its terminating NULL.
796 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
797 if (!dst) {
798 return NO_MEMORY;
799 }
800
801 utf8_to_utf16(strData, strLen, (char16_t*)dst);
802
803 return NO_ERROR;
804}
805
806status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
807 if (!str) {
808 return writeInt32(-1);
809 }
810 return writeUtf8AsUtf16(*str);
811}
812
Casey Dahlinb9872622015-11-25 15:09:45 -0800813status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
814{
815 if (!val) {
816 return writeInt32(-1);
817 }
818
819 return writeByteVector(*val);
820}
821
Casey Dahlin451ff582015-10-19 18:12:18 -0700822status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
823{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700824 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700825 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700826 status = BAD_VALUE;
827 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700828 }
829
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700830 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700831 if (status != OK) {
832 return status;
833 }
834
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700835 void* data = writeInplace(val.size());
836 if (!data) {
837 status = BAD_VALUE;
838 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700839 }
840
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700841 memcpy(data, val.data(), val.size());
842 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700843}
844
845status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
846{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800847 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700848}
849
Casey Dahlinb9872622015-11-25 15:09:45 -0800850status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeInt32);
853}
854
Casey Dahlin451ff582015-10-19 18:12:18 -0700855status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
856{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800857 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700858}
859
Casey Dahlinb9872622015-11-25 15:09:45 -0800860status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeInt64);
863}
864
Casey Dahlin451ff582015-10-19 18:12:18 -0700865status_t Parcel::writeFloatVector(const std::vector<float>& val)
866{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800867 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700868}
869
Casey Dahlinb9872622015-11-25 15:09:45 -0800870status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
871{
872 return writeNullableTypedVector(val, &Parcel::writeFloat);
873}
874
Casey Dahlin451ff582015-10-19 18:12:18 -0700875status_t Parcel::writeDoubleVector(const std::vector<double>& val)
876{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800877 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700878}
879
Casey Dahlinb9872622015-11-25 15:09:45 -0800880status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
881{
882 return writeNullableTypedVector(val, &Parcel::writeDouble);
883}
884
Casey Dahlin451ff582015-10-19 18:12:18 -0700885status_t Parcel::writeBoolVector(const std::vector<bool>& val)
886{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800887 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700888}
889
Casey Dahlinb9872622015-11-25 15:09:45 -0800890status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
891{
892 return writeNullableTypedVector(val, &Parcel::writeBool);
893}
894
Casey Dahlin451ff582015-10-19 18:12:18 -0700895status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
896{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800897 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700898}
899
Casey Dahlinb9872622015-11-25 15:09:45 -0800900status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
901{
902 return writeNullableTypedVector(val, &Parcel::writeChar);
903}
904
Casey Dahlin451ff582015-10-19 18:12:18 -0700905status_t Parcel::writeString16Vector(const std::vector<String16>& val)
906{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800907 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700908}
909
Casey Dahlinb9872622015-11-25 15:09:45 -0800910status_t Parcel::writeString16Vector(
911 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
912{
913 return writeNullableTypedVector(val, &Parcel::writeString16);
914}
915
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800916status_t Parcel::writeUtf8VectorAsUtf16Vector(
917 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
918 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
919}
920
921status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
922 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
923}
924
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925status_t Parcel::writeInt32(int32_t val)
926{
Andreas Huber84a6d042009-08-17 13:33:27 -0700927 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700928}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800929
930status_t Parcel::writeUint32(uint32_t val)
931{
932 return writeAligned(val);
933}
934
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700935status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700936 if (len > INT32_MAX) {
937 // don't accept size_t values which may have come from an
938 // inadvertent conversion from a negative int.
939 return BAD_VALUE;
940 }
941
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700942 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700943 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700944 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700945 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700946 if (ret == NO_ERROR) {
947 ret = write(val, len * sizeof(*val));
948 }
949 return ret;
950}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700951status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700952 if (len > INT32_MAX) {
953 // don't accept size_t values which may have come from an
954 // inadvertent conversion from a negative int.
955 return BAD_VALUE;
956 }
957
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700958 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700959 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700960 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700961 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700962 if (ret == NO_ERROR) {
963 ret = write(val, len * sizeof(*val));
964 }
965 return ret;
966}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967
Casey Dahlind6848f52015-10-15 15:44:59 -0700968status_t Parcel::writeBool(bool val)
969{
970 return writeInt32(int32_t(val));
971}
972
973status_t Parcel::writeChar(char16_t val)
974{
975 return writeInt32(int32_t(val));
976}
977
978status_t Parcel::writeByte(int8_t val)
979{
980 return writeInt32(int32_t(val));
981}
982
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983status_t Parcel::writeInt64(int64_t val)
984{
Andreas Huber84a6d042009-08-17 13:33:27 -0700985 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986}
987
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700988status_t Parcel::writeUint64(uint64_t val)
989{
990 return writeAligned(val);
991}
992
Serban Constantinescuf683e012013-11-05 16:53:55 +0000993status_t Parcel::writePointer(uintptr_t val)
994{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800995 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000996}
997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998status_t Parcel::writeFloat(float val)
999{
Andreas Huber84a6d042009-08-17 13:33:27 -07001000 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001001}
1002
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001003#if defined(__mips__) && defined(__mips_hard_float)
1004
1005status_t Parcel::writeDouble(double val)
1006{
1007 union {
1008 double d;
1009 unsigned long long ll;
1010 } u;
1011 u.d = val;
1012 return writeAligned(u.ll);
1013}
1014
1015#else
1016
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001017status_t Parcel::writeDouble(double val)
1018{
Andreas Huber84a6d042009-08-17 13:33:27 -07001019 return writeAligned(val);
1020}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001022#endif
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024status_t Parcel::writeCString(const char* str)
1025{
1026 return write(str, strlen(str)+1);
1027}
1028
1029status_t Parcel::writeString8(const String8& str)
1030{
1031 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001032 // only write string if its length is more than zero characters,
1033 // as readString8 will only read if the length field is non-zero.
1034 // this is slightly different from how writeString16 works.
1035 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036 err = write(str.string(), str.bytes()+1);
1037 }
1038 return err;
1039}
1040
Casey Dahlinb9872622015-11-25 15:09:45 -08001041status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1042{
1043 if (!str) {
1044 return writeInt32(-1);
1045 }
1046
1047 return writeString16(*str);
1048}
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050status_t Parcel::writeString16(const String16& str)
1051{
1052 return writeString16(str.string(), str.size());
1053}
1054
1055status_t Parcel::writeString16(const char16_t* str, size_t len)
1056{
1057 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059 status_t err = writeInt32(len);
1060 if (err == NO_ERROR) {
1061 len *= sizeof(char16_t);
1062 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1063 if (data) {
1064 memcpy(data, str, len);
1065 *reinterpret_cast<char16_t*>(data+len) = 0;
1066 return NO_ERROR;
1067 }
1068 err = mError;
1069 }
1070 return err;
1071}
1072
1073status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1074{
1075 return flatten_binder(ProcessState::self(), val, this);
1076}
1077
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001078status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1079{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001080 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001081}
1082
Casey Dahlinb9872622015-11-25 15:09:45 -08001083status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1084{
1085 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1086}
1087
1088status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1089 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1090}
1091
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001092status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001093 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001094}
1095
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001096status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1097{
1098 return flatten_binder(ProcessState::self(), val, this);
1099}
1100
Casey Dahlinb9872622015-11-25 15:09:45 -08001101status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1102 if (!parcelable) {
1103 return writeInt32(0);
1104 }
1105
1106 return writeParcelable(*parcelable);
1107}
1108
Christopher Wiley97f048d2015-11-19 06:49:05 -08001109status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1110 status_t status = writeInt32(1); // parcelable is not null.
1111 if (status != OK) {
1112 return status;
1113 }
1114 return parcelable.writeToParcel(this);
1115}
1116
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001117status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001118{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001119 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001120 return BAD_TYPE;
1121
1122 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001123 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001124 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001126 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001127 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001129 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1130 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131
1132 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001133 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 return err;
1135 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001136 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137 return err;
1138}
1139
Jeff Brown93ff1f92011-11-04 19:01:44 -07001140status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001141{
1142 flat_binder_object obj;
1143 obj.type = BINDER_TYPE_FD;
1144 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001145 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001147 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001148 return writeObject(obj, true);
1149}
1150
1151status_t Parcel::writeDupFileDescriptor(int fd)
1152{
Jeff Brownd341c712011-11-04 20:19:33 -07001153 int dupFd = dup(fd);
1154 if (dupFd < 0) {
1155 return -errno;
1156 }
1157 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001158 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001159 close(dupFd);
1160 }
1161 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162}
1163
Casey Dahlin06673e32015-11-23 13:24:23 -08001164status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1165 return writeDupFileDescriptor(fd.get());
1166}
1167
1168status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1169 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1170}
1171
Casey Dahlinb9872622015-11-25 15:09:45 -08001172status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1173 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1174}
1175
Jeff Brown13b16042014-11-11 16:44:25 -08001176status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001177{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001178 if (len > INT32_MAX) {
1179 // don't accept size_t values which may have come from an
1180 // inadvertent conversion from a negative int.
1181 return BAD_VALUE;
1182 }
1183
Jeff Brown13b16042014-11-11 16:44:25 -08001184 status_t status;
1185 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001186 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001187 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001188 if (status) return status;
1189
1190 void* ptr = writeInplace(len);
1191 if (!ptr) return NO_MEMORY;
1192
Jeff Brown13b16042014-11-11 16:44:25 -08001193 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001194 return NO_ERROR;
1195 }
1196
Steve Block6807e592011-10-20 11:56:00 +01001197 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001198 int fd = ashmem_create_region("Parcel Blob", len);
1199 if (fd < 0) return NO_MEMORY;
1200
1201 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1202 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001203 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204 } else {
1205 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1206 if (ptr == MAP_FAILED) {
1207 status = -errno;
1208 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001209 if (!mutableCopy) {
1210 result = ashmem_set_prot_region(fd, PROT_READ);
1211 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001213 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001215 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001217 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001219 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 return NO_ERROR;
1221 }
1222 }
1223 }
1224 }
1225 ::munmap(ptr, len);
1226 }
1227 ::close(fd);
1228 return status;
1229}
1230
Jeff Brown13b16042014-11-11 16:44:25 -08001231status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1232{
1233 // Must match up with what's done in writeBlob.
1234 if (!mAllowFds) return FDS_NOT_ALLOWED;
1235 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1236 if (status) return status;
1237 return writeDupFileDescriptor(fd);
1238}
1239
Mathias Agopiane1424282013-07-29 21:24:40 -07001240status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001241{
1242 status_t err;
1243
1244 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001245 const size_t len = val.getFlattenedSize();
1246 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001247
Nick Kralevichb6b14232015-04-02 09:36:02 -07001248 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1249 // don't accept size_t values which may have come from an
1250 // inadvertent conversion from a negative int.
1251 return BAD_VALUE;
1252 }
1253
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001254 err = this->writeInt32(len);
1255 if (err) return err;
1256
1257 err = this->writeInt32(fd_count);
1258 if (err) return err;
1259
1260 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001261 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001262 if (buf == NULL)
1263 return BAD_VALUE;
1264
1265 int* fds = NULL;
1266 if (fd_count) {
1267 fds = new int[fd_count];
1268 }
1269
1270 err = val.flatten(buf, len, fds, fd_count);
1271 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1272 err = this->writeDupFileDescriptor( fds[i] );
1273 }
1274
1275 if (fd_count) {
1276 delete [] fds;
1277 }
1278
1279 return err;
1280}
1281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001282status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1283{
1284 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1285 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1286 if (enoughData && enoughObjects) {
1287restart_write:
1288 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001289
Christopher Tate98e67d32015-06-03 18:44:15 -07001290 // remember if it's a file descriptor
1291 if (val.type == BINDER_TYPE_FD) {
1292 if (!mAllowFds) {
1293 // fail before modifying our object index
1294 return FDS_NOT_ALLOWED;
1295 }
1296 mHasFds = mFdsKnown = true;
1297 }
1298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001300 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001302 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 mObjectsSize++;
1304 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001306 return finishWrite(sizeof(flat_binder_object));
1307 }
1308
1309 if (!enoughData) {
1310 const status_t err = growData(sizeof(val));
1311 if (err != NO_ERROR) return err;
1312 }
1313 if (!enoughObjects) {
1314 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001315 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001316 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317 if (objects == NULL) return NO_MEMORY;
1318 mObjects = objects;
1319 mObjectsCapacity = newSize;
1320 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322 goto restart_write;
1323}
1324
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001325status_t Parcel::writeNoException()
1326{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001327 binder::Status status;
1328 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001329}
1330
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001331void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332{
1333 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1334}
1335
1336status_t Parcel::read(void* outData, size_t len) const
1337{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001338 if (len > INT32_MAX) {
1339 // don't accept size_t values which may have come from an
1340 // inadvertent conversion from a negative int.
1341 return BAD_VALUE;
1342 }
1343
1344 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1345 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001347 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001348 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 return NO_ERROR;
1350 }
1351 return NOT_ENOUGH_DATA;
1352}
1353
1354const void* Parcel::readInplace(size_t len) const
1355{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001356 if (len > INT32_MAX) {
1357 // don't accept size_t values which may have come from an
1358 // inadvertent conversion from a negative int.
1359 return NULL;
1360 }
1361
1362 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1363 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001365 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001366 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367 return data;
1368 }
1369 return NULL;
1370}
1371
Andreas Huber84a6d042009-08-17 13:33:27 -07001372template<class T>
1373status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001374 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001375
1376 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001378 mDataPos += sizeof(T);
1379 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 return NO_ERROR;
1381 } else {
1382 return NOT_ENOUGH_DATA;
1383 }
1384}
1385
Andreas Huber84a6d042009-08-17 13:33:27 -07001386template<class T>
1387T Parcel::readAligned() const {
1388 T result;
1389 if (readAligned(&result) != NO_ERROR) {
1390 result = 0;
1391 }
1392
1393 return result;
1394}
1395
1396template<class T>
1397status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001398 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001399
1400 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1401restart_write:
1402 *reinterpret_cast<T*>(mData+mDataPos) = val;
1403 return finishWrite(sizeof(val));
1404 }
1405
1406 status_t err = growData(sizeof(val));
1407 if (err == NO_ERROR) goto restart_write;
1408 return err;
1409}
1410
Casey Dahlin451ff582015-10-19 18:12:18 -07001411status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1412 val->clear();
1413
1414 int32_t size;
1415 status_t status = readInt32(&size);
1416
1417 if (status != OK) {
1418 return status;
1419 }
1420
Christopher Wiley4db672d2015-11-10 09:44:30 -08001421 if (size < 0) {
1422 status = UNEXPECTED_NULL;
1423 return status;
1424 }
1425 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001426 status = BAD_VALUE;
1427 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001428 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001429
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001430 const void* data = readInplace(size);
1431 if (!data) {
1432 status = BAD_VALUE;
1433 return status;
1434 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001435 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001436 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001437
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001438 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001439}
1440
Casey Dahlinb9872622015-11-25 15:09:45 -08001441status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1442 const int32_t start = dataPosition();
1443 int32_t size;
1444 status_t status = readInt32(&size);
1445 val->reset();
1446
1447 if (status != OK || size < 0) {
1448 return status;
1449 }
1450
1451 setDataPosition(start);
1452 val->reset(new std::vector<int8_t>());
1453
1454 status = readByteVector(val->get());
1455
1456 if (status != OK) {
1457 val->reset();
1458 }
1459
1460 return status;
1461}
1462
1463status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1464 return readNullableTypedVector(val, &Parcel::readInt32);
1465}
1466
Casey Dahlin451ff582015-10-19 18:12:18 -07001467status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001468 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001469}
1470
Casey Dahlinb9872622015-11-25 15:09:45 -08001471status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1472 return readNullableTypedVector(val, &Parcel::readInt64);
1473}
1474
Casey Dahlin451ff582015-10-19 18:12:18 -07001475status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001476 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001477}
1478
Casey Dahlinb9872622015-11-25 15:09:45 -08001479status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1480 return readNullableTypedVector(val, &Parcel::readFloat);
1481}
1482
Casey Dahlin451ff582015-10-19 18:12:18 -07001483status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001484 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001485}
1486
Casey Dahlinb9872622015-11-25 15:09:45 -08001487status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1488 return readNullableTypedVector(val, &Parcel::readDouble);
1489}
1490
Casey Dahlin451ff582015-10-19 18:12:18 -07001491status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001492 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001493}
1494
Casey Dahlinb9872622015-11-25 15:09:45 -08001495status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1496 const int32_t start = dataPosition();
1497 int32_t size;
1498 status_t status = readInt32(&size);
1499 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001500
Casey Dahlinb9872622015-11-25 15:09:45 -08001501 if (status != OK || size < 0) {
1502 return status;
1503 }
1504
1505 setDataPosition(start);
1506 val->reset(new std::vector<bool>());
1507
1508 status = readBoolVector(val->get());
1509
1510 if (status != OK) {
1511 val->reset();
1512 }
1513
1514 return status;
1515}
1516
1517status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001518 int32_t size;
1519 status_t status = readInt32(&size);
1520
1521 if (status != OK) {
1522 return status;
1523 }
1524
1525 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001526 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001527 }
1528
1529 val->resize(size);
1530
1531 /* C++ bool handling means a vector of bools isn't necessarily addressable
1532 * (we might use individual bits)
1533 */
Christopher Wiley97887982015-10-27 16:33:47 -07001534 bool data;
1535 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001536 status = readBool(&data);
1537 (*val)[i] = data;
1538
1539 if (status != OK) {
1540 return status;
1541 }
1542 }
1543
1544 return OK;
1545}
1546
Casey Dahlinb9872622015-11-25 15:09:45 -08001547status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1548 return readNullableTypedVector(val, &Parcel::readChar);
1549}
1550
Casey Dahlin451ff582015-10-19 18:12:18 -07001551status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001552 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001553}
1554
Casey Dahlinb9872622015-11-25 15:09:45 -08001555status_t Parcel::readString16Vector(
1556 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1557 return readNullableTypedVector(val, &Parcel::readString16);
1558}
1559
Casey Dahlin451ff582015-10-19 18:12:18 -07001560status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001561 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001562}
1563
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001564status_t Parcel::readUtf8VectorFromUtf16Vector(
1565 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1566 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1567}
1568
1569status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1570 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1571}
Casey Dahlin451ff582015-10-19 18:12:18 -07001572
Andreas Huber84a6d042009-08-17 13:33:27 -07001573status_t Parcel::readInt32(int32_t *pArg) const
1574{
1575 return readAligned(pArg);
1576}
1577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001578int32_t Parcel::readInt32() const
1579{
Andreas Huber84a6d042009-08-17 13:33:27 -07001580 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581}
1582
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001583status_t Parcel::readUint32(uint32_t *pArg) const
1584{
1585 return readAligned(pArg);
1586}
1587
1588uint32_t Parcel::readUint32() const
1589{
1590 return readAligned<uint32_t>();
1591}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592
1593status_t Parcel::readInt64(int64_t *pArg) const
1594{
Andreas Huber84a6d042009-08-17 13:33:27 -07001595 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001596}
1597
1598
1599int64_t Parcel::readInt64() const
1600{
Andreas Huber84a6d042009-08-17 13:33:27 -07001601 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602}
1603
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001604status_t Parcel::readUint64(uint64_t *pArg) const
1605{
1606 return readAligned(pArg);
1607}
1608
1609uint64_t Parcel::readUint64() const
1610{
1611 return readAligned<uint64_t>();
1612}
1613
Serban Constantinescuf683e012013-11-05 16:53:55 +00001614status_t Parcel::readPointer(uintptr_t *pArg) const
1615{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001616 status_t ret;
1617 binder_uintptr_t ptr;
1618 ret = readAligned(&ptr);
1619 if (!ret)
1620 *pArg = ptr;
1621 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001622}
1623
1624uintptr_t Parcel::readPointer() const
1625{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001626 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001627}
1628
1629
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630status_t Parcel::readFloat(float *pArg) const
1631{
Andreas Huber84a6d042009-08-17 13:33:27 -07001632 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633}
1634
1635
1636float Parcel::readFloat() const
1637{
Andreas Huber84a6d042009-08-17 13:33:27 -07001638 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639}
1640
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001641#if defined(__mips__) && defined(__mips_hard_float)
1642
1643status_t Parcel::readDouble(double *pArg) const
1644{
1645 union {
1646 double d;
1647 unsigned long long ll;
1648 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001649 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001650 status_t status;
1651 status = readAligned(&u.ll);
1652 *pArg = u.d;
1653 return status;
1654}
1655
1656double Parcel::readDouble() const
1657{
1658 union {
1659 double d;
1660 unsigned long long ll;
1661 } u;
1662 u.ll = readAligned<unsigned long long>();
1663 return u.d;
1664}
1665
1666#else
1667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668status_t Parcel::readDouble(double *pArg) const
1669{
Andreas Huber84a6d042009-08-17 13:33:27 -07001670 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671}
1672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673double Parcel::readDouble() const
1674{
Andreas Huber84a6d042009-08-17 13:33:27 -07001675 return readAligned<double>();
1676}
1677
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001678#endif
1679
Andreas Huber84a6d042009-08-17 13:33:27 -07001680status_t Parcel::readIntPtr(intptr_t *pArg) const
1681{
1682 return readAligned(pArg);
1683}
1684
1685
1686intptr_t Parcel::readIntPtr() const
1687{
1688 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689}
1690
Casey Dahlind6848f52015-10-15 15:44:59 -07001691status_t Parcel::readBool(bool *pArg) const
1692{
1693 int32_t tmp;
1694 status_t ret = readInt32(&tmp);
1695 *pArg = (tmp != 0);
1696 return ret;
1697}
1698
1699bool Parcel::readBool() const
1700{
1701 return readInt32() != 0;
1702}
1703
1704status_t Parcel::readChar(char16_t *pArg) const
1705{
1706 int32_t tmp;
1707 status_t ret = readInt32(&tmp);
1708 *pArg = char16_t(tmp);
1709 return ret;
1710}
1711
1712char16_t Parcel::readChar() const
1713{
1714 return char16_t(readInt32());
1715}
1716
1717status_t Parcel::readByte(int8_t *pArg) const
1718{
1719 int32_t tmp;
1720 status_t ret = readInt32(&tmp);
1721 *pArg = int8_t(tmp);
1722 return ret;
1723}
1724
1725int8_t Parcel::readByte() const
1726{
1727 return int8_t(readInt32());
1728}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001730status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1731 size_t utf16Size = 0;
1732 const char16_t* src = readString16Inplace(&utf16Size);
1733 if (!src) {
1734 return UNEXPECTED_NULL;
1735 }
1736
1737 // Save ourselves the trouble, we're done.
1738 if (utf16Size == 0u) {
1739 str->clear();
1740 return NO_ERROR;
1741 }
1742
1743 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1744 if (utf8Size < 0) {
1745 return BAD_VALUE;
1746 }
1747 // Note that while it is probably safe to assume string::resize keeps a
1748 // spare byte around for the trailing null, we're going to be explicit.
1749 str->resize(utf8Size + 1);
1750 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1751 str->resize(utf8Size);
1752 return NO_ERROR;
1753}
1754
1755status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1756 const int32_t start = dataPosition();
1757 int32_t size;
1758 status_t status = readInt32(&size);
1759 str->reset();
1760
1761 if (status != OK || size < 0) {
1762 return status;
1763 }
1764
1765 setDataPosition(start);
1766 str->reset(new std::string());
1767 return readUtf8FromUtf16(str->get());
1768}
1769
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770const char* Parcel::readCString() const
1771{
1772 const size_t avail = mDataSize-mDataPos;
1773 if (avail > 0) {
1774 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1775 // is the string's trailing NUL within the parcel's valid bounds?
1776 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1777 if (eos) {
1778 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001779 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001780 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781 return str;
1782 }
1783 }
1784 return NULL;
1785}
1786
1787String8 Parcel::readString8() const
1788{
1789 int32_t size = readInt32();
1790 // watch for potential int overflow adding 1 for trailing NUL
1791 if (size > 0 && size < INT32_MAX) {
1792 const char* str = (const char*)readInplace(size+1);
1793 if (str) return String8(str, size);
1794 }
1795 return String8();
1796}
1797
1798String16 Parcel::readString16() const
1799{
1800 size_t len;
1801 const char16_t* str = readString16Inplace(&len);
1802 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001803 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804 return String16();
1805}
1806
Casey Dahlinb9872622015-11-25 15:09:45 -08001807status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1808{
1809 const int32_t start = dataPosition();
1810 int32_t size;
1811 status_t status = readInt32(&size);
1812 pArg->reset();
1813
1814 if (status != OK || size < 0) {
1815 return status;
1816 }
1817
1818 setDataPosition(start);
1819 pArg->reset(new String16());
1820
1821 status = readString16(pArg->get());
1822
1823 if (status != OK) {
1824 pArg->reset();
1825 }
1826
1827 return status;
1828}
1829
Casey Dahlin451ff582015-10-19 18:12:18 -07001830status_t Parcel::readString16(String16* pArg) const
1831{
1832 size_t len;
1833 const char16_t* str = readString16Inplace(&len);
1834 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001835 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001836 return 0;
1837 } else {
1838 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001839 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001840 }
1841}
1842
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1844{
1845 int32_t size = readInt32();
1846 // watch for potential int overflow from size+1
1847 if (size >= 0 && size < INT32_MAX) {
1848 *outLen = size;
1849 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1850 if (str != NULL) {
1851 return str;
1852 }
1853 }
1854 *outLen = 0;
1855 return NULL;
1856}
1857
Casey Dahlinf0c13772015-10-27 18:33:56 -07001858status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1859{
1860 return unflatten_binder(ProcessState::self(), *this, val);
1861}
1862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863sp<IBinder> Parcel::readStrongBinder() const
1864{
1865 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001866 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 return val;
1868}
1869
1870wp<IBinder> Parcel::readWeakBinder() const
1871{
1872 wp<IBinder> val;
1873 unflatten_binder(ProcessState::self(), *this, &val);
1874 return val;
1875}
1876
Christopher Wiley97f048d2015-11-19 06:49:05 -08001877status_t Parcel::readParcelable(Parcelable* parcelable) const {
1878 int32_t have_parcelable = 0;
1879 status_t status = readInt32(&have_parcelable);
1880 if (status != OK) {
1881 return status;
1882 }
1883 if (!have_parcelable) {
1884 return UNEXPECTED_NULL;
1885 }
1886 return parcelable->readFromParcel(this);
1887}
1888
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001889int32_t Parcel::readExceptionCode() const
1890{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001891 binder::Status status;
1892 status.readFromParcel(*this);
1893 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001894}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001895
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001896native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001897{
1898 int numFds, numInts;
1899 status_t err;
1900 err = readInt32(&numFds);
1901 if (err != NO_ERROR) return 0;
1902 err = readInt32(&numInts);
1903 if (err != NO_ERROR) return 0;
1904
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001905 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001906 if (!h) {
1907 return 0;
1908 }
1909
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001910 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001911 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001912 if (h->data[i] < 0) err = BAD_VALUE;
1913 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001914 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001915 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001916 native_handle_close(h);
1917 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001918 h = 0;
1919 }
1920 return h;
1921}
1922
1923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924int Parcel::readFileDescriptor() const
1925{
1926 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001927
1928 if (flat && flat->type == BINDER_TYPE_FD) {
1929 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932 return BAD_TYPE;
1933}
1934
Casey Dahlin06673e32015-11-23 13:24:23 -08001935status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1936{
1937 int got = readFileDescriptor();
1938
1939 if (got == BAD_TYPE) {
1940 return BAD_TYPE;
1941 }
1942
1943 val->reset(dup(got));
1944
1945 if (val->get() < 0) {
1946 return BAD_VALUE;
1947 }
1948
1949 return OK;
1950}
1951
1952
Casey Dahlinb9872622015-11-25 15:09:45 -08001953status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1954 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1955}
1956
Casey Dahlin06673e32015-11-23 13:24:23 -08001957status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1958 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1959}
1960
Jeff Brown5707dbf2011-09-23 21:17:56 -07001961status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1962{
Jeff Brown13b16042014-11-11 16:44:25 -08001963 int32_t blobType;
1964 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001965 if (status) return status;
1966
Jeff Brown13b16042014-11-11 16:44:25 -08001967 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001968 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001969 const void* ptr = readInplace(len);
1970 if (!ptr) return BAD_VALUE;
1971
Jeff Brown13b16042014-11-11 16:44:25 -08001972 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001973 return NO_ERROR;
1974 }
1975
Steve Block6807e592011-10-20 11:56:00 +01001976 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001977 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001978 int fd = readFileDescriptor();
1979 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1980
Jeff Brown13b16042014-11-11 16:44:25 -08001981 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1982 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001983 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001984
Jeff Brown13b16042014-11-11 16:44:25 -08001985 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001986 return NO_ERROR;
1987}
1988
Mathias Agopiane1424282013-07-29 21:24:40 -07001989status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001990{
1991 // size
1992 const size_t len = this->readInt32();
1993 const size_t fd_count = this->readInt32();
1994
Nick Kralevichb6b14232015-04-02 09:36:02 -07001995 if (len > INT32_MAX) {
1996 // don't accept size_t values which may have come from an
1997 // inadvertent conversion from a negative int.
1998 return BAD_VALUE;
1999 }
2000
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002001 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002002 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002003 if (buf == NULL)
2004 return BAD_VALUE;
2005
2006 int* fds = NULL;
2007 if (fd_count) {
2008 fds = new int[fd_count];
2009 }
2010
2011 status_t err = NO_ERROR;
2012 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002013 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002014 if (fds[i] < 0) {
2015 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002016 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2017 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002018 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002019 }
2020
2021 if (err == NO_ERROR) {
2022 err = val.unflatten(buf, len, fds, fd_count);
2023 }
2024
2025 if (fd_count) {
2026 delete [] fds;
2027 }
2028
2029 return err;
2030}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002031const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2032{
2033 const size_t DPOS = mDataPos;
2034 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2035 const flat_binder_object* obj
2036 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2037 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002038 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002039 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040 // the object list, so we don't want to check for it when
2041 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002042 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043 return obj;
2044 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002047 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048 const size_t N = mObjectsSize;
2049 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002052 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055 // Start at the current hint position, looking for an object at
2056 // the current data position.
2057 if (opos < N) {
2058 while (opos < (N-1) && OBJS[opos] < DPOS) {
2059 opos++;
2060 }
2061 } else {
2062 opos = N-1;
2063 }
2064 if (OBJS[opos] == DPOS) {
2065 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 this, DPOS, opos);
2068 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002069 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070 return obj;
2071 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002073 // Look backwards for it...
2074 while (opos > 0 && OBJS[opos] > DPOS) {
2075 opos--;
2076 }
2077 if (OBJS[opos] == DPOS) {
2078 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002079 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 this, DPOS, opos);
2081 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002082 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 return obj;
2084 }
2085 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002086 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 -07002087 this, DPOS);
2088 }
2089 return NULL;
2090}
2091
2092void Parcel::closeFileDescriptors()
2093{
2094 size_t i = mObjectsSize;
2095 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002096 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 }
2098 while (i > 0) {
2099 i--;
2100 const flat_binder_object* flat
2101 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2102 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002103 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002104 close(flat->handle);
2105 }
2106 }
2107}
2108
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002109uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002111 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112}
2113
2114size_t Parcel::ipcDataSize() const
2115{
2116 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2117}
2118
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002119uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002121 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122}
2123
2124size_t Parcel::ipcObjectsCount() const
2125{
2126 return mObjectsSize;
2127}
2128
2129void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002130 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002132 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 freeDataNoInit();
2134 mError = NO_ERROR;
2135 mData = const_cast<uint8_t*>(data);
2136 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002139 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002140 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 mObjectsSize = mObjectsCapacity = objectsCount;
2142 mNextObjectHint = 0;
2143 mOwner = relFunc;
2144 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002145 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002146 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002147 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002148 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002149 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002150 mObjectsSize = 0;
2151 break;
2152 }
2153 minOffset = offset + sizeof(flat_binder_object);
2154 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 scanForFds();
2156}
2157
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002158void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159{
2160 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 if (errorCheck() != NO_ERROR) {
2163 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002164 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 } else if (dataSize() > 0) {
2166 const uint8_t* DATA = data();
2167 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002168 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169 const size_t N = objectsCount();
2170 for (size_t i=0; i<N; i++) {
2171 const flat_binder_object* flat
2172 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2173 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2174 << TypeCode(flat->type & 0x7f7f7f00)
2175 << " = " << flat->binder;
2176 }
2177 } else {
2178 to << "NULL";
2179 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 to << ")";
2182}
2183
2184void Parcel::releaseObjects()
2185{
2186 const sp<ProcessState> proc(ProcessState::self());
2187 size_t i = mObjectsSize;
2188 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002189 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 while (i > 0) {
2191 i--;
2192 const flat_binder_object* flat
2193 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002194 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002195 }
2196}
2197
2198void Parcel::acquireObjects()
2199{
2200 const sp<ProcessState> proc(ProcessState::self());
2201 size_t i = mObjectsSize;
2202 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002203 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 while (i > 0) {
2205 i--;
2206 const flat_binder_object* flat
2207 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002208 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 }
2210}
2211
2212void Parcel::freeData()
2213{
2214 freeDataNoInit();
2215 initState();
2216}
2217
2218void Parcel::freeDataNoInit()
2219{
2220 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002221 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002222 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2224 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002225 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002227 if (mData) {
2228 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002229 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002230 if (mDataCapacity <= gParcelGlobalAllocSize) {
2231 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2232 } else {
2233 gParcelGlobalAllocSize = 0;
2234 }
2235 if (gParcelGlobalAllocCount > 0) {
2236 gParcelGlobalAllocCount--;
2237 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002238 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002239 free(mData);
2240 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 if (mObjects) free(mObjects);
2242 }
2243}
2244
2245status_t Parcel::growData(size_t len)
2246{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002247 if (len > INT32_MAX) {
2248 // don't accept size_t values which may have come from an
2249 // inadvertent conversion from a negative int.
2250 return BAD_VALUE;
2251 }
2252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 size_t newSize = ((mDataSize+len)*3)/2;
2254 return (newSize <= mDataSize)
2255 ? (status_t) NO_MEMORY
2256 : continueWrite(newSize);
2257}
2258
2259status_t Parcel::restartWrite(size_t desired)
2260{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002261 if (desired > INT32_MAX) {
2262 // don't accept size_t values which may have come from an
2263 // inadvertent conversion from a negative int.
2264 return BAD_VALUE;
2265 }
2266
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 if (mOwner) {
2268 freeData();
2269 return continueWrite(desired);
2270 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 uint8_t* data = (uint8_t*)realloc(mData, desired);
2273 if (!data && desired > mDataCapacity) {
2274 mError = NO_MEMORY;
2275 return NO_MEMORY;
2276 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002279
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002281 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002282 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002283 gParcelGlobalAllocSize += desired;
2284 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002285 if (!mData) {
2286 gParcelGlobalAllocCount++;
2287 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002288 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002289 mData = data;
2290 mDataCapacity = desired;
2291 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002292
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002294 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2295 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002297 free(mObjects);
2298 mObjects = NULL;
2299 mObjectsSize = mObjectsCapacity = 0;
2300 mNextObjectHint = 0;
2301 mHasFds = false;
2302 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002303 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 return NO_ERROR;
2306}
2307
2308status_t Parcel::continueWrite(size_t desired)
2309{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002310 if (desired > INT32_MAX) {
2311 // don't accept size_t values which may have come from an
2312 // inadvertent conversion from a negative int.
2313 return BAD_VALUE;
2314 }
2315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 // If shrinking, first adjust for any objects that appear
2317 // after the new data size.
2318 size_t objectsSize = mObjectsSize;
2319 if (desired < mDataSize) {
2320 if (desired == 0) {
2321 objectsSize = 0;
2322 } else {
2323 while (objectsSize > 0) {
2324 if (mObjects[objectsSize-1] < desired)
2325 break;
2326 objectsSize--;
2327 }
2328 }
2329 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 if (mOwner) {
2332 // If the size is going to zero, just release the owner's data.
2333 if (desired == 0) {
2334 freeData();
2335 return NO_ERROR;
2336 }
2337
2338 // If there is a different owner, we need to take
2339 // posession.
2340 uint8_t* data = (uint8_t*)malloc(desired);
2341 if (!data) {
2342 mError = NO_MEMORY;
2343 return NO_MEMORY;
2344 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002345 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002346
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002348 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002350 free(data);
2351
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 mError = NO_MEMORY;
2353 return NO_MEMORY;
2354 }
2355
2356 // Little hack to only acquire references on objects
2357 // we will be keeping.
2358 size_t oldObjectsSize = mObjectsSize;
2359 mObjectsSize = objectsSize;
2360 acquireObjects();
2361 mObjectsSize = oldObjectsSize;
2362 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 if (mData) {
2365 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2366 }
2367 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002368 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002370 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002371 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2372 mOwner = NULL;
2373
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002374 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002375 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002376 gParcelGlobalAllocSize += desired;
2377 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002378 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 mData = data;
2381 mObjects = objects;
2382 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002383 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 mDataCapacity = desired;
2385 mObjectsSize = mObjectsCapacity = objectsSize;
2386 mNextObjectHint = 0;
2387
2388 } else if (mData) {
2389 if (objectsSize < mObjectsSize) {
2390 // Need to release refs on any objects we are dropping.
2391 const sp<ProcessState> proc(ProcessState::self());
2392 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2393 const flat_binder_object* flat
2394 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2395 if (flat->type == BINDER_TYPE_FD) {
2396 // will need to rescan because we may have lopped off the only FDs
2397 mFdsKnown = false;
2398 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002399 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002401 binder_size_t* objects =
2402 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 if (objects) {
2404 mObjects = objects;
2405 }
2406 mObjectsSize = objectsSize;
2407 mNextObjectHint = 0;
2408 }
2409
2410 // We own the data, so we can just do a realloc().
2411 if (desired > mDataCapacity) {
2412 uint8_t* data = (uint8_t*)realloc(mData, desired);
2413 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002414 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2415 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002416 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002417 gParcelGlobalAllocSize += desired;
2418 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002419 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 mData = data;
2421 mDataCapacity = desired;
2422 } else if (desired > mDataCapacity) {
2423 mError = NO_MEMORY;
2424 return NO_MEMORY;
2425 }
2426 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002427 if (mDataSize > desired) {
2428 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002430 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 if (mDataPos > desired) {
2432 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 }
2435 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 } else {
2438 // This is the first data. Easy!
2439 uint8_t* data = (uint8_t*)malloc(desired);
2440 if (!data) {
2441 mError = NO_MEMORY;
2442 return NO_MEMORY;
2443 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 if(!(mDataCapacity == 0 && mObjects == NULL
2446 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002447 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002449
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002450 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002451 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002452 gParcelGlobalAllocSize += desired;
2453 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002454 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002455
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 mData = data;
2457 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002458 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2459 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 mDataCapacity = desired;
2461 }
2462
2463 return NO_ERROR;
2464}
2465
2466void Parcel::initState()
2467{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002468 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002469 mError = NO_ERROR;
2470 mData = 0;
2471 mDataSize = 0;
2472 mDataCapacity = 0;
2473 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002474 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2475 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 mObjects = NULL;
2477 mObjectsSize = 0;
2478 mObjectsCapacity = 0;
2479 mNextObjectHint = 0;
2480 mHasFds = false;
2481 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002482 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002484 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485}
2486
2487void Parcel::scanForFds() const
2488{
2489 bool hasFds = false;
2490 for (size_t i=0; i<mObjectsSize; i++) {
2491 const flat_binder_object* flat
2492 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2493 if (flat->type == BINDER_TYPE_FD) {
2494 hasFds = true;
2495 break;
2496 }
2497 }
2498 mHasFds = hasFds;
2499 mFdsKnown = true;
2500}
2501
Dan Sandleraa5c2342015-04-10 10:08:45 -04002502size_t Parcel::getBlobAshmemSize() const
2503{
Adrian Roos6bb31142015-10-22 16:46:12 -07002504 // This used to return the size of all blobs that were written to ashmem, now we're returning
2505 // the ashmem currently referenced by this Parcel, which should be equivalent.
2506 // TODO: Remove method once ABI can be changed.
2507 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002508}
2509
Adrian Rooscbf37262015-10-22 16:12:53 -07002510size_t Parcel::getOpenAshmemSize() const
2511{
2512 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002513}
2514
2515// --- Parcel::Blob ---
2516
2517Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002518 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002519}
2520
2521Parcel::Blob::~Blob() {
2522 release();
2523}
2524
2525void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002526 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002527 ::munmap(mData, mSize);
2528 }
2529 clear();
2530}
2531
Jeff Brown13b16042014-11-11 16:44:25 -08002532void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2533 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002534 mData = data;
2535 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002536 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002537}
2538
2539void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002540 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002541 mData = NULL;
2542 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002543 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002544}
2545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546}; // namespace android