blob: 39bb078fd1caeb7bde724c5c94be8789b9180fad [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
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700556int Parcel::compareData(const Parcel& other) {
557 size_t size = dataSize();
558 if (size != other.dataSize()) {
559 return size < other.dataSize() ? -1 : 1;
560 }
561 return memcmp(data(), other.data(), size);
562}
563
Jeff Brown13b16042014-11-11 16:44:25 -0800564bool Parcel::allowFds() const
565{
566 return mAllowFds;
567}
568
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700569bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400570{
571 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572 if (!allowFds) {
573 mAllowFds = false;
574 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400575 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576}
577
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700578void Parcel::restoreAllowFds(bool lastValue)
579{
580 mAllowFds = lastValue;
581}
582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583bool Parcel::hasFileDescriptors() const
584{
585 if (!mFdsKnown) {
586 scanForFds();
587 }
588 return mHasFds;
589}
590
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700591// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592status_t Parcel::writeInterfaceToken(const String16& interface)
593{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700594 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
595 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596 // currently the interface identification token is just its name as a string
597 return writeString16(interface);
598}
599
Mathias Agopian83c04462009-05-22 19:00:22 -0700600bool Parcel::checkInterface(IBinder* binder) const
601{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700602 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700603}
604
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700605bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700606 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700607{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700608 int32_t strictPolicy = readInt32();
609 if (threadState == NULL) {
610 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700611 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700612 if ((threadState->getLastTransactionBinderFlags() &
613 IBinder::FLAG_ONEWAY) != 0) {
614 // For one-way calls, the callee is running entirely
615 // disconnected from the caller, so disable StrictMode entirely.
616 // Not only does disk/network usage not impact the caller, but
617 // there's no way to commuicate back any violations anyway.
618 threadState->setStrictModePolicy(0);
619 } else {
620 threadState->setStrictModePolicy(strictPolicy);
621 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700622 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (str == interface) {
624 return true;
625 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700626 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627 String8(interface).string(), String8(str).string());
628 return false;
629 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700630}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800632const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700633{
634 return mObjects;
635}
636
637size_t Parcel::objectsCount() const
638{
639 return mObjectsSize;
640}
641
642status_t Parcel::errorCheck() const
643{
644 return mError;
645}
646
647void Parcel::setError(status_t err)
648{
649 mError = err;
650}
651
652status_t Parcel::finishWrite(size_t len)
653{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700654 if (len > INT32_MAX) {
655 // don't accept size_t values which may have come from an
656 // inadvertent conversion from a negative int.
657 return BAD_VALUE;
658 }
659
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 //printf("Finish write of %d\n", len);
661 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700662 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663 if (mDataPos > mDataSize) {
664 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700665 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 }
667 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
668 return NO_ERROR;
669}
670
671status_t Parcel::writeUnpadded(const void* data, size_t len)
672{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700673 if (len > INT32_MAX) {
674 // don't accept size_t values which may have come from an
675 // inadvertent conversion from a negative int.
676 return BAD_VALUE;
677 }
678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679 size_t end = mDataPos + len;
680 if (end < mDataPos) {
681 // integer overflow
682 return BAD_VALUE;
683 }
684
685 if (end <= mDataCapacity) {
686restart_write:
687 memcpy(mData+mDataPos, data, len);
688 return finishWrite(len);
689 }
690
691 status_t err = growData(len);
692 if (err == NO_ERROR) goto restart_write;
693 return err;
694}
695
696status_t Parcel::write(const void* data, size_t len)
697{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700698 if (len > INT32_MAX) {
699 // don't accept size_t values which may have come from an
700 // inadvertent conversion from a negative int.
701 return BAD_VALUE;
702 }
703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700704 void* const d = writeInplace(len);
705 if (d) {
706 memcpy(d, data, len);
707 return NO_ERROR;
708 }
709 return mError;
710}
711
712void* Parcel::writeInplace(size_t len)
713{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700714 if (len > INT32_MAX) {
715 // don't accept size_t values which may have come from an
716 // inadvertent conversion from a negative int.
717 return NULL;
718 }
719
720 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721
722 // sanity check for integer overflow
723 if (mDataPos+padded < mDataPos) {
724 return NULL;
725 }
726
727 if ((mDataPos+padded) <= mDataCapacity) {
728restart_write:
729 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
730 uint8_t* const data = mData+mDataPos;
731
732 // Need to pad at end?
733 if (padded != len) {
734#if BYTE_ORDER == BIG_ENDIAN
735 static const uint32_t mask[4] = {
736 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
737 };
738#endif
739#if BYTE_ORDER == LITTLE_ENDIAN
740 static const uint32_t mask[4] = {
741 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
742 };
743#endif
744 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
745 // *reinterpret_cast<void**>(data+padded-4));
746 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
747 }
748
749 finishWrite(padded);
750 return data;
751 }
752
753 status_t err = growData(padded);
754 if (err == NO_ERROR) goto restart_write;
755 return NULL;
756}
757
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800758status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
759 const uint8_t* strData = (uint8_t*)str.data();
760 const size_t strLen= str.length();
761 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100762 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800763 return BAD_VALUE;
764 }
765
766 status_t err = writeInt32(utf16Len);
767 if (err) {
768 return err;
769 }
770
771 // Allocate enough bytes to hold our converted string and its terminating NULL.
772 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
773 if (!dst) {
774 return NO_MEMORY;
775 }
776
Sergio Girof4607432016-07-21 14:46:35 +0100777 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800778
779 return NO_ERROR;
780}
781
782status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
783 if (!str) {
784 return writeInt32(-1);
785 }
786 return writeUtf8AsUtf16(*str);
787}
788
Casey Dahlin185d3442016-02-09 11:08:35 -0800789namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800790
Casey Dahlin185d3442016-02-09 11:08:35 -0800791template<typename T>
792status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700793{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700794 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700795 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700796 status = BAD_VALUE;
797 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700798 }
799
Casey Dahlin185d3442016-02-09 11:08:35 -0800800 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700801 if (status != OK) {
802 return status;
803 }
804
Casey Dahlin185d3442016-02-09 11:08:35 -0800805 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700806 if (!data) {
807 status = BAD_VALUE;
808 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700809 }
810
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700811 memcpy(data, val.data(), val.size());
812 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700813}
814
Casey Dahlin185d3442016-02-09 11:08:35 -0800815template<typename T>
816status_t writeByteVectorInternalPtr(Parcel* parcel,
817 const std::unique_ptr<std::vector<T>>& val)
818{
819 if (!val) {
820 return parcel->writeInt32(-1);
821 }
822
823 return writeByteVectorInternal(parcel, *val);
824}
825
826} // namespace
827
828status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
829 return writeByteVectorInternal(this, val);
830}
831
832status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
833{
834 return writeByteVectorInternalPtr(this, val);
835}
836
837status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
838 return writeByteVectorInternal(this, val);
839}
840
841status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
842{
843 return writeByteVectorInternalPtr(this, val);
844}
845
Casey Dahlin451ff582015-10-19 18:12:18 -0700846status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
847{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800848 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700849}
850
Casey Dahlinb9872622015-11-25 15:09:45 -0800851status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
852{
853 return writeNullableTypedVector(val, &Parcel::writeInt32);
854}
855
Casey Dahlin451ff582015-10-19 18:12:18 -0700856status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
857{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800858 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700859}
860
Casey Dahlinb9872622015-11-25 15:09:45 -0800861status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
862{
863 return writeNullableTypedVector(val, &Parcel::writeInt64);
864}
865
Casey Dahlin451ff582015-10-19 18:12:18 -0700866status_t Parcel::writeFloatVector(const std::vector<float>& val)
867{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800868 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700869}
870
Casey Dahlinb9872622015-11-25 15:09:45 -0800871status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
872{
873 return writeNullableTypedVector(val, &Parcel::writeFloat);
874}
875
Casey Dahlin451ff582015-10-19 18:12:18 -0700876status_t Parcel::writeDoubleVector(const std::vector<double>& val)
877{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800878 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700879}
880
Casey Dahlinb9872622015-11-25 15:09:45 -0800881status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
882{
883 return writeNullableTypedVector(val, &Parcel::writeDouble);
884}
885
Casey Dahlin451ff582015-10-19 18:12:18 -0700886status_t Parcel::writeBoolVector(const std::vector<bool>& val)
887{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800888 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700889}
890
Casey Dahlinb9872622015-11-25 15:09:45 -0800891status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
892{
893 return writeNullableTypedVector(val, &Parcel::writeBool);
894}
895
Casey Dahlin451ff582015-10-19 18:12:18 -0700896status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
897{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800898 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700899}
900
Casey Dahlinb9872622015-11-25 15:09:45 -0800901status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
902{
903 return writeNullableTypedVector(val, &Parcel::writeChar);
904}
905
Casey Dahlin451ff582015-10-19 18:12:18 -0700906status_t Parcel::writeString16Vector(const std::vector<String16>& val)
907{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800908 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700909}
910
Casey Dahlinb9872622015-11-25 15:09:45 -0800911status_t Parcel::writeString16Vector(
912 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
913{
914 return writeNullableTypedVector(val, &Parcel::writeString16);
915}
916
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800917status_t Parcel::writeUtf8VectorAsUtf16Vector(
918 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
919 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
920}
921
922status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
923 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
924}
925
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700926status_t Parcel::writeInt32(int32_t val)
927{
Andreas Huber84a6d042009-08-17 13:33:27 -0700928 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800930
931status_t Parcel::writeUint32(uint32_t val)
932{
933 return writeAligned(val);
934}
935
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700936status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700937 if (len > INT32_MAX) {
938 // don't accept size_t values which may have come from an
939 // inadvertent conversion from a negative int.
940 return BAD_VALUE;
941 }
942
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700943 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700944 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700945 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700946 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700947 if (ret == NO_ERROR) {
948 ret = write(val, len * sizeof(*val));
949 }
950 return ret;
951}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700952status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700953 if (len > INT32_MAX) {
954 // don't accept size_t values which may have come from an
955 // inadvertent conversion from a negative int.
956 return BAD_VALUE;
957 }
958
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700959 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700960 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700961 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700962 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700963 if (ret == NO_ERROR) {
964 ret = write(val, len * sizeof(*val));
965 }
966 return ret;
967}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968
Casey Dahlind6848f52015-10-15 15:44:59 -0700969status_t Parcel::writeBool(bool val)
970{
971 return writeInt32(int32_t(val));
972}
973
974status_t Parcel::writeChar(char16_t val)
975{
976 return writeInt32(int32_t(val));
977}
978
979status_t Parcel::writeByte(int8_t val)
980{
981 return writeInt32(int32_t(val));
982}
983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984status_t Parcel::writeInt64(int64_t val)
985{
Andreas Huber84a6d042009-08-17 13:33:27 -0700986 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987}
988
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700989status_t Parcel::writeUint64(uint64_t val)
990{
991 return writeAligned(val);
992}
993
Serban Constantinescuf683e012013-11-05 16:53:55 +0000994status_t Parcel::writePointer(uintptr_t val)
995{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800996 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000997}
998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999status_t Parcel::writeFloat(float val)
1000{
Andreas Huber84a6d042009-08-17 13:33:27 -07001001 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002}
1003
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001004#if defined(__mips__) && defined(__mips_hard_float)
1005
1006status_t Parcel::writeDouble(double val)
1007{
1008 union {
1009 double d;
1010 unsigned long long ll;
1011 } u;
1012 u.d = val;
1013 return writeAligned(u.ll);
1014}
1015
1016#else
1017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018status_t Parcel::writeDouble(double val)
1019{
Andreas Huber84a6d042009-08-17 13:33:27 -07001020 return writeAligned(val);
1021}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001023#endif
1024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025status_t Parcel::writeCString(const char* str)
1026{
1027 return write(str, strlen(str)+1);
1028}
1029
1030status_t Parcel::writeString8(const String8& str)
1031{
1032 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001033 // only write string if its length is more than zero characters,
1034 // as readString8 will only read if the length field is non-zero.
1035 // this is slightly different from how writeString16 works.
1036 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037 err = write(str.string(), str.bytes()+1);
1038 }
1039 return err;
1040}
1041
Casey Dahlinb9872622015-11-25 15:09:45 -08001042status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1043{
1044 if (!str) {
1045 return writeInt32(-1);
1046 }
1047
1048 return writeString16(*str);
1049}
1050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051status_t Parcel::writeString16(const String16& str)
1052{
1053 return writeString16(str.string(), str.size());
1054}
1055
1056status_t Parcel::writeString16(const char16_t* str, size_t len)
1057{
1058 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060 status_t err = writeInt32(len);
1061 if (err == NO_ERROR) {
1062 len *= sizeof(char16_t);
1063 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1064 if (data) {
1065 memcpy(data, str, len);
1066 *reinterpret_cast<char16_t*>(data+len) = 0;
1067 return NO_ERROR;
1068 }
1069 err = mError;
1070 }
1071 return err;
1072}
1073
1074status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1075{
1076 return flatten_binder(ProcessState::self(), val, this);
1077}
1078
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001079status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1080{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001081 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001082}
1083
Casey Dahlinb9872622015-11-25 15:09:45 -08001084status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1085{
1086 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1087}
1088
1089status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001090 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001091}
1092
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001093status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001094 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001095}
1096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1098{
1099 return flatten_binder(ProcessState::self(), val, this);
1100}
1101
Casey Dahlinb9872622015-11-25 15:09:45 -08001102status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1103 if (!parcelable) {
1104 return writeInt32(0);
1105 }
1106
1107 return writeParcelable(*parcelable);
1108}
1109
Christopher Wiley97f048d2015-11-19 06:49:05 -08001110status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1111 status_t status = writeInt32(1); // parcelable is not null.
1112 if (status != OK) {
1113 return status;
1114 }
1115 return parcelable.writeToParcel(this);
1116}
1117
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001118status_t Parcel::writeValue(const binder::Value& value) {
1119 return value.writeToParcel(this);
1120}
1121
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001122status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001123{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001124 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001125 return BAD_TYPE;
1126
1127 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001128 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001129 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001131 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001132 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001134 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1135 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136
1137 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001138 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 return err;
1140 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001141 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001142 return err;
1143}
1144
Jeff Brown93ff1f92011-11-04 19:01:44 -07001145status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146{
1147 flat_binder_object obj;
1148 obj.type = BINDER_TYPE_FD;
1149 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001150 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001151 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001152 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153 return writeObject(obj, true);
1154}
1155
1156status_t Parcel::writeDupFileDescriptor(int fd)
1157{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001158 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001159 if (dupFd < 0) {
1160 return -errno;
1161 }
1162 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001163 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001164 close(dupFd);
1165 }
1166 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167}
1168
Dianne Hackborn1941a402016-08-29 12:30:43 -07001169status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1170{
1171 writeInt32(0);
1172 return writeFileDescriptor(fd, takeOwnership);
1173}
1174
Christopher Wiley2cf19952016-04-11 11:09:37 -07001175status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001176 return writeDupFileDescriptor(fd.get());
1177}
1178
Christopher Wiley2cf19952016-04-11 11:09:37 -07001179status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001180 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1181}
1182
Christopher Wiley2cf19952016-04-11 11:09:37 -07001183status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001184 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1185}
1186
Jeff Brown13b16042014-11-11 16:44:25 -08001187status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001188{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001189 if (len > INT32_MAX) {
1190 // don't accept size_t values which may have come from an
1191 // inadvertent conversion from a negative int.
1192 return BAD_VALUE;
1193 }
1194
Jeff Brown13b16042014-11-11 16:44:25 -08001195 status_t status;
1196 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001197 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001198 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001199 if (status) return status;
1200
1201 void* ptr = writeInplace(len);
1202 if (!ptr) return NO_MEMORY;
1203
Jeff Brown13b16042014-11-11 16:44:25 -08001204 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001205 return NO_ERROR;
1206 }
1207
Steve Block6807e592011-10-20 11:56:00 +01001208 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001209 int fd = ashmem_create_region("Parcel Blob", len);
1210 if (fd < 0) return NO_MEMORY;
1211
1212 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1213 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001214 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215 } else {
1216 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1217 if (ptr == MAP_FAILED) {
1218 status = -errno;
1219 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001220 if (!mutableCopy) {
1221 result = ashmem_set_prot_region(fd, PROT_READ);
1222 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001224 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001225 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001226 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001227 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001228 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001230 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001231 return NO_ERROR;
1232 }
1233 }
1234 }
1235 }
1236 ::munmap(ptr, len);
1237 }
1238 ::close(fd);
1239 return status;
1240}
1241
Jeff Brown13b16042014-11-11 16:44:25 -08001242status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1243{
1244 // Must match up with what's done in writeBlob.
1245 if (!mAllowFds) return FDS_NOT_ALLOWED;
1246 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1247 if (status) return status;
1248 return writeDupFileDescriptor(fd);
1249}
1250
Mathias Agopiane1424282013-07-29 21:24:40 -07001251status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001252{
1253 status_t err;
1254
1255 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001256 const size_t len = val.getFlattenedSize();
1257 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001258
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001259 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001260 // don't accept size_t values which may have come from an
1261 // inadvertent conversion from a negative int.
1262 return BAD_VALUE;
1263 }
1264
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001265 err = this->writeInt32(len);
1266 if (err) return err;
1267
1268 err = this->writeInt32(fd_count);
1269 if (err) return err;
1270
1271 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001272 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001273 if (buf == NULL)
1274 return BAD_VALUE;
1275
1276 int* fds = NULL;
1277 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001278 fds = new (std::nothrow) int[fd_count];
1279 if (fds == nullptr) {
1280 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1281 return BAD_VALUE;
1282 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001283 }
1284
1285 err = val.flatten(buf, len, fds, fd_count);
1286 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1287 err = this->writeDupFileDescriptor( fds[i] );
1288 }
1289
1290 if (fd_count) {
1291 delete [] fds;
1292 }
1293
1294 return err;
1295}
1296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1298{
1299 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1300 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1301 if (enoughData && enoughObjects) {
1302restart_write:
1303 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001304
Christopher Tate98e67d32015-06-03 18:44:15 -07001305 // remember if it's a file descriptor
1306 if (val.type == BINDER_TYPE_FD) {
1307 if (!mAllowFds) {
1308 // fail before modifying our object index
1309 return FDS_NOT_ALLOWED;
1310 }
1311 mHasFds = mFdsKnown = true;
1312 }
1313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001315 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001317 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318 mObjectsSize++;
1319 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321 return finishWrite(sizeof(flat_binder_object));
1322 }
1323
1324 if (!enoughData) {
1325 const status_t err = growData(sizeof(val));
1326 if (err != NO_ERROR) return err;
1327 }
1328 if (!enoughObjects) {
1329 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001330 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001331 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 if (objects == NULL) return NO_MEMORY;
1333 mObjects = objects;
1334 mObjectsCapacity = newSize;
1335 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001336
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 goto restart_write;
1338}
1339
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001340status_t Parcel::writeNoException()
1341{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001342 binder::Status status;
1343 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001344}
1345
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001346status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1347{
1348 using ::std::map;
1349 using ::android::binder::Value;
1350 using ::android::binder::Map;
1351
1352 Map::const_iterator iter;
1353 status_t ret;
1354
1355 ret = writeInt32(map_in.size());
1356
1357 if (ret != NO_ERROR) {
1358 return ret;
1359 }
1360
1361 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1362 ret = writeValue(Value(iter->first));
1363 if (ret != NO_ERROR) {
1364 return ret;
1365 }
1366
1367 ret = writeValue(iter->second);
1368 if (ret != NO_ERROR) {
1369 return ret;
1370 }
1371 }
1372
1373 return ret;
1374}
1375
1376status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1377{
1378 if (map == NULL) {
1379 return writeInt32(-1);
1380 }
1381
1382 return writeMap(*map.get());
1383}
1384
1385status_t Parcel::readMap(::android::binder::Map* map_out)const
1386{
1387 using ::std::map;
1388 using ::android::String16;
1389 using ::android::String8;
1390 using ::android::binder::Value;
1391 using ::android::binder::Map;
1392
1393 status_t ret = NO_ERROR;
1394 int32_t count;
1395
1396 ret = readInt32(&count);
1397 if (ret != NO_ERROR) {
1398 return ret;
1399 }
1400
1401 if (count < 0) {
1402 ALOGE("readMap: Unexpected count: %d", count);
1403 return (count == -1)
1404 ? UNEXPECTED_NULL
1405 : BAD_VALUE;
1406 }
1407
1408 map_out->clear();
1409
1410 while (count--) {
1411 Map::key_type key;
1412 Value value;
1413
1414 ret = readValue(&value);
1415 if (ret != NO_ERROR) {
1416 return ret;
1417 }
1418
1419 if (!value.getString(&key)) {
1420 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1421 return BAD_VALUE;
1422 }
1423
1424 ret = readValue(&value);
1425 if (ret != NO_ERROR) {
1426 return ret;
1427 }
1428
1429 (*map_out)[key] = value;
1430 }
1431
1432 return ret;
1433}
1434
1435status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1436{
1437 const size_t start = dataPosition();
1438 int32_t count;
1439 status_t status = readInt32(&count);
1440 map->reset();
1441
1442 if (status != OK || count == -1) {
1443 return status;
1444 }
1445
1446 setDataPosition(start);
1447 map->reset(new binder::Map());
1448
1449 status = readMap(map->get());
1450
1451 if (status != OK) {
1452 map->reset();
1453 }
1454
1455 return status;
1456}
1457
1458
1459
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001460void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461{
1462 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1463}
1464
1465status_t Parcel::read(void* outData, size_t len) const
1466{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001467 if (len > INT32_MAX) {
1468 // don't accept size_t values which may have come from an
1469 // inadvertent conversion from a negative int.
1470 return BAD_VALUE;
1471 }
1472
1473 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1474 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001475 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001476 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001477 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478 return NO_ERROR;
1479 }
1480 return NOT_ENOUGH_DATA;
1481}
1482
1483const void* Parcel::readInplace(size_t len) const
1484{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001485 if (len > INT32_MAX) {
1486 // don't accept size_t values which may have come from an
1487 // inadvertent conversion from a negative int.
1488 return NULL;
1489 }
1490
1491 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1492 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001493 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001494 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001495 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 return data;
1497 }
1498 return NULL;
1499}
1500
Andreas Huber84a6d042009-08-17 13:33:27 -07001501template<class T>
1502status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001503 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001504
1505 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001506 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001507 mDataPos += sizeof(T);
1508 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001509 return NO_ERROR;
1510 } else {
1511 return NOT_ENOUGH_DATA;
1512 }
1513}
1514
Andreas Huber84a6d042009-08-17 13:33:27 -07001515template<class T>
1516T Parcel::readAligned() const {
1517 T result;
1518 if (readAligned(&result) != NO_ERROR) {
1519 result = 0;
1520 }
1521
1522 return result;
1523}
1524
1525template<class T>
1526status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001527 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001528
1529 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1530restart_write:
1531 *reinterpret_cast<T*>(mData+mDataPos) = val;
1532 return finishWrite(sizeof(val));
1533 }
1534
1535 status_t err = growData(sizeof(val));
1536 if (err == NO_ERROR) goto restart_write;
1537 return err;
1538}
1539
Casey Dahlin185d3442016-02-09 11:08:35 -08001540namespace {
1541
1542template<typename T>
1543status_t readByteVectorInternal(const Parcel* parcel,
1544 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001545 val->clear();
1546
1547 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001548 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001549
1550 if (status != OK) {
1551 return status;
1552 }
1553
Christopher Wiley4db672d2015-11-10 09:44:30 -08001554 if (size < 0) {
1555 status = UNEXPECTED_NULL;
1556 return status;
1557 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001558 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001559 status = BAD_VALUE;
1560 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001561 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001562
Paul Lietar433e87b2016-09-16 10:39:32 -07001563 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001564 if (!data) {
1565 status = BAD_VALUE;
1566 return status;
1567 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001568 val->reserve(size);
1569 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001570
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001571 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001572}
1573
Casey Dahlin185d3442016-02-09 11:08:35 -08001574template<typename T>
1575status_t readByteVectorInternalPtr(
1576 const Parcel* parcel,
1577 std::unique_ptr<std::vector<T>>* val) {
1578 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001579 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001580 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001581 val->reset();
1582
1583 if (status != OK || size < 0) {
1584 return status;
1585 }
1586
Casey Dahlin185d3442016-02-09 11:08:35 -08001587 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001588 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001589
Casey Dahlin185d3442016-02-09 11:08:35 -08001590 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001591
1592 if (status != OK) {
1593 val->reset();
1594 }
1595
1596 return status;
1597}
1598
Casey Dahlin185d3442016-02-09 11:08:35 -08001599} // namespace
1600
1601status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1602 return readByteVectorInternal(this, val);
1603}
1604
1605status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1606 return readByteVectorInternal(this, val);
1607}
1608
1609status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1610 return readByteVectorInternalPtr(this, val);
1611}
1612
1613status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1614 return readByteVectorInternalPtr(this, val);
1615}
1616
Casey Dahlinb9872622015-11-25 15:09:45 -08001617status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1618 return readNullableTypedVector(val, &Parcel::readInt32);
1619}
1620
Casey Dahlin451ff582015-10-19 18:12:18 -07001621status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001622 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001623}
1624
Casey Dahlinb9872622015-11-25 15:09:45 -08001625status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1626 return readNullableTypedVector(val, &Parcel::readInt64);
1627}
1628
Casey Dahlin451ff582015-10-19 18:12:18 -07001629status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001630 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001631}
1632
Casey Dahlinb9872622015-11-25 15:09:45 -08001633status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1634 return readNullableTypedVector(val, &Parcel::readFloat);
1635}
1636
Casey Dahlin451ff582015-10-19 18:12:18 -07001637status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001638 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001639}
1640
Casey Dahlinb9872622015-11-25 15:09:45 -08001641status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1642 return readNullableTypedVector(val, &Parcel::readDouble);
1643}
1644
Casey Dahlin451ff582015-10-19 18:12:18 -07001645status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001646 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001647}
1648
Casey Dahlinb9872622015-11-25 15:09:45 -08001649status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1650 const int32_t start = dataPosition();
1651 int32_t size;
1652 status_t status = readInt32(&size);
1653 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001654
Casey Dahlinb9872622015-11-25 15:09:45 -08001655 if (status != OK || size < 0) {
1656 return status;
1657 }
1658
1659 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001660 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001661
1662 status = readBoolVector(val->get());
1663
1664 if (status != OK) {
1665 val->reset();
1666 }
1667
1668 return status;
1669}
1670
1671status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001672 int32_t size;
1673 status_t status = readInt32(&size);
1674
1675 if (status != OK) {
1676 return status;
1677 }
1678
1679 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001680 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001681 }
1682
1683 val->resize(size);
1684
1685 /* C++ bool handling means a vector of bools isn't necessarily addressable
1686 * (we might use individual bits)
1687 */
Christopher Wiley97887982015-10-27 16:33:47 -07001688 bool data;
1689 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001690 status = readBool(&data);
1691 (*val)[i] = data;
1692
1693 if (status != OK) {
1694 return status;
1695 }
1696 }
1697
1698 return OK;
1699}
1700
Casey Dahlinb9872622015-11-25 15:09:45 -08001701status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1702 return readNullableTypedVector(val, &Parcel::readChar);
1703}
1704
Casey Dahlin451ff582015-10-19 18:12:18 -07001705status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001706 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001707}
1708
Casey Dahlinb9872622015-11-25 15:09:45 -08001709status_t Parcel::readString16Vector(
1710 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1711 return readNullableTypedVector(val, &Parcel::readString16);
1712}
1713
Casey Dahlin451ff582015-10-19 18:12:18 -07001714status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001715 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001716}
1717
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001718status_t Parcel::readUtf8VectorFromUtf16Vector(
1719 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1720 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1721}
1722
1723status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1724 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1725}
Casey Dahlin451ff582015-10-19 18:12:18 -07001726
Andreas Huber84a6d042009-08-17 13:33:27 -07001727status_t Parcel::readInt32(int32_t *pArg) const
1728{
1729 return readAligned(pArg);
1730}
1731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732int32_t Parcel::readInt32() const
1733{
Andreas Huber84a6d042009-08-17 13:33:27 -07001734 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735}
1736
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001737status_t Parcel::readUint32(uint32_t *pArg) const
1738{
1739 return readAligned(pArg);
1740}
1741
1742uint32_t Parcel::readUint32() const
1743{
1744 return readAligned<uint32_t>();
1745}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001746
1747status_t Parcel::readInt64(int64_t *pArg) const
1748{
Andreas Huber84a6d042009-08-17 13:33:27 -07001749 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001750}
1751
1752
1753int64_t Parcel::readInt64() const
1754{
Andreas Huber84a6d042009-08-17 13:33:27 -07001755 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756}
1757
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001758status_t Parcel::readUint64(uint64_t *pArg) const
1759{
1760 return readAligned(pArg);
1761}
1762
1763uint64_t Parcel::readUint64() const
1764{
1765 return readAligned<uint64_t>();
1766}
1767
Serban Constantinescuf683e012013-11-05 16:53:55 +00001768status_t Parcel::readPointer(uintptr_t *pArg) const
1769{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001770 status_t ret;
1771 binder_uintptr_t ptr;
1772 ret = readAligned(&ptr);
1773 if (!ret)
1774 *pArg = ptr;
1775 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001776}
1777
1778uintptr_t Parcel::readPointer() const
1779{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001780 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001781}
1782
1783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784status_t Parcel::readFloat(float *pArg) const
1785{
Andreas Huber84a6d042009-08-17 13:33:27 -07001786 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001787}
1788
1789
1790float Parcel::readFloat() const
1791{
Andreas Huber84a6d042009-08-17 13:33:27 -07001792 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793}
1794
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001795#if defined(__mips__) && defined(__mips_hard_float)
1796
1797status_t Parcel::readDouble(double *pArg) const
1798{
1799 union {
1800 double d;
1801 unsigned long long ll;
1802 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001803 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001804 status_t status;
1805 status = readAligned(&u.ll);
1806 *pArg = u.d;
1807 return status;
1808}
1809
1810double Parcel::readDouble() const
1811{
1812 union {
1813 double d;
1814 unsigned long long ll;
1815 } u;
1816 u.ll = readAligned<unsigned long long>();
1817 return u.d;
1818}
1819
1820#else
1821
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822status_t Parcel::readDouble(double *pArg) const
1823{
Andreas Huber84a6d042009-08-17 13:33:27 -07001824 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001825}
1826
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827double Parcel::readDouble() const
1828{
Andreas Huber84a6d042009-08-17 13:33:27 -07001829 return readAligned<double>();
1830}
1831
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001832#endif
1833
Andreas Huber84a6d042009-08-17 13:33:27 -07001834status_t Parcel::readIntPtr(intptr_t *pArg) const
1835{
1836 return readAligned(pArg);
1837}
1838
1839
1840intptr_t Parcel::readIntPtr() const
1841{
1842 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843}
1844
Casey Dahlind6848f52015-10-15 15:44:59 -07001845status_t Parcel::readBool(bool *pArg) const
1846{
1847 int32_t tmp;
1848 status_t ret = readInt32(&tmp);
1849 *pArg = (tmp != 0);
1850 return ret;
1851}
1852
1853bool Parcel::readBool() const
1854{
1855 return readInt32() != 0;
1856}
1857
1858status_t Parcel::readChar(char16_t *pArg) const
1859{
1860 int32_t tmp;
1861 status_t ret = readInt32(&tmp);
1862 *pArg = char16_t(tmp);
1863 return ret;
1864}
1865
1866char16_t Parcel::readChar() const
1867{
1868 return char16_t(readInt32());
1869}
1870
1871status_t Parcel::readByte(int8_t *pArg) const
1872{
1873 int32_t tmp;
1874 status_t ret = readInt32(&tmp);
1875 *pArg = int8_t(tmp);
1876 return ret;
1877}
1878
1879int8_t Parcel::readByte() const
1880{
1881 return int8_t(readInt32());
1882}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001883
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001884status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1885 size_t utf16Size = 0;
1886 const char16_t* src = readString16Inplace(&utf16Size);
1887 if (!src) {
1888 return UNEXPECTED_NULL;
1889 }
1890
1891 // Save ourselves the trouble, we're done.
1892 if (utf16Size == 0u) {
1893 str->clear();
1894 return NO_ERROR;
1895 }
1896
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001897 // Allow for closing '\0'
1898 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1899 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001900 return BAD_VALUE;
1901 }
1902 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001903 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001904 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001905 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1906 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001907 return NO_ERROR;
1908}
1909
1910status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1911 const int32_t start = dataPosition();
1912 int32_t size;
1913 status_t status = readInt32(&size);
1914 str->reset();
1915
1916 if (status != OK || size < 0) {
1917 return status;
1918 }
1919
1920 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001921 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001922 return readUtf8FromUtf16(str->get());
1923}
1924
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925const char* Parcel::readCString() const
1926{
1927 const size_t avail = mDataSize-mDataPos;
1928 if (avail > 0) {
1929 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1930 // is the string's trailing NUL within the parcel's valid bounds?
1931 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1932 if (eos) {
1933 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001934 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001935 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001936 return str;
1937 }
1938 }
1939 return NULL;
1940}
1941
1942String8 Parcel::readString8() const
1943{
Roshan Pius87b64d22016-07-18 12:51:02 -07001944 String8 retString;
1945 status_t status = readString8(&retString);
1946 if (status != OK) {
1947 // We don't care about errors here, so just return an empty string.
1948 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001950 return retString;
1951}
1952
1953status_t Parcel::readString8(String8* pArg) const
1954{
1955 int32_t size;
1956 status_t status = readInt32(&size);
1957 if (status != OK) {
1958 return status;
1959 }
1960 // watch for potential int overflow from size+1
1961 if (size < 0 || size >= INT32_MAX) {
1962 return BAD_VALUE;
1963 }
1964 // |writeString8| writes nothing for empty string.
1965 if (size == 0) {
1966 *pArg = String8();
1967 return OK;
1968 }
1969 const char* str = (const char*)readInplace(size + 1);
1970 if (str == NULL) {
1971 return BAD_VALUE;
1972 }
1973 pArg->setTo(str, size);
1974 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975}
1976
1977String16 Parcel::readString16() const
1978{
1979 size_t len;
1980 const char16_t* str = readString16Inplace(&len);
1981 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001982 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 return String16();
1984}
1985
Casey Dahlinb9872622015-11-25 15:09:45 -08001986status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1987{
1988 const int32_t start = dataPosition();
1989 int32_t size;
1990 status_t status = readInt32(&size);
1991 pArg->reset();
1992
1993 if (status != OK || size < 0) {
1994 return status;
1995 }
1996
1997 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001998 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08001999
2000 status = readString16(pArg->get());
2001
2002 if (status != OK) {
2003 pArg->reset();
2004 }
2005
2006 return status;
2007}
2008
Casey Dahlin451ff582015-10-19 18:12:18 -07002009status_t Parcel::readString16(String16* pArg) const
2010{
2011 size_t len;
2012 const char16_t* str = readString16Inplace(&len);
2013 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002014 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002015 return 0;
2016 } else {
2017 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002018 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002019 }
2020}
2021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2023{
2024 int32_t size = readInt32();
2025 // watch for potential int overflow from size+1
2026 if (size >= 0 && size < INT32_MAX) {
2027 *outLen = size;
2028 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
2029 if (str != NULL) {
2030 return str;
2031 }
2032 }
2033 *outLen = 0;
2034 return NULL;
2035}
2036
Casey Dahlinf0c13772015-10-27 18:33:56 -07002037status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2038{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002039 status_t status = readNullableStrongBinder(val);
2040 if (status == OK && !val->get()) {
2041 status = UNEXPECTED_NULL;
2042 }
2043 return status;
2044}
2045
2046status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2047{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002048 return unflatten_binder(ProcessState::self(), *this, val);
2049}
2050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051sp<IBinder> Parcel::readStrongBinder() const
2052{
2053 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002054 // Note that a lot of code in Android reads binders by hand with this
2055 // method, and that code has historically been ok with getting nullptr
2056 // back (while ignoring error codes).
2057 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 return val;
2059}
2060
2061wp<IBinder> Parcel::readWeakBinder() const
2062{
2063 wp<IBinder> val;
2064 unflatten_binder(ProcessState::self(), *this, &val);
2065 return val;
2066}
2067
Christopher Wiley97f048d2015-11-19 06:49:05 -08002068status_t Parcel::readParcelable(Parcelable* parcelable) const {
2069 int32_t have_parcelable = 0;
2070 status_t status = readInt32(&have_parcelable);
2071 if (status != OK) {
2072 return status;
2073 }
2074 if (!have_parcelable) {
2075 return UNEXPECTED_NULL;
2076 }
2077 return parcelable->readFromParcel(this);
2078}
2079
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002080status_t Parcel::readValue(binder::Value* value) const {
2081 return value->readFromParcel(this);
2082}
2083
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002084int32_t Parcel::readExceptionCode() const
2085{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002086 binder::Status status;
2087 status.readFromParcel(*this);
2088 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002089}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002090
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002091native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002092{
2093 int numFds, numInts;
2094 status_t err;
2095 err = readInt32(&numFds);
2096 if (err != NO_ERROR) return 0;
2097 err = readInt32(&numInts);
2098 if (err != NO_ERROR) return 0;
2099
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002100 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002101 if (!h) {
2102 return 0;
2103 }
2104
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002105 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002106 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002107 if (h->data[i] < 0) {
2108 for (int j = 0; j < i; j++) {
2109 close(h->data[j]);
2110 }
2111 native_handle_delete(h);
2112 return 0;
2113 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002114 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002115 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002116 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002117 native_handle_close(h);
2118 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002119 h = 0;
2120 }
2121 return h;
2122}
2123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124int Parcel::readFileDescriptor() const
2125{
2126 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002127
2128 if (flat && flat->type == BINDER_TYPE_FD) {
2129 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002131
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132 return BAD_TYPE;
2133}
2134
Dianne Hackborn1941a402016-08-29 12:30:43 -07002135int Parcel::readParcelFileDescriptor() const
2136{
2137 int32_t hasComm = readInt32();
2138 int fd = readFileDescriptor();
2139 if (hasComm != 0) {
2140 // skip
2141 readFileDescriptor();
2142 }
2143 return fd;
2144}
2145
Christopher Wiley2cf19952016-04-11 11:09:37 -07002146status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002147{
2148 int got = readFileDescriptor();
2149
2150 if (got == BAD_TYPE) {
2151 return BAD_TYPE;
2152 }
2153
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002154 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002155
2156 if (val->get() < 0) {
2157 return BAD_VALUE;
2158 }
2159
2160 return OK;
2161}
2162
2163
Christopher Wiley2cf19952016-04-11 11:09:37 -07002164status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002165 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2166}
2167
Christopher Wiley2cf19952016-04-11 11:09:37 -07002168status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002169 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2170}
2171
Jeff Brown5707dbf2011-09-23 21:17:56 -07002172status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2173{
Jeff Brown13b16042014-11-11 16:44:25 -08002174 int32_t blobType;
2175 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002176 if (status) return status;
2177
Jeff Brown13b16042014-11-11 16:44:25 -08002178 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002179 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002180 const void* ptr = readInplace(len);
2181 if (!ptr) return BAD_VALUE;
2182
Jeff Brown13b16042014-11-11 16:44:25 -08002183 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002184 return NO_ERROR;
2185 }
2186
Steve Block6807e592011-10-20 11:56:00 +01002187 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002188 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002189 int fd = readFileDescriptor();
2190 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2191
Jeff Brown13b16042014-11-11 16:44:25 -08002192 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2193 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002194 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002195
Jeff Brown13b16042014-11-11 16:44:25 -08002196 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002197 return NO_ERROR;
2198}
2199
Mathias Agopiane1424282013-07-29 21:24:40 -07002200status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002201{
2202 // size
2203 const size_t len = this->readInt32();
2204 const size_t fd_count = this->readInt32();
2205
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002206 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002207 // don't accept size_t values which may have come from an
2208 // inadvertent conversion from a negative int.
2209 return BAD_VALUE;
2210 }
2211
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002212 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002213 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002214 if (buf == NULL)
2215 return BAD_VALUE;
2216
2217 int* fds = NULL;
2218 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002219 fds = new (std::nothrow) int[fd_count];
2220 if (fds == nullptr) {
2221 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2222 return BAD_VALUE;
2223 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002224 }
2225
2226 status_t err = NO_ERROR;
2227 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002228 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002229 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002230 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002231 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 -07002232 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2233 // Close all the file descriptors that were dup-ed.
2234 for (size_t j=0; j<i ;j++) {
2235 close(fds[j]);
2236 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002237 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002238 }
2239
2240 if (err == NO_ERROR) {
2241 err = val.unflatten(buf, len, fds, fd_count);
2242 }
2243
2244 if (fd_count) {
2245 delete [] fds;
2246 }
2247
2248 return err;
2249}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2251{
2252 const size_t DPOS = mDataPos;
2253 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2254 const flat_binder_object* obj
2255 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2256 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002257 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002258 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 // the object list, so we don't want to check for it when
2260 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002261 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 return obj;
2263 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002266 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 const size_t N = mObjectsSize;
2268 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 // Start at the current hint position, looking for an object at
2275 // the current data position.
2276 if (opos < N) {
2277 while (opos < (N-1) && OBJS[opos] < DPOS) {
2278 opos++;
2279 }
2280 } else {
2281 opos = N-1;
2282 }
2283 if (OBJS[opos] == DPOS) {
2284 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 this, DPOS, opos);
2287 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002288 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002289 return obj;
2290 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002292 // Look backwards for it...
2293 while (opos > 0 && OBJS[opos] > DPOS) {
2294 opos--;
2295 }
2296 if (OBJS[opos] == DPOS) {
2297 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002298 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 this, DPOS, opos);
2300 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002301 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 return obj;
2303 }
2304 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002305 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 -07002306 this, DPOS);
2307 }
2308 return NULL;
2309}
2310
2311void Parcel::closeFileDescriptors()
2312{
2313 size_t i = mObjectsSize;
2314 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002315 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 }
2317 while (i > 0) {
2318 i--;
2319 const flat_binder_object* flat
2320 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2321 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002322 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 close(flat->handle);
2324 }
2325 }
2326}
2327
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002328uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002330 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331}
2332
2333size_t Parcel::ipcDataSize() const
2334{
2335 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2336}
2337
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002338uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002339{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002340 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341}
2342
2343size_t Parcel::ipcObjectsCount() const
2344{
2345 return mObjectsSize;
2346}
2347
2348void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002349 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002351 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 freeDataNoInit();
2353 mError = NO_ERROR;
2354 mData = const_cast<uint8_t*>(data);
2355 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002358 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002359 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 mObjectsSize = mObjectsCapacity = objectsCount;
2361 mNextObjectHint = 0;
2362 mOwner = relFunc;
2363 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002364 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002365 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002366 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002367 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002368 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002369 mObjectsSize = 0;
2370 break;
2371 }
2372 minOffset = offset + sizeof(flat_binder_object);
2373 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 scanForFds();
2375}
2376
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002377void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378{
2379 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 if (errorCheck() != NO_ERROR) {
2382 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002383 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 } else if (dataSize() > 0) {
2385 const uint8_t* DATA = data();
2386 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002387 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 const size_t N = objectsCount();
2389 for (size_t i=0; i<N; i++) {
2390 const flat_binder_object* flat
2391 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2392 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2393 << TypeCode(flat->type & 0x7f7f7f00)
2394 << " = " << flat->binder;
2395 }
2396 } else {
2397 to << "NULL";
2398 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 to << ")";
2401}
2402
2403void Parcel::releaseObjects()
2404{
2405 const sp<ProcessState> proc(ProcessState::self());
2406 size_t i = mObjectsSize;
2407 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002408 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 while (i > 0) {
2410 i--;
2411 const flat_binder_object* flat
2412 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002413 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 }
2415}
2416
2417void Parcel::acquireObjects()
2418{
2419 const sp<ProcessState> proc(ProcessState::self());
2420 size_t i = mObjectsSize;
2421 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002422 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002423 while (i > 0) {
2424 i--;
2425 const flat_binder_object* flat
2426 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002427 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428 }
2429}
2430
2431void Parcel::freeData()
2432{
2433 freeDataNoInit();
2434 initState();
2435}
2436
2437void Parcel::freeDataNoInit()
2438{
2439 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002440 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2443 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002444 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002446 if (mData) {
2447 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002448 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002449 if (mDataCapacity <= gParcelGlobalAllocSize) {
2450 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2451 } else {
2452 gParcelGlobalAllocSize = 0;
2453 }
2454 if (gParcelGlobalAllocCount > 0) {
2455 gParcelGlobalAllocCount--;
2456 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002457 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002458 free(mData);
2459 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 if (mObjects) free(mObjects);
2461 }
2462}
2463
2464status_t Parcel::growData(size_t len)
2465{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002466 if (len > INT32_MAX) {
2467 // don't accept size_t values which may have come from an
2468 // inadvertent conversion from a negative int.
2469 return BAD_VALUE;
2470 }
2471
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 size_t newSize = ((mDataSize+len)*3)/2;
2473 return (newSize <= mDataSize)
2474 ? (status_t) NO_MEMORY
2475 : continueWrite(newSize);
2476}
2477
2478status_t Parcel::restartWrite(size_t desired)
2479{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002480 if (desired > INT32_MAX) {
2481 // don't accept size_t values which may have come from an
2482 // inadvertent conversion from a negative int.
2483 return BAD_VALUE;
2484 }
2485
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 if (mOwner) {
2487 freeData();
2488 return continueWrite(desired);
2489 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 uint8_t* data = (uint8_t*)realloc(mData, desired);
2492 if (!data && desired > mDataCapacity) {
2493 mError = NO_MEMORY;
2494 return NO_MEMORY;
2495 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002500 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002501 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002502 gParcelGlobalAllocSize += desired;
2503 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002504 if (!mData) {
2505 gParcelGlobalAllocCount++;
2506 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002507 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 mData = data;
2509 mDataCapacity = desired;
2510 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2514 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 free(mObjects);
2517 mObjects = NULL;
2518 mObjectsSize = mObjectsCapacity = 0;
2519 mNextObjectHint = 0;
2520 mHasFds = false;
2521 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002522 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 return NO_ERROR;
2525}
2526
2527status_t Parcel::continueWrite(size_t desired)
2528{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002529 if (desired > INT32_MAX) {
2530 // don't accept size_t values which may have come from an
2531 // inadvertent conversion from a negative int.
2532 return BAD_VALUE;
2533 }
2534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 // If shrinking, first adjust for any objects that appear
2536 // after the new data size.
2537 size_t objectsSize = mObjectsSize;
2538 if (desired < mDataSize) {
2539 if (desired == 0) {
2540 objectsSize = 0;
2541 } else {
2542 while (objectsSize > 0) {
2543 if (mObjects[objectsSize-1] < desired)
2544 break;
2545 objectsSize--;
2546 }
2547 }
2548 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002549
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002550 if (mOwner) {
2551 // If the size is going to zero, just release the owner's data.
2552 if (desired == 0) {
2553 freeData();
2554 return NO_ERROR;
2555 }
2556
2557 // If there is a different owner, we need to take
2558 // posession.
2559 uint8_t* data = (uint8_t*)malloc(desired);
2560 if (!data) {
2561 mError = NO_MEMORY;
2562 return NO_MEMORY;
2563 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002564 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002567 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002569 free(data);
2570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mError = NO_MEMORY;
2572 return NO_MEMORY;
2573 }
2574
2575 // Little hack to only acquire references on objects
2576 // we will be keeping.
2577 size_t oldObjectsSize = mObjectsSize;
2578 mObjectsSize = objectsSize;
2579 acquireObjects();
2580 mObjectsSize = oldObjectsSize;
2581 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583 if (mData) {
2584 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2585 }
2586 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002587 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2591 mOwner = NULL;
2592
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002593 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002594 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002595 gParcelGlobalAllocSize += desired;
2596 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002597 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 mData = data;
2600 mObjects = objects;
2601 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002602 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 mDataCapacity = desired;
2604 mObjectsSize = mObjectsCapacity = objectsSize;
2605 mNextObjectHint = 0;
2606
2607 } else if (mData) {
2608 if (objectsSize < mObjectsSize) {
2609 // Need to release refs on any objects we are dropping.
2610 const sp<ProcessState> proc(ProcessState::self());
2611 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2612 const flat_binder_object* flat
2613 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2614 if (flat->type == BINDER_TYPE_FD) {
2615 // will need to rescan because we may have lopped off the only FDs
2616 mFdsKnown = false;
2617 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002618 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002620 binder_size_t* objects =
2621 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 if (objects) {
2623 mObjects = objects;
2624 }
2625 mObjectsSize = objectsSize;
2626 mNextObjectHint = 0;
2627 }
2628
2629 // We own the data, so we can just do a realloc().
2630 if (desired > mDataCapacity) {
2631 uint8_t* data = (uint8_t*)realloc(mData, desired);
2632 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002633 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2634 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002635 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002636 gParcelGlobalAllocSize += desired;
2637 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002638 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 mData = data;
2640 mDataCapacity = desired;
2641 } else if (desired > mDataCapacity) {
2642 mError = NO_MEMORY;
2643 return NO_MEMORY;
2644 }
2645 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002646 if (mDataSize > desired) {
2647 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002648 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002649 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 if (mDataPos > desired) {
2651 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002652 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653 }
2654 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002656 } else {
2657 // This is the first data. Easy!
2658 uint8_t* data = (uint8_t*)malloc(desired);
2659 if (!data) {
2660 mError = NO_MEMORY;
2661 return NO_MEMORY;
2662 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 if(!(mDataCapacity == 0 && mObjects == NULL
2665 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002666 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002667 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002668
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002669 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002670 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002671 gParcelGlobalAllocSize += desired;
2672 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002673 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mData = data;
2676 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002677 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2678 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 mDataCapacity = desired;
2680 }
2681
2682 return NO_ERROR;
2683}
2684
2685void Parcel::initState()
2686{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002687 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 mError = NO_ERROR;
2689 mData = 0;
2690 mDataSize = 0;
2691 mDataCapacity = 0;
2692 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002693 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2694 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mObjects = NULL;
2696 mObjectsSize = 0;
2697 mObjectsCapacity = 0;
2698 mNextObjectHint = 0;
2699 mHasFds = false;
2700 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002701 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002703 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002704
2705 // racing multiple init leads only to multiple identical write
2706 if (gMaxFds == 0) {
2707 struct rlimit result;
2708 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2709 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002710 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002711 } else {
2712 ALOGW("Unable to getrlimit: %s", strerror(errno));
2713 gMaxFds = 1024;
2714 }
2715 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716}
2717
2718void Parcel::scanForFds() const
2719{
2720 bool hasFds = false;
2721 for (size_t i=0; i<mObjectsSize; i++) {
2722 const flat_binder_object* flat
2723 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2724 if (flat->type == BINDER_TYPE_FD) {
2725 hasFds = true;
2726 break;
2727 }
2728 }
2729 mHasFds = hasFds;
2730 mFdsKnown = true;
2731}
2732
Dan Sandleraa5c2342015-04-10 10:08:45 -04002733size_t Parcel::getBlobAshmemSize() const
2734{
Adrian Roos6bb31142015-10-22 16:46:12 -07002735 // This used to return the size of all blobs that were written to ashmem, now we're returning
2736 // the ashmem currently referenced by this Parcel, which should be equivalent.
2737 // TODO: Remove method once ABI can be changed.
2738 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002739}
2740
Adrian Rooscbf37262015-10-22 16:12:53 -07002741size_t Parcel::getOpenAshmemSize() const
2742{
2743 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002744}
2745
2746// --- Parcel::Blob ---
2747
2748Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002749 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002750}
2751
2752Parcel::Blob::~Blob() {
2753 release();
2754}
2755
2756void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002757 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002758 ::munmap(mData, mSize);
2759 }
2760 clear();
2761}
2762
Jeff Brown13b16042014-11-11 16:44:25 -08002763void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2764 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002765 mData = data;
2766 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002767 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002768}
2769
2770void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002771 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002772 mData = NULL;
2773 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002774 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002775}
2776
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002777}; // namespace android