blob: e53a5519a7e7a5d28e38021d8bd7655c55e085bf [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>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
40
Mark Salyzynabed7f72016-01-27 08:02:48 -080041#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070042#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/String8.h>
47#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
Mathias Agopian208059f2009-05-18 15:08:03 -070049#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080050#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052#ifndef INT32_MAX
53#define INT32_MAX ((int32_t)(2147483647))
54#endif
55
56#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
68static size_t pad_size(size_t s) {
69 if (s > (SIZE_T_MAX - 3)) {
70 abort();
71 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080076#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078// XXX This can be made public if we want to provide
79// support for typed data.
80struct small_flat_data
81{
82 uint32_t type;
83 uint32_t data;
84};
85
86namespace android {
87
Dianne Hackborna4cff882014-11-13 17:07:40 -080088static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
89static size_t gParcelGlobalAllocSize = 0;
90static size_t gParcelGlobalAllocCount = 0;
91
Christopher Tatee4e0ae82016-03-24 16:03:44 -070092static size_t gMaxFds = 0;
93
Jeff Brown13b16042014-11-11 16:44:25 -080094// Maximum size of a blob to transfer in-place.
95static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
96
97enum {
98 BLOB_INPLACE = 0,
99 BLOB_ASHMEM_IMMUTABLE = 1,
100 BLOB_ASHMEM_MUTABLE = 2,
101};
102
Mark Salyzyn70f36652016-02-02 10:27:03 -0800103static dev_t ashmem_rdev()
104{
105 static dev_t __ashmem_rdev;
106 static pthread_mutex_t __ashmem_rdev_lock = PTHREAD_MUTEX_INITIALIZER;
107
108 pthread_mutex_lock(&__ashmem_rdev_lock);
109
110 dev_t rdev = __ashmem_rdev;
111 if (!rdev) {
112 int fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDONLY));
113 if (fd >= 0) {
114 struct stat st;
115
116 int ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
117 close(fd);
118 if ((ret >= 0) && S_ISCHR(st.st_mode)) {
119 rdev = __ashmem_rdev = st.st_rdev;
120 }
121 }
122 }
123
124 pthread_mutex_unlock(&__ashmem_rdev_lock);
125
126 return rdev;
127}
128
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131{
132 switch (obj.type) {
133 case BINDER_TYPE_BINDER:
134 if (obj.binder) {
135 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800136 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 }
138 return;
139 case BINDER_TYPE_WEAK_BINDER:
140 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800141 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142 return;
143 case BINDER_TYPE_HANDLE: {
144 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
145 if (b != NULL) {
146 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
147 b->incStrong(who);
148 }
149 return;
150 }
151 case BINDER_TYPE_WEAK_HANDLE: {
152 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
153 if (b != NULL) b.get_refs()->incWeak(who);
154 return;
155 }
156 case BINDER_TYPE_FD: {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800157 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
158 struct stat st;
159 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800160 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700161 // If we own an ashmem fd, keep track of how much memory it refers to.
162 int size = ashmem_get_size_region(obj.handle);
163 if (size > 0) {
164 *outAshmemSize += size;
165 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700166 }
167 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168 return;
169 }
170 }
171
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800172 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173}
174
Adrian Roos6bb31142015-10-22 16:46:12 -0700175void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176 const flat_binder_object& obj, const void* who)
177{
Adrian Roos6bb31142015-10-22 16:46:12 -0700178 acquire_object(proc, obj, who, NULL);
179}
180
181static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700182 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183{
184 switch (obj.type) {
185 case BINDER_TYPE_BINDER:
186 if (obj.binder) {
187 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800188 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 }
190 return;
191 case BINDER_TYPE_WEAK_BINDER:
192 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800193 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 return;
195 case BINDER_TYPE_HANDLE: {
196 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
197 if (b != NULL) {
198 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
199 b->decStrong(who);
200 }
201 return;
202 }
203 case BINDER_TYPE_WEAK_HANDLE: {
204 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
205 if (b != NULL) b.get_refs()->decWeak(who);
206 return;
207 }
208 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800209 if (obj.cookie != 0) { // owned
210 if (outAshmemSize != NULL) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800211 struct stat st;
212 int ret = fstat(obj.handle, &st);
Mark Salyzyn70f36652016-02-02 10:27:03 -0800213 if (!ret && S_ISCHR(st.st_mode) && (st.st_rdev == ashmem_rdev())) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800214 int size = ashmem_get_size_region(obj.handle);
215 if (size > 0) {
216 *outAshmemSize -= size;
217 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700218 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700219 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800220
221 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700222 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 return;
224 }
225 }
226
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800227 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228}
229
Adrian Roos6bb31142015-10-22 16:46:12 -0700230void release_object(const sp<ProcessState>& proc,
231 const flat_binder_object& obj, const void* who)
232{
233 release_object(proc, obj, who, NULL);
234}
235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800237 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238{
239 return out->writeObject(flat, false);
240}
241
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800242status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 const sp<IBinder>& binder, Parcel* out)
244{
245 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700247 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
248 if (binder != NULL) {
249 IBinder *local = binder->localBinder();
250 if (!local) {
251 BpBinder *proxy = binder->remoteBinder();
252 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000253 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 }
255 const int32_t handle = proxy ? proxy->handle() : 0;
256 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800257 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 } else {
261 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
263 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 }
265 } else {
266 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.binder = 0;
268 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 return finish_flatten_binder(binder, obj, out);
272}
273
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800274status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 const wp<IBinder>& binder, Parcel* out)
276{
277 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
280 if (binder != NULL) {
281 sp<IBinder> real = binder.promote();
282 if (real != NULL) {
283 IBinder *local = real->localBinder();
284 if (!local) {
285 BpBinder *proxy = real->remoteBinder();
286 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000287 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700288 }
289 const int32_t handle = proxy ? proxy->handle() : 0;
290 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800291 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800293 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 } else {
295 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800296 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
297 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 }
299 return finish_flatten_binder(real, obj, out);
300 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302 // XXX How to deal? In order to flatten the given binder,
303 // we need to probe it for information, which requires a primary
304 // reference... but we don't have one.
305 //
306 // The OpenBinder implementation uses a dynamic_cast<> here,
307 // but we can't do that with the different reference counting
308 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000309 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800311 obj.binder = 0;
312 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 } else {
316 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800317 obj.binder = 0;
318 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 return finish_flatten_binder(NULL, obj, out);
320 }
321}
322
323inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800324 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
325 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326{
327 return NO_ERROR;
328}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330status_t unflatten_binder(const sp<ProcessState>& proc,
331 const Parcel& in, sp<IBinder>* out)
332{
333 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 if (flat) {
336 switch (flat->type) {
337 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800338 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339 return finish_unflatten_binder(NULL, *flat, in);
340 case BINDER_TYPE_HANDLE:
341 *out = proc->getStrongProxyForHandle(flat->handle);
342 return finish_unflatten_binder(
343 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700344 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345 }
346 return BAD_TYPE;
347}
348
349status_t unflatten_binder(const sp<ProcessState>& proc,
350 const Parcel& in, wp<IBinder>* out)
351{
352 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700353
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354 if (flat) {
355 switch (flat->type) {
356 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800357 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700358 return finish_unflatten_binder(NULL, *flat, in);
359 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800360 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700361 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800362 reinterpret_cast<IBinder*>(flat->cookie),
363 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364 } else {
365 *out = NULL;
366 }
367 return finish_unflatten_binder(NULL, *flat, in);
368 case BINDER_TYPE_HANDLE:
369 case BINDER_TYPE_WEAK_HANDLE:
370 *out = proc->getWeakProxyForHandle(flat->handle);
371 return finish_unflatten_binder(
372 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
373 }
374 }
375 return BAD_TYPE;
376}
377
378// ---------------------------------------------------------------------------
379
380Parcel::Parcel()
381{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800382 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383 initState();
384}
385
386Parcel::~Parcel()
387{
388 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800389 LOG_ALLOC("Parcel %p: destroyed", this);
390}
391
392size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800393 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
394 size_t size = gParcelGlobalAllocSize;
395 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
396 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800397}
398
399size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800400 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
401 size_t count = gParcelGlobalAllocCount;
402 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
403 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404}
405
406const uint8_t* Parcel::data() const
407{
408 return mData;
409}
410
411size_t Parcel::dataSize() const
412{
413 return (mDataSize > mDataPos ? mDataSize : mDataPos);
414}
415
416size_t Parcel::dataAvail() const
417{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700418 size_t result = dataSize() - dataPosition();
419 if (result > INT32_MAX) {
420 abort();
421 }
422 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423}
424
425size_t Parcel::dataPosition() const
426{
427 return mDataPos;
428}
429
430size_t Parcel::dataCapacity() const
431{
432 return mDataCapacity;
433}
434
435status_t Parcel::setDataSize(size_t size)
436{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700437 if (size > INT32_MAX) {
438 // don't accept size_t values which may have come from an
439 // inadvertent conversion from a negative int.
440 return BAD_VALUE;
441 }
442
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 status_t err;
444 err = continueWrite(size);
445 if (err == NO_ERROR) {
446 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700447 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 }
449 return err;
450}
451
452void Parcel::setDataPosition(size_t pos) const
453{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700454 if (pos > INT32_MAX) {
455 // don't accept size_t values which may have come from an
456 // inadvertent conversion from a negative int.
457 abort();
458 }
459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 mDataPos = pos;
461 mNextObjectHint = 0;
462}
463
464status_t Parcel::setDataCapacity(size_t size)
465{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700466 if (size > INT32_MAX) {
467 // don't accept size_t values which may have come from an
468 // inadvertent conversion from a negative int.
469 return BAD_VALUE;
470 }
471
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700472 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 return NO_ERROR;
474}
475
476status_t Parcel::setData(const uint8_t* buffer, size_t len)
477{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700478 if (len > INT32_MAX) {
479 // don't accept size_t values which may have come from an
480 // inadvertent conversion from a negative int.
481 return BAD_VALUE;
482 }
483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 status_t err = restartWrite(len);
485 if (err == NO_ERROR) {
486 memcpy(const_cast<uint8_t*>(data()), buffer, len);
487 mDataSize = len;
488 mFdsKnown = false;
489 }
490 return err;
491}
492
Andreas Huber51faf462011-04-13 10:21:56 -0700493status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494{
495 const sp<ProcessState> proc(ProcessState::self());
496 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700497 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800498 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 size_t size = parcel->mObjectsSize;
500 int startPos = mDataPos;
501 int firstIndex = -1, lastIndex = -2;
502
503 if (len == 0) {
504 return NO_ERROR;
505 }
506
Nick Kralevichb6b14232015-04-02 09:36:02 -0700507 if (len > INT32_MAX) {
508 // don't accept size_t values which may have come from an
509 // inadvertent conversion from a negative int.
510 return BAD_VALUE;
511 }
512
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 // range checks against the source parcel size
514 if ((offset > parcel->mDataSize)
515 || (len > parcel->mDataSize)
516 || (offset + len > parcel->mDataSize)) {
517 return BAD_VALUE;
518 }
519
520 // Count objects in range
521 for (int i = 0; i < (int) size; i++) {
522 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700523 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 if (firstIndex == -1) {
525 firstIndex = i;
526 }
527 lastIndex = i;
528 }
529 }
530 int numObjects = lastIndex - firstIndex + 1;
531
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700532 if ((mDataSize+len) > mDataCapacity) {
533 // grow data
534 err = growData(len);
535 if (err != NO_ERROR) {
536 return err;
537 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 }
539
540 // append data
541 memcpy(mData + mDataPos, data + offset, len);
542 mDataPos += len;
543 mDataSize += len;
544
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400545 err = NO_ERROR;
546
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 if (numObjects > 0) {
548 // grow objects
549 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700550 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
551 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 binder_size_t *objects =
553 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
554 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 return NO_MEMORY;
556 }
557 mObjects = objects;
558 mObjectsCapacity = newSize;
559 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700561 // append and acquire objects
562 int idx = mObjectsSize;
563 for (int i = firstIndex; i <= lastIndex; i++) {
564 size_t off = objects[i] - offset + startPos;
565 mObjects[idx++] = off;
566 mObjectsSize++;
567
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700568 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700570 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700573 // If this is a file descriptor, we need to dup it so the
574 // new Parcel now owns its own fd, and can declare that we
575 // officially know we have fds.
576 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800577 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400579 if (!mAllowFds) {
580 err = FDS_NOT_ALLOWED;
581 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700582 }
583 }
584 }
585
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400586 return err;
587}
588
Jeff Brown13b16042014-11-11 16:44:25 -0800589bool Parcel::allowFds() const
590{
591 return mAllowFds;
592}
593
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700594bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400595{
596 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700597 if (!allowFds) {
598 mAllowFds = false;
599 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400600 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601}
602
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700603void Parcel::restoreAllowFds(bool lastValue)
604{
605 mAllowFds = lastValue;
606}
607
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700608bool Parcel::hasFileDescriptors() const
609{
610 if (!mFdsKnown) {
611 scanForFds();
612 }
613 return mHasFds;
614}
615
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700616// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617status_t Parcel::writeInterfaceToken(const String16& interface)
618{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700619 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
620 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 // currently the interface identification token is just its name as a string
622 return writeString16(interface);
623}
624
Mathias Agopian83c04462009-05-22 19:00:22 -0700625bool Parcel::checkInterface(IBinder* binder) const
626{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700627 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700628}
629
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700630bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700631 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700632{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700633 int32_t strictPolicy = readInt32();
634 if (threadState == NULL) {
635 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700636 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700637 if ((threadState->getLastTransactionBinderFlags() &
638 IBinder::FLAG_ONEWAY) != 0) {
639 // For one-way calls, the callee is running entirely
640 // disconnected from the caller, so disable StrictMode entirely.
641 // Not only does disk/network usage not impact the caller, but
642 // there's no way to commuicate back any violations anyway.
643 threadState->setStrictModePolicy(0);
644 } else {
645 threadState->setStrictModePolicy(strictPolicy);
646 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700647 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648 if (str == interface) {
649 return true;
650 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700651 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 String8(interface).string(), String8(str).string());
653 return false;
654 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700655}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800657const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700658{
659 return mObjects;
660}
661
662size_t Parcel::objectsCount() const
663{
664 return mObjectsSize;
665}
666
667status_t Parcel::errorCheck() const
668{
669 return mError;
670}
671
672void Parcel::setError(status_t err)
673{
674 mError = err;
675}
676
677status_t Parcel::finishWrite(size_t len)
678{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700679 if (len > INT32_MAX) {
680 // don't accept size_t values which may have come from an
681 // inadvertent conversion from a negative int.
682 return BAD_VALUE;
683 }
684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 //printf("Finish write of %d\n", len);
686 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700687 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700688 if (mDataPos > mDataSize) {
689 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700690 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691 }
692 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
693 return NO_ERROR;
694}
695
696status_t Parcel::writeUnpadded(const void* data, size_t len)
697{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700698 if (len > INT32_MAX) {
699 // don't accept size_t values which may have come from an
700 // inadvertent conversion from a negative int.
701 return BAD_VALUE;
702 }
703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704 size_t end = mDataPos + len;
705 if (end < mDataPos) {
706 // integer overflow
707 return BAD_VALUE;
708 }
709
710 if (end <= mDataCapacity) {
711restart_write:
712 memcpy(mData+mDataPos, data, len);
713 return finishWrite(len);
714 }
715
716 status_t err = growData(len);
717 if (err == NO_ERROR) goto restart_write;
718 return err;
719}
720
721status_t Parcel::write(const void* data, size_t len)
722{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700723 if (len > INT32_MAX) {
724 // don't accept size_t values which may have come from an
725 // inadvertent conversion from a negative int.
726 return BAD_VALUE;
727 }
728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700729 void* const d = writeInplace(len);
730 if (d) {
731 memcpy(d, data, len);
732 return NO_ERROR;
733 }
734 return mError;
735}
736
737void* Parcel::writeInplace(size_t len)
738{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700739 if (len > INT32_MAX) {
740 // don't accept size_t values which may have come from an
741 // inadvertent conversion from a negative int.
742 return NULL;
743 }
744
745 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700746
747 // sanity check for integer overflow
748 if (mDataPos+padded < mDataPos) {
749 return NULL;
750 }
751
752 if ((mDataPos+padded) <= mDataCapacity) {
753restart_write:
754 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
755 uint8_t* const data = mData+mDataPos;
756
757 // Need to pad at end?
758 if (padded != len) {
759#if BYTE_ORDER == BIG_ENDIAN
760 static const uint32_t mask[4] = {
761 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
762 };
763#endif
764#if BYTE_ORDER == LITTLE_ENDIAN
765 static const uint32_t mask[4] = {
766 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
767 };
768#endif
769 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
770 // *reinterpret_cast<void**>(data+padded-4));
771 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
772 }
773
774 finishWrite(padded);
775 return data;
776 }
777
778 status_t err = growData(padded);
779 if (err == NO_ERROR) goto restart_write;
780 return NULL;
781}
782
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800783status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
784 const uint8_t* strData = (uint8_t*)str.data();
785 const size_t strLen= str.length();
786 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
787 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
788 return BAD_VALUE;
789 }
790
791 status_t err = writeInt32(utf16Len);
792 if (err) {
793 return err;
794 }
795
796 // Allocate enough bytes to hold our converted string and its terminating NULL.
797 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
798 if (!dst) {
799 return NO_MEMORY;
800 }
801
802 utf8_to_utf16(strData, strLen, (char16_t*)dst);
803
804 return NO_ERROR;
805}
806
807status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
808 if (!str) {
809 return writeInt32(-1);
810 }
811 return writeUtf8AsUtf16(*str);
812}
813
Casey Dahlin185d3442016-02-09 11:08:35 -0800814namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800815
Casey Dahlin185d3442016-02-09 11:08:35 -0800816template<typename T>
817status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700818{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700819 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700820 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700821 status = BAD_VALUE;
822 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700823 }
824
Casey Dahlin185d3442016-02-09 11:08:35 -0800825 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700826 if (status != OK) {
827 return status;
828 }
829
Casey Dahlin185d3442016-02-09 11:08:35 -0800830 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700831 if (!data) {
832 status = BAD_VALUE;
833 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700834 }
835
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700836 memcpy(data, val.data(), val.size());
837 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700838}
839
Casey Dahlin185d3442016-02-09 11:08:35 -0800840template<typename T>
841status_t writeByteVectorInternalPtr(Parcel* parcel,
842 const std::unique_ptr<std::vector<T>>& val)
843{
844 if (!val) {
845 return parcel->writeInt32(-1);
846 }
847
848 return writeByteVectorInternal(parcel, *val);
849}
850
851} // namespace
852
853status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
854 return writeByteVectorInternal(this, val);
855}
856
857status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
858{
859 return writeByteVectorInternalPtr(this, val);
860}
861
862status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
863 return writeByteVectorInternal(this, val);
864}
865
866status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
867{
868 return writeByteVectorInternalPtr(this, val);
869}
870
Casey Dahlin451ff582015-10-19 18:12:18 -0700871status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
872{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800873 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700874}
875
Casey Dahlinb9872622015-11-25 15:09:45 -0800876status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
877{
878 return writeNullableTypedVector(val, &Parcel::writeInt32);
879}
880
Casey Dahlin451ff582015-10-19 18:12:18 -0700881status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
882{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800883 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700884}
885
Casey Dahlinb9872622015-11-25 15:09:45 -0800886status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
887{
888 return writeNullableTypedVector(val, &Parcel::writeInt64);
889}
890
Casey Dahlin451ff582015-10-19 18:12:18 -0700891status_t Parcel::writeFloatVector(const std::vector<float>& val)
892{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800893 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700894}
895
Casey Dahlinb9872622015-11-25 15:09:45 -0800896status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
897{
898 return writeNullableTypedVector(val, &Parcel::writeFloat);
899}
900
Casey Dahlin451ff582015-10-19 18:12:18 -0700901status_t Parcel::writeDoubleVector(const std::vector<double>& val)
902{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800903 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700904}
905
Casey Dahlinb9872622015-11-25 15:09:45 -0800906status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
907{
908 return writeNullableTypedVector(val, &Parcel::writeDouble);
909}
910
Casey Dahlin451ff582015-10-19 18:12:18 -0700911status_t Parcel::writeBoolVector(const std::vector<bool>& val)
912{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800913 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700914}
915
Casey Dahlinb9872622015-11-25 15:09:45 -0800916status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
917{
918 return writeNullableTypedVector(val, &Parcel::writeBool);
919}
920
Casey Dahlin451ff582015-10-19 18:12:18 -0700921status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
922{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800923 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700924}
925
Casey Dahlinb9872622015-11-25 15:09:45 -0800926status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
927{
928 return writeNullableTypedVector(val, &Parcel::writeChar);
929}
930
Casey Dahlin451ff582015-10-19 18:12:18 -0700931status_t Parcel::writeString16Vector(const std::vector<String16>& val)
932{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800933 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700934}
935
Casey Dahlinb9872622015-11-25 15:09:45 -0800936status_t Parcel::writeString16Vector(
937 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
938{
939 return writeNullableTypedVector(val, &Parcel::writeString16);
940}
941
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800942status_t Parcel::writeUtf8VectorAsUtf16Vector(
943 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
944 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
945}
946
947status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
948 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
949}
950
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951status_t Parcel::writeInt32(int32_t val)
952{
Andreas Huber84a6d042009-08-17 13:33:27 -0700953 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800955
956status_t Parcel::writeUint32(uint32_t val)
957{
958 return writeAligned(val);
959}
960
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700961status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700962 if (len > INT32_MAX) {
963 // don't accept size_t values which may have come from an
964 // inadvertent conversion from a negative int.
965 return BAD_VALUE;
966 }
967
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700968 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700969 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700970 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700971 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700972 if (ret == NO_ERROR) {
973 ret = write(val, len * sizeof(*val));
974 }
975 return ret;
976}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700977status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700978 if (len > INT32_MAX) {
979 // don't accept size_t values which may have come from an
980 // inadvertent conversion from a negative int.
981 return BAD_VALUE;
982 }
983
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700984 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700985 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700986 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700987 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700988 if (ret == NO_ERROR) {
989 ret = write(val, len * sizeof(*val));
990 }
991 return ret;
992}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700993
Casey Dahlind6848f52015-10-15 15:44:59 -0700994status_t Parcel::writeBool(bool val)
995{
996 return writeInt32(int32_t(val));
997}
998
999status_t Parcel::writeChar(char16_t val)
1000{
1001 return writeInt32(int32_t(val));
1002}
1003
1004status_t Parcel::writeByte(int8_t val)
1005{
1006 return writeInt32(int32_t(val));
1007}
1008
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009status_t Parcel::writeInt64(int64_t val)
1010{
Andreas Huber84a6d042009-08-17 13:33:27 -07001011 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012}
1013
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001014status_t Parcel::writeUint64(uint64_t val)
1015{
1016 return writeAligned(val);
1017}
1018
Serban Constantinescuf683e012013-11-05 16:53:55 +00001019status_t Parcel::writePointer(uintptr_t val)
1020{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001021 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001022}
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024status_t Parcel::writeFloat(float val)
1025{
Andreas Huber84a6d042009-08-17 13:33:27 -07001026 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027}
1028
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001029#if defined(__mips__) && defined(__mips_hard_float)
1030
1031status_t Parcel::writeDouble(double val)
1032{
1033 union {
1034 double d;
1035 unsigned long long ll;
1036 } u;
1037 u.d = val;
1038 return writeAligned(u.ll);
1039}
1040
1041#else
1042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043status_t Parcel::writeDouble(double val)
1044{
Andreas Huber84a6d042009-08-17 13:33:27 -07001045 return writeAligned(val);
1046}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001047
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001048#endif
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050status_t Parcel::writeCString(const char* str)
1051{
1052 return write(str, strlen(str)+1);
1053}
1054
1055status_t Parcel::writeString8(const String8& str)
1056{
1057 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001058 // only write string if its length is more than zero characters,
1059 // as readString8 will only read if the length field is non-zero.
1060 // this is slightly different from how writeString16 works.
1061 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 err = write(str.string(), str.bytes()+1);
1063 }
1064 return err;
1065}
1066
Casey Dahlinb9872622015-11-25 15:09:45 -08001067status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1068{
1069 if (!str) {
1070 return writeInt32(-1);
1071 }
1072
1073 return writeString16(*str);
1074}
1075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076status_t Parcel::writeString16(const String16& str)
1077{
1078 return writeString16(str.string(), str.size());
1079}
1080
1081status_t Parcel::writeString16(const char16_t* str, size_t len)
1082{
1083 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085 status_t err = writeInt32(len);
1086 if (err == NO_ERROR) {
1087 len *= sizeof(char16_t);
1088 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1089 if (data) {
1090 memcpy(data, str, len);
1091 *reinterpret_cast<char16_t*>(data+len) = 0;
1092 return NO_ERROR;
1093 }
1094 err = mError;
1095 }
1096 return err;
1097}
1098
1099status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1100{
1101 return flatten_binder(ProcessState::self(), val, this);
1102}
1103
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001104status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1105{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001106 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001107}
1108
Casey Dahlinb9872622015-11-25 15:09:45 -08001109status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1110{
1111 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1112}
1113
1114status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001115 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001116}
1117
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001118status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001119 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001120}
1121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1123{
1124 return flatten_binder(ProcessState::self(), val, this);
1125}
1126
Casey Dahlinb9872622015-11-25 15:09:45 -08001127status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1128 if (!parcelable) {
1129 return writeInt32(0);
1130 }
1131
1132 return writeParcelable(*parcelable);
1133}
1134
Christopher Wiley97f048d2015-11-19 06:49:05 -08001135status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1136 status_t status = writeInt32(1); // parcelable is not null.
1137 if (status != OK) {
1138 return status;
1139 }
1140 return parcelable.writeToParcel(this);
1141}
1142
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001143status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001144{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001145 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001146 return BAD_TYPE;
1147
1148 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001149 err = writeInt32(handle->numFds);
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 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001153 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001155 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1156 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157
1158 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001159 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 return err;
1161 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001162 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001163 return err;
1164}
1165
Jeff Brown93ff1f92011-11-04 19:01:44 -07001166status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167{
1168 flat_binder_object obj;
1169 obj.type = BINDER_TYPE_FD;
1170 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001171 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001172 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001173 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174 return writeObject(obj, true);
1175}
1176
1177status_t Parcel::writeDupFileDescriptor(int fd)
1178{
Jeff Brownd341c712011-11-04 20:19:33 -07001179 int dupFd = dup(fd);
1180 if (dupFd < 0) {
1181 return -errno;
1182 }
1183 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001184 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001185 close(dupFd);
1186 }
1187 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188}
1189
Christopher Wiley2cf19952016-04-11 11:09:37 -07001190status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001191 return writeDupFileDescriptor(fd.get());
1192}
1193
Christopher Wiley2cf19952016-04-11 11:09:37 -07001194status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001195 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1196}
1197
Christopher Wiley2cf19952016-04-11 11:09:37 -07001198status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001199 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1200}
1201
Jeff Brown13b16042014-11-11 16:44:25 -08001202status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001203{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001204 if (len > INT32_MAX) {
1205 // don't accept size_t values which may have come from an
1206 // inadvertent conversion from a negative int.
1207 return BAD_VALUE;
1208 }
1209
Jeff Brown13b16042014-11-11 16:44:25 -08001210 status_t status;
1211 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001212 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001213 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001214 if (status) return status;
1215
1216 void* ptr = writeInplace(len);
1217 if (!ptr) return NO_MEMORY;
1218
Jeff Brown13b16042014-11-11 16:44:25 -08001219 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001220 return NO_ERROR;
1221 }
1222
Steve Block6807e592011-10-20 11:56:00 +01001223 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001224 int fd = ashmem_create_region("Parcel Blob", len);
1225 if (fd < 0) return NO_MEMORY;
1226
1227 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1228 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001229 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 } else {
1231 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1232 if (ptr == MAP_FAILED) {
1233 status = -errno;
1234 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001235 if (!mutableCopy) {
1236 result = ashmem_set_prot_region(fd, PROT_READ);
1237 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001239 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001240 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001241 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001242 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001243 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001245 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 return NO_ERROR;
1247 }
1248 }
1249 }
1250 }
1251 ::munmap(ptr, len);
1252 }
1253 ::close(fd);
1254 return status;
1255}
1256
Jeff Brown13b16042014-11-11 16:44:25 -08001257status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1258{
1259 // Must match up with what's done in writeBlob.
1260 if (!mAllowFds) return FDS_NOT_ALLOWED;
1261 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1262 if (status) return status;
1263 return writeDupFileDescriptor(fd);
1264}
1265
Mathias Agopiane1424282013-07-29 21:24:40 -07001266status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001267{
1268 status_t err;
1269
1270 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001271 const size_t len = val.getFlattenedSize();
1272 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001273
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001274 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001275 // don't accept size_t values which may have come from an
1276 // inadvertent conversion from a negative int.
1277 return BAD_VALUE;
1278 }
1279
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001280 err = this->writeInt32(len);
1281 if (err) return err;
1282
1283 err = this->writeInt32(fd_count);
1284 if (err) return err;
1285
1286 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001287 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001288 if (buf == NULL)
1289 return BAD_VALUE;
1290
1291 int* fds = NULL;
1292 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001293 fds = new (std::nothrow) int[fd_count];
1294 if (fds == nullptr) {
1295 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1296 return BAD_VALUE;
1297 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001298 }
1299
1300 err = val.flatten(buf, len, fds, fd_count);
1301 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1302 err = this->writeDupFileDescriptor( fds[i] );
1303 }
1304
1305 if (fd_count) {
1306 delete [] fds;
1307 }
1308
1309 return err;
1310}
1311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001312status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1313{
1314 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1315 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1316 if (enoughData && enoughObjects) {
1317restart_write:
1318 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001319
Christopher Tate98e67d32015-06-03 18:44:15 -07001320 // remember if it's a file descriptor
1321 if (val.type == BINDER_TYPE_FD) {
1322 if (!mAllowFds) {
1323 // fail before modifying our object index
1324 return FDS_NOT_ALLOWED;
1325 }
1326 mHasFds = mFdsKnown = true;
1327 }
1328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001330 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001332 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333 mObjectsSize++;
1334 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 return finishWrite(sizeof(flat_binder_object));
1337 }
1338
1339 if (!enoughData) {
1340 const status_t err = growData(sizeof(val));
1341 if (err != NO_ERROR) return err;
1342 }
1343 if (!enoughObjects) {
1344 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001345 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001346 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347 if (objects == NULL) return NO_MEMORY;
1348 mObjects = objects;
1349 mObjectsCapacity = newSize;
1350 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001351
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 goto restart_write;
1353}
1354
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001355status_t Parcel::writeNoException()
1356{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001357 binder::Status status;
1358 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001359}
1360
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001361void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362{
1363 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1364}
1365
1366status_t Parcel::read(void* outData, size_t len) const
1367{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001368 if (len > INT32_MAX) {
1369 // don't accept size_t values which may have come from an
1370 // inadvertent conversion from a negative int.
1371 return BAD_VALUE;
1372 }
1373
1374 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1375 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001376 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001377 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001378 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379 return NO_ERROR;
1380 }
1381 return NOT_ENOUGH_DATA;
1382}
1383
1384const void* Parcel::readInplace(size_t len) const
1385{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001386 if (len > INT32_MAX) {
1387 // don't accept size_t values which may have come from an
1388 // inadvertent conversion from a negative int.
1389 return NULL;
1390 }
1391
1392 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1393 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001394 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001395 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001396 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 return data;
1398 }
1399 return NULL;
1400}
1401
Andreas Huber84a6d042009-08-17 13:33:27 -07001402template<class T>
1403status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001404 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001405
1406 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001408 mDataPos += sizeof(T);
1409 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 return NO_ERROR;
1411 } else {
1412 return NOT_ENOUGH_DATA;
1413 }
1414}
1415
Andreas Huber84a6d042009-08-17 13:33:27 -07001416template<class T>
1417T Parcel::readAligned() const {
1418 T result;
1419 if (readAligned(&result) != NO_ERROR) {
1420 result = 0;
1421 }
1422
1423 return result;
1424}
1425
1426template<class T>
1427status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001428 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001429
1430 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1431restart_write:
1432 *reinterpret_cast<T*>(mData+mDataPos) = val;
1433 return finishWrite(sizeof(val));
1434 }
1435
1436 status_t err = growData(sizeof(val));
1437 if (err == NO_ERROR) goto restart_write;
1438 return err;
1439}
1440
Casey Dahlin185d3442016-02-09 11:08:35 -08001441namespace {
1442
1443template<typename T>
1444status_t readByteVectorInternal(const Parcel* parcel,
1445 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001446 val->clear();
1447
1448 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001449 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001450
1451 if (status != OK) {
1452 return status;
1453 }
1454
Christopher Wiley4db672d2015-11-10 09:44:30 -08001455 if (size < 0) {
1456 status = UNEXPECTED_NULL;
1457 return status;
1458 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001459 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001460 status = BAD_VALUE;
1461 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001462 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001463
Casey Dahlin185d3442016-02-09 11:08:35 -08001464 const void* data = parcel->readInplace(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001465 if (!data) {
1466 status = BAD_VALUE;
1467 return status;
1468 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001469 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001470 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001471
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001472 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001473}
1474
Casey Dahlin185d3442016-02-09 11:08:35 -08001475template<typename T>
1476status_t readByteVectorInternalPtr(
1477 const Parcel* parcel,
1478 std::unique_ptr<std::vector<T>>* val) {
1479 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001480 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001481 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001482 val->reset();
1483
1484 if (status != OK || size < 0) {
1485 return status;
1486 }
1487
Casey Dahlin185d3442016-02-09 11:08:35 -08001488 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001489 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001490
Casey Dahlin185d3442016-02-09 11:08:35 -08001491 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001492
1493 if (status != OK) {
1494 val->reset();
1495 }
1496
1497 return status;
1498}
1499
Casey Dahlin185d3442016-02-09 11:08:35 -08001500} // namespace
1501
1502status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1503 return readByteVectorInternal(this, val);
1504}
1505
1506status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1507 return readByteVectorInternal(this, val);
1508}
1509
1510status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1511 return readByteVectorInternalPtr(this, val);
1512}
1513
1514status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1515 return readByteVectorInternalPtr(this, val);
1516}
1517
Casey Dahlinb9872622015-11-25 15:09:45 -08001518status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1519 return readNullableTypedVector(val, &Parcel::readInt32);
1520}
1521
Casey Dahlin451ff582015-10-19 18:12:18 -07001522status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001523 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001524}
1525
Casey Dahlinb9872622015-11-25 15:09:45 -08001526status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1527 return readNullableTypedVector(val, &Parcel::readInt64);
1528}
1529
Casey Dahlin451ff582015-10-19 18:12:18 -07001530status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001531 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001532}
1533
Casey Dahlinb9872622015-11-25 15:09:45 -08001534status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1535 return readNullableTypedVector(val, &Parcel::readFloat);
1536}
1537
Casey Dahlin451ff582015-10-19 18:12:18 -07001538status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001539 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001540}
1541
Casey Dahlinb9872622015-11-25 15:09:45 -08001542status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1543 return readNullableTypedVector(val, &Parcel::readDouble);
1544}
1545
Casey Dahlin451ff582015-10-19 18:12:18 -07001546status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001547 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001548}
1549
Casey Dahlinb9872622015-11-25 15:09:45 -08001550status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1551 const int32_t start = dataPosition();
1552 int32_t size;
1553 status_t status = readInt32(&size);
1554 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001555
Casey Dahlinb9872622015-11-25 15:09:45 -08001556 if (status != OK || size < 0) {
1557 return status;
1558 }
1559
1560 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001561 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001562
1563 status = readBoolVector(val->get());
1564
1565 if (status != OK) {
1566 val->reset();
1567 }
1568
1569 return status;
1570}
1571
1572status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001573 int32_t size;
1574 status_t status = readInt32(&size);
1575
1576 if (status != OK) {
1577 return status;
1578 }
1579
1580 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001581 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001582 }
1583
1584 val->resize(size);
1585
1586 /* C++ bool handling means a vector of bools isn't necessarily addressable
1587 * (we might use individual bits)
1588 */
Christopher Wiley97887982015-10-27 16:33:47 -07001589 bool data;
1590 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001591 status = readBool(&data);
1592 (*val)[i] = data;
1593
1594 if (status != OK) {
1595 return status;
1596 }
1597 }
1598
1599 return OK;
1600}
1601
Casey Dahlinb9872622015-11-25 15:09:45 -08001602status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1603 return readNullableTypedVector(val, &Parcel::readChar);
1604}
1605
Casey Dahlin451ff582015-10-19 18:12:18 -07001606status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001607 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001608}
1609
Casey Dahlinb9872622015-11-25 15:09:45 -08001610status_t Parcel::readString16Vector(
1611 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1612 return readNullableTypedVector(val, &Parcel::readString16);
1613}
1614
Casey Dahlin451ff582015-10-19 18:12:18 -07001615status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001616 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001617}
1618
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001619status_t Parcel::readUtf8VectorFromUtf16Vector(
1620 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1621 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1622}
1623
1624status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1625 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1626}
Casey Dahlin451ff582015-10-19 18:12:18 -07001627
Andreas Huber84a6d042009-08-17 13:33:27 -07001628status_t Parcel::readInt32(int32_t *pArg) const
1629{
1630 return readAligned(pArg);
1631}
1632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633int32_t Parcel::readInt32() const
1634{
Andreas Huber84a6d042009-08-17 13:33:27 -07001635 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001636}
1637
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001638status_t Parcel::readUint32(uint32_t *pArg) const
1639{
1640 return readAligned(pArg);
1641}
1642
1643uint32_t Parcel::readUint32() const
1644{
1645 return readAligned<uint32_t>();
1646}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647
1648status_t Parcel::readInt64(int64_t *pArg) const
1649{
Andreas Huber84a6d042009-08-17 13:33:27 -07001650 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651}
1652
1653
1654int64_t Parcel::readInt64() const
1655{
Andreas Huber84a6d042009-08-17 13:33:27 -07001656 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001657}
1658
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001659status_t Parcel::readUint64(uint64_t *pArg) const
1660{
1661 return readAligned(pArg);
1662}
1663
1664uint64_t Parcel::readUint64() const
1665{
1666 return readAligned<uint64_t>();
1667}
1668
Serban Constantinescuf683e012013-11-05 16:53:55 +00001669status_t Parcel::readPointer(uintptr_t *pArg) const
1670{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001671 status_t ret;
1672 binder_uintptr_t ptr;
1673 ret = readAligned(&ptr);
1674 if (!ret)
1675 *pArg = ptr;
1676 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001677}
1678
1679uintptr_t Parcel::readPointer() const
1680{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001681 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001682}
1683
1684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001685status_t Parcel::readFloat(float *pArg) const
1686{
Andreas Huber84a6d042009-08-17 13:33:27 -07001687 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688}
1689
1690
1691float Parcel::readFloat() const
1692{
Andreas Huber84a6d042009-08-17 13:33:27 -07001693 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694}
1695
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001696#if defined(__mips__) && defined(__mips_hard_float)
1697
1698status_t Parcel::readDouble(double *pArg) const
1699{
1700 union {
1701 double d;
1702 unsigned long long ll;
1703 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001704 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001705 status_t status;
1706 status = readAligned(&u.ll);
1707 *pArg = u.d;
1708 return status;
1709}
1710
1711double Parcel::readDouble() const
1712{
1713 union {
1714 double d;
1715 unsigned long long ll;
1716 } u;
1717 u.ll = readAligned<unsigned long long>();
1718 return u.d;
1719}
1720
1721#else
1722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723status_t Parcel::readDouble(double *pArg) const
1724{
Andreas Huber84a6d042009-08-17 13:33:27 -07001725 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726}
1727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001728double Parcel::readDouble() const
1729{
Andreas Huber84a6d042009-08-17 13:33:27 -07001730 return readAligned<double>();
1731}
1732
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001733#endif
1734
Andreas Huber84a6d042009-08-17 13:33:27 -07001735status_t Parcel::readIntPtr(intptr_t *pArg) const
1736{
1737 return readAligned(pArg);
1738}
1739
1740
1741intptr_t Parcel::readIntPtr() const
1742{
1743 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744}
1745
Casey Dahlind6848f52015-10-15 15:44:59 -07001746status_t Parcel::readBool(bool *pArg) const
1747{
1748 int32_t tmp;
1749 status_t ret = readInt32(&tmp);
1750 *pArg = (tmp != 0);
1751 return ret;
1752}
1753
1754bool Parcel::readBool() const
1755{
1756 return readInt32() != 0;
1757}
1758
1759status_t Parcel::readChar(char16_t *pArg) const
1760{
1761 int32_t tmp;
1762 status_t ret = readInt32(&tmp);
1763 *pArg = char16_t(tmp);
1764 return ret;
1765}
1766
1767char16_t Parcel::readChar() const
1768{
1769 return char16_t(readInt32());
1770}
1771
1772status_t Parcel::readByte(int8_t *pArg) const
1773{
1774 int32_t tmp;
1775 status_t ret = readInt32(&tmp);
1776 *pArg = int8_t(tmp);
1777 return ret;
1778}
1779
1780int8_t Parcel::readByte() const
1781{
1782 return int8_t(readInt32());
1783}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001785status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1786 size_t utf16Size = 0;
1787 const char16_t* src = readString16Inplace(&utf16Size);
1788 if (!src) {
1789 return UNEXPECTED_NULL;
1790 }
1791
1792 // Save ourselves the trouble, we're done.
1793 if (utf16Size == 0u) {
1794 str->clear();
1795 return NO_ERROR;
1796 }
1797
1798 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1799 if (utf8Size < 0) {
1800 return BAD_VALUE;
1801 }
1802 // Note that while it is probably safe to assume string::resize keeps a
1803 // spare byte around for the trailing null, we're going to be explicit.
1804 str->resize(utf8Size + 1);
1805 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1806 str->resize(utf8Size);
1807 return NO_ERROR;
1808}
1809
1810status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1811 const int32_t start = dataPosition();
1812 int32_t size;
1813 status_t status = readInt32(&size);
1814 str->reset();
1815
1816 if (status != OK || size < 0) {
1817 return status;
1818 }
1819
1820 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001821 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001822 return readUtf8FromUtf16(str->get());
1823}
1824
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001825const char* Parcel::readCString() const
1826{
1827 const size_t avail = mDataSize-mDataPos;
1828 if (avail > 0) {
1829 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1830 // is the string's trailing NUL within the parcel's valid bounds?
1831 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1832 if (eos) {
1833 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001834 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001835 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836 return str;
1837 }
1838 }
1839 return NULL;
1840}
1841
1842String8 Parcel::readString8() const
1843{
Roshan Pius87b64d22016-07-18 12:51:02 -07001844 String8 retString;
1845 status_t status = readString8(&retString);
1846 if (status != OK) {
1847 // We don't care about errors here, so just return an empty string.
1848 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001849 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001850 return retString;
1851}
1852
1853status_t Parcel::readString8(String8* pArg) const
1854{
1855 int32_t size;
1856 status_t status = readInt32(&size);
1857 if (status != OK) {
1858 return status;
1859 }
1860 // watch for potential int overflow from size+1
1861 if (size < 0 || size >= INT32_MAX) {
1862 return BAD_VALUE;
1863 }
1864 // |writeString8| writes nothing for empty string.
1865 if (size == 0) {
1866 *pArg = String8();
1867 return OK;
1868 }
1869 const char* str = (const char*)readInplace(size + 1);
1870 if (str == NULL) {
1871 return BAD_VALUE;
1872 }
1873 pArg->setTo(str, size);
1874 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875}
1876
1877String16 Parcel::readString16() const
1878{
1879 size_t len;
1880 const char16_t* str = readString16Inplace(&len);
1881 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001882 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001883 return String16();
1884}
1885
Casey Dahlinb9872622015-11-25 15:09:45 -08001886status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1887{
1888 const int32_t start = dataPosition();
1889 int32_t size;
1890 status_t status = readInt32(&size);
1891 pArg->reset();
1892
1893 if (status != OK || size < 0) {
1894 return status;
1895 }
1896
1897 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001898 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001899
1900 status = readString16(pArg->get());
1901
1902 if (status != OK) {
1903 pArg->reset();
1904 }
1905
1906 return status;
1907}
1908
Casey Dahlin451ff582015-10-19 18:12:18 -07001909status_t Parcel::readString16(String16* pArg) const
1910{
1911 size_t len;
1912 const char16_t* str = readString16Inplace(&len);
1913 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001914 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001915 return 0;
1916 } else {
1917 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001918 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001919 }
1920}
1921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1923{
1924 int32_t size = readInt32();
1925 // watch for potential int overflow from size+1
1926 if (size >= 0 && size < INT32_MAX) {
1927 *outLen = size;
1928 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1929 if (str != NULL) {
1930 return str;
1931 }
1932 }
1933 *outLen = 0;
1934 return NULL;
1935}
1936
Casey Dahlinf0c13772015-10-27 18:33:56 -07001937status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1938{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001939 status_t status = readNullableStrongBinder(val);
1940 if (status == OK && !val->get()) {
1941 status = UNEXPECTED_NULL;
1942 }
1943 return status;
1944}
1945
1946status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1947{
Casey Dahlinf0c13772015-10-27 18:33:56 -07001948 return unflatten_binder(ProcessState::self(), *this, val);
1949}
1950
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951sp<IBinder> Parcel::readStrongBinder() const
1952{
1953 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001954 // Note that a lot of code in Android reads binders by hand with this
1955 // method, and that code has historically been ok with getting nullptr
1956 // back (while ignoring error codes).
1957 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958 return val;
1959}
1960
1961wp<IBinder> Parcel::readWeakBinder() const
1962{
1963 wp<IBinder> val;
1964 unflatten_binder(ProcessState::self(), *this, &val);
1965 return val;
1966}
1967
Christopher Wiley97f048d2015-11-19 06:49:05 -08001968status_t Parcel::readParcelable(Parcelable* parcelable) const {
1969 int32_t have_parcelable = 0;
1970 status_t status = readInt32(&have_parcelable);
1971 if (status != OK) {
1972 return status;
1973 }
1974 if (!have_parcelable) {
1975 return UNEXPECTED_NULL;
1976 }
1977 return parcelable->readFromParcel(this);
1978}
1979
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001980int32_t Parcel::readExceptionCode() const
1981{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001982 binder::Status status;
1983 status.readFromParcel(*this);
1984 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001985}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001986
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001987native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001988{
1989 int numFds, numInts;
1990 status_t err;
1991 err = readInt32(&numFds);
1992 if (err != NO_ERROR) return 0;
1993 err = readInt32(&numInts);
1994 if (err != NO_ERROR) return 0;
1995
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001996 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001997 if (!h) {
1998 return 0;
1999 }
2000
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002001 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08002002 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07002003 if (h->data[i] < 0) {
2004 for (int j = 0; j < i; j++) {
2005 close(h->data[j]);
2006 }
2007 native_handle_delete(h);
2008 return 0;
2009 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002010 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002011 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002012 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002013 native_handle_close(h);
2014 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002015 h = 0;
2016 }
2017 return h;
2018}
2019
2020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021int Parcel::readFileDescriptor() const
2022{
2023 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002024
2025 if (flat && flat->type == BINDER_TYPE_FD) {
2026 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002027 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 return BAD_TYPE;
2030}
2031
Christopher Wiley2cf19952016-04-11 11:09:37 -07002032status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002033{
2034 int got = readFileDescriptor();
2035
2036 if (got == BAD_TYPE) {
2037 return BAD_TYPE;
2038 }
2039
2040 val->reset(dup(got));
2041
2042 if (val->get() < 0) {
2043 return BAD_VALUE;
2044 }
2045
2046 return OK;
2047}
2048
2049
Christopher Wiley2cf19952016-04-11 11:09:37 -07002050status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002051 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2052}
2053
Christopher Wiley2cf19952016-04-11 11:09:37 -07002054status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002055 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2056}
2057
Jeff Brown5707dbf2011-09-23 21:17:56 -07002058status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2059{
Jeff Brown13b16042014-11-11 16:44:25 -08002060 int32_t blobType;
2061 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002062 if (status) return status;
2063
Jeff Brown13b16042014-11-11 16:44:25 -08002064 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002065 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002066 const void* ptr = readInplace(len);
2067 if (!ptr) return BAD_VALUE;
2068
Jeff Brown13b16042014-11-11 16:44:25 -08002069 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002070 return NO_ERROR;
2071 }
2072
Steve Block6807e592011-10-20 11:56:00 +01002073 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002074 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002075 int fd = readFileDescriptor();
2076 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2077
Jeff Brown13b16042014-11-11 16:44:25 -08002078 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2079 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002080 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002081
Jeff Brown13b16042014-11-11 16:44:25 -08002082 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002083 return NO_ERROR;
2084}
2085
Mathias Agopiane1424282013-07-29 21:24:40 -07002086status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002087{
2088 // size
2089 const size_t len = this->readInt32();
2090 const size_t fd_count = this->readInt32();
2091
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002092 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002093 // don't accept size_t values which may have come from an
2094 // inadvertent conversion from a negative int.
2095 return BAD_VALUE;
2096 }
2097
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002098 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002099 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002100 if (buf == NULL)
2101 return BAD_VALUE;
2102
2103 int* fds = NULL;
2104 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002105 fds = new (std::nothrow) int[fd_count];
2106 if (fds == nullptr) {
2107 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2108 return BAD_VALUE;
2109 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002110 }
2111
2112 status_t err = NO_ERROR;
2113 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08002114 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002115 if (fds[i] < 0) {
2116 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08002117 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
2118 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002119 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002120 }
2121
2122 if (err == NO_ERROR) {
2123 err = val.unflatten(buf, len, fds, fd_count);
2124 }
2125
2126 if (fd_count) {
2127 delete [] fds;
2128 }
2129
2130 return err;
2131}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2133{
2134 const size_t DPOS = mDataPos;
2135 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2136 const flat_binder_object* obj
2137 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2138 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002139 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002140 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 // the object list, so we don't want to check for it when
2142 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002143 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144 return obj;
2145 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002146
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002148 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 const size_t N = mObjectsSize;
2150 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156 // Start at the current hint position, looking for an object at
2157 // the current data position.
2158 if (opos < N) {
2159 while (opos < (N-1) && OBJS[opos] < DPOS) {
2160 opos++;
2161 }
2162 } else {
2163 opos = N-1;
2164 }
2165 if (OBJS[opos] == DPOS) {
2166 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 this, DPOS, opos);
2169 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002170 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 return obj;
2172 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002173
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 // Look backwards for it...
2175 while (opos > 0 && OBJS[opos] > DPOS) {
2176 opos--;
2177 }
2178 if (OBJS[opos] == DPOS) {
2179 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002180 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 this, DPOS, opos);
2182 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002183 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 return obj;
2185 }
2186 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002187 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 -07002188 this, DPOS);
2189 }
2190 return NULL;
2191}
2192
2193void Parcel::closeFileDescriptors()
2194{
2195 size_t i = mObjectsSize;
2196 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002197 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 }
2199 while (i > 0) {
2200 i--;
2201 const flat_binder_object* flat
2202 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2203 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 close(flat->handle);
2206 }
2207 }
2208}
2209
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002210uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002212 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213}
2214
2215size_t Parcel::ipcDataSize() const
2216{
2217 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2218}
2219
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002220uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002222 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223}
2224
2225size_t Parcel::ipcObjectsCount() const
2226{
2227 return mObjectsSize;
2228}
2229
2230void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002231 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002233 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 freeDataNoInit();
2235 mError = NO_ERROR;
2236 mData = const_cast<uint8_t*>(data);
2237 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002240 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002241 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 mObjectsSize = mObjectsCapacity = objectsCount;
2243 mNextObjectHint = 0;
2244 mOwner = relFunc;
2245 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002246 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002247 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002248 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002249 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002250 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002251 mObjectsSize = 0;
2252 break;
2253 }
2254 minOffset = offset + sizeof(flat_binder_object);
2255 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256 scanForFds();
2257}
2258
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002259void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260{
2261 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 if (errorCheck() != NO_ERROR) {
2264 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002265 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 } else if (dataSize() > 0) {
2267 const uint8_t* DATA = data();
2268 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002269 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 const size_t N = objectsCount();
2271 for (size_t i=0; i<N; i++) {
2272 const flat_binder_object* flat
2273 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2274 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2275 << TypeCode(flat->type & 0x7f7f7f00)
2276 << " = " << flat->binder;
2277 }
2278 } else {
2279 to << "NULL";
2280 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 to << ")";
2283}
2284
2285void Parcel::releaseObjects()
2286{
2287 const sp<ProcessState> proc(ProcessState::self());
2288 size_t i = mObjectsSize;
2289 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002290 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 while (i > 0) {
2292 i--;
2293 const flat_binder_object* flat
2294 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002295 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 }
2297}
2298
2299void Parcel::acquireObjects()
2300{
2301 const sp<ProcessState> proc(ProcessState::self());
2302 size_t i = mObjectsSize;
2303 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002304 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 while (i > 0) {
2306 i--;
2307 const flat_binder_object* flat
2308 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002309 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002310 }
2311}
2312
2313void Parcel::freeData()
2314{
2315 freeDataNoInit();
2316 initState();
2317}
2318
2319void Parcel::freeDataNoInit()
2320{
2321 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002322 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002323 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2325 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002326 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002328 if (mData) {
2329 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002330 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002331 if (mDataCapacity <= gParcelGlobalAllocSize) {
2332 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2333 } else {
2334 gParcelGlobalAllocSize = 0;
2335 }
2336 if (gParcelGlobalAllocCount > 0) {
2337 gParcelGlobalAllocCount--;
2338 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002339 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002340 free(mData);
2341 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 if (mObjects) free(mObjects);
2343 }
2344}
2345
2346status_t Parcel::growData(size_t len)
2347{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002348 if (len > INT32_MAX) {
2349 // don't accept size_t values which may have come from an
2350 // inadvertent conversion from a negative int.
2351 return BAD_VALUE;
2352 }
2353
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002354 size_t newSize = ((mDataSize+len)*3)/2;
2355 return (newSize <= mDataSize)
2356 ? (status_t) NO_MEMORY
2357 : continueWrite(newSize);
2358}
2359
2360status_t Parcel::restartWrite(size_t desired)
2361{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002362 if (desired > INT32_MAX) {
2363 // don't accept size_t values which may have come from an
2364 // inadvertent conversion from a negative int.
2365 return BAD_VALUE;
2366 }
2367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 if (mOwner) {
2369 freeData();
2370 return continueWrite(desired);
2371 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 uint8_t* data = (uint8_t*)realloc(mData, desired);
2374 if (!data && desired > mDataCapacity) {
2375 mError = NO_MEMORY;
2376 return NO_MEMORY;
2377 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002378
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002382 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002383 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002384 gParcelGlobalAllocSize += desired;
2385 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002386 if (!mData) {
2387 gParcelGlobalAllocCount++;
2388 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002389 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 mData = data;
2391 mDataCapacity = desired;
2392 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002393
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002394 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002395 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2396 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 free(mObjects);
2399 mObjects = NULL;
2400 mObjectsSize = mObjectsCapacity = 0;
2401 mNextObjectHint = 0;
2402 mHasFds = false;
2403 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002404 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 return NO_ERROR;
2407}
2408
2409status_t Parcel::continueWrite(size_t desired)
2410{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002411 if (desired > INT32_MAX) {
2412 // don't accept size_t values which may have come from an
2413 // inadvertent conversion from a negative int.
2414 return BAD_VALUE;
2415 }
2416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 // If shrinking, first adjust for any objects that appear
2418 // after the new data size.
2419 size_t objectsSize = mObjectsSize;
2420 if (desired < mDataSize) {
2421 if (desired == 0) {
2422 objectsSize = 0;
2423 } else {
2424 while (objectsSize > 0) {
2425 if (mObjects[objectsSize-1] < desired)
2426 break;
2427 objectsSize--;
2428 }
2429 }
2430 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 if (mOwner) {
2433 // If the size is going to zero, just release the owner's data.
2434 if (desired == 0) {
2435 freeData();
2436 return NO_ERROR;
2437 }
2438
2439 // If there is a different owner, we need to take
2440 // posession.
2441 uint8_t* data = (uint8_t*)malloc(desired);
2442 if (!data) {
2443 mError = NO_MEMORY;
2444 return NO_MEMORY;
2445 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002446 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002449 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002451 free(data);
2452
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453 mError = NO_MEMORY;
2454 return NO_MEMORY;
2455 }
2456
2457 // Little hack to only acquire references on objects
2458 // we will be keeping.
2459 size_t oldObjectsSize = mObjectsSize;
2460 mObjectsSize = objectsSize;
2461 acquireObjects();
2462 mObjectsSize = oldObjectsSize;
2463 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002464
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002465 if (mData) {
2466 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2467 }
2468 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002469 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002470 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002471 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2473 mOwner = NULL;
2474
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002475 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002476 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002477 gParcelGlobalAllocSize += desired;
2478 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002479 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 mData = data;
2482 mObjects = objects;
2483 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002484 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 mDataCapacity = desired;
2486 mObjectsSize = mObjectsCapacity = objectsSize;
2487 mNextObjectHint = 0;
2488
2489 } else if (mData) {
2490 if (objectsSize < mObjectsSize) {
2491 // Need to release refs on any objects we are dropping.
2492 const sp<ProcessState> proc(ProcessState::self());
2493 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2494 const flat_binder_object* flat
2495 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2496 if (flat->type == BINDER_TYPE_FD) {
2497 // will need to rescan because we may have lopped off the only FDs
2498 mFdsKnown = false;
2499 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002500 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002502 binder_size_t* objects =
2503 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 if (objects) {
2505 mObjects = objects;
2506 }
2507 mObjectsSize = objectsSize;
2508 mNextObjectHint = 0;
2509 }
2510
2511 // We own the data, so we can just do a realloc().
2512 if (desired > mDataCapacity) {
2513 uint8_t* data = (uint8_t*)realloc(mData, desired);
2514 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002515 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2516 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002517 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002518 gParcelGlobalAllocSize += desired;
2519 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002520 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mData = data;
2522 mDataCapacity = desired;
2523 } else if (desired > mDataCapacity) {
2524 mError = NO_MEMORY;
2525 return NO_MEMORY;
2526 }
2527 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002528 if (mDataSize > desired) {
2529 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002531 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 if (mDataPos > desired) {
2533 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002534 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 }
2536 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002537
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 } else {
2539 // This is the first data. Easy!
2540 uint8_t* data = (uint8_t*)malloc(desired);
2541 if (!data) {
2542 mError = NO_MEMORY;
2543 return NO_MEMORY;
2544 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 if(!(mDataCapacity == 0 && mObjects == NULL
2547 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002548 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002550
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002551 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002552 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002553 gParcelGlobalAllocSize += desired;
2554 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002555 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 mData = data;
2558 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002559 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2560 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002561 mDataCapacity = desired;
2562 }
2563
2564 return NO_ERROR;
2565}
2566
2567void Parcel::initState()
2568{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002569 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mError = NO_ERROR;
2571 mData = 0;
2572 mDataSize = 0;
2573 mDataCapacity = 0;
2574 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002575 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2576 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 mObjects = NULL;
2578 mObjectsSize = 0;
2579 mObjectsCapacity = 0;
2580 mNextObjectHint = 0;
2581 mHasFds = false;
2582 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002583 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002585 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002586
2587 // racing multiple init leads only to multiple identical write
2588 if (gMaxFds == 0) {
2589 struct rlimit result;
2590 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2591 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002592 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002593 } else {
2594 ALOGW("Unable to getrlimit: %s", strerror(errno));
2595 gMaxFds = 1024;
2596 }
2597 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598}
2599
2600void Parcel::scanForFds() const
2601{
2602 bool hasFds = false;
2603 for (size_t i=0; i<mObjectsSize; i++) {
2604 const flat_binder_object* flat
2605 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2606 if (flat->type == BINDER_TYPE_FD) {
2607 hasFds = true;
2608 break;
2609 }
2610 }
2611 mHasFds = hasFds;
2612 mFdsKnown = true;
2613}
2614
Dan Sandleraa5c2342015-04-10 10:08:45 -04002615size_t Parcel::getBlobAshmemSize() const
2616{
Adrian Roos6bb31142015-10-22 16:46:12 -07002617 // This used to return the size of all blobs that were written to ashmem, now we're returning
2618 // the ashmem currently referenced by this Parcel, which should be equivalent.
2619 // TODO: Remove method once ABI can be changed.
2620 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002621}
2622
Adrian Rooscbf37262015-10-22 16:12:53 -07002623size_t Parcel::getOpenAshmemSize() const
2624{
2625 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002626}
2627
2628// --- Parcel::Blob ---
2629
2630Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002631 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002632}
2633
2634Parcel::Blob::~Blob() {
2635 release();
2636}
2637
2638void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002639 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002640 ::munmap(mData, mSize);
2641 }
2642 clear();
2643}
2644
Jeff Brown13b16042014-11-11 16:44:25 -08002645void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2646 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002647 mData = data;
2648 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002649 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002650}
2651
2652void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002653 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002654 mData = NULL;
2655 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002656 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002657}
2658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659}; // namespace android