blob: da943051a6886b94695293babe096948f4057420 [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>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
107 switch (obj.type) {
108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
120 if (b != NULL) {
121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
128 if (b != NULL) b.get_refs()->incWeak(who);
129 return;
130 }
131 case BINDER_TYPE_FD: {
Mark Salyzyn80589362016-08-23 16:15:04 -0700132 if ((obj.cookie != 0) && (outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800143 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Adrian Roos6bb31142015-10-22 16:46:12 -0700149 acquire_object(proc, obj, who, NULL);
150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
155 switch (obj.type) {
156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
168 if (b != NULL) {
169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
176 if (b != NULL) b.get_refs()->decWeak(who);
177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Mark Salyzyn80589362016-08-23 16:15:04 -0700181 if ((outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800194 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
200 release_object(proc, obj, who, NULL);
201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
215 if (binder != NULL) {
216 IBinder *local = binder->localBinder();
217 if (!local) {
218 BpBinder *proxy = binder->remoteBinder();
219 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000220 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 }
222 const int32_t handle = proxy ? proxy->handle() : 0;
223 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800224 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 } else {
228 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
230 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 } else {
233 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800234 obj.binder = 0;
235 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 return finish_flatten_binder(binder, obj, out);
239}
240
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800241status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 const wp<IBinder>& binder, Parcel* out)
243{
244 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
247 if (binder != NULL) {
248 sp<IBinder> real = binder.promote();
249 if (real != NULL) {
250 IBinder *local = real->localBinder();
251 if (!local) {
252 BpBinder *proxy = real->remoteBinder();
253 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000254 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 }
256 const int32_t handle = proxy ? proxy->handle() : 0;
257 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800258 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800260 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 } else {
262 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800263 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
264 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 }
266 return finish_flatten_binder(real, obj, out);
267 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700268
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 // XXX How to deal? In order to flatten the given binder,
270 // we need to probe it for information, which requires a primary
271 // reference... but we don't have one.
272 //
273 // The OpenBinder implementation uses a dynamic_cast<> here,
274 // but we can't do that with the different reference counting
275 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000276 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800278 obj.binder = 0;
279 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700281
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 } else {
283 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800284 obj.binder = 0;
285 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286 return finish_flatten_binder(NULL, obj, out);
287 }
288}
289
290inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800291 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
292 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293{
294 return NO_ERROR;
295}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297status_t unflatten_binder(const sp<ProcessState>& proc,
298 const Parcel& in, sp<IBinder>* out)
299{
300 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302 if (flat) {
303 switch (flat->type) {
304 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800305 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 return finish_unflatten_binder(NULL, *flat, in);
307 case BINDER_TYPE_HANDLE:
308 *out = proc->getStrongProxyForHandle(flat->handle);
309 return finish_unflatten_binder(
310 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700311 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700312 }
313 return BAD_TYPE;
314}
315
316status_t unflatten_binder(const sp<ProcessState>& proc,
317 const Parcel& in, wp<IBinder>* out)
318{
319 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 if (flat) {
322 switch (flat->type) {
323 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800324 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 return finish_unflatten_binder(NULL, *flat, in);
326 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800327 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 reinterpret_cast<IBinder*>(flat->cookie),
330 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700331 } else {
332 *out = NULL;
333 }
334 return finish_unflatten_binder(NULL, *flat, in);
335 case BINDER_TYPE_HANDLE:
336 case BINDER_TYPE_WEAK_HANDLE:
337 *out = proc->getWeakProxyForHandle(flat->handle);
338 return finish_unflatten_binder(
339 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
340 }
341 }
342 return BAD_TYPE;
343}
344
345// ---------------------------------------------------------------------------
346
347Parcel::Parcel()
348{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700350 initState();
351}
352
353Parcel::~Parcel()
354{
355 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: destroyed", this);
357}
358
359size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800360 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
361 size_t size = gParcelGlobalAllocSize;
362 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
363 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800364}
365
366size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t count = gParcelGlobalAllocCount;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
370 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700371}
372
373const uint8_t* Parcel::data() const
374{
375 return mData;
376}
377
378size_t Parcel::dataSize() const
379{
380 return (mDataSize > mDataPos ? mDataSize : mDataPos);
381}
382
383size_t Parcel::dataAvail() const
384{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700385 size_t result = dataSize() - dataPosition();
386 if (result > INT32_MAX) {
387 abort();
388 }
389 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700390}
391
392size_t Parcel::dataPosition() const
393{
394 return mDataPos;
395}
396
397size_t Parcel::dataCapacity() const
398{
399 return mDataCapacity;
400}
401
402status_t Parcel::setDataSize(size_t size)
403{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700404 if (size > INT32_MAX) {
405 // don't accept size_t values which may have come from an
406 // inadvertent conversion from a negative int.
407 return BAD_VALUE;
408 }
409
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 status_t err;
411 err = continueWrite(size);
412 if (err == NO_ERROR) {
413 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700414 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415 }
416 return err;
417}
418
419void Parcel::setDataPosition(size_t pos) const
420{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700421 if (pos > INT32_MAX) {
422 // don't accept size_t values which may have come from an
423 // inadvertent conversion from a negative int.
424 abort();
425 }
426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 mDataPos = pos;
428 mNextObjectHint = 0;
429}
430
431status_t Parcel::setDataCapacity(size_t size)
432{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700433 if (size > INT32_MAX) {
434 // don't accept size_t values which may have come from an
435 // inadvertent conversion from a negative int.
436 return BAD_VALUE;
437 }
438
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700439 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700440 return NO_ERROR;
441}
442
443status_t Parcel::setData(const uint8_t* buffer, size_t len)
444{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700445 if (len > INT32_MAX) {
446 // don't accept size_t values which may have come from an
447 // inadvertent conversion from a negative int.
448 return BAD_VALUE;
449 }
450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700451 status_t err = restartWrite(len);
452 if (err == NO_ERROR) {
453 memcpy(const_cast<uint8_t*>(data()), buffer, len);
454 mDataSize = len;
455 mFdsKnown = false;
456 }
457 return err;
458}
459
Andreas Huber51faf462011-04-13 10:21:56 -0700460status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461{
462 const sp<ProcessState> proc(ProcessState::self());
463 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700464 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800465 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700466 size_t size = parcel->mObjectsSize;
467 int startPos = mDataPos;
468 int firstIndex = -1, lastIndex = -2;
469
470 if (len == 0) {
471 return NO_ERROR;
472 }
473
Nick Kralevichb6b14232015-04-02 09:36:02 -0700474 if (len > INT32_MAX) {
475 // don't accept size_t values which may have come from an
476 // inadvertent conversion from a negative int.
477 return BAD_VALUE;
478 }
479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480 // range checks against the source parcel size
481 if ((offset > parcel->mDataSize)
482 || (len > parcel->mDataSize)
483 || (offset + len > parcel->mDataSize)) {
484 return BAD_VALUE;
485 }
486
487 // Count objects in range
488 for (int i = 0; i < (int) size; i++) {
489 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700490 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700491 if (firstIndex == -1) {
492 firstIndex = i;
493 }
494 lastIndex = i;
495 }
496 }
497 int numObjects = lastIndex - firstIndex + 1;
498
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700499 if ((mDataSize+len) > mDataCapacity) {
500 // grow data
501 err = growData(len);
502 if (err != NO_ERROR) {
503 return err;
504 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 }
506
507 // append data
508 memcpy(mData + mDataPos, data + offset, len);
509 mDataPos += len;
510 mDataSize += len;
511
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400512 err = NO_ERROR;
513
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700514 if (numObjects > 0) {
515 // grow objects
516 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700517 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700518 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800519 binder_size_t *objects =
520 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
521 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 return NO_MEMORY;
523 }
524 mObjects = objects;
525 mObjectsCapacity = newSize;
526 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700527
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528 // append and acquire objects
529 int idx = mObjectsSize;
530 for (int i = firstIndex; i <= lastIndex; i++) {
531 size_t off = objects[i] - offset + startPos;
532 mObjects[idx++] = off;
533 mObjectsSize++;
534
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700535 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700537 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700540 // If this is a file descriptor, we need to dup it so the
541 // new Parcel now owns its own fd, and can declare that we
542 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800543 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800544 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400546 if (!mAllowFds) {
547 err = FDS_NOT_ALLOWED;
548 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549 }
550 }
551 }
552
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400553 return err;
554}
555
Jeff Brown13b16042014-11-11 16:44:25 -0800556bool Parcel::allowFds() const
557{
558 return mAllowFds;
559}
560
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700561bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400562{
563 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700564 if (!allowFds) {
565 mAllowFds = false;
566 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400567 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700568}
569
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700570void Parcel::restoreAllowFds(bool lastValue)
571{
572 mAllowFds = lastValue;
573}
574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700575bool Parcel::hasFileDescriptors() const
576{
577 if (!mFdsKnown) {
578 scanForFds();
579 }
580 return mHasFds;
581}
582
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700583// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584status_t Parcel::writeInterfaceToken(const String16& interface)
585{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700586 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
587 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700588 // currently the interface identification token is just its name as a string
589 return writeString16(interface);
590}
591
Mathias Agopian83c04462009-05-22 19:00:22 -0700592bool Parcel::checkInterface(IBinder* binder) const
593{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700594 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700595}
596
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700597bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700598 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700600 int32_t strictPolicy = readInt32();
601 if (threadState == NULL) {
602 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700603 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700604 if ((threadState->getLastTransactionBinderFlags() &
605 IBinder::FLAG_ONEWAY) != 0) {
606 // For one-way calls, the callee is running entirely
607 // disconnected from the caller, so disable StrictMode entirely.
608 // Not only does disk/network usage not impact the caller, but
609 // there's no way to commuicate back any violations anyway.
610 threadState->setStrictModePolicy(0);
611 } else {
612 threadState->setStrictModePolicy(strictPolicy);
613 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700614 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 if (str == interface) {
616 return true;
617 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700618 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619 String8(interface).string(), String8(str).string());
620 return false;
621 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700622}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800624const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625{
626 return mObjects;
627}
628
629size_t Parcel::objectsCount() const
630{
631 return mObjectsSize;
632}
633
634status_t Parcel::errorCheck() const
635{
636 return mError;
637}
638
639void Parcel::setError(status_t err)
640{
641 mError = err;
642}
643
644status_t Parcel::finishWrite(size_t len)
645{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700646 if (len > INT32_MAX) {
647 // don't accept size_t values which may have come from an
648 // inadvertent conversion from a negative int.
649 return BAD_VALUE;
650 }
651
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 //printf("Finish write of %d\n", len);
653 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700654 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655 if (mDataPos > mDataSize) {
656 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700657 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700658 }
659 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
660 return NO_ERROR;
661}
662
663status_t Parcel::writeUnpadded(const void* data, size_t len)
664{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700665 if (len > INT32_MAX) {
666 // don't accept size_t values which may have come from an
667 // inadvertent conversion from a negative int.
668 return BAD_VALUE;
669 }
670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671 size_t end = mDataPos + len;
672 if (end < mDataPos) {
673 // integer overflow
674 return BAD_VALUE;
675 }
676
677 if (end <= mDataCapacity) {
678restart_write:
679 memcpy(mData+mDataPos, data, len);
680 return finishWrite(len);
681 }
682
683 status_t err = growData(len);
684 if (err == NO_ERROR) goto restart_write;
685 return err;
686}
687
688status_t Parcel::write(const void* data, size_t len)
689{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700690 if (len > INT32_MAX) {
691 // don't accept size_t values which may have come from an
692 // inadvertent conversion from a negative int.
693 return BAD_VALUE;
694 }
695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700696 void* const d = writeInplace(len);
697 if (d) {
698 memcpy(d, data, len);
699 return NO_ERROR;
700 }
701 return mError;
702}
703
704void* Parcel::writeInplace(size_t len)
705{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700706 if (len > INT32_MAX) {
707 // don't accept size_t values which may have come from an
708 // inadvertent conversion from a negative int.
709 return NULL;
710 }
711
712 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700713
714 // sanity check for integer overflow
715 if (mDataPos+padded < mDataPos) {
716 return NULL;
717 }
718
719 if ((mDataPos+padded) <= mDataCapacity) {
720restart_write:
721 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
722 uint8_t* const data = mData+mDataPos;
723
724 // Need to pad at end?
725 if (padded != len) {
726#if BYTE_ORDER == BIG_ENDIAN
727 static const uint32_t mask[4] = {
728 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
729 };
730#endif
731#if BYTE_ORDER == LITTLE_ENDIAN
732 static const uint32_t mask[4] = {
733 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
734 };
735#endif
736 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
737 // *reinterpret_cast<void**>(data+padded-4));
738 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
739 }
740
741 finishWrite(padded);
742 return data;
743 }
744
745 status_t err = growData(padded);
746 if (err == NO_ERROR) goto restart_write;
747 return NULL;
748}
749
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800750status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
751 const uint8_t* strData = (uint8_t*)str.data();
752 const size_t strLen= str.length();
753 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100754 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800755 return BAD_VALUE;
756 }
757
758 status_t err = writeInt32(utf16Len);
759 if (err) {
760 return err;
761 }
762
763 // Allocate enough bytes to hold our converted string and its terminating NULL.
764 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
765 if (!dst) {
766 return NO_MEMORY;
767 }
768
Sergio Girof4607432016-07-21 14:46:35 +0100769 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800770
771 return NO_ERROR;
772}
773
774status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
775 if (!str) {
776 return writeInt32(-1);
777 }
778 return writeUtf8AsUtf16(*str);
779}
780
Casey Dahlin185d3442016-02-09 11:08:35 -0800781namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800782
Casey Dahlin185d3442016-02-09 11:08:35 -0800783template<typename T>
784status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700785{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700786 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700787 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700788 status = BAD_VALUE;
789 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700790 }
791
Casey Dahlin185d3442016-02-09 11:08:35 -0800792 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700793 if (status != OK) {
794 return status;
795 }
796
Casey Dahlin185d3442016-02-09 11:08:35 -0800797 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700798 if (!data) {
799 status = BAD_VALUE;
800 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700801 }
802
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700803 memcpy(data, val.data(), val.size());
804 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700805}
806
Casey Dahlin185d3442016-02-09 11:08:35 -0800807template<typename T>
808status_t writeByteVectorInternalPtr(Parcel* parcel,
809 const std::unique_ptr<std::vector<T>>& val)
810{
811 if (!val) {
812 return parcel->writeInt32(-1);
813 }
814
815 return writeByteVectorInternal(parcel, *val);
816}
817
818} // namespace
819
820status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
821 return writeByteVectorInternal(this, val);
822}
823
824status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
825{
826 return writeByteVectorInternalPtr(this, val);
827}
828
829status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
830 return writeByteVectorInternal(this, val);
831}
832
833status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
834{
835 return writeByteVectorInternalPtr(this, val);
836}
837
Casey Dahlin451ff582015-10-19 18:12:18 -0700838status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
839{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800840 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700841}
842
Casey Dahlinb9872622015-11-25 15:09:45 -0800843status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
844{
845 return writeNullableTypedVector(val, &Parcel::writeInt32);
846}
847
Casey Dahlin451ff582015-10-19 18:12:18 -0700848status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
849{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800850 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700851}
852
Casey Dahlinb9872622015-11-25 15:09:45 -0800853status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
854{
855 return writeNullableTypedVector(val, &Parcel::writeInt64);
856}
857
Casey Dahlin451ff582015-10-19 18:12:18 -0700858status_t Parcel::writeFloatVector(const std::vector<float>& val)
859{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800860 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700861}
862
Casey Dahlinb9872622015-11-25 15:09:45 -0800863status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
864{
865 return writeNullableTypedVector(val, &Parcel::writeFloat);
866}
867
Casey Dahlin451ff582015-10-19 18:12:18 -0700868status_t Parcel::writeDoubleVector(const std::vector<double>& val)
869{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800870 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700871}
872
Casey Dahlinb9872622015-11-25 15:09:45 -0800873status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
874{
875 return writeNullableTypedVector(val, &Parcel::writeDouble);
876}
877
Casey Dahlin451ff582015-10-19 18:12:18 -0700878status_t Parcel::writeBoolVector(const std::vector<bool>& val)
879{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800880 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700881}
882
Casey Dahlinb9872622015-11-25 15:09:45 -0800883status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
884{
885 return writeNullableTypedVector(val, &Parcel::writeBool);
886}
887
Casey Dahlin451ff582015-10-19 18:12:18 -0700888status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
889{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800890 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700891}
892
Casey Dahlinb9872622015-11-25 15:09:45 -0800893status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
894{
895 return writeNullableTypedVector(val, &Parcel::writeChar);
896}
897
Casey Dahlin451ff582015-10-19 18:12:18 -0700898status_t Parcel::writeString16Vector(const std::vector<String16>& val)
899{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800900 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700901}
902
Casey Dahlinb9872622015-11-25 15:09:45 -0800903status_t Parcel::writeString16Vector(
904 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
905{
906 return writeNullableTypedVector(val, &Parcel::writeString16);
907}
908
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800909status_t Parcel::writeUtf8VectorAsUtf16Vector(
910 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
911 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
912}
913
914status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
915 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
916}
917
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918status_t Parcel::writeInt32(int32_t val)
919{
Andreas Huber84a6d042009-08-17 13:33:27 -0700920 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800922
923status_t Parcel::writeUint32(uint32_t val)
924{
925 return writeAligned(val);
926}
927
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700928status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700929 if (len > INT32_MAX) {
930 // don't accept size_t values which may have come from an
931 // inadvertent conversion from a negative int.
932 return BAD_VALUE;
933 }
934
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700935 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700936 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700937 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700938 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700939 if (ret == NO_ERROR) {
940 ret = write(val, len * sizeof(*val));
941 }
942 return ret;
943}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700944status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700945 if (len > INT32_MAX) {
946 // don't accept size_t values which may have come from an
947 // inadvertent conversion from a negative int.
948 return BAD_VALUE;
949 }
950
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700951 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700952 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700953 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700954 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700955 if (ret == NO_ERROR) {
956 ret = write(val, len * sizeof(*val));
957 }
958 return ret;
959}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700960
Casey Dahlind6848f52015-10-15 15:44:59 -0700961status_t Parcel::writeBool(bool val)
962{
963 return writeInt32(int32_t(val));
964}
965
966status_t Parcel::writeChar(char16_t val)
967{
968 return writeInt32(int32_t(val));
969}
970
971status_t Parcel::writeByte(int8_t val)
972{
973 return writeInt32(int32_t(val));
974}
975
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976status_t Parcel::writeInt64(int64_t val)
977{
Andreas Huber84a6d042009-08-17 13:33:27 -0700978 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
980
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700981status_t Parcel::writeUint64(uint64_t val)
982{
983 return writeAligned(val);
984}
985
Serban Constantinescuf683e012013-11-05 16:53:55 +0000986status_t Parcel::writePointer(uintptr_t val)
987{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800988 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000989}
990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991status_t Parcel::writeFloat(float val)
992{
Andreas Huber84a6d042009-08-17 13:33:27 -0700993 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994}
995
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800996#if defined(__mips__) && defined(__mips_hard_float)
997
998status_t Parcel::writeDouble(double val)
999{
1000 union {
1001 double d;
1002 unsigned long long ll;
1003 } u;
1004 u.d = val;
1005 return writeAligned(u.ll);
1006}
1007
1008#else
1009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010status_t Parcel::writeDouble(double val)
1011{
Andreas Huber84a6d042009-08-17 13:33:27 -07001012 return writeAligned(val);
1013}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001015#endif
1016
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001017status_t Parcel::writeCString(const char* str)
1018{
1019 return write(str, strlen(str)+1);
1020}
1021
1022status_t Parcel::writeString8(const String8& str)
1023{
1024 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001025 // only write string if its length is more than zero characters,
1026 // as readString8 will only read if the length field is non-zero.
1027 // this is slightly different from how writeString16 works.
1028 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029 err = write(str.string(), str.bytes()+1);
1030 }
1031 return err;
1032}
1033
Casey Dahlinb9872622015-11-25 15:09:45 -08001034status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1035{
1036 if (!str) {
1037 return writeInt32(-1);
1038 }
1039
1040 return writeString16(*str);
1041}
1042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043status_t Parcel::writeString16(const String16& str)
1044{
1045 return writeString16(str.string(), str.size());
1046}
1047
1048status_t Parcel::writeString16(const char16_t* str, size_t len)
1049{
1050 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052 status_t err = writeInt32(len);
1053 if (err == NO_ERROR) {
1054 len *= sizeof(char16_t);
1055 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1056 if (data) {
1057 memcpy(data, str, len);
1058 *reinterpret_cast<char16_t*>(data+len) = 0;
1059 return NO_ERROR;
1060 }
1061 err = mError;
1062 }
1063 return err;
1064}
1065
1066status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1067{
1068 return flatten_binder(ProcessState::self(), val, this);
1069}
1070
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001071status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1072{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001073 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001074}
1075
Casey Dahlinb9872622015-11-25 15:09:45 -08001076status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1077{
1078 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1079}
1080
1081status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001082 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001083}
1084
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001085status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001086 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001087}
1088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001089status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1090{
1091 return flatten_binder(ProcessState::self(), val, this);
1092}
1093
Casey Dahlinb9872622015-11-25 15:09:45 -08001094status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1095 if (!parcelable) {
1096 return writeInt32(0);
1097 }
1098
1099 return writeParcelable(*parcelable);
1100}
1101
Christopher Wiley97f048d2015-11-19 06:49:05 -08001102status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1103 status_t status = writeInt32(1); // parcelable is not null.
1104 if (status != OK) {
1105 return status;
1106 }
1107 return parcelable.writeToParcel(this);
1108}
1109
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001110status_t Parcel::writeValue(const binder::Value& value) {
1111 return value.writeToParcel(this);
1112}
1113
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001114status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001115{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001116 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001117 return BAD_TYPE;
1118
1119 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001120 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001121 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001123 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001124 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001126 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1127 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128
1129 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001130 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131 return err;
1132 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001133 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001134 return err;
1135}
1136
Jeff Brown93ff1f92011-11-04 19:01:44 -07001137status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138{
1139 flat_binder_object obj;
1140 obj.type = BINDER_TYPE_FD;
1141 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001142 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001143 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001144 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145 return writeObject(obj, true);
1146}
1147
1148status_t Parcel::writeDupFileDescriptor(int fd)
1149{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001150 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001151 if (dupFd < 0) {
1152 return -errno;
1153 }
1154 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001155 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001156 close(dupFd);
1157 }
1158 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159}
1160
Dianne Hackborn1941a402016-08-29 12:30:43 -07001161status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1162{
1163 writeInt32(0);
1164 return writeFileDescriptor(fd, takeOwnership);
1165}
1166
Christopher Wiley2cf19952016-04-11 11:09:37 -07001167status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001168 return writeDupFileDescriptor(fd.get());
1169}
1170
Christopher Wiley2cf19952016-04-11 11:09:37 -07001171status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001172 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1173}
1174
Christopher Wiley2cf19952016-04-11 11:09:37 -07001175status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001176 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1177}
1178
Jeff Brown13b16042014-11-11 16:44:25 -08001179status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001180{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001181 if (len > INT32_MAX) {
1182 // don't accept size_t values which may have come from an
1183 // inadvertent conversion from a negative int.
1184 return BAD_VALUE;
1185 }
1186
Jeff Brown13b16042014-11-11 16:44:25 -08001187 status_t status;
1188 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001189 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001190 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001191 if (status) return status;
1192
1193 void* ptr = writeInplace(len);
1194 if (!ptr) return NO_MEMORY;
1195
Jeff Brown13b16042014-11-11 16:44:25 -08001196 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001197 return NO_ERROR;
1198 }
1199
Steve Block6807e592011-10-20 11:56:00 +01001200 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001201 int fd = ashmem_create_region("Parcel Blob", len);
1202 if (fd < 0) return NO_MEMORY;
1203
1204 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1205 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001206 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207 } else {
1208 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1209 if (ptr == MAP_FAILED) {
1210 status = -errno;
1211 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001212 if (!mutableCopy) {
1213 result = ashmem_set_prot_region(fd, PROT_READ);
1214 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001216 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001218 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001219 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001220 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001221 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001222 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 return NO_ERROR;
1224 }
1225 }
1226 }
1227 }
1228 ::munmap(ptr, len);
1229 }
1230 ::close(fd);
1231 return status;
1232}
1233
Jeff Brown13b16042014-11-11 16:44:25 -08001234status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1235{
1236 // Must match up with what's done in writeBlob.
1237 if (!mAllowFds) return FDS_NOT_ALLOWED;
1238 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1239 if (status) return status;
1240 return writeDupFileDescriptor(fd);
1241}
1242
Mathias Agopiane1424282013-07-29 21:24:40 -07001243status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001244{
1245 status_t err;
1246
1247 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001248 const size_t len = val.getFlattenedSize();
1249 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001250
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001251 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001252 // don't accept size_t values which may have come from an
1253 // inadvertent conversion from a negative int.
1254 return BAD_VALUE;
1255 }
1256
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001257 err = this->writeInt32(len);
1258 if (err) return err;
1259
1260 err = this->writeInt32(fd_count);
1261 if (err) return err;
1262
1263 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001264 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001265 if (buf == NULL)
1266 return BAD_VALUE;
1267
1268 int* fds = NULL;
1269 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001270 fds = new (std::nothrow) int[fd_count];
1271 if (fds == nullptr) {
1272 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1273 return BAD_VALUE;
1274 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001275 }
1276
1277 err = val.flatten(buf, len, fds, fd_count);
1278 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1279 err = this->writeDupFileDescriptor( fds[i] );
1280 }
1281
1282 if (fd_count) {
1283 delete [] fds;
1284 }
1285
1286 return err;
1287}
1288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001289status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1290{
1291 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1292 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1293 if (enoughData && enoughObjects) {
1294restart_write:
1295 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001296
Christopher Tate98e67d32015-06-03 18:44:15 -07001297 // remember if it's a file descriptor
1298 if (val.type == BINDER_TYPE_FD) {
1299 if (!mAllowFds) {
1300 // fail before modifying our object index
1301 return FDS_NOT_ALLOWED;
1302 }
1303 mHasFds = mFdsKnown = true;
1304 }
1305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001306 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001307 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001308 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001309 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310 mObjectsSize++;
1311 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313 return finishWrite(sizeof(flat_binder_object));
1314 }
1315
1316 if (!enoughData) {
1317 const status_t err = growData(sizeof(val));
1318 if (err != NO_ERROR) return err;
1319 }
1320 if (!enoughObjects) {
1321 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001322 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001323 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 if (objects == NULL) return NO_MEMORY;
1325 mObjects = objects;
1326 mObjectsCapacity = newSize;
1327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 goto restart_write;
1330}
1331
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001332status_t Parcel::writeNoException()
1333{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001334 binder::Status status;
1335 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001336}
1337
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001338status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1339{
1340 using ::std::map;
1341 using ::android::binder::Value;
1342 using ::android::binder::Map;
1343
1344 Map::const_iterator iter;
1345 status_t ret;
1346
1347 ret = writeInt32(map_in.size());
1348
1349 if (ret != NO_ERROR) {
1350 return ret;
1351 }
1352
1353 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1354 ret = writeValue(Value(iter->first));
1355 if (ret != NO_ERROR) {
1356 return ret;
1357 }
1358
1359 ret = writeValue(iter->second);
1360 if (ret != NO_ERROR) {
1361 return ret;
1362 }
1363 }
1364
1365 return ret;
1366}
1367
1368status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1369{
1370 if (map == NULL) {
1371 return writeInt32(-1);
1372 }
1373
1374 return writeMap(*map.get());
1375}
1376
1377status_t Parcel::readMap(::android::binder::Map* map_out)const
1378{
1379 using ::std::map;
1380 using ::android::String16;
1381 using ::android::String8;
1382 using ::android::binder::Value;
1383 using ::android::binder::Map;
1384
1385 status_t ret = NO_ERROR;
1386 int32_t count;
1387
1388 ret = readInt32(&count);
1389 if (ret != NO_ERROR) {
1390 return ret;
1391 }
1392
1393 if (count < 0) {
1394 ALOGE("readMap: Unexpected count: %d", count);
1395 return (count == -1)
1396 ? UNEXPECTED_NULL
1397 : BAD_VALUE;
1398 }
1399
1400 map_out->clear();
1401
1402 while (count--) {
1403 Map::key_type key;
1404 Value value;
1405
1406 ret = readValue(&value);
1407 if (ret != NO_ERROR) {
1408 return ret;
1409 }
1410
1411 if (!value.getString(&key)) {
1412 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1413 return BAD_VALUE;
1414 }
1415
1416 ret = readValue(&value);
1417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420
1421 (*map_out)[key] = value;
1422 }
1423
1424 return ret;
1425}
1426
1427status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1428{
1429 const size_t start = dataPosition();
1430 int32_t count;
1431 status_t status = readInt32(&count);
1432 map->reset();
1433
1434 if (status != OK || count == -1) {
1435 return status;
1436 }
1437
1438 setDataPosition(start);
1439 map->reset(new binder::Map());
1440
1441 status = readMap(map->get());
1442
1443 if (status != OK) {
1444 map->reset();
1445 }
1446
1447 return status;
1448}
1449
1450
1451
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001452void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001453{
1454 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1455}
1456
1457status_t Parcel::read(void* outData, size_t len) const
1458{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001459 if (len > INT32_MAX) {
1460 // don't accept size_t values which may have come from an
1461 // inadvertent conversion from a negative int.
1462 return BAD_VALUE;
1463 }
1464
1465 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1466 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001467 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001468 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001469 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001470 return NO_ERROR;
1471 }
1472 return NOT_ENOUGH_DATA;
1473}
1474
1475const void* Parcel::readInplace(size_t len) const
1476{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001477 if (len > INT32_MAX) {
1478 // don't accept size_t values which may have come from an
1479 // inadvertent conversion from a negative int.
1480 return NULL;
1481 }
1482
1483 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1484 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001486 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001487 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488 return data;
1489 }
1490 return NULL;
1491}
1492
Andreas Huber84a6d042009-08-17 13:33:27 -07001493template<class T>
1494status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001495 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001496
1497 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001499 mDataPos += sizeof(T);
1500 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001501 return NO_ERROR;
1502 } else {
1503 return NOT_ENOUGH_DATA;
1504 }
1505}
1506
Andreas Huber84a6d042009-08-17 13:33:27 -07001507template<class T>
1508T Parcel::readAligned() const {
1509 T result;
1510 if (readAligned(&result) != NO_ERROR) {
1511 result = 0;
1512 }
1513
1514 return result;
1515}
1516
1517template<class T>
1518status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001519 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001520
1521 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1522restart_write:
1523 *reinterpret_cast<T*>(mData+mDataPos) = val;
1524 return finishWrite(sizeof(val));
1525 }
1526
1527 status_t err = growData(sizeof(val));
1528 if (err == NO_ERROR) goto restart_write;
1529 return err;
1530}
1531
Casey Dahlin185d3442016-02-09 11:08:35 -08001532namespace {
1533
1534template<typename T>
1535status_t readByteVectorInternal(const Parcel* parcel,
1536 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001537 val->clear();
1538
1539 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001540 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001541
1542 if (status != OK) {
1543 return status;
1544 }
1545
Christopher Wiley4db672d2015-11-10 09:44:30 -08001546 if (size < 0) {
1547 status = UNEXPECTED_NULL;
1548 return status;
1549 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001550 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001551 status = BAD_VALUE;
1552 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001553 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001554
Paul Lietar433e87b2016-09-16 10:39:32 -07001555 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001556 if (!data) {
1557 status = BAD_VALUE;
1558 return status;
1559 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001560 val->reserve(size);
1561 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001562
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001563 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001564}
1565
Casey Dahlin185d3442016-02-09 11:08:35 -08001566template<typename T>
1567status_t readByteVectorInternalPtr(
1568 const Parcel* parcel,
1569 std::unique_ptr<std::vector<T>>* val) {
1570 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001571 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001572 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001573 val->reset();
1574
1575 if (status != OK || size < 0) {
1576 return status;
1577 }
1578
Casey Dahlin185d3442016-02-09 11:08:35 -08001579 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001580 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001581
Casey Dahlin185d3442016-02-09 11:08:35 -08001582 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001583
1584 if (status != OK) {
1585 val->reset();
1586 }
1587
1588 return status;
1589}
1590
Casey Dahlin185d3442016-02-09 11:08:35 -08001591} // namespace
1592
1593status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1594 return readByteVectorInternal(this, val);
1595}
1596
1597status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1598 return readByteVectorInternal(this, val);
1599}
1600
1601status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1602 return readByteVectorInternalPtr(this, val);
1603}
1604
1605status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1606 return readByteVectorInternalPtr(this, val);
1607}
1608
Casey Dahlinb9872622015-11-25 15:09:45 -08001609status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1610 return readNullableTypedVector(val, &Parcel::readInt32);
1611}
1612
Casey Dahlin451ff582015-10-19 18:12:18 -07001613status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001614 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001615}
1616
Casey Dahlinb9872622015-11-25 15:09:45 -08001617status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1618 return readNullableTypedVector(val, &Parcel::readInt64);
1619}
1620
Casey Dahlin451ff582015-10-19 18:12:18 -07001621status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001622 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001623}
1624
Casey Dahlinb9872622015-11-25 15:09:45 -08001625status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1626 return readNullableTypedVector(val, &Parcel::readFloat);
1627}
1628
Casey Dahlin451ff582015-10-19 18:12:18 -07001629status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001630 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001631}
1632
Casey Dahlinb9872622015-11-25 15:09:45 -08001633status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1634 return readNullableTypedVector(val, &Parcel::readDouble);
1635}
1636
Casey Dahlin451ff582015-10-19 18:12:18 -07001637status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001638 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001639}
1640
Casey Dahlinb9872622015-11-25 15:09:45 -08001641status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1642 const int32_t start = dataPosition();
1643 int32_t size;
1644 status_t status = readInt32(&size);
1645 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001646
Casey Dahlinb9872622015-11-25 15:09:45 -08001647 if (status != OK || size < 0) {
1648 return status;
1649 }
1650
1651 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001652 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001653
1654 status = readBoolVector(val->get());
1655
1656 if (status != OK) {
1657 val->reset();
1658 }
1659
1660 return status;
1661}
1662
1663status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001664 int32_t size;
1665 status_t status = readInt32(&size);
1666
1667 if (status != OK) {
1668 return status;
1669 }
1670
1671 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001672 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001673 }
1674
1675 val->resize(size);
1676
1677 /* C++ bool handling means a vector of bools isn't necessarily addressable
1678 * (we might use individual bits)
1679 */
Christopher Wiley97887982015-10-27 16:33:47 -07001680 bool data;
1681 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001682 status = readBool(&data);
1683 (*val)[i] = data;
1684
1685 if (status != OK) {
1686 return status;
1687 }
1688 }
1689
1690 return OK;
1691}
1692
Casey Dahlinb9872622015-11-25 15:09:45 -08001693status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1694 return readNullableTypedVector(val, &Parcel::readChar);
1695}
1696
Casey Dahlin451ff582015-10-19 18:12:18 -07001697status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001698 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001699}
1700
Casey Dahlinb9872622015-11-25 15:09:45 -08001701status_t Parcel::readString16Vector(
1702 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1703 return readNullableTypedVector(val, &Parcel::readString16);
1704}
1705
Casey Dahlin451ff582015-10-19 18:12:18 -07001706status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001707 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001708}
1709
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001710status_t Parcel::readUtf8VectorFromUtf16Vector(
1711 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1712 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1713}
1714
1715status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1716 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1717}
Casey Dahlin451ff582015-10-19 18:12:18 -07001718
Andreas Huber84a6d042009-08-17 13:33:27 -07001719status_t Parcel::readInt32(int32_t *pArg) const
1720{
1721 return readAligned(pArg);
1722}
1723
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724int32_t Parcel::readInt32() const
1725{
Andreas Huber84a6d042009-08-17 13:33:27 -07001726 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727}
1728
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001729status_t Parcel::readUint32(uint32_t *pArg) const
1730{
1731 return readAligned(pArg);
1732}
1733
1734uint32_t Parcel::readUint32() const
1735{
1736 return readAligned<uint32_t>();
1737}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738
1739status_t Parcel::readInt64(int64_t *pArg) const
1740{
Andreas Huber84a6d042009-08-17 13:33:27 -07001741 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742}
1743
1744
1745int64_t Parcel::readInt64() const
1746{
Andreas Huber84a6d042009-08-17 13:33:27 -07001747 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748}
1749
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001750status_t Parcel::readUint64(uint64_t *pArg) const
1751{
1752 return readAligned(pArg);
1753}
1754
1755uint64_t Parcel::readUint64() const
1756{
1757 return readAligned<uint64_t>();
1758}
1759
Serban Constantinescuf683e012013-11-05 16:53:55 +00001760status_t Parcel::readPointer(uintptr_t *pArg) const
1761{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001762 status_t ret;
1763 binder_uintptr_t ptr;
1764 ret = readAligned(&ptr);
1765 if (!ret)
1766 *pArg = ptr;
1767 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001768}
1769
1770uintptr_t Parcel::readPointer() const
1771{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001772 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001773}
1774
1775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001776status_t Parcel::readFloat(float *pArg) const
1777{
Andreas Huber84a6d042009-08-17 13:33:27 -07001778 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779}
1780
1781
1782float Parcel::readFloat() const
1783{
Andreas Huber84a6d042009-08-17 13:33:27 -07001784 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785}
1786
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001787#if defined(__mips__) && defined(__mips_hard_float)
1788
1789status_t Parcel::readDouble(double *pArg) const
1790{
1791 union {
1792 double d;
1793 unsigned long long ll;
1794 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001795 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001796 status_t status;
1797 status = readAligned(&u.ll);
1798 *pArg = u.d;
1799 return status;
1800}
1801
1802double Parcel::readDouble() const
1803{
1804 union {
1805 double d;
1806 unsigned long long ll;
1807 } u;
1808 u.ll = readAligned<unsigned long long>();
1809 return u.d;
1810}
1811
1812#else
1813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814status_t Parcel::readDouble(double *pArg) const
1815{
Andreas Huber84a6d042009-08-17 13:33:27 -07001816 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817}
1818
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001819double Parcel::readDouble() const
1820{
Andreas Huber84a6d042009-08-17 13:33:27 -07001821 return readAligned<double>();
1822}
1823
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001824#endif
1825
Andreas Huber84a6d042009-08-17 13:33:27 -07001826status_t Parcel::readIntPtr(intptr_t *pArg) const
1827{
1828 return readAligned(pArg);
1829}
1830
1831
1832intptr_t Parcel::readIntPtr() const
1833{
1834 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001835}
1836
Casey Dahlind6848f52015-10-15 15:44:59 -07001837status_t Parcel::readBool(bool *pArg) const
1838{
1839 int32_t tmp;
1840 status_t ret = readInt32(&tmp);
1841 *pArg = (tmp != 0);
1842 return ret;
1843}
1844
1845bool Parcel::readBool() const
1846{
1847 return readInt32() != 0;
1848}
1849
1850status_t Parcel::readChar(char16_t *pArg) const
1851{
1852 int32_t tmp;
1853 status_t ret = readInt32(&tmp);
1854 *pArg = char16_t(tmp);
1855 return ret;
1856}
1857
1858char16_t Parcel::readChar() const
1859{
1860 return char16_t(readInt32());
1861}
1862
1863status_t Parcel::readByte(int8_t *pArg) const
1864{
1865 int32_t tmp;
1866 status_t ret = readInt32(&tmp);
1867 *pArg = int8_t(tmp);
1868 return ret;
1869}
1870
1871int8_t Parcel::readByte() const
1872{
1873 return int8_t(readInt32());
1874}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001876status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1877 size_t utf16Size = 0;
1878 const char16_t* src = readString16Inplace(&utf16Size);
1879 if (!src) {
1880 return UNEXPECTED_NULL;
1881 }
1882
1883 // Save ourselves the trouble, we're done.
1884 if (utf16Size == 0u) {
1885 str->clear();
1886 return NO_ERROR;
1887 }
1888
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001889 // Allow for closing '\0'
1890 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1891 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001892 return BAD_VALUE;
1893 }
1894 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001895 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001896 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001897 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1898 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001899 return NO_ERROR;
1900}
1901
1902status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1903 const int32_t start = dataPosition();
1904 int32_t size;
1905 status_t status = readInt32(&size);
1906 str->reset();
1907
1908 if (status != OK || size < 0) {
1909 return status;
1910 }
1911
1912 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001913 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001914 return readUtf8FromUtf16(str->get());
1915}
1916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917const char* Parcel::readCString() const
1918{
1919 const size_t avail = mDataSize-mDataPos;
1920 if (avail > 0) {
1921 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1922 // is the string's trailing NUL within the parcel's valid bounds?
1923 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1924 if (eos) {
1925 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001926 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001927 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928 return str;
1929 }
1930 }
1931 return NULL;
1932}
1933
1934String8 Parcel::readString8() const
1935{
Roshan Pius87b64d22016-07-18 12:51:02 -07001936 String8 retString;
1937 status_t status = readString8(&retString);
1938 if (status != OK) {
1939 // We don't care about errors here, so just return an empty string.
1940 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001942 return retString;
1943}
1944
1945status_t Parcel::readString8(String8* pArg) const
1946{
1947 int32_t size;
1948 status_t status = readInt32(&size);
1949 if (status != OK) {
1950 return status;
1951 }
1952 // watch for potential int overflow from size+1
1953 if (size < 0 || size >= INT32_MAX) {
1954 return BAD_VALUE;
1955 }
1956 // |writeString8| writes nothing for empty string.
1957 if (size == 0) {
1958 *pArg = String8();
1959 return OK;
1960 }
1961 const char* str = (const char*)readInplace(size + 1);
1962 if (str == NULL) {
1963 return BAD_VALUE;
1964 }
1965 pArg->setTo(str, size);
1966 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967}
1968
1969String16 Parcel::readString16() const
1970{
1971 size_t len;
1972 const char16_t* str = readString16Inplace(&len);
1973 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001974 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975 return String16();
1976}
1977
Casey Dahlinb9872622015-11-25 15:09:45 -08001978status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1979{
1980 const int32_t start = dataPosition();
1981 int32_t size;
1982 status_t status = readInt32(&size);
1983 pArg->reset();
1984
1985 if (status != OK || size < 0) {
1986 return status;
1987 }
1988
1989 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001990 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001991
1992 status = readString16(pArg->get());
1993
1994 if (status != OK) {
1995 pArg->reset();
1996 }
1997
1998 return status;
1999}
2000
Casey Dahlin451ff582015-10-19 18:12:18 -07002001status_t Parcel::readString16(String16* pArg) const
2002{
2003 size_t len;
2004 const char16_t* str = readString16Inplace(&len);
2005 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002006 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002007 return 0;
2008 } else {
2009 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002010 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002011 }
2012}
2013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2015{
2016 int32_t size = readInt32();
2017 // watch for potential int overflow from size+1
2018 if (size >= 0 && size < INT32_MAX) {
2019 *outLen = size;
2020 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
2021 if (str != NULL) {
2022 return str;
2023 }
2024 }
2025 *outLen = 0;
2026 return NULL;
2027}
2028
Casey Dahlinf0c13772015-10-27 18:33:56 -07002029status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2030{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002031 status_t status = readNullableStrongBinder(val);
2032 if (status == OK && !val->get()) {
2033 status = UNEXPECTED_NULL;
2034 }
2035 return status;
2036}
2037
2038status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2039{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002040 return unflatten_binder(ProcessState::self(), *this, val);
2041}
2042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043sp<IBinder> Parcel::readStrongBinder() const
2044{
2045 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002046 // Note that a lot of code in Android reads binders by hand with this
2047 // method, and that code has historically been ok with getting nullptr
2048 // back (while ignoring error codes).
2049 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050 return val;
2051}
2052
2053wp<IBinder> Parcel::readWeakBinder() const
2054{
2055 wp<IBinder> val;
2056 unflatten_binder(ProcessState::self(), *this, &val);
2057 return val;
2058}
2059
Christopher Wiley97f048d2015-11-19 06:49:05 -08002060status_t Parcel::readParcelable(Parcelable* parcelable) const {
2061 int32_t have_parcelable = 0;
2062 status_t status = readInt32(&have_parcelable);
2063 if (status != OK) {
2064 return status;
2065 }
2066 if (!have_parcelable) {
2067 return UNEXPECTED_NULL;
2068 }
2069 return parcelable->readFromParcel(this);
2070}
2071
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002072status_t Parcel::readValue(binder::Value* value) const {
2073 return value->readFromParcel(this);
2074}
2075
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002076int32_t Parcel::readExceptionCode() const
2077{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002078 binder::Status status;
2079 status.readFromParcel(*this);
2080 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002081}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002082
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002083native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002084{
2085 int numFds, numInts;
2086 status_t err;
2087 err = readInt32(&numFds);
2088 if (err != NO_ERROR) return 0;
2089 err = readInt32(&numInts);
2090 if (err != NO_ERROR) return 0;
2091
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002092 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002093 if (!h) {
2094 return 0;
2095 }
2096
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002097 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002098 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002099 if (h->data[i] < 0) {
2100 for (int j = 0; j < i; j++) {
2101 close(h->data[j]);
2102 }
2103 native_handle_delete(h);
2104 return 0;
2105 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002106 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002107 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002108 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002109 native_handle_close(h);
2110 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002111 h = 0;
2112 }
2113 return h;
2114}
2115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116int Parcel::readFileDescriptor() const
2117{
2118 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002119
2120 if (flat && flat->type == BINDER_TYPE_FD) {
2121 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 return BAD_TYPE;
2125}
2126
Dianne Hackborn1941a402016-08-29 12:30:43 -07002127int Parcel::readParcelFileDescriptor() const
2128{
2129 int32_t hasComm = readInt32();
2130 int fd = readFileDescriptor();
2131 if (hasComm != 0) {
2132 // skip
2133 readFileDescriptor();
2134 }
2135 return fd;
2136}
2137
Christopher Wiley2cf19952016-04-11 11:09:37 -07002138status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002139{
2140 int got = readFileDescriptor();
2141
2142 if (got == BAD_TYPE) {
2143 return BAD_TYPE;
2144 }
2145
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002146 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002147
2148 if (val->get() < 0) {
2149 return BAD_VALUE;
2150 }
2151
2152 return OK;
2153}
2154
2155
Christopher Wiley2cf19952016-04-11 11:09:37 -07002156status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002157 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2158}
2159
Christopher Wiley2cf19952016-04-11 11:09:37 -07002160status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002161 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2162}
2163
Jeff Brown5707dbf2011-09-23 21:17:56 -07002164status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2165{
Jeff Brown13b16042014-11-11 16:44:25 -08002166 int32_t blobType;
2167 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002168 if (status) return status;
2169
Jeff Brown13b16042014-11-11 16:44:25 -08002170 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002171 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002172 const void* ptr = readInplace(len);
2173 if (!ptr) return BAD_VALUE;
2174
Jeff Brown13b16042014-11-11 16:44:25 -08002175 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002176 return NO_ERROR;
2177 }
2178
Steve Block6807e592011-10-20 11:56:00 +01002179 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002180 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002181 int fd = readFileDescriptor();
2182 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2183
Jeff Brown13b16042014-11-11 16:44:25 -08002184 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2185 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002186 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002187
Jeff Brown13b16042014-11-11 16:44:25 -08002188 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002189 return NO_ERROR;
2190}
2191
Mathias Agopiane1424282013-07-29 21:24:40 -07002192status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002193{
2194 // size
2195 const size_t len = this->readInt32();
2196 const size_t fd_count = this->readInt32();
2197
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002198 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002199 // don't accept size_t values which may have come from an
2200 // inadvertent conversion from a negative int.
2201 return BAD_VALUE;
2202 }
2203
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002204 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002205 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002206 if (buf == NULL)
2207 return BAD_VALUE;
2208
2209 int* fds = NULL;
2210 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002211 fds = new (std::nothrow) int[fd_count];
2212 if (fds == nullptr) {
2213 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2214 return BAD_VALUE;
2215 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002216 }
2217
2218 status_t err = NO_ERROR;
2219 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002220 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002221 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002222 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002223 ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002224 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2225 // Close all the file descriptors that were dup-ed.
2226 for (size_t j=0; j<i ;j++) {
2227 close(fds[j]);
2228 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002229 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002230 }
2231
2232 if (err == NO_ERROR) {
2233 err = val.unflatten(buf, len, fds, fd_count);
2234 }
2235
2236 if (fd_count) {
2237 delete [] fds;
2238 }
2239
2240 return err;
2241}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2243{
2244 const size_t DPOS = mDataPos;
2245 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2246 const flat_binder_object* obj
2247 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2248 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002249 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002250 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 // the object list, so we don't want to check for it when
2252 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002253 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254 return obj;
2255 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002256
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002258 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 const size_t N = mObjectsSize;
2260 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002263 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 // Start at the current hint position, looking for an object at
2267 // the current data position.
2268 if (opos < N) {
2269 while (opos < (N-1) && OBJS[opos] < DPOS) {
2270 opos++;
2271 }
2272 } else {
2273 opos = N-1;
2274 }
2275 if (OBJS[opos] == DPOS) {
2276 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002277 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 this, DPOS, opos);
2279 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002280 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 return obj;
2282 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284 // Look backwards for it...
2285 while (opos > 0 && OBJS[opos] > DPOS) {
2286 opos--;
2287 }
2288 if (OBJS[opos] == DPOS) {
2289 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002290 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 this, DPOS, opos);
2292 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002293 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 return obj;
2295 }
2296 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002297 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 -07002298 this, DPOS);
2299 }
2300 return NULL;
2301}
2302
2303void Parcel::closeFileDescriptors()
2304{
2305 size_t i = mObjectsSize;
2306 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002307 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 }
2309 while (i > 0) {
2310 i--;
2311 const flat_binder_object* flat
2312 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2313 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002314 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 close(flat->handle);
2316 }
2317 }
2318}
2319
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002320uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002322 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323}
2324
2325size_t Parcel::ipcDataSize() const
2326{
2327 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2328}
2329
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002330uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002332 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333}
2334
2335size_t Parcel::ipcObjectsCount() const
2336{
2337 return mObjectsSize;
2338}
2339
2340void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002341 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002343 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 freeDataNoInit();
2345 mError = NO_ERROR;
2346 mData = const_cast<uint8_t*>(data);
2347 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002350 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002351 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 mObjectsSize = mObjectsCapacity = objectsCount;
2353 mNextObjectHint = 0;
2354 mOwner = relFunc;
2355 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002356 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002357 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002358 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002359 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002360 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002361 mObjectsSize = 0;
2362 break;
2363 }
2364 minOffset = offset + sizeof(flat_binder_object);
2365 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002366 scanForFds();
2367}
2368
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002369void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370{
2371 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002372
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 if (errorCheck() != NO_ERROR) {
2374 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002375 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 } else if (dataSize() > 0) {
2377 const uint8_t* DATA = data();
2378 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002379 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002380 const size_t N = objectsCount();
2381 for (size_t i=0; i<N; i++) {
2382 const flat_binder_object* flat
2383 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2384 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2385 << TypeCode(flat->type & 0x7f7f7f00)
2386 << " = " << flat->binder;
2387 }
2388 } else {
2389 to << "NULL";
2390 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 to << ")";
2393}
2394
2395void Parcel::releaseObjects()
2396{
2397 const sp<ProcessState> proc(ProcessState::self());
2398 size_t i = mObjectsSize;
2399 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002400 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 while (i > 0) {
2402 i--;
2403 const flat_binder_object* flat
2404 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002405 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002406 }
2407}
2408
2409void Parcel::acquireObjects()
2410{
2411 const sp<ProcessState> proc(ProcessState::self());
2412 size_t i = mObjectsSize;
2413 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002414 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 while (i > 0) {
2416 i--;
2417 const flat_binder_object* flat
2418 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002419 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 }
2421}
2422
2423void Parcel::freeData()
2424{
2425 freeDataNoInit();
2426 initState();
2427}
2428
2429void Parcel::freeDataNoInit()
2430{
2431 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002432 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2435 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002436 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002438 if (mData) {
2439 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002440 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002441 if (mDataCapacity <= gParcelGlobalAllocSize) {
2442 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2443 } else {
2444 gParcelGlobalAllocSize = 0;
2445 }
2446 if (gParcelGlobalAllocCount > 0) {
2447 gParcelGlobalAllocCount--;
2448 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002449 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002450 free(mData);
2451 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 if (mObjects) free(mObjects);
2453 }
2454}
2455
2456status_t Parcel::growData(size_t len)
2457{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002458 if (len > INT32_MAX) {
2459 // don't accept size_t values which may have come from an
2460 // inadvertent conversion from a negative int.
2461 return BAD_VALUE;
2462 }
2463
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 size_t newSize = ((mDataSize+len)*3)/2;
2465 return (newSize <= mDataSize)
2466 ? (status_t) NO_MEMORY
2467 : continueWrite(newSize);
2468}
2469
2470status_t Parcel::restartWrite(size_t desired)
2471{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002472 if (desired > INT32_MAX) {
2473 // don't accept size_t values which may have come from an
2474 // inadvertent conversion from a negative int.
2475 return BAD_VALUE;
2476 }
2477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 if (mOwner) {
2479 freeData();
2480 return continueWrite(desired);
2481 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002482
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 uint8_t* data = (uint8_t*)realloc(mData, desired);
2484 if (!data && desired > mDataCapacity) {
2485 mError = NO_MEMORY;
2486 return NO_MEMORY;
2487 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002492 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002493 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002494 gParcelGlobalAllocSize += desired;
2495 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002496 if (!mData) {
2497 gParcelGlobalAllocCount++;
2498 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002499 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 mData = data;
2501 mDataCapacity = desired;
2502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2506 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 free(mObjects);
2509 mObjects = NULL;
2510 mObjectsSize = mObjectsCapacity = 0;
2511 mNextObjectHint = 0;
2512 mHasFds = false;
2513 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002514 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 return NO_ERROR;
2517}
2518
2519status_t Parcel::continueWrite(size_t desired)
2520{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002521 if (desired > INT32_MAX) {
2522 // don't accept size_t values which may have come from an
2523 // inadvertent conversion from a negative int.
2524 return BAD_VALUE;
2525 }
2526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 // If shrinking, first adjust for any objects that appear
2528 // after the new data size.
2529 size_t objectsSize = mObjectsSize;
2530 if (desired < mDataSize) {
2531 if (desired == 0) {
2532 objectsSize = 0;
2533 } else {
2534 while (objectsSize > 0) {
2535 if (mObjects[objectsSize-1] < desired)
2536 break;
2537 objectsSize--;
2538 }
2539 }
2540 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542 if (mOwner) {
2543 // If the size is going to zero, just release the owner's data.
2544 if (desired == 0) {
2545 freeData();
2546 return NO_ERROR;
2547 }
2548
2549 // If there is a different owner, we need to take
2550 // posession.
2551 uint8_t* data = (uint8_t*)malloc(desired);
2552 if (!data) {
2553 mError = NO_MEMORY;
2554 return NO_MEMORY;
2555 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002556 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002557
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002559 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002561 free(data);
2562
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 mError = NO_MEMORY;
2564 return NO_MEMORY;
2565 }
2566
2567 // Little hack to only acquire references on objects
2568 // we will be keeping.
2569 size_t oldObjectsSize = mObjectsSize;
2570 mObjectsSize = objectsSize;
2571 acquireObjects();
2572 mObjectsSize = oldObjectsSize;
2573 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 if (mData) {
2576 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2577 }
2578 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002579 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002581 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2583 mOwner = NULL;
2584
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002585 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002586 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002587 gParcelGlobalAllocSize += desired;
2588 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002589 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 mData = data;
2592 mObjects = objects;
2593 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002594 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 mDataCapacity = desired;
2596 mObjectsSize = mObjectsCapacity = objectsSize;
2597 mNextObjectHint = 0;
2598
2599 } else if (mData) {
2600 if (objectsSize < mObjectsSize) {
2601 // Need to release refs on any objects we are dropping.
2602 const sp<ProcessState> proc(ProcessState::self());
2603 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2604 const flat_binder_object* flat
2605 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2606 if (flat->type == BINDER_TYPE_FD) {
2607 // will need to rescan because we may have lopped off the only FDs
2608 mFdsKnown = false;
2609 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002610 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002612 binder_size_t* objects =
2613 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 if (objects) {
2615 mObjects = objects;
2616 }
2617 mObjectsSize = objectsSize;
2618 mNextObjectHint = 0;
2619 }
2620
2621 // We own the data, so we can just do a realloc().
2622 if (desired > mDataCapacity) {
2623 uint8_t* data = (uint8_t*)realloc(mData, desired);
2624 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002625 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2626 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002627 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002628 gParcelGlobalAllocSize += desired;
2629 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002630 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 mData = data;
2632 mDataCapacity = desired;
2633 } else if (desired > mDataCapacity) {
2634 mError = NO_MEMORY;
2635 return NO_MEMORY;
2636 }
2637 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002638 if (mDataSize > desired) {
2639 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002641 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 if (mDataPos > desired) {
2643 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002644 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002645 }
2646 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648 } else {
2649 // This is the first data. Easy!
2650 uint8_t* data = (uint8_t*)malloc(desired);
2651 if (!data) {
2652 mError = NO_MEMORY;
2653 return NO_MEMORY;
2654 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002656 if(!(mDataCapacity == 0 && mObjects == NULL
2657 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002658 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002660
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002661 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002662 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002663 gParcelGlobalAllocSize += desired;
2664 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002665 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002667 mData = data;
2668 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002669 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2670 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 mDataCapacity = desired;
2672 }
2673
2674 return NO_ERROR;
2675}
2676
2677void Parcel::initState()
2678{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002679 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002680 mError = NO_ERROR;
2681 mData = 0;
2682 mDataSize = 0;
2683 mDataCapacity = 0;
2684 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002685 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2686 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 mObjects = NULL;
2688 mObjectsSize = 0;
2689 mObjectsCapacity = 0;
2690 mNextObjectHint = 0;
2691 mHasFds = false;
2692 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002693 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002694 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002695 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002696
2697 // racing multiple init leads only to multiple identical write
2698 if (gMaxFds == 0) {
2699 struct rlimit result;
2700 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2701 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002702 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002703 } else {
2704 ALOGW("Unable to getrlimit: %s", strerror(errno));
2705 gMaxFds = 1024;
2706 }
2707 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002708}
2709
2710void Parcel::scanForFds() const
2711{
2712 bool hasFds = false;
2713 for (size_t i=0; i<mObjectsSize; i++) {
2714 const flat_binder_object* flat
2715 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2716 if (flat->type == BINDER_TYPE_FD) {
2717 hasFds = true;
2718 break;
2719 }
2720 }
2721 mHasFds = hasFds;
2722 mFdsKnown = true;
2723}
2724
Dan Sandleraa5c2342015-04-10 10:08:45 -04002725size_t Parcel::getBlobAshmemSize() const
2726{
Adrian Roos6bb31142015-10-22 16:46:12 -07002727 // This used to return the size of all blobs that were written to ashmem, now we're returning
2728 // the ashmem currently referenced by this Parcel, which should be equivalent.
2729 // TODO: Remove method once ABI can be changed.
2730 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002731}
2732
Adrian Rooscbf37262015-10-22 16:12:53 -07002733size_t Parcel::getOpenAshmemSize() const
2734{
2735 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002736}
2737
2738// --- Parcel::Blob ---
2739
2740Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002741 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002742}
2743
2744Parcel::Blob::~Blob() {
2745 release();
2746}
2747
2748void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002749 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002750 ::munmap(mData, mSize);
2751 }
2752 clear();
2753}
2754
Jeff Brown13b16042014-11-11 16:44:25 -08002755void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2756 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002757 mData = data;
2758 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002759 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002760}
2761
2762void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002763 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002764 mData = NULL;
2765 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002766 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002767}
2768
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769}; // namespace android