blob: 259c77f367fab950ee20edb96c3f3715689ab7b1 [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 Dahlin185d3442016-02-09 11:08:35 -0800813namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800814
Casey Dahlin185d3442016-02-09 11:08:35 -0800815template<typename T>
816status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700817{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700818 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700819 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700820 status = BAD_VALUE;
821 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700822 }
823
Casey Dahlin185d3442016-02-09 11:08:35 -0800824 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700825 if (status != OK) {
826 return status;
827 }
828
Casey Dahlin185d3442016-02-09 11:08:35 -0800829 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700830 if (!data) {
831 status = BAD_VALUE;
832 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700833 }
834
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700835 memcpy(data, val.data(), val.size());
836 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700837}
838
Casey Dahlin185d3442016-02-09 11:08:35 -0800839template<typename T>
840status_t writeByteVectorInternalPtr(Parcel* parcel,
841 const std::unique_ptr<std::vector<T>>& val)
842{
843 if (!val) {
844 return parcel->writeInt32(-1);
845 }
846
847 return writeByteVectorInternal(parcel, *val);
848}
849
850} // namespace
851
852status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
853 return writeByteVectorInternal(this, val);
854}
855
856status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
857{
858 return writeByteVectorInternalPtr(this, val);
859}
860
861status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
862 return writeByteVectorInternal(this, val);
863}
864
865status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
866{
867 return writeByteVectorInternalPtr(this, val);
868}
869
Casey Dahlin451ff582015-10-19 18:12:18 -0700870status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
871{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800872 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700873}
874
Casey Dahlinb9872622015-11-25 15:09:45 -0800875status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeInt32);
878}
879
Casey Dahlin451ff582015-10-19 18:12:18 -0700880status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
881{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800882 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700883}
884
Casey Dahlinb9872622015-11-25 15:09:45 -0800885status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
886{
887 return writeNullableTypedVector(val, &Parcel::writeInt64);
888}
889
Casey Dahlin451ff582015-10-19 18:12:18 -0700890status_t Parcel::writeFloatVector(const std::vector<float>& val)
891{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800892 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700893}
894
Casey Dahlinb9872622015-11-25 15:09:45 -0800895status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
896{
897 return writeNullableTypedVector(val, &Parcel::writeFloat);
898}
899
Casey Dahlin451ff582015-10-19 18:12:18 -0700900status_t Parcel::writeDoubleVector(const std::vector<double>& val)
901{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800902 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700903}
904
Casey Dahlinb9872622015-11-25 15:09:45 -0800905status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
906{
907 return writeNullableTypedVector(val, &Parcel::writeDouble);
908}
909
Casey Dahlin451ff582015-10-19 18:12:18 -0700910status_t Parcel::writeBoolVector(const std::vector<bool>& val)
911{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800912 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700913}
914
Casey Dahlinb9872622015-11-25 15:09:45 -0800915status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
916{
917 return writeNullableTypedVector(val, &Parcel::writeBool);
918}
919
Casey Dahlin451ff582015-10-19 18:12:18 -0700920status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
921{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800922 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700923}
924
Casey Dahlinb9872622015-11-25 15:09:45 -0800925status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
926{
927 return writeNullableTypedVector(val, &Parcel::writeChar);
928}
929
Casey Dahlin451ff582015-10-19 18:12:18 -0700930status_t Parcel::writeString16Vector(const std::vector<String16>& val)
931{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800932 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700933}
934
Casey Dahlinb9872622015-11-25 15:09:45 -0800935status_t Parcel::writeString16Vector(
936 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
937{
938 return writeNullableTypedVector(val, &Parcel::writeString16);
939}
940
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800941status_t Parcel::writeUtf8VectorAsUtf16Vector(
942 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
943 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
944}
945
946status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
947 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
948}
949
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950status_t Parcel::writeInt32(int32_t val)
951{
Andreas Huber84a6d042009-08-17 13:33:27 -0700952 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800954
955status_t Parcel::writeUint32(uint32_t val)
956{
957 return writeAligned(val);
958}
959
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700960status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700961 if (len > INT32_MAX) {
962 // don't accept size_t values which may have come from an
963 // inadvertent conversion from a negative int.
964 return BAD_VALUE;
965 }
966
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700967 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700968 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700969 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700970 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700971 if (ret == NO_ERROR) {
972 ret = write(val, len * sizeof(*val));
973 }
974 return ret;
975}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700976status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700977 if (len > INT32_MAX) {
978 // don't accept size_t values which may have come from an
979 // inadvertent conversion from a negative int.
980 return BAD_VALUE;
981 }
982
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700983 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700984 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700985 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700986 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700987 if (ret == NO_ERROR) {
988 ret = write(val, len * sizeof(*val));
989 }
990 return ret;
991}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992
Casey Dahlind6848f52015-10-15 15:44:59 -0700993status_t Parcel::writeBool(bool val)
994{
995 return writeInt32(int32_t(val));
996}
997
998status_t Parcel::writeChar(char16_t val)
999{
1000 return writeInt32(int32_t(val));
1001}
1002
1003status_t Parcel::writeByte(int8_t val)
1004{
1005 return writeInt32(int32_t(val));
1006}
1007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008status_t Parcel::writeInt64(int64_t val)
1009{
Andreas Huber84a6d042009-08-17 13:33:27 -07001010 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011}
1012
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001013status_t Parcel::writeUint64(uint64_t val)
1014{
1015 return writeAligned(val);
1016}
1017
Serban Constantinescuf683e012013-11-05 16:53:55 +00001018status_t Parcel::writePointer(uintptr_t val)
1019{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001020 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001021}
1022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023status_t Parcel::writeFloat(float val)
1024{
Andreas Huber84a6d042009-08-17 13:33:27 -07001025 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026}
1027
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001028#if defined(__mips__) && defined(__mips_hard_float)
1029
1030status_t Parcel::writeDouble(double val)
1031{
1032 union {
1033 double d;
1034 unsigned long long ll;
1035 } u;
1036 u.d = val;
1037 return writeAligned(u.ll);
1038}
1039
1040#else
1041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042status_t Parcel::writeDouble(double val)
1043{
Andreas Huber84a6d042009-08-17 13:33:27 -07001044 return writeAligned(val);
1045}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001047#endif
1048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049status_t Parcel::writeCString(const char* str)
1050{
1051 return write(str, strlen(str)+1);
1052}
1053
1054status_t Parcel::writeString8(const String8& str)
1055{
1056 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001057 // only write string if its length is more than zero characters,
1058 // as readString8 will only read if the length field is non-zero.
1059 // this is slightly different from how writeString16 works.
1060 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001061 err = write(str.string(), str.bytes()+1);
1062 }
1063 return err;
1064}
1065
Casey Dahlinb9872622015-11-25 15:09:45 -08001066status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1067{
1068 if (!str) {
1069 return writeInt32(-1);
1070 }
1071
1072 return writeString16(*str);
1073}
1074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075status_t Parcel::writeString16(const String16& str)
1076{
1077 return writeString16(str.string(), str.size());
1078}
1079
1080status_t Parcel::writeString16(const char16_t* str, size_t len)
1081{
1082 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001083
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 status_t err = writeInt32(len);
1085 if (err == NO_ERROR) {
1086 len *= sizeof(char16_t);
1087 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1088 if (data) {
1089 memcpy(data, str, len);
1090 *reinterpret_cast<char16_t*>(data+len) = 0;
1091 return NO_ERROR;
1092 }
1093 err = mError;
1094 }
1095 return err;
1096}
1097
1098status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1099{
1100 return flatten_binder(ProcessState::self(), val, this);
1101}
1102
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001103status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1104{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001105 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001106}
1107
Casey Dahlinb9872622015-11-25 15:09:45 -08001108status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1109{
1110 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1111}
1112
1113status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001114 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001115}
1116
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001117status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001118 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001119}
1120
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001121status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1122{
1123 return flatten_binder(ProcessState::self(), val, this);
1124}
1125
Casey Dahlinb9872622015-11-25 15:09:45 -08001126status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1127 if (!parcelable) {
1128 return writeInt32(0);
1129 }
1130
1131 return writeParcelable(*parcelable);
1132}
1133
Christopher Wiley97f048d2015-11-19 06:49:05 -08001134status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1135 status_t status = writeInt32(1); // parcelable is not null.
1136 if (status != OK) {
1137 return status;
1138 }
1139 return parcelable.writeToParcel(this);
1140}
1141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001144 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001145 return BAD_TYPE;
1146
1147 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001151 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001152 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001154 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1155 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156
1157 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001158 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 return err;
1160 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001161 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 return err;
1163}
1164
Jeff Brown93ff1f92011-11-04 19:01:44 -07001165status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166{
1167 flat_binder_object obj;
1168 obj.type = BINDER_TYPE_FD;
1169 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001170 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001172 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001173 return writeObject(obj, true);
1174}
1175
1176status_t Parcel::writeDupFileDescriptor(int fd)
1177{
Jeff Brownd341c712011-11-04 20:19:33 -07001178 int dupFd = dup(fd);
1179 if (dupFd < 0) {
1180 return -errno;
1181 }
1182 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001183 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001184 close(dupFd);
1185 }
1186 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001187}
1188
Casey Dahlin06673e32015-11-23 13:24:23 -08001189status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1190 return writeDupFileDescriptor(fd.get());
1191}
1192
1193status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1194 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1195}
1196
Casey Dahlinb9872622015-11-25 15:09:45 -08001197status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1198 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1199}
1200
Jeff Brown13b16042014-11-11 16:44:25 -08001201status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001202{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001203 if (len > INT32_MAX) {
1204 // don't accept size_t values which may have come from an
1205 // inadvertent conversion from a negative int.
1206 return BAD_VALUE;
1207 }
1208
Jeff Brown13b16042014-11-11 16:44:25 -08001209 status_t status;
1210 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001211 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001212 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001213 if (status) return status;
1214
1215 void* ptr = writeInplace(len);
1216 if (!ptr) return NO_MEMORY;
1217
Jeff Brown13b16042014-11-11 16:44:25 -08001218 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001219 return NO_ERROR;
1220 }
1221
Steve Block6807e592011-10-20 11:56:00 +01001222 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 int fd = ashmem_create_region("Parcel Blob", len);
1224 if (fd < 0) return NO_MEMORY;
1225
1226 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1227 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001228 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 } else {
1230 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1231 if (ptr == MAP_FAILED) {
1232 status = -errno;
1233 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001234 if (!mutableCopy) {
1235 result = ashmem_set_prot_region(fd, PROT_READ);
1236 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001238 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001240 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001242 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001244 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 return NO_ERROR;
1246 }
1247 }
1248 }
1249 }
1250 ::munmap(ptr, len);
1251 }
1252 ::close(fd);
1253 return status;
1254}
1255
Jeff Brown13b16042014-11-11 16:44:25 -08001256status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1257{
1258 // Must match up with what's done in writeBlob.
1259 if (!mAllowFds) return FDS_NOT_ALLOWED;
1260 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1261 if (status) return status;
1262 return writeDupFileDescriptor(fd);
1263}
1264
Mathias Agopiane1424282013-07-29 21:24:40 -07001265status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001266{
1267 status_t err;
1268
1269 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001270 const size_t len = val.getFlattenedSize();
1271 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272
Nick Kralevichb6b14232015-04-02 09:36:02 -07001273 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1274 // don't accept size_t values which may have come from an
1275 // inadvertent conversion from a negative int.
1276 return BAD_VALUE;
1277 }
1278
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001279 err = this->writeInt32(len);
1280 if (err) return err;
1281
1282 err = this->writeInt32(fd_count);
1283 if (err) return err;
1284
1285 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001286 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001287 if (buf == NULL)
1288 return BAD_VALUE;
1289
1290 int* fds = NULL;
1291 if (fd_count) {
1292 fds = new int[fd_count];
1293 }
1294
1295 err = val.flatten(buf, len, fds, fd_count);
1296 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1297 err = this->writeDupFileDescriptor( fds[i] );
1298 }
1299
1300 if (fd_count) {
1301 delete [] fds;
1302 }
1303
1304 return err;
1305}
1306
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1308{
1309 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1310 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1311 if (enoughData && enoughObjects) {
1312restart_write:
1313 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001314
Christopher Tate98e67d32015-06-03 18:44:15 -07001315 // remember if it's a file descriptor
1316 if (val.type == BINDER_TYPE_FD) {
1317 if (!mAllowFds) {
1318 // fail before modifying our object index
1319 return FDS_NOT_ALLOWED;
1320 }
1321 mHasFds = mFdsKnown = true;
1322 }
1323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001325 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001327 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 mObjectsSize++;
1329 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 return finishWrite(sizeof(flat_binder_object));
1332 }
1333
1334 if (!enoughData) {
1335 const status_t err = growData(sizeof(val));
1336 if (err != NO_ERROR) return err;
1337 }
1338 if (!enoughObjects) {
1339 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001340 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001341 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342 if (objects == NULL) return NO_MEMORY;
1343 mObjects = objects;
1344 mObjectsCapacity = newSize;
1345 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001346
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347 goto restart_write;
1348}
1349
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001350status_t Parcel::writeNoException()
1351{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001352 binder::Status status;
1353 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001354}
1355
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001356void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357{
1358 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1359}
1360
1361status_t Parcel::read(void* outData, size_t len) const
1362{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001363 if (len > INT32_MAX) {
1364 // don't accept size_t values which may have come from an
1365 // inadvertent conversion from a negative int.
1366 return BAD_VALUE;
1367 }
1368
1369 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1370 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001372 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001373 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001374 return NO_ERROR;
1375 }
1376 return NOT_ENOUGH_DATA;
1377}
1378
1379const void* Parcel::readInplace(size_t len) const
1380{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001381 if (len > INT32_MAX) {
1382 // don't accept size_t values which may have come from an
1383 // inadvertent conversion from a negative int.
1384 return NULL;
1385 }
1386
1387 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1388 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001390 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001391 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392 return data;
1393 }
1394 return NULL;
1395}
1396
Andreas Huber84a6d042009-08-17 13:33:27 -07001397template<class T>
1398status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001399 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001400
1401 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001403 mDataPos += sizeof(T);
1404 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405 return NO_ERROR;
1406 } else {
1407 return NOT_ENOUGH_DATA;
1408 }
1409}
1410
Andreas Huber84a6d042009-08-17 13:33:27 -07001411template<class T>
1412T Parcel::readAligned() const {
1413 T result;
1414 if (readAligned(&result) != NO_ERROR) {
1415 result = 0;
1416 }
1417
1418 return result;
1419}
1420
1421template<class T>
1422status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001423 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001424
1425 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1426restart_write:
1427 *reinterpret_cast<T*>(mData+mDataPos) = val;
1428 return finishWrite(sizeof(val));
1429 }
1430
1431 status_t err = growData(sizeof(val));
1432 if (err == NO_ERROR) goto restart_write;
1433 return err;
1434}
1435
Casey Dahlin185d3442016-02-09 11:08:35 -08001436namespace {
1437
1438template<typename T>
1439status_t readByteVectorInternal(const Parcel* parcel,
1440 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001441 val->clear();
1442
1443 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001444 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001445
1446 if (status != OK) {
1447 return status;
1448 }
1449
Christopher Wiley4db672d2015-11-10 09:44:30 -08001450 if (size < 0) {
1451 status = UNEXPECTED_NULL;
1452 return status;
1453 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001454 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001455 status = BAD_VALUE;
1456 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001457 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001458
Casey Dahlin185d3442016-02-09 11:08:35 -08001459 const void* data = parcel->readInplace(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001460 if (!data) {
1461 status = BAD_VALUE;
1462 return status;
1463 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001464 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001465 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001466
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001467 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001468}
1469
Casey Dahlin185d3442016-02-09 11:08:35 -08001470template<typename T>
1471status_t readByteVectorInternalPtr(
1472 const Parcel* parcel,
1473 std::unique_ptr<std::vector<T>>* val) {
1474 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001475 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001476 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001477 val->reset();
1478
1479 if (status != OK || size < 0) {
1480 return status;
1481 }
1482
Casey Dahlin185d3442016-02-09 11:08:35 -08001483 parcel->setDataPosition(start);
1484 val->reset(new std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001485
Casey Dahlin185d3442016-02-09 11:08:35 -08001486 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001487
1488 if (status != OK) {
1489 val->reset();
1490 }
1491
1492 return status;
1493}
1494
Casey Dahlin185d3442016-02-09 11:08:35 -08001495} // namespace
1496
1497status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1498 return readByteVectorInternal(this, val);
1499}
1500
1501status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1502 return readByteVectorInternal(this, val);
1503}
1504
1505status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1506 return readByteVectorInternalPtr(this, val);
1507}
1508
1509status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1510 return readByteVectorInternalPtr(this, val);
1511}
1512
Casey Dahlinb9872622015-11-25 15:09:45 -08001513status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1514 return readNullableTypedVector(val, &Parcel::readInt32);
1515}
1516
Casey Dahlin451ff582015-10-19 18:12:18 -07001517status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001518 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001519}
1520
Casey Dahlinb9872622015-11-25 15:09:45 -08001521status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1522 return readNullableTypedVector(val, &Parcel::readInt64);
1523}
1524
Casey Dahlin451ff582015-10-19 18:12:18 -07001525status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001526 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001527}
1528
Casey Dahlinb9872622015-11-25 15:09:45 -08001529status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1530 return readNullableTypedVector(val, &Parcel::readFloat);
1531}
1532
Casey Dahlin451ff582015-10-19 18:12:18 -07001533status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001534 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001535}
1536
Casey Dahlinb9872622015-11-25 15:09:45 -08001537status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1538 return readNullableTypedVector(val, &Parcel::readDouble);
1539}
1540
Casey Dahlin451ff582015-10-19 18:12:18 -07001541status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001542 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001543}
1544
Casey Dahlinb9872622015-11-25 15:09:45 -08001545status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1546 const int32_t start = dataPosition();
1547 int32_t size;
1548 status_t status = readInt32(&size);
1549 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001550
Casey Dahlinb9872622015-11-25 15:09:45 -08001551 if (status != OK || size < 0) {
1552 return status;
1553 }
1554
1555 setDataPosition(start);
1556 val->reset(new std::vector<bool>());
1557
1558 status = readBoolVector(val->get());
1559
1560 if (status != OK) {
1561 val->reset();
1562 }
1563
1564 return status;
1565}
1566
1567status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001568 int32_t size;
1569 status_t status = readInt32(&size);
1570
1571 if (status != OK) {
1572 return status;
1573 }
1574
1575 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001576 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001577 }
1578
1579 val->resize(size);
1580
1581 /* C++ bool handling means a vector of bools isn't necessarily addressable
1582 * (we might use individual bits)
1583 */
Christopher Wiley97887982015-10-27 16:33:47 -07001584 bool data;
1585 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001586 status = readBool(&data);
1587 (*val)[i] = data;
1588
1589 if (status != OK) {
1590 return status;
1591 }
1592 }
1593
1594 return OK;
1595}
1596
Casey Dahlinb9872622015-11-25 15:09:45 -08001597status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1598 return readNullableTypedVector(val, &Parcel::readChar);
1599}
1600
Casey Dahlin451ff582015-10-19 18:12:18 -07001601status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001602 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001603}
1604
Casey Dahlinb9872622015-11-25 15:09:45 -08001605status_t Parcel::readString16Vector(
1606 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1607 return readNullableTypedVector(val, &Parcel::readString16);
1608}
1609
Casey Dahlin451ff582015-10-19 18:12:18 -07001610status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001611 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001612}
1613
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001614status_t Parcel::readUtf8VectorFromUtf16Vector(
1615 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1616 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1617}
1618
1619status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1620 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1621}
Casey Dahlin451ff582015-10-19 18:12:18 -07001622
Andreas Huber84a6d042009-08-17 13:33:27 -07001623status_t Parcel::readInt32(int32_t *pArg) const
1624{
1625 return readAligned(pArg);
1626}
1627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628int32_t Parcel::readInt32() const
1629{
Andreas Huber84a6d042009-08-17 13:33:27 -07001630 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001631}
1632
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001633status_t Parcel::readUint32(uint32_t *pArg) const
1634{
1635 return readAligned(pArg);
1636}
1637
1638uint32_t Parcel::readUint32() const
1639{
1640 return readAligned<uint32_t>();
1641}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642
1643status_t Parcel::readInt64(int64_t *pArg) const
1644{
Andreas Huber84a6d042009-08-17 13:33:27 -07001645 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646}
1647
1648
1649int64_t Parcel::readInt64() const
1650{
Andreas Huber84a6d042009-08-17 13:33:27 -07001651 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652}
1653
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001654status_t Parcel::readUint64(uint64_t *pArg) const
1655{
1656 return readAligned(pArg);
1657}
1658
1659uint64_t Parcel::readUint64() const
1660{
1661 return readAligned<uint64_t>();
1662}
1663
Serban Constantinescuf683e012013-11-05 16:53:55 +00001664status_t Parcel::readPointer(uintptr_t *pArg) const
1665{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001666 status_t ret;
1667 binder_uintptr_t ptr;
1668 ret = readAligned(&ptr);
1669 if (!ret)
1670 *pArg = ptr;
1671 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001672}
1673
1674uintptr_t Parcel::readPointer() const
1675{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001676 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001677}
1678
1679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680status_t Parcel::readFloat(float *pArg) const
1681{
Andreas Huber84a6d042009-08-17 13:33:27 -07001682 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683}
1684
1685
1686float Parcel::readFloat() const
1687{
Andreas Huber84a6d042009-08-17 13:33:27 -07001688 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689}
1690
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001691#if defined(__mips__) && defined(__mips_hard_float)
1692
1693status_t Parcel::readDouble(double *pArg) const
1694{
1695 union {
1696 double d;
1697 unsigned long long ll;
1698 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001699 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001700 status_t status;
1701 status = readAligned(&u.ll);
1702 *pArg = u.d;
1703 return status;
1704}
1705
1706double Parcel::readDouble() const
1707{
1708 union {
1709 double d;
1710 unsigned long long ll;
1711 } u;
1712 u.ll = readAligned<unsigned long long>();
1713 return u.d;
1714}
1715
1716#else
1717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718status_t Parcel::readDouble(double *pArg) const
1719{
Andreas Huber84a6d042009-08-17 13:33:27 -07001720 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001721}
1722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723double Parcel::readDouble() const
1724{
Andreas Huber84a6d042009-08-17 13:33:27 -07001725 return readAligned<double>();
1726}
1727
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001728#endif
1729
Andreas Huber84a6d042009-08-17 13:33:27 -07001730status_t Parcel::readIntPtr(intptr_t *pArg) const
1731{
1732 return readAligned(pArg);
1733}
1734
1735
1736intptr_t Parcel::readIntPtr() const
1737{
1738 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001739}
1740
Casey Dahlind6848f52015-10-15 15:44:59 -07001741status_t Parcel::readBool(bool *pArg) const
1742{
1743 int32_t tmp;
1744 status_t ret = readInt32(&tmp);
1745 *pArg = (tmp != 0);
1746 return ret;
1747}
1748
1749bool Parcel::readBool() const
1750{
1751 return readInt32() != 0;
1752}
1753
1754status_t Parcel::readChar(char16_t *pArg) const
1755{
1756 int32_t tmp;
1757 status_t ret = readInt32(&tmp);
1758 *pArg = char16_t(tmp);
1759 return ret;
1760}
1761
1762char16_t Parcel::readChar() const
1763{
1764 return char16_t(readInt32());
1765}
1766
1767status_t Parcel::readByte(int8_t *pArg) const
1768{
1769 int32_t tmp;
1770 status_t ret = readInt32(&tmp);
1771 *pArg = int8_t(tmp);
1772 return ret;
1773}
1774
1775int8_t Parcel::readByte() const
1776{
1777 return int8_t(readInt32());
1778}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001780status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1781 size_t utf16Size = 0;
1782 const char16_t* src = readString16Inplace(&utf16Size);
1783 if (!src) {
1784 return UNEXPECTED_NULL;
1785 }
1786
1787 // Save ourselves the trouble, we're done.
1788 if (utf16Size == 0u) {
1789 str->clear();
1790 return NO_ERROR;
1791 }
1792
1793 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1794 if (utf8Size < 0) {
1795 return BAD_VALUE;
1796 }
1797 // Note that while it is probably safe to assume string::resize keeps a
1798 // spare byte around for the trailing null, we're going to be explicit.
1799 str->resize(utf8Size + 1);
1800 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1801 str->resize(utf8Size);
1802 return NO_ERROR;
1803}
1804
1805status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1806 const int32_t start = dataPosition();
1807 int32_t size;
1808 status_t status = readInt32(&size);
1809 str->reset();
1810
1811 if (status != OK || size < 0) {
1812 return status;
1813 }
1814
1815 setDataPosition(start);
1816 str->reset(new std::string());
1817 return readUtf8FromUtf16(str->get());
1818}
1819
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820const char* Parcel::readCString() const
1821{
1822 const size_t avail = mDataSize-mDataPos;
1823 if (avail > 0) {
1824 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1825 // is the string's trailing NUL within the parcel's valid bounds?
1826 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1827 if (eos) {
1828 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001829 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001830 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831 return str;
1832 }
1833 }
1834 return NULL;
1835}
1836
1837String8 Parcel::readString8() const
1838{
1839 int32_t size = readInt32();
1840 // watch for potential int overflow adding 1 for trailing NUL
1841 if (size > 0 && size < INT32_MAX) {
1842 const char* str = (const char*)readInplace(size+1);
1843 if (str) return String8(str, size);
1844 }
1845 return String8();
1846}
1847
1848String16 Parcel::readString16() const
1849{
1850 size_t len;
1851 const char16_t* str = readString16Inplace(&len);
1852 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001853 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854 return String16();
1855}
1856
Casey Dahlinb9872622015-11-25 15:09:45 -08001857status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1858{
1859 const int32_t start = dataPosition();
1860 int32_t size;
1861 status_t status = readInt32(&size);
1862 pArg->reset();
1863
1864 if (status != OK || size < 0) {
1865 return status;
1866 }
1867
1868 setDataPosition(start);
1869 pArg->reset(new String16());
1870
1871 status = readString16(pArg->get());
1872
1873 if (status != OK) {
1874 pArg->reset();
1875 }
1876
1877 return status;
1878}
1879
Casey Dahlin451ff582015-10-19 18:12:18 -07001880status_t Parcel::readString16(String16* pArg) const
1881{
1882 size_t len;
1883 const char16_t* str = readString16Inplace(&len);
1884 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001885 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001886 return 0;
1887 } else {
1888 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001889 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001890 }
1891}
1892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1894{
1895 int32_t size = readInt32();
1896 // watch for potential int overflow from size+1
1897 if (size >= 0 && size < INT32_MAX) {
1898 *outLen = size;
1899 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1900 if (str != NULL) {
1901 return str;
1902 }
1903 }
1904 *outLen = 0;
1905 return NULL;
1906}
1907
Casey Dahlinf0c13772015-10-27 18:33:56 -07001908status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1909{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001910 status_t status = readNullableStrongBinder(val);
1911 if (status == OK && !val->get()) {
1912 status = UNEXPECTED_NULL;
1913 }
1914 return status;
1915}
1916
1917status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1918{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001919 return unflatten_binder(ProcessState::self(), *this, val);
1920}
1921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922sp<IBinder> Parcel::readStrongBinder() const
1923{
1924 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001925 // Note that a lot of code in Android reads binders by hand with this
1926 // method, and that code has historically been ok with getting nullptr
1927 // back (while ignoring error codes).
1928 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001929 return val;
1930}
1931
1932wp<IBinder> Parcel::readWeakBinder() const
1933{
1934 wp<IBinder> val;
1935 unflatten_binder(ProcessState::self(), *this, &val);
1936 return val;
1937}
1938
Christopher Wiley97f048d2015-11-19 06:49:05 -08001939status_t Parcel::readParcelable(Parcelable* parcelable) const {
1940 int32_t have_parcelable = 0;
1941 status_t status = readInt32(&have_parcelable);
1942 if (status != OK) {
1943 return status;
1944 }
1945 if (!have_parcelable) {
1946 return UNEXPECTED_NULL;
1947 }
1948 return parcelable->readFromParcel(this);
1949}
1950
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001951int32_t Parcel::readExceptionCode() const
1952{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001953 binder::Status status;
1954 status.readFromParcel(*this);
1955 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001956}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001957
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001958native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001959{
1960 int numFds, numInts;
1961 status_t err;
1962 err = readInt32(&numFds);
1963 if (err != NO_ERROR) return 0;
1964 err = readInt32(&numInts);
1965 if (err != NO_ERROR) return 0;
1966
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001967 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001968 if (!h) {
1969 return 0;
1970 }
1971
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001972 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001973 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001974 if (h->data[i] < 0) err = BAD_VALUE;
1975 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001976 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001977 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001978 native_handle_close(h);
1979 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001980 h = 0;
1981 }
1982 return h;
1983}
1984
1985
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986int Parcel::readFileDescriptor() const
1987{
1988 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001989
1990 if (flat && flat->type == BINDER_TYPE_FD) {
1991 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 return BAD_TYPE;
1995}
1996
Casey Dahlin06673e32015-11-23 13:24:23 -08001997status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1998{
1999 int got = readFileDescriptor();
2000
2001 if (got == BAD_TYPE) {
2002 return BAD_TYPE;
2003 }
2004
2005 val->reset(dup(got));
2006
2007 if (val->get() < 0) {
2008 return BAD_VALUE;
2009 }
2010
2011 return OK;
2012}
2013
2014
Casey Dahlinb9872622015-11-25 15:09:45 -08002015status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
2016 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2017}
2018
Casey Dahlin06673e32015-11-23 13:24:23 -08002019status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
2020 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2021}
2022
Jeff Brown5707dbf2011-09-23 21:17:56 -07002023status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2024{
Jeff Brown13b16042014-11-11 16:44:25 -08002025 int32_t blobType;
2026 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002027 if (status) return status;
2028
Jeff Brown13b16042014-11-11 16:44:25 -08002029 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002030 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002031 const void* ptr = readInplace(len);
2032 if (!ptr) return BAD_VALUE;
2033
Jeff Brown13b16042014-11-11 16:44:25 -08002034 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002035 return NO_ERROR;
2036 }
2037
Steve Block6807e592011-10-20 11:56:00 +01002038 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002039 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002040 int fd = readFileDescriptor();
2041 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2042
Jeff Brown13b16042014-11-11 16:44:25 -08002043 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2044 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002045 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002046
Jeff Brown13b16042014-11-11 16:44:25 -08002047 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002048 return NO_ERROR;
2049}
2050
Mathias Agopiane1424282013-07-29 21:24:40 -07002051status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002052{
2053 // size
2054 const size_t len = this->readInt32();
2055 const size_t fd_count = this->readInt32();
2056
Nick Kralevichb6b14232015-04-02 09:36:02 -07002057 if (len > INT32_MAX) {
2058 // don't accept size_t values which may have come from an
2059 // inadvertent conversion from a negative int.
2060 return BAD_VALUE;
2061 }
2062
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002063 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002064 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002065 if (buf == NULL)
2066 return BAD_VALUE;
2067
2068 int* fds = NULL;
2069 if (fd_count) {
2070 fds = new int[fd_count];
2071 }
2072
2073 status_t err = NO_ERROR;
2074 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002075 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002076 if (fds[i] < 0) {
2077 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002078 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2079 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002080 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002081 }
2082
2083 if (err == NO_ERROR) {
2084 err = val.unflatten(buf, len, fds, fd_count);
2085 }
2086
2087 if (fd_count) {
2088 delete [] fds;
2089 }
2090
2091 return err;
2092}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2094{
2095 const size_t DPOS = mDataPos;
2096 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2097 const flat_binder_object* obj
2098 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2099 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002100 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002101 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 // the object list, so we don't want to check for it when
2103 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002104 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 return obj;
2106 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002107
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002109 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 const size_t N = mObjectsSize;
2111 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002112
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002114 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002115 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117 // Start at the current hint position, looking for an object at
2118 // the current data position.
2119 if (opos < N) {
2120 while (opos < (N-1) && OBJS[opos] < DPOS) {
2121 opos++;
2122 }
2123 } else {
2124 opos = N-1;
2125 }
2126 if (OBJS[opos] == DPOS) {
2127 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002128 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129 this, DPOS, opos);
2130 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002131 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132 return obj;
2133 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 // Look backwards for it...
2136 while (opos > 0 && OBJS[opos] > DPOS) {
2137 opos--;
2138 }
2139 if (OBJS[opos] == DPOS) {
2140 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002141 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142 this, DPOS, opos);
2143 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002144 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145 return obj;
2146 }
2147 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002148 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 -07002149 this, DPOS);
2150 }
2151 return NULL;
2152}
2153
2154void Parcel::closeFileDescriptors()
2155{
2156 size_t i = mObjectsSize;
2157 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002158 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 }
2160 while (i > 0) {
2161 i--;
2162 const flat_binder_object* flat
2163 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2164 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002165 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166 close(flat->handle);
2167 }
2168 }
2169}
2170
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002171uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002173 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174}
2175
2176size_t Parcel::ipcDataSize() const
2177{
2178 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2179}
2180
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002181uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002183 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184}
2185
2186size_t Parcel::ipcObjectsCount() const
2187{
2188 return mObjectsSize;
2189}
2190
2191void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002192 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002194 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002195 freeDataNoInit();
2196 mError = NO_ERROR;
2197 mData = const_cast<uint8_t*>(data);
2198 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002199 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002201 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002202 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002203 mObjectsSize = mObjectsCapacity = objectsCount;
2204 mNextObjectHint = 0;
2205 mOwner = relFunc;
2206 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002207 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002208 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002209 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002210 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002211 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002212 mObjectsSize = 0;
2213 break;
2214 }
2215 minOffset = offset + sizeof(flat_binder_object);
2216 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217 scanForFds();
2218}
2219
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002220void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221{
2222 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002223
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 if (errorCheck() != NO_ERROR) {
2225 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002226 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 } else if (dataSize() > 0) {
2228 const uint8_t* DATA = data();
2229 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002230 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 const size_t N = objectsCount();
2232 for (size_t i=0; i<N; i++) {
2233 const flat_binder_object* flat
2234 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2235 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2236 << TypeCode(flat->type & 0x7f7f7f00)
2237 << " = " << flat->binder;
2238 }
2239 } else {
2240 to << "NULL";
2241 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 to << ")";
2244}
2245
2246void Parcel::releaseObjects()
2247{
2248 const sp<ProcessState> proc(ProcessState::self());
2249 size_t i = mObjectsSize;
2250 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002251 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 while (i > 0) {
2253 i--;
2254 const flat_binder_object* flat
2255 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002256 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 }
2258}
2259
2260void Parcel::acquireObjects()
2261{
2262 const sp<ProcessState> proc(ProcessState::self());
2263 size_t i = mObjectsSize;
2264 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002265 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 while (i > 0) {
2267 i--;
2268 const flat_binder_object* flat
2269 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002270 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002271 }
2272}
2273
2274void Parcel::freeData()
2275{
2276 freeDataNoInit();
2277 initState();
2278}
2279
2280void Parcel::freeDataNoInit()
2281{
2282 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002283 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002284 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2286 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002287 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002288 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002289 if (mData) {
2290 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002291 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002292 if (mDataCapacity <= gParcelGlobalAllocSize) {
2293 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2294 } else {
2295 gParcelGlobalAllocSize = 0;
2296 }
2297 if (gParcelGlobalAllocCount > 0) {
2298 gParcelGlobalAllocCount--;
2299 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002300 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002301 free(mData);
2302 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 if (mObjects) free(mObjects);
2304 }
2305}
2306
2307status_t Parcel::growData(size_t len)
2308{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002309 if (len > INT32_MAX) {
2310 // don't accept size_t values which may have come from an
2311 // inadvertent conversion from a negative int.
2312 return BAD_VALUE;
2313 }
2314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 size_t newSize = ((mDataSize+len)*3)/2;
2316 return (newSize <= mDataSize)
2317 ? (status_t) NO_MEMORY
2318 : continueWrite(newSize);
2319}
2320
2321status_t Parcel::restartWrite(size_t desired)
2322{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002323 if (desired > INT32_MAX) {
2324 // don't accept size_t values which may have come from an
2325 // inadvertent conversion from a negative int.
2326 return BAD_VALUE;
2327 }
2328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 if (mOwner) {
2330 freeData();
2331 return continueWrite(desired);
2332 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 uint8_t* data = (uint8_t*)realloc(mData, desired);
2335 if (!data && desired > mDataCapacity) {
2336 mError = NO_MEMORY;
2337 return NO_MEMORY;
2338 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002343 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002344 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002345 gParcelGlobalAllocSize += desired;
2346 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002347 if (!mData) {
2348 gParcelGlobalAllocCount++;
2349 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002350 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 mData = data;
2352 mDataCapacity = desired;
2353 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2357 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 free(mObjects);
2360 mObjects = NULL;
2361 mObjectsSize = mObjectsCapacity = 0;
2362 mNextObjectHint = 0;
2363 mHasFds = false;
2364 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002365 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002366
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 return NO_ERROR;
2368}
2369
2370status_t Parcel::continueWrite(size_t desired)
2371{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002372 if (desired > INT32_MAX) {
2373 // don't accept size_t values which may have come from an
2374 // inadvertent conversion from a negative int.
2375 return BAD_VALUE;
2376 }
2377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378 // If shrinking, first adjust for any objects that appear
2379 // after the new data size.
2380 size_t objectsSize = mObjectsSize;
2381 if (desired < mDataSize) {
2382 if (desired == 0) {
2383 objectsSize = 0;
2384 } else {
2385 while (objectsSize > 0) {
2386 if (mObjects[objectsSize-1] < desired)
2387 break;
2388 objectsSize--;
2389 }
2390 }
2391 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 if (mOwner) {
2394 // If the size is going to zero, just release the owner's data.
2395 if (desired == 0) {
2396 freeData();
2397 return NO_ERROR;
2398 }
2399
2400 // If there is a different owner, we need to take
2401 // posession.
2402 uint8_t* data = (uint8_t*)malloc(desired);
2403 if (!data) {
2404 mError = NO_MEMORY;
2405 return NO_MEMORY;
2406 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002407 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002410 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002412 free(data);
2413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 mError = NO_MEMORY;
2415 return NO_MEMORY;
2416 }
2417
2418 // Little hack to only acquire references on objects
2419 // we will be keeping.
2420 size_t oldObjectsSize = mObjectsSize;
2421 mObjectsSize = objectsSize;
2422 acquireObjects();
2423 mObjectsSize = oldObjectsSize;
2424 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 if (mData) {
2427 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2428 }
2429 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002430 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002432 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2434 mOwner = NULL;
2435
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002436 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002437 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002438 gParcelGlobalAllocSize += desired;
2439 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002440 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 mData = data;
2443 mObjects = objects;
2444 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002445 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 mDataCapacity = desired;
2447 mObjectsSize = mObjectsCapacity = objectsSize;
2448 mNextObjectHint = 0;
2449
2450 } else if (mData) {
2451 if (objectsSize < mObjectsSize) {
2452 // Need to release refs on any objects we are dropping.
2453 const sp<ProcessState> proc(ProcessState::self());
2454 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2455 const flat_binder_object* flat
2456 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2457 if (flat->type == BINDER_TYPE_FD) {
2458 // will need to rescan because we may have lopped off the only FDs
2459 mFdsKnown = false;
2460 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002461 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002463 binder_size_t* objects =
2464 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 if (objects) {
2466 mObjects = objects;
2467 }
2468 mObjectsSize = objectsSize;
2469 mNextObjectHint = 0;
2470 }
2471
2472 // We own the data, so we can just do a realloc().
2473 if (desired > mDataCapacity) {
2474 uint8_t* data = (uint8_t*)realloc(mData, desired);
2475 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002476 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2477 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002478 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002479 gParcelGlobalAllocSize += desired;
2480 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002481 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 mData = data;
2483 mDataCapacity = desired;
2484 } else if (desired > mDataCapacity) {
2485 mError = NO_MEMORY;
2486 return NO_MEMORY;
2487 }
2488 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002489 if (mDataSize > desired) {
2490 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002492 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 if (mDataPos > desired) {
2494 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 }
2497 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 } else {
2500 // This is the first data. Easy!
2501 uint8_t* data = (uint8_t*)malloc(desired);
2502 if (!data) {
2503 mError = NO_MEMORY;
2504 return NO_MEMORY;
2505 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507 if(!(mDataCapacity == 0 && mObjects == NULL
2508 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002509 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002512 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002513 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002514 gParcelGlobalAllocSize += desired;
2515 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002516 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 mData = data;
2519 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2521 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 mDataCapacity = desired;
2523 }
2524
2525 return NO_ERROR;
2526}
2527
2528void Parcel::initState()
2529{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002530 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 mError = NO_ERROR;
2532 mData = 0;
2533 mDataSize = 0;
2534 mDataCapacity = 0;
2535 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002536 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2537 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 mObjects = NULL;
2539 mObjectsSize = 0;
2540 mObjectsCapacity = 0;
2541 mNextObjectHint = 0;
2542 mHasFds = false;
2543 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002544 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002546 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547}
2548
2549void Parcel::scanForFds() const
2550{
2551 bool hasFds = false;
2552 for (size_t i=0; i<mObjectsSize; i++) {
2553 const flat_binder_object* flat
2554 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2555 if (flat->type == BINDER_TYPE_FD) {
2556 hasFds = true;
2557 break;
2558 }
2559 }
2560 mHasFds = hasFds;
2561 mFdsKnown = true;
2562}
2563
Dan Sandleraa5c2342015-04-10 10:08:45 -04002564size_t Parcel::getBlobAshmemSize() const
2565{
Adrian Roos6bb31142015-10-22 16:46:12 -07002566 // This used to return the size of all blobs that were written to ashmem, now we're returning
2567 // the ashmem currently referenced by this Parcel, which should be equivalent.
2568 // TODO: Remove method once ABI can be changed.
2569 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002570}
2571
Adrian Rooscbf37262015-10-22 16:12:53 -07002572size_t Parcel::getOpenAshmemSize() const
2573{
2574 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002575}
2576
2577// --- Parcel::Blob ---
2578
2579Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002580 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002581}
2582
2583Parcel::Blob::~Blob() {
2584 release();
2585}
2586
2587void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002588 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002589 ::munmap(mData, mSize);
2590 }
2591 clear();
2592}
2593
Jeff Brown13b16042014-11-11 16:44:25 -08002594void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2595 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002596 mData = data;
2597 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002598 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002599}
2600
2601void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002602 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002603 mData = NULL;
2604 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002605 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002606}
2607
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002608}; // namespace android