blob: 6fd6ddc073d2c857b07d33b44b72058faf201b96 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/Binder.h>
33#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080034#include <binder/IPCThreadState.h>
35#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070036#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080037#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070038#include <binder/TextOutput.h>
39
Mark Salyzynabed7f72016-01-27 08:02:48 -080040#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/String8.h>
46#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047
Mathias Agopian208059f2009-05-18 15:08:03 -070048#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080049#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051#ifndef INT32_MAX
52#define INT32_MAX ((int32_t)(2147483647))
53#endif
54
55#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080057#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070059
60// ---------------------------------------------------------------------------
61
Nick Kralevichb6b14232015-04-02 09:36:02 -070062// This macro should never be used at runtime, as a too large value
63// of s could cause an integer overflow. Instead, you should always
64// use the wrapper function pad_size()
65#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
66
67static size_t pad_size(size_t s) {
68 if (s > (SIZE_T_MAX - 3)) {
69 abort();
70 }
71 return PAD_SIZE_UNSAFE(s);
72}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080075#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070077// XXX This can be made public if we want to provide
78// support for typed data.
79struct small_flat_data
80{
81 uint32_t type;
82 uint32_t data;
83};
84
85namespace android {
86
Dianne Hackborna4cff882014-11-13 17:07:40 -080087static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
88static size_t gParcelGlobalAllocSize = 0;
89static size_t gParcelGlobalAllocCount = 0;
90
Jeff Brown13b16042014-11-11 16:44:25 -080091// Maximum size of a blob to transfer in-place.
92static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
93
94enum {
95 BLOB_INPLACE = 0,
96 BLOB_ASHMEM_IMMUTABLE = 1,
97 BLOB_ASHMEM_MUTABLE = 2,
98};
99
Mark Salyzyn70f36652016-02-02 10:27:03 -0800100static dev_t ashmem_rdev()
101{
102 static dev_t __ashmem_rdev;
103 static pthread_mutex_t __ashmem_rdev_lock = PTHREAD_MUTEX_INITIALIZER;
104
105 pthread_mutex_lock(&__ashmem_rdev_lock);
106
107 dev_t rdev = __ashmem_rdev;
108 if (!rdev) {
109 int fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDONLY));
110 if (fd >= 0) {
111 struct stat st;
112
113 int ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
114 close(fd);
115 if ((ret >= 0) && S_ISCHR(st.st_mode)) {
116 rdev = __ashmem_rdev = st.st_rdev;
117 }
118 }
119 }
120
121 pthread_mutex_unlock(&__ashmem_rdev_lock);
122
123 return rdev;
124}
125
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700127 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128{
129 switch (obj.type) {
130 case BINDER_TYPE_BINDER:
131 if (obj.binder) {
132 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800133 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 }
135 return;
136 case BINDER_TYPE_WEAK_BINDER:
137 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800138 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 case BINDER_TYPE_HANDLE: {
141 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
142 if (b != NULL) {
143 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
144 b->incStrong(who);
145 }
146 return;
147 }
148 case BINDER_TYPE_WEAK_HANDLE: {
149 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
150 if (b != NULL) b.get_refs()->incWeak(who);
151 return;
152 }
153 case BINDER_TYPE_FD: {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800154 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
155 struct stat st;
156 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800157 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700158 // If we own an ashmem fd, keep track of how much memory it refers to.
159 int size = ashmem_get_size_region(obj.handle);
160 if (size > 0) {
161 *outAshmemSize += size;
162 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700163 }
164 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 }
167 }
168
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800169 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170}
171
Adrian Roos6bb31142015-10-22 16:46:12 -0700172void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 const flat_binder_object& obj, const void* who)
174{
Adrian Roos6bb31142015-10-22 16:46:12 -0700175 acquire_object(proc, obj, who, NULL);
176}
177
178static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700179 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180{
181 switch (obj.type) {
182 case BINDER_TYPE_BINDER:
183 if (obj.binder) {
184 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800185 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 }
187 return;
188 case BINDER_TYPE_WEAK_BINDER:
189 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800190 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 return;
192 case BINDER_TYPE_HANDLE: {
193 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
194 if (b != NULL) {
195 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
196 b->decStrong(who);
197 }
198 return;
199 }
200 case BINDER_TYPE_WEAK_HANDLE: {
201 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
202 if (b != NULL) b.get_refs()->decWeak(who);
203 return;
204 }
205 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800206 if (obj.cookie != 0) { // owned
207 if (outAshmemSize != NULL) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800208 struct stat st;
209 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800210 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800211 int size = ashmem_get_size_region(obj.handle);
212 if (size > 0) {
213 *outAshmemSize -= size;
214 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700215 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700216 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800217
218 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700219 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 return;
221 }
222 }
223
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800224 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225}
226
Adrian Roos6bb31142015-10-22 16:46:12 -0700227void release_object(const sp<ProcessState>& proc,
228 const flat_binder_object& obj, const void* who)
229{
230 release_object(proc, obj, who, NULL);
231}
232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800234 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235{
236 return out->writeObject(flat, false);
237}
238
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800239status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 const sp<IBinder>& binder, Parcel* out)
241{
242 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
245 if (binder != NULL) {
246 IBinder *local = binder->localBinder();
247 if (!local) {
248 BpBinder *proxy = binder->remoteBinder();
249 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 }
252 const int32_t handle = proxy ? proxy->handle() : 0;
253 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800254 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 } else {
258 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
260 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
262 } else {
263 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 obj.binder = 0;
265 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 return finish_flatten_binder(binder, obj, out);
269}
270
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800271status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 const wp<IBinder>& binder, Parcel* out)
273{
274 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
277 if (binder != NULL) {
278 sp<IBinder> real = binder.promote();
279 if (real != NULL) {
280 IBinder *local = real->localBinder();
281 if (!local) {
282 BpBinder *proxy = real->remoteBinder();
283 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000284 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285 }
286 const int32_t handle = proxy ? proxy->handle() : 0;
287 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800288 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800290 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 } else {
292 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800293 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
294 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 }
296 return finish_flatten_binder(real, obj, out);
297 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 // XXX How to deal? In order to flatten the given binder,
300 // we need to probe it for information, which requires a primary
301 // reference... but we don't have one.
302 //
303 // The OpenBinder implementation uses a dynamic_cast<> here,
304 // but we can't do that with the different reference counting
305 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000306 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800308 obj.binder = 0;
309 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 } else {
313 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800314 obj.binder = 0;
315 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 return finish_flatten_binder(NULL, obj, out);
317 }
318}
319
320inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800321 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
322 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323{
324 return NO_ERROR;
325}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327status_t unflatten_binder(const sp<ProcessState>& proc,
328 const Parcel& in, sp<IBinder>* out)
329{
330 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332 if (flat) {
333 switch (flat->type) {
334 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800335 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336 return finish_unflatten_binder(NULL, *flat, in);
337 case BINDER_TYPE_HANDLE:
338 *out = proc->getStrongProxyForHandle(flat->handle);
339 return finish_unflatten_binder(
340 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700341 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 }
343 return BAD_TYPE;
344}
345
346status_t unflatten_binder(const sp<ProcessState>& proc,
347 const Parcel& in, wp<IBinder>* out)
348{
349 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700351 if (flat) {
352 switch (flat->type) {
353 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800354 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700355 return finish_unflatten_binder(NULL, *flat, in);
356 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800357 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700358 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800359 reinterpret_cast<IBinder*>(flat->cookie),
360 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700361 } else {
362 *out = NULL;
363 }
364 return finish_unflatten_binder(NULL, *flat, in);
365 case BINDER_TYPE_HANDLE:
366 case BINDER_TYPE_WEAK_HANDLE:
367 *out = proc->getWeakProxyForHandle(flat->handle);
368 return finish_unflatten_binder(
369 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
370 }
371 }
372 return BAD_TYPE;
373}
374
375// ---------------------------------------------------------------------------
376
377Parcel::Parcel()
378{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800379 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 initState();
381}
382
383Parcel::~Parcel()
384{
385 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800386 LOG_ALLOC("Parcel %p: destroyed", this);
387}
388
389size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800390 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
391 size_t size = gParcelGlobalAllocSize;
392 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
393 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800394}
395
396size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800397 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
398 size_t count = gParcelGlobalAllocCount;
399 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
400 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700401}
402
403const uint8_t* Parcel::data() const
404{
405 return mData;
406}
407
408size_t Parcel::dataSize() const
409{
410 return (mDataSize > mDataPos ? mDataSize : mDataPos);
411}
412
413size_t Parcel::dataAvail() const
414{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700415 size_t result = dataSize() - dataPosition();
416 if (result > INT32_MAX) {
417 abort();
418 }
419 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420}
421
422size_t Parcel::dataPosition() const
423{
424 return mDataPos;
425}
426
427size_t Parcel::dataCapacity() const
428{
429 return mDataCapacity;
430}
431
432status_t Parcel::setDataSize(size_t size)
433{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700434 if (size > INT32_MAX) {
435 // don't accept size_t values which may have come from an
436 // inadvertent conversion from a negative int.
437 return BAD_VALUE;
438 }
439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 status_t err;
441 err = continueWrite(size);
442 if (err == NO_ERROR) {
443 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700444 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 }
446 return err;
447}
448
449void Parcel::setDataPosition(size_t pos) const
450{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700451 if (pos > INT32_MAX) {
452 // don't accept size_t values which may have come from an
453 // inadvertent conversion from a negative int.
454 abort();
455 }
456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 mDataPos = pos;
458 mNextObjectHint = 0;
459}
460
461status_t Parcel::setDataCapacity(size_t size)
462{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700463 if (size > INT32_MAX) {
464 // don't accept size_t values which may have come from an
465 // inadvertent conversion from a negative int.
466 return BAD_VALUE;
467 }
468
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700469 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 return NO_ERROR;
471}
472
473status_t Parcel::setData(const uint8_t* buffer, size_t len)
474{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700475 if (len > INT32_MAX) {
476 // don't accept size_t values which may have come from an
477 // inadvertent conversion from a negative int.
478 return BAD_VALUE;
479 }
480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481 status_t err = restartWrite(len);
482 if (err == NO_ERROR) {
483 memcpy(const_cast<uint8_t*>(data()), buffer, len);
484 mDataSize = len;
485 mFdsKnown = false;
486 }
487 return err;
488}
489
Andreas Huber51faf462011-04-13 10:21:56 -0700490status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491{
492 const sp<ProcessState> proc(ProcessState::self());
493 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700494 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800495 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 size_t size = parcel->mObjectsSize;
497 int startPos = mDataPos;
498 int firstIndex = -1, lastIndex = -2;
499
500 if (len == 0) {
501 return NO_ERROR;
502 }
503
Nick Kralevichb6b14232015-04-02 09:36:02 -0700504 if (len > INT32_MAX) {
505 // don't accept size_t values which may have come from an
506 // inadvertent conversion from a negative int.
507 return BAD_VALUE;
508 }
509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 // range checks against the source parcel size
511 if ((offset > parcel->mDataSize)
512 || (len > parcel->mDataSize)
513 || (offset + len > parcel->mDataSize)) {
514 return BAD_VALUE;
515 }
516
517 // Count objects in range
518 for (int i = 0; i < (int) size; i++) {
519 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700520 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 if (firstIndex == -1) {
522 firstIndex = i;
523 }
524 lastIndex = i;
525 }
526 }
527 int numObjects = lastIndex - firstIndex + 1;
528
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700529 if ((mDataSize+len) > mDataCapacity) {
530 // grow data
531 err = growData(len);
532 if (err != NO_ERROR) {
533 return err;
534 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 }
536
537 // append data
538 memcpy(mData + mDataPos, data + offset, len);
539 mDataPos += len;
540 mDataSize += len;
541
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400542 err = NO_ERROR;
543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 if (numObjects > 0) {
545 // grow objects
546 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700547 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
548 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800549 binder_size_t *objects =
550 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
551 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 return NO_MEMORY;
553 }
554 mObjects = objects;
555 mObjectsCapacity = newSize;
556 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700557
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558 // append and acquire objects
559 int idx = mObjectsSize;
560 for (int i = firstIndex; i <= lastIndex; i++) {
561 size_t off = objects[i] - offset + startPos;
562 mObjects[idx++] = off;
563 mObjectsSize++;
564
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700565 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700566 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700567 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700570 // If this is a file descriptor, we need to dup it so the
571 // new Parcel now owns its own fd, and can declare that we
572 // officially know we have fds.
573 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800574 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700575 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400576 if (!mAllowFds) {
577 err = FDS_NOT_ALLOWED;
578 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579 }
580 }
581 }
582
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return err;
584}
585
Jeff Brown13b16042014-11-11 16:44:25 -0800586bool Parcel::allowFds() const
587{
588 return mAllowFds;
589}
590
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700591bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400592{
593 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700594 if (!allowFds) {
595 mAllowFds = false;
596 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400597 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598}
599
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700600void Parcel::restoreAllowFds(bool lastValue)
601{
602 mAllowFds = lastValue;
603}
604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605bool Parcel::hasFileDescriptors() const
606{
607 if (!mFdsKnown) {
608 scanForFds();
609 }
610 return mHasFds;
611}
612
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700613// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614status_t Parcel::writeInterfaceToken(const String16& interface)
615{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700616 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
617 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618 // currently the interface identification token is just its name as a string
619 return writeString16(interface);
620}
621
Mathias Agopian83c04462009-05-22 19:00:22 -0700622bool Parcel::checkInterface(IBinder* binder) const
623{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700624 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700625}
626
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700627bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700628 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700630 int32_t strictPolicy = readInt32();
631 if (threadState == NULL) {
632 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700633 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700634 if ((threadState->getLastTransactionBinderFlags() &
635 IBinder::FLAG_ONEWAY) != 0) {
636 // For one-way calls, the callee is running entirely
637 // disconnected from the caller, so disable StrictMode entirely.
638 // Not only does disk/network usage not impact the caller, but
639 // there's no way to commuicate back any violations anyway.
640 threadState->setStrictModePolicy(0);
641 } else {
642 threadState->setStrictModePolicy(strictPolicy);
643 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700644 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645 if (str == interface) {
646 return true;
647 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700648 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 String8(interface).string(), String8(str).string());
650 return false;
651 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700652}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800654const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655{
656 return mObjects;
657}
658
659size_t Parcel::objectsCount() const
660{
661 return mObjectsSize;
662}
663
664status_t Parcel::errorCheck() const
665{
666 return mError;
667}
668
669void Parcel::setError(status_t err)
670{
671 mError = err;
672}
673
674status_t Parcel::finishWrite(size_t len)
675{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700676 if (len > INT32_MAX) {
677 // don't accept size_t values which may have come from an
678 // inadvertent conversion from a negative int.
679 return BAD_VALUE;
680 }
681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 //printf("Finish write of %d\n", len);
683 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700684 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 if (mDataPos > mDataSize) {
686 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700687 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 }
689 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
690 return NO_ERROR;
691}
692
693status_t Parcel::writeUnpadded(const void* data, size_t len)
694{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700695 if (len > INT32_MAX) {
696 // don't accept size_t values which may have come from an
697 // inadvertent conversion from a negative int.
698 return BAD_VALUE;
699 }
700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700701 size_t end = mDataPos + len;
702 if (end < mDataPos) {
703 // integer overflow
704 return BAD_VALUE;
705 }
706
707 if (end <= mDataCapacity) {
708restart_write:
709 memcpy(mData+mDataPos, data, len);
710 return finishWrite(len);
711 }
712
713 status_t err = growData(len);
714 if (err == NO_ERROR) goto restart_write;
715 return err;
716}
717
718status_t Parcel::write(const void* data, size_t len)
719{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700720 if (len > INT32_MAX) {
721 // don't accept size_t values which may have come from an
722 // inadvertent conversion from a negative int.
723 return BAD_VALUE;
724 }
725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700726 void* const d = writeInplace(len);
727 if (d) {
728 memcpy(d, data, len);
729 return NO_ERROR;
730 }
731 return mError;
732}
733
734void* Parcel::writeInplace(size_t len)
735{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700736 if (len > INT32_MAX) {
737 // don't accept size_t values which may have come from an
738 // inadvertent conversion from a negative int.
739 return NULL;
740 }
741
742 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743
744 // sanity check for integer overflow
745 if (mDataPos+padded < mDataPos) {
746 return NULL;
747 }
748
749 if ((mDataPos+padded) <= mDataCapacity) {
750restart_write:
751 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
752 uint8_t* const data = mData+mDataPos;
753
754 // Need to pad at end?
755 if (padded != len) {
756#if BYTE_ORDER == BIG_ENDIAN
757 static const uint32_t mask[4] = {
758 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
759 };
760#endif
761#if BYTE_ORDER == LITTLE_ENDIAN
762 static const uint32_t mask[4] = {
763 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
764 };
765#endif
766 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
767 // *reinterpret_cast<void**>(data+padded-4));
768 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
769 }
770
771 finishWrite(padded);
772 return data;
773 }
774
775 status_t err = growData(padded);
776 if (err == NO_ERROR) goto restart_write;
777 return NULL;
778}
779
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800780status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
781 const uint8_t* strData = (uint8_t*)str.data();
782 const size_t strLen= str.length();
783 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
784 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
785 return BAD_VALUE;
786 }
787
788 status_t err = writeInt32(utf16Len);
789 if (err) {
790 return err;
791 }
792
793 // Allocate enough bytes to hold our converted string and its terminating NULL.
794 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
795 if (!dst) {
796 return NO_MEMORY;
797 }
798
799 utf8_to_utf16(strData, strLen, (char16_t*)dst);
800
801 return NO_ERROR;
802}
803
804status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
805 if (!str) {
806 return writeInt32(-1);
807 }
808 return writeUtf8AsUtf16(*str);
809}
810
Casey Dahlin185d3442016-02-09 11:08:35 -0800811namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800812
Casey Dahlin185d3442016-02-09 11:08:35 -0800813template<typename T>
814status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700815{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700816 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700817 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700818 status = BAD_VALUE;
819 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700820 }
821
Casey Dahlin185d3442016-02-09 11:08:35 -0800822 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700823 if (status != OK) {
824 return status;
825 }
826
Casey Dahlin185d3442016-02-09 11:08:35 -0800827 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700828 if (!data) {
829 status = BAD_VALUE;
830 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700831 }
832
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700833 memcpy(data, val.data(), val.size());
834 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700835}
836
Casey Dahlin185d3442016-02-09 11:08:35 -0800837template<typename T>
838status_t writeByteVectorInternalPtr(Parcel* parcel,
839 const std::unique_ptr<std::vector<T>>& val)
840{
841 if (!val) {
842 return parcel->writeInt32(-1);
843 }
844
845 return writeByteVectorInternal(parcel, *val);
846}
847
848} // namespace
849
850status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
851 return writeByteVectorInternal(this, val);
852}
853
854status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
855{
856 return writeByteVectorInternalPtr(this, val);
857}
858
859status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
860 return writeByteVectorInternal(this, val);
861}
862
863status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
864{
865 return writeByteVectorInternalPtr(this, val);
866}
867
Casey Dahlin451ff582015-10-19 18:12:18 -0700868status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
869{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800870 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700871}
872
Casey Dahlinb9872622015-11-25 15:09:45 -0800873status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
874{
875 return writeNullableTypedVector(val, &Parcel::writeInt32);
876}
877
Casey Dahlin451ff582015-10-19 18:12:18 -0700878status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
879{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800880 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeInt64);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeFloatVector(const std::vector<float>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Casey Dahlinb9872622015-11-25 15:09:45 -0800893status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
894{
895 return writeNullableTypedVector(val, &Parcel::writeFloat);
896}
897
Casey Dahlin451ff582015-10-19 18:12:18 -0700898status_t Parcel::writeDoubleVector(const std::vector<double>& val)
899{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800900 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700901}
902
Casey Dahlinb9872622015-11-25 15:09:45 -0800903status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
904{
905 return writeNullableTypedVector(val, &Parcel::writeDouble);
906}
907
Casey Dahlin451ff582015-10-19 18:12:18 -0700908status_t Parcel::writeBoolVector(const std::vector<bool>& val)
909{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800910 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700911}
912
Casey Dahlinb9872622015-11-25 15:09:45 -0800913status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
914{
915 return writeNullableTypedVector(val, &Parcel::writeBool);
916}
917
Casey Dahlin451ff582015-10-19 18:12:18 -0700918status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
919{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800920 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700921}
922
Casey Dahlinb9872622015-11-25 15:09:45 -0800923status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
924{
925 return writeNullableTypedVector(val, &Parcel::writeChar);
926}
927
Casey Dahlin451ff582015-10-19 18:12:18 -0700928status_t Parcel::writeString16Vector(const std::vector<String16>& val)
929{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800930 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700931}
932
Casey Dahlinb9872622015-11-25 15:09:45 -0800933status_t Parcel::writeString16Vector(
934 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
935{
936 return writeNullableTypedVector(val, &Parcel::writeString16);
937}
938
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800939status_t Parcel::writeUtf8VectorAsUtf16Vector(
940 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
941 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
942}
943
944status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
945 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
946}
947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948status_t Parcel::writeInt32(int32_t val)
949{
Andreas Huber84a6d042009-08-17 13:33:27 -0700950 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800952
953status_t Parcel::writeUint32(uint32_t val)
954{
955 return writeAligned(val);
956}
957
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700958status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700959 if (len > INT32_MAX) {
960 // don't accept size_t values which may have come from an
961 // inadvertent conversion from a negative int.
962 return BAD_VALUE;
963 }
964
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700965 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700966 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700967 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700968 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700969 if (ret == NO_ERROR) {
970 ret = write(val, len * sizeof(*val));
971 }
972 return ret;
973}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700974status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700975 if (len > INT32_MAX) {
976 // don't accept size_t values which may have come from an
977 // inadvertent conversion from a negative int.
978 return BAD_VALUE;
979 }
980
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700981 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700982 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700983 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700984 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700985 if (ret == NO_ERROR) {
986 ret = write(val, len * sizeof(*val));
987 }
988 return ret;
989}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990
Casey Dahlind6848f52015-10-15 15:44:59 -0700991status_t Parcel::writeBool(bool val)
992{
993 return writeInt32(int32_t(val));
994}
995
996status_t Parcel::writeChar(char16_t val)
997{
998 return writeInt32(int32_t(val));
999}
1000
1001status_t Parcel::writeByte(int8_t val)
1002{
1003 return writeInt32(int32_t(val));
1004}
1005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006status_t Parcel::writeInt64(int64_t val)
1007{
Andreas Huber84a6d042009-08-17 13:33:27 -07001008 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009}
1010
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001011status_t Parcel::writeUint64(uint64_t val)
1012{
1013 return writeAligned(val);
1014}
1015
Serban Constantinescuf683e012013-11-05 16:53:55 +00001016status_t Parcel::writePointer(uintptr_t val)
1017{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001018 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001019}
1020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021status_t Parcel::writeFloat(float val)
1022{
Andreas Huber84a6d042009-08-17 13:33:27 -07001023 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024}
1025
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001026#if defined(__mips__) && defined(__mips_hard_float)
1027
1028status_t Parcel::writeDouble(double val)
1029{
1030 union {
1031 double d;
1032 unsigned long long ll;
1033 } u;
1034 u.d = val;
1035 return writeAligned(u.ll);
1036}
1037
1038#else
1039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040status_t Parcel::writeDouble(double val)
1041{
Andreas Huber84a6d042009-08-17 13:33:27 -07001042 return writeAligned(val);
1043}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001045#endif
1046
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047status_t Parcel::writeCString(const char* str)
1048{
1049 return write(str, strlen(str)+1);
1050}
1051
1052status_t Parcel::writeString8(const String8& str)
1053{
1054 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001055 // only write string if its length is more than zero characters,
1056 // as readString8 will only read if the length field is non-zero.
1057 // this is slightly different from how writeString16 works.
1058 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059 err = write(str.string(), str.bytes()+1);
1060 }
1061 return err;
1062}
1063
Casey Dahlinb9872622015-11-25 15:09:45 -08001064status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1065{
1066 if (!str) {
1067 return writeInt32(-1);
1068 }
1069
1070 return writeString16(*str);
1071}
1072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073status_t Parcel::writeString16(const String16& str)
1074{
1075 return writeString16(str.string(), str.size());
1076}
1077
1078status_t Parcel::writeString16(const char16_t* str, size_t len)
1079{
1080 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001081
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001082 status_t err = writeInt32(len);
1083 if (err == NO_ERROR) {
1084 len *= sizeof(char16_t);
1085 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1086 if (data) {
1087 memcpy(data, str, len);
1088 *reinterpret_cast<char16_t*>(data+len) = 0;
1089 return NO_ERROR;
1090 }
1091 err = mError;
1092 }
1093 return err;
1094}
1095
1096status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1097{
1098 return flatten_binder(ProcessState::self(), val, this);
1099}
1100
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001101status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1102{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001103 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001104}
1105
Casey Dahlinb9872622015-11-25 15:09:45 -08001106status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1107{
1108 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1109}
1110
1111status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001112 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001113}
1114
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001115status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001116 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001117}
1118
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1120{
1121 return flatten_binder(ProcessState::self(), val, this);
1122}
1123
Casey Dahlinb9872622015-11-25 15:09:45 -08001124status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1125 if (!parcelable) {
1126 return writeInt32(0);
1127 }
1128
1129 return writeParcelable(*parcelable);
1130}
1131
Christopher Wiley97f048d2015-11-19 06:49:05 -08001132status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1133 status_t status = writeInt32(1); // parcelable is not null.
1134 if (status != OK) {
1135 return status;
1136 }
1137 return parcelable.writeToParcel(this);
1138}
1139
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001140status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001141{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001142 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143 return BAD_TYPE;
1144
1145 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001146 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001147 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001149 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001150 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001152 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1153 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154
1155 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001156 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 return err;
1158 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001159 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001160 return err;
1161}
1162
Jeff Brown93ff1f92011-11-04 19:01:44 -07001163status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164{
1165 flat_binder_object obj;
1166 obj.type = BINDER_TYPE_FD;
1167 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001168 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001170 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171 return writeObject(obj, true);
1172}
1173
1174status_t Parcel::writeDupFileDescriptor(int fd)
1175{
Jeff Brownd341c712011-11-04 20:19:33 -07001176 int dupFd = dup(fd);
1177 if (dupFd < 0) {
1178 return -errno;
1179 }
1180 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001181 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001182 close(dupFd);
1183 }
1184 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185}
1186
Christopher Wiley2cf19952016-04-11 11:09:37 -07001187status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001188 return writeDupFileDescriptor(fd.get());
1189}
1190
Christopher Wiley2cf19952016-04-11 11:09:37 -07001191status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001192 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1193}
1194
Christopher Wiley2cf19952016-04-11 11:09:37 -07001195status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001196 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1197}
1198
Jeff Brown13b16042014-11-11 16:44:25 -08001199status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001200{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001201 if (len > INT32_MAX) {
1202 // don't accept size_t values which may have come from an
1203 // inadvertent conversion from a negative int.
1204 return BAD_VALUE;
1205 }
1206
Jeff Brown13b16042014-11-11 16:44:25 -08001207 status_t status;
1208 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001209 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001210 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001211 if (status) return status;
1212
1213 void* ptr = writeInplace(len);
1214 if (!ptr) return NO_MEMORY;
1215
Jeff Brown13b16042014-11-11 16:44:25 -08001216 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 return NO_ERROR;
1218 }
1219
Steve Block6807e592011-10-20 11:56:00 +01001220 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001221 int fd = ashmem_create_region("Parcel Blob", len);
1222 if (fd < 0) return NO_MEMORY;
1223
1224 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1225 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001226 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001227 } else {
1228 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1229 if (ptr == MAP_FAILED) {
1230 status = -errno;
1231 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001232 if (!mutableCopy) {
1233 result = ashmem_set_prot_region(fd, PROT_READ);
1234 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001236 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001238 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001240 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001242 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 return NO_ERROR;
1244 }
1245 }
1246 }
1247 }
1248 ::munmap(ptr, len);
1249 }
1250 ::close(fd);
1251 return status;
1252}
1253
Jeff Brown13b16042014-11-11 16:44:25 -08001254status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1255{
1256 // Must match up with what's done in writeBlob.
1257 if (!mAllowFds) return FDS_NOT_ALLOWED;
1258 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1259 if (status) return status;
1260 return writeDupFileDescriptor(fd);
1261}
1262
Mathias Agopiane1424282013-07-29 21:24:40 -07001263status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001264{
1265 status_t err;
1266
1267 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001268 const size_t len = val.getFlattenedSize();
1269 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001270
Nick Kralevichb6b14232015-04-02 09:36:02 -07001271 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1272 // don't accept size_t values which may have come from an
1273 // inadvertent conversion from a negative int.
1274 return BAD_VALUE;
1275 }
1276
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001277 err = this->writeInt32(len);
1278 if (err) return err;
1279
1280 err = this->writeInt32(fd_count);
1281 if (err) return err;
1282
1283 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001284 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001285 if (buf == NULL)
1286 return BAD_VALUE;
1287
1288 int* fds = NULL;
1289 if (fd_count) {
1290 fds = new int[fd_count];
1291 }
1292
1293 err = val.flatten(buf, len, fds, fd_count);
1294 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1295 err = this->writeDupFileDescriptor( fds[i] );
1296 }
1297
1298 if (fd_count) {
1299 delete [] fds;
1300 }
1301
1302 return err;
1303}
1304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1306{
1307 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1308 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1309 if (enoughData && enoughObjects) {
1310restart_write:
1311 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001312
Christopher Tate98e67d32015-06-03 18:44:15 -07001313 // remember if it's a file descriptor
1314 if (val.type == BINDER_TYPE_FD) {
1315 if (!mAllowFds) {
1316 // fail before modifying our object index
1317 return FDS_NOT_ALLOWED;
1318 }
1319 mHasFds = mFdsKnown = true;
1320 }
1321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001323 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001325 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326 mObjectsSize++;
1327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 return finishWrite(sizeof(flat_binder_object));
1330 }
1331
1332 if (!enoughData) {
1333 const status_t err = growData(sizeof(val));
1334 if (err != NO_ERROR) return err;
1335 }
1336 if (!enoughObjects) {
1337 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001338 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001339 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340 if (objects == NULL) return NO_MEMORY;
1341 mObjects = objects;
1342 mObjectsCapacity = newSize;
1343 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 goto restart_write;
1346}
1347
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001348status_t Parcel::writeNoException()
1349{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001350 binder::Status status;
1351 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001352}
1353
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001354void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001355{
1356 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1357}
1358
1359status_t Parcel::read(void* outData, size_t len) const
1360{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001361 if (len > INT32_MAX) {
1362 // don't accept size_t values which may have come from an
1363 // inadvertent conversion from a negative int.
1364 return BAD_VALUE;
1365 }
1366
1367 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1368 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001370 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001371 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 return NO_ERROR;
1373 }
1374 return NOT_ENOUGH_DATA;
1375}
1376
1377const void* Parcel::readInplace(size_t len) const
1378{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001379 if (len > INT32_MAX) {
1380 // don't accept size_t values which may have come from an
1381 // inadvertent conversion from a negative int.
1382 return NULL;
1383 }
1384
1385 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1386 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001388 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001389 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390 return data;
1391 }
1392 return NULL;
1393}
1394
Andreas Huber84a6d042009-08-17 13:33:27 -07001395template<class T>
1396status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001397 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001398
1399 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001401 mDataPos += sizeof(T);
1402 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403 return NO_ERROR;
1404 } else {
1405 return NOT_ENOUGH_DATA;
1406 }
1407}
1408
Andreas Huber84a6d042009-08-17 13:33:27 -07001409template<class T>
1410T Parcel::readAligned() const {
1411 T result;
1412 if (readAligned(&result) != NO_ERROR) {
1413 result = 0;
1414 }
1415
1416 return result;
1417}
1418
1419template<class T>
1420status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001421 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001422
1423 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1424restart_write:
1425 *reinterpret_cast<T*>(mData+mDataPos) = val;
1426 return finishWrite(sizeof(val));
1427 }
1428
1429 status_t err = growData(sizeof(val));
1430 if (err == NO_ERROR) goto restart_write;
1431 return err;
1432}
1433
Casey Dahlin185d3442016-02-09 11:08:35 -08001434namespace {
1435
1436template<typename T>
1437status_t readByteVectorInternal(const Parcel* parcel,
1438 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001439 val->clear();
1440
1441 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001442 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001443
1444 if (status != OK) {
1445 return status;
1446 }
1447
Christopher Wiley4db672d2015-11-10 09:44:30 -08001448 if (size < 0) {
1449 status = UNEXPECTED_NULL;
1450 return status;
1451 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001452 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001453 status = BAD_VALUE;
1454 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001455 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001456
Casey Dahlin185d3442016-02-09 11:08:35 -08001457 const void* data = parcel->readInplace(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001458 if (!data) {
1459 status = BAD_VALUE;
1460 return status;
1461 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001462 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001463 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001464
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001465 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001466}
1467
Casey Dahlin185d3442016-02-09 11:08:35 -08001468template<typename T>
1469status_t readByteVectorInternalPtr(
1470 const Parcel* parcel,
1471 std::unique_ptr<std::vector<T>>* val) {
1472 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001473 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001474 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001475 val->reset();
1476
1477 if (status != OK || size < 0) {
1478 return status;
1479 }
1480
Casey Dahlin185d3442016-02-09 11:08:35 -08001481 parcel->setDataPosition(start);
1482 val->reset(new std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001483
Casey Dahlin185d3442016-02-09 11:08:35 -08001484 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001485
1486 if (status != OK) {
1487 val->reset();
1488 }
1489
1490 return status;
1491}
1492
Casey Dahlin185d3442016-02-09 11:08:35 -08001493} // namespace
1494
1495status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1496 return readByteVectorInternal(this, val);
1497}
1498
1499status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1500 return readByteVectorInternal(this, val);
1501}
1502
1503status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1504 return readByteVectorInternalPtr(this, val);
1505}
1506
1507status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1508 return readByteVectorInternalPtr(this, val);
1509}
1510
Casey Dahlinb9872622015-11-25 15:09:45 -08001511status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1512 return readNullableTypedVector(val, &Parcel::readInt32);
1513}
1514
Casey Dahlin451ff582015-10-19 18:12:18 -07001515status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001516 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001517}
1518
Casey Dahlinb9872622015-11-25 15:09:45 -08001519status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1520 return readNullableTypedVector(val, &Parcel::readInt64);
1521}
1522
Casey Dahlin451ff582015-10-19 18:12:18 -07001523status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001524 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001525}
1526
Casey Dahlinb9872622015-11-25 15:09:45 -08001527status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1528 return readNullableTypedVector(val, &Parcel::readFloat);
1529}
1530
Casey Dahlin451ff582015-10-19 18:12:18 -07001531status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001532 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001533}
1534
Casey Dahlinb9872622015-11-25 15:09:45 -08001535status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1536 return readNullableTypedVector(val, &Parcel::readDouble);
1537}
1538
Casey Dahlin451ff582015-10-19 18:12:18 -07001539status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001540 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001541}
1542
Casey Dahlinb9872622015-11-25 15:09:45 -08001543status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1544 const int32_t start = dataPosition();
1545 int32_t size;
1546 status_t status = readInt32(&size);
1547 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001548
Casey Dahlinb9872622015-11-25 15:09:45 -08001549 if (status != OK || size < 0) {
1550 return status;
1551 }
1552
1553 setDataPosition(start);
1554 val->reset(new std::vector<bool>());
1555
1556 status = readBoolVector(val->get());
1557
1558 if (status != OK) {
1559 val->reset();
1560 }
1561
1562 return status;
1563}
1564
1565status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001566 int32_t size;
1567 status_t status = readInt32(&size);
1568
1569 if (status != OK) {
1570 return status;
1571 }
1572
1573 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001574 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001575 }
1576
1577 val->resize(size);
1578
1579 /* C++ bool handling means a vector of bools isn't necessarily addressable
1580 * (we might use individual bits)
1581 */
Christopher Wiley97887982015-10-27 16:33:47 -07001582 bool data;
1583 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001584 status = readBool(&data);
1585 (*val)[i] = data;
1586
1587 if (status != OK) {
1588 return status;
1589 }
1590 }
1591
1592 return OK;
1593}
1594
Casey Dahlinb9872622015-11-25 15:09:45 -08001595status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1596 return readNullableTypedVector(val, &Parcel::readChar);
1597}
1598
Casey Dahlin451ff582015-10-19 18:12:18 -07001599status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001600 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001601}
1602
Casey Dahlinb9872622015-11-25 15:09:45 -08001603status_t Parcel::readString16Vector(
1604 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1605 return readNullableTypedVector(val, &Parcel::readString16);
1606}
1607
Casey Dahlin451ff582015-10-19 18:12:18 -07001608status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001609 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001610}
1611
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001612status_t Parcel::readUtf8VectorFromUtf16Vector(
1613 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1614 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1615}
1616
1617status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1618 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1619}
Casey Dahlin451ff582015-10-19 18:12:18 -07001620
Andreas Huber84a6d042009-08-17 13:33:27 -07001621status_t Parcel::readInt32(int32_t *pArg) const
1622{
1623 return readAligned(pArg);
1624}
1625
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001626int32_t Parcel::readInt32() const
1627{
Andreas Huber84a6d042009-08-17 13:33:27 -07001628 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629}
1630
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001631status_t Parcel::readUint32(uint32_t *pArg) const
1632{
1633 return readAligned(pArg);
1634}
1635
1636uint32_t Parcel::readUint32() const
1637{
1638 return readAligned<uint32_t>();
1639}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001640
1641status_t Parcel::readInt64(int64_t *pArg) const
1642{
Andreas Huber84a6d042009-08-17 13:33:27 -07001643 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001644}
1645
1646
1647int64_t Parcel::readInt64() const
1648{
Andreas Huber84a6d042009-08-17 13:33:27 -07001649 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001650}
1651
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001652status_t Parcel::readUint64(uint64_t *pArg) const
1653{
1654 return readAligned(pArg);
1655}
1656
1657uint64_t Parcel::readUint64() const
1658{
1659 return readAligned<uint64_t>();
1660}
1661
Serban Constantinescuf683e012013-11-05 16:53:55 +00001662status_t Parcel::readPointer(uintptr_t *pArg) const
1663{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001664 status_t ret;
1665 binder_uintptr_t ptr;
1666 ret = readAligned(&ptr);
1667 if (!ret)
1668 *pArg = ptr;
1669 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001670}
1671
1672uintptr_t Parcel::readPointer() const
1673{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001674 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001675}
1676
1677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678status_t Parcel::readFloat(float *pArg) const
1679{
Andreas Huber84a6d042009-08-17 13:33:27 -07001680 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681}
1682
1683
1684float Parcel::readFloat() const
1685{
Andreas Huber84a6d042009-08-17 13:33:27 -07001686 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687}
1688
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001689#if defined(__mips__) && defined(__mips_hard_float)
1690
1691status_t Parcel::readDouble(double *pArg) const
1692{
1693 union {
1694 double d;
1695 unsigned long long ll;
1696 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001697 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001698 status_t status;
1699 status = readAligned(&u.ll);
1700 *pArg = u.d;
1701 return status;
1702}
1703
1704double Parcel::readDouble() const
1705{
1706 union {
1707 double d;
1708 unsigned long long ll;
1709 } u;
1710 u.ll = readAligned<unsigned long long>();
1711 return u.d;
1712}
1713
1714#else
1715
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716status_t Parcel::readDouble(double *pArg) const
1717{
Andreas Huber84a6d042009-08-17 13:33:27 -07001718 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001719}
1720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001721double Parcel::readDouble() const
1722{
Andreas Huber84a6d042009-08-17 13:33:27 -07001723 return readAligned<double>();
1724}
1725
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001726#endif
1727
Andreas Huber84a6d042009-08-17 13:33:27 -07001728status_t Parcel::readIntPtr(intptr_t *pArg) const
1729{
1730 return readAligned(pArg);
1731}
1732
1733
1734intptr_t Parcel::readIntPtr() const
1735{
1736 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001737}
1738
Casey Dahlind6848f52015-10-15 15:44:59 -07001739status_t Parcel::readBool(bool *pArg) const
1740{
1741 int32_t tmp;
1742 status_t ret = readInt32(&tmp);
1743 *pArg = (tmp != 0);
1744 return ret;
1745}
1746
1747bool Parcel::readBool() const
1748{
1749 return readInt32() != 0;
1750}
1751
1752status_t Parcel::readChar(char16_t *pArg) const
1753{
1754 int32_t tmp;
1755 status_t ret = readInt32(&tmp);
1756 *pArg = char16_t(tmp);
1757 return ret;
1758}
1759
1760char16_t Parcel::readChar() const
1761{
1762 return char16_t(readInt32());
1763}
1764
1765status_t Parcel::readByte(int8_t *pArg) const
1766{
1767 int32_t tmp;
1768 status_t ret = readInt32(&tmp);
1769 *pArg = int8_t(tmp);
1770 return ret;
1771}
1772
1773int8_t Parcel::readByte() const
1774{
1775 return int8_t(readInt32());
1776}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001778status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1779 size_t utf16Size = 0;
1780 const char16_t* src = readString16Inplace(&utf16Size);
1781 if (!src) {
1782 return UNEXPECTED_NULL;
1783 }
1784
1785 // Save ourselves the trouble, we're done.
1786 if (utf16Size == 0u) {
1787 str->clear();
1788 return NO_ERROR;
1789 }
1790
1791 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1792 if (utf8Size < 0) {
1793 return BAD_VALUE;
1794 }
1795 // Note that while it is probably safe to assume string::resize keeps a
1796 // spare byte around for the trailing null, we're going to be explicit.
1797 str->resize(utf8Size + 1);
1798 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1799 str->resize(utf8Size);
1800 return NO_ERROR;
1801}
1802
1803status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1804 const int32_t start = dataPosition();
1805 int32_t size;
1806 status_t status = readInt32(&size);
1807 str->reset();
1808
1809 if (status != OK || size < 0) {
1810 return status;
1811 }
1812
1813 setDataPosition(start);
1814 str->reset(new std::string());
1815 return readUtf8FromUtf16(str->get());
1816}
1817
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818const char* Parcel::readCString() const
1819{
1820 const size_t avail = mDataSize-mDataPos;
1821 if (avail > 0) {
1822 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1823 // is the string's trailing NUL within the parcel's valid bounds?
1824 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1825 if (eos) {
1826 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001827 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001828 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829 return str;
1830 }
1831 }
1832 return NULL;
1833}
1834
1835String8 Parcel::readString8() const
1836{
1837 int32_t size = readInt32();
1838 // watch for potential int overflow adding 1 for trailing NUL
1839 if (size > 0 && size < INT32_MAX) {
1840 const char* str = (const char*)readInplace(size+1);
1841 if (str) return String8(str, size);
1842 }
1843 return String8();
1844}
1845
1846String16 Parcel::readString16() const
1847{
1848 size_t len;
1849 const char16_t* str = readString16Inplace(&len);
1850 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001851 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852 return String16();
1853}
1854
Casey Dahlinb9872622015-11-25 15:09:45 -08001855status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1856{
1857 const int32_t start = dataPosition();
1858 int32_t size;
1859 status_t status = readInt32(&size);
1860 pArg->reset();
1861
1862 if (status != OK || size < 0) {
1863 return status;
1864 }
1865
1866 setDataPosition(start);
1867 pArg->reset(new String16());
1868
1869 status = readString16(pArg->get());
1870
1871 if (status != OK) {
1872 pArg->reset();
1873 }
1874
1875 return status;
1876}
1877
Casey Dahlin451ff582015-10-19 18:12:18 -07001878status_t Parcel::readString16(String16* pArg) const
1879{
1880 size_t len;
1881 const char16_t* str = readString16Inplace(&len);
1882 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001883 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001884 return 0;
1885 } else {
1886 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001887 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001888 }
1889}
1890
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001891const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1892{
1893 int32_t size = readInt32();
1894 // watch for potential int overflow from size+1
1895 if (size >= 0 && size < INT32_MAX) {
1896 *outLen = size;
1897 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1898 if (str != NULL) {
1899 return str;
1900 }
1901 }
1902 *outLen = 0;
1903 return NULL;
1904}
1905
Casey Dahlinf0c13772015-10-27 18:33:56 -07001906status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1907{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001908 status_t status = readNullableStrongBinder(val);
1909 if (status == OK && !val->get()) {
1910 status = UNEXPECTED_NULL;
1911 }
1912 return status;
1913}
1914
1915status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1916{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001917 return unflatten_binder(ProcessState::self(), *this, val);
1918}
1919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920sp<IBinder> Parcel::readStrongBinder() const
1921{
1922 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001923 // Note that a lot of code in Android reads binders by hand with this
1924 // method, and that code has historically been ok with getting nullptr
1925 // back (while ignoring error codes).
1926 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 return val;
1928}
1929
1930wp<IBinder> Parcel::readWeakBinder() const
1931{
1932 wp<IBinder> val;
1933 unflatten_binder(ProcessState::self(), *this, &val);
1934 return val;
1935}
1936
Christopher Wiley97f048d2015-11-19 06:49:05 -08001937status_t Parcel::readParcelable(Parcelable* parcelable) const {
1938 int32_t have_parcelable = 0;
1939 status_t status = readInt32(&have_parcelable);
1940 if (status != OK) {
1941 return status;
1942 }
1943 if (!have_parcelable) {
1944 return UNEXPECTED_NULL;
1945 }
1946 return parcelable->readFromParcel(this);
1947}
1948
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001949int32_t Parcel::readExceptionCode() const
1950{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001951 binder::Status status;
1952 status.readFromParcel(*this);
1953 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001954}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001955
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001956native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001957{
1958 int numFds, numInts;
1959 status_t err;
1960 err = readInt32(&numFds);
1961 if (err != NO_ERROR) return 0;
1962 err = readInt32(&numInts);
1963 if (err != NO_ERROR) return 0;
1964
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001965 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001966 if (!h) {
1967 return 0;
1968 }
1969
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001970 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001971 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001972 if (h->data[i] < 0) err = BAD_VALUE;
1973 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001974 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001975 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001976 native_handle_close(h);
1977 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001978 h = 0;
1979 }
1980 return h;
1981}
1982
1983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984int Parcel::readFileDescriptor() const
1985{
1986 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001987
1988 if (flat && flat->type == BINDER_TYPE_FD) {
1989 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992 return BAD_TYPE;
1993}
1994
Christopher Wiley2cf19952016-04-11 11:09:37 -07001995status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001996{
1997 int got = readFileDescriptor();
1998
1999 if (got == BAD_TYPE) {
2000 return BAD_TYPE;
2001 }
2002
2003 val->reset(dup(got));
2004
2005 if (val->get() < 0) {
2006 return BAD_VALUE;
2007 }
2008
2009 return OK;
2010}
2011
2012
Christopher Wiley2cf19952016-04-11 11:09:37 -07002013status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002014 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2015}
2016
Christopher Wiley2cf19952016-04-11 11:09:37 -07002017status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002018 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2019}
2020
Jeff Brown5707dbf2011-09-23 21:17:56 -07002021status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2022{
Jeff Brown13b16042014-11-11 16:44:25 -08002023 int32_t blobType;
2024 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002025 if (status) return status;
2026
Jeff Brown13b16042014-11-11 16:44:25 -08002027 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002028 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002029 const void* ptr = readInplace(len);
2030 if (!ptr) return BAD_VALUE;
2031
Jeff Brown13b16042014-11-11 16:44:25 -08002032 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002033 return NO_ERROR;
2034 }
2035
Steve Block6807e592011-10-20 11:56:00 +01002036 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002037 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002038 int fd = readFileDescriptor();
2039 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2040
Jeff Brown13b16042014-11-11 16:44:25 -08002041 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2042 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002043 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002044
Jeff Brown13b16042014-11-11 16:44:25 -08002045 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002046 return NO_ERROR;
2047}
2048
Mathias Agopiane1424282013-07-29 21:24:40 -07002049status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002050{
2051 // size
2052 const size_t len = this->readInt32();
2053 const size_t fd_count = this->readInt32();
2054
Nick Kralevichb6b14232015-04-02 09:36:02 -07002055 if (len > INT32_MAX) {
2056 // don't accept size_t values which may have come from an
2057 // inadvertent conversion from a negative int.
2058 return BAD_VALUE;
2059 }
2060
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002061 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002062 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002063 if (buf == NULL)
2064 return BAD_VALUE;
2065
2066 int* fds = NULL;
2067 if (fd_count) {
2068 fds = new int[fd_count];
2069 }
2070
2071 status_t err = NO_ERROR;
2072 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002073 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002074 if (fds[i] < 0) {
2075 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002076 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2077 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002078 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002079 }
2080
2081 if (err == NO_ERROR) {
2082 err = val.unflatten(buf, len, fds, fd_count);
2083 }
2084
2085 if (fd_count) {
2086 delete [] fds;
2087 }
2088
2089 return err;
2090}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2092{
2093 const size_t DPOS = mDataPos;
2094 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2095 const flat_binder_object* obj
2096 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2097 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002098 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002099 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100 // the object list, so we don't want to check for it when
2101 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002102 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103 return obj;
2104 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002105
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002107 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 const size_t N = mObjectsSize;
2109 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002112 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002114
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002115 // Start at the current hint position, looking for an object at
2116 // the current data position.
2117 if (opos < N) {
2118 while (opos < (N-1) && OBJS[opos] < DPOS) {
2119 opos++;
2120 }
2121 } else {
2122 opos = N-1;
2123 }
2124 if (OBJS[opos] == DPOS) {
2125 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002126 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 this, DPOS, opos);
2128 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002129 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130 return obj;
2131 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002132
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 // Look backwards for it...
2134 while (opos > 0 && OBJS[opos] > DPOS) {
2135 opos--;
2136 }
2137 if (OBJS[opos] == DPOS) {
2138 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002139 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140 this, DPOS, opos);
2141 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002142 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002143 return obj;
2144 }
2145 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002146 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 -07002147 this, DPOS);
2148 }
2149 return NULL;
2150}
2151
2152void Parcel::closeFileDescriptors()
2153{
2154 size_t i = mObjectsSize;
2155 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002156 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002157 }
2158 while (i > 0) {
2159 i--;
2160 const flat_binder_object* flat
2161 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2162 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002163 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 close(flat->handle);
2165 }
2166 }
2167}
2168
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002169uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002170{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002171 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172}
2173
2174size_t Parcel::ipcDataSize() const
2175{
2176 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2177}
2178
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002179uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002181 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182}
2183
2184size_t Parcel::ipcObjectsCount() const
2185{
2186 return mObjectsSize;
2187}
2188
2189void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002190 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002192 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 freeDataNoInit();
2194 mError = NO_ERROR;
2195 mData = const_cast<uint8_t*>(data);
2196 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002197 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002199 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002200 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201 mObjectsSize = mObjectsCapacity = objectsCount;
2202 mNextObjectHint = 0;
2203 mOwner = relFunc;
2204 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002205 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002206 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002207 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002208 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002209 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002210 mObjectsSize = 0;
2211 break;
2212 }
2213 minOffset = offset + sizeof(flat_binder_object);
2214 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 scanForFds();
2216}
2217
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002218void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219{
2220 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 if (errorCheck() != NO_ERROR) {
2223 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002224 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 } else if (dataSize() > 0) {
2226 const uint8_t* DATA = data();
2227 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002228 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 const size_t N = objectsCount();
2230 for (size_t i=0; i<N; i++) {
2231 const flat_binder_object* flat
2232 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2233 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2234 << TypeCode(flat->type & 0x7f7f7f00)
2235 << " = " << flat->binder;
2236 }
2237 } else {
2238 to << "NULL";
2239 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002240
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 to << ")";
2242}
2243
2244void Parcel::releaseObjects()
2245{
2246 const sp<ProcessState> proc(ProcessState::self());
2247 size_t i = mObjectsSize;
2248 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002249 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 while (i > 0) {
2251 i--;
2252 const flat_binder_object* flat
2253 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002254 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 }
2256}
2257
2258void Parcel::acquireObjects()
2259{
2260 const sp<ProcessState> proc(ProcessState::self());
2261 size_t i = mObjectsSize;
2262 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002263 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264 while (i > 0) {
2265 i--;
2266 const flat_binder_object* flat
2267 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002268 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 }
2270}
2271
2272void Parcel::freeData()
2273{
2274 freeDataNoInit();
2275 initState();
2276}
2277
2278void Parcel::freeDataNoInit()
2279{
2280 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002281 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002282 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2284 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002285 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002287 if (mData) {
2288 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002289 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002290 if (mDataCapacity <= gParcelGlobalAllocSize) {
2291 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2292 } else {
2293 gParcelGlobalAllocSize = 0;
2294 }
2295 if (gParcelGlobalAllocCount > 0) {
2296 gParcelGlobalAllocCount--;
2297 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002298 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002299 free(mData);
2300 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 if (mObjects) free(mObjects);
2302 }
2303}
2304
2305status_t Parcel::growData(size_t len)
2306{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002307 if (len > INT32_MAX) {
2308 // don't accept size_t values which may have come from an
2309 // inadvertent conversion from a negative int.
2310 return BAD_VALUE;
2311 }
2312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 size_t newSize = ((mDataSize+len)*3)/2;
2314 return (newSize <= mDataSize)
2315 ? (status_t) NO_MEMORY
2316 : continueWrite(newSize);
2317}
2318
2319status_t Parcel::restartWrite(size_t desired)
2320{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002321 if (desired > INT32_MAX) {
2322 // don't accept size_t values which may have come from an
2323 // inadvertent conversion from a negative int.
2324 return BAD_VALUE;
2325 }
2326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 if (mOwner) {
2328 freeData();
2329 return continueWrite(desired);
2330 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002331
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002332 uint8_t* data = (uint8_t*)realloc(mData, desired);
2333 if (!data && desired > mDataCapacity) {
2334 mError = NO_MEMORY;
2335 return NO_MEMORY;
2336 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002341 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002342 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002343 gParcelGlobalAllocSize += desired;
2344 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002345 if (!mData) {
2346 gParcelGlobalAllocCount++;
2347 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002348 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mData = data;
2350 mDataCapacity = desired;
2351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2355 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 free(mObjects);
2358 mObjects = NULL;
2359 mObjectsSize = mObjectsCapacity = 0;
2360 mNextObjectHint = 0;
2361 mHasFds = false;
2362 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002363 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 return NO_ERROR;
2366}
2367
2368status_t Parcel::continueWrite(size_t desired)
2369{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002370 if (desired > INT32_MAX) {
2371 // don't accept size_t values which may have come from an
2372 // inadvertent conversion from a negative int.
2373 return BAD_VALUE;
2374 }
2375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 // If shrinking, first adjust for any objects that appear
2377 // after the new data size.
2378 size_t objectsSize = mObjectsSize;
2379 if (desired < mDataSize) {
2380 if (desired == 0) {
2381 objectsSize = 0;
2382 } else {
2383 while (objectsSize > 0) {
2384 if (mObjects[objectsSize-1] < desired)
2385 break;
2386 objectsSize--;
2387 }
2388 }
2389 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 if (mOwner) {
2392 // If the size is going to zero, just release the owner's data.
2393 if (desired == 0) {
2394 freeData();
2395 return NO_ERROR;
2396 }
2397
2398 // If there is a different owner, we need to take
2399 // posession.
2400 uint8_t* data = (uint8_t*)malloc(desired);
2401 if (!data) {
2402 mError = NO_MEMORY;
2403 return NO_MEMORY;
2404 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002405 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002408 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002410 free(data);
2411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 mError = NO_MEMORY;
2413 return NO_MEMORY;
2414 }
2415
2416 // Little hack to only acquire references on objects
2417 // we will be keeping.
2418 size_t oldObjectsSize = mObjectsSize;
2419 mObjectsSize = objectsSize;
2420 acquireObjects();
2421 mObjectsSize = oldObjectsSize;
2422 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 if (mData) {
2425 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2426 }
2427 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002428 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002430 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2432 mOwner = NULL;
2433
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002434 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002435 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002436 gParcelGlobalAllocSize += desired;
2437 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002438 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 mData = data;
2441 mObjects = objects;
2442 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 mDataCapacity = desired;
2445 mObjectsSize = mObjectsCapacity = objectsSize;
2446 mNextObjectHint = 0;
2447
2448 } else if (mData) {
2449 if (objectsSize < mObjectsSize) {
2450 // Need to release refs on any objects we are dropping.
2451 const sp<ProcessState> proc(ProcessState::self());
2452 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2453 const flat_binder_object* flat
2454 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2455 if (flat->type == BINDER_TYPE_FD) {
2456 // will need to rescan because we may have lopped off the only FDs
2457 mFdsKnown = false;
2458 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002459 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002461 binder_size_t* objects =
2462 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002463 if (objects) {
2464 mObjects = objects;
2465 }
2466 mObjectsSize = objectsSize;
2467 mNextObjectHint = 0;
2468 }
2469
2470 // We own the data, so we can just do a realloc().
2471 if (desired > mDataCapacity) {
2472 uint8_t* data = (uint8_t*)realloc(mData, desired);
2473 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002474 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2475 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002476 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002477 gParcelGlobalAllocSize += desired;
2478 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002479 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 mData = data;
2481 mDataCapacity = desired;
2482 } else if (desired > mDataCapacity) {
2483 mError = NO_MEMORY;
2484 return NO_MEMORY;
2485 }
2486 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002487 if (mDataSize > desired) {
2488 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002490 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 if (mDataPos > desired) {
2492 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002493 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002494 }
2495 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 } else {
2498 // This is the first data. Easy!
2499 uint8_t* data = (uint8_t*)malloc(desired);
2500 if (!data) {
2501 mError = NO_MEMORY;
2502 return NO_MEMORY;
2503 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002504
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505 if(!(mDataCapacity == 0 && mObjects == NULL
2506 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002507 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002509
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002510 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002511 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002512 gParcelGlobalAllocSize += desired;
2513 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002514 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 mData = data;
2517 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2519 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mDataCapacity = desired;
2521 }
2522
2523 return NO_ERROR;
2524}
2525
2526void Parcel::initState()
2527{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002528 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002529 mError = NO_ERROR;
2530 mData = 0;
2531 mDataSize = 0;
2532 mDataCapacity = 0;
2533 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002534 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2535 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 mObjects = NULL;
2537 mObjectsSize = 0;
2538 mObjectsCapacity = 0;
2539 mNextObjectHint = 0;
2540 mHasFds = false;
2541 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002542 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002544 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545}
2546
2547void Parcel::scanForFds() const
2548{
2549 bool hasFds = false;
2550 for (size_t i=0; i<mObjectsSize; i++) {
2551 const flat_binder_object* flat
2552 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2553 if (flat->type == BINDER_TYPE_FD) {
2554 hasFds = true;
2555 break;
2556 }
2557 }
2558 mHasFds = hasFds;
2559 mFdsKnown = true;
2560}
2561
Dan Sandleraa5c2342015-04-10 10:08:45 -04002562size_t Parcel::getBlobAshmemSize() const
2563{
Adrian Roos6bb31142015-10-22 16:46:12 -07002564 // This used to return the size of all blobs that were written to ashmem, now we're returning
2565 // the ashmem currently referenced by this Parcel, which should be equivalent.
2566 // TODO: Remove method once ABI can be changed.
2567 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002568}
2569
Adrian Rooscbf37262015-10-22 16:12:53 -07002570size_t Parcel::getOpenAshmemSize() const
2571{
2572 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002573}
2574
2575// --- Parcel::Blob ---
2576
2577Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002578 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002579}
2580
2581Parcel::Blob::~Blob() {
2582 release();
2583}
2584
2585void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002586 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002587 ::munmap(mData, mSize);
2588 }
2589 clear();
2590}
2591
Jeff Brown13b16042014-11-11 16:44:25 -08002592void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2593 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002594 mData = data;
2595 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002596 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002597}
2598
2599void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002600 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002601 mData = NULL;
2602 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002603 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002604}
2605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606}; // namespace android