blob: 9f68e7aa8cd2dc1a19d252675a951bdd041af1b0 [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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070026#include <binder/TextOutput.h>
27
Jun Jiangabf8a2c2014-04-29 14:22:10 +080028#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070029#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080034#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070035#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036
Mathias Agopian208059f2009-05-18 15:08:03 -070037#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080038#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080040#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070044#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
46#ifndef INT32_MAX
47#define INT32_MAX ((int32_t)(2147483647))
48#endif
49
50#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010051//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080052#define LOG_ALLOC(...)
53//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
55// ---------------------------------------------------------------------------
56
Nick Kralevichb6b14232015-04-02 09:36:02 -070057// This macro should never be used at runtime, as a too large value
58// of s could cause an integer overflow. Instead, you should always
59// use the wrapper function pad_size()
60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
61
62static size_t pad_size(size_t s) {
63 if (s > (SIZE_T_MAX - 3)) {
64 abort();
65 }
66 return PAD_SIZE_UNSAFE(s);
67}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080070#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070072// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
73#define EX_HAS_REPLY_HEADER -128
74
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborna4cff882014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098void acquire_object(const sp<ProcessState>& proc,
99 const flat_binder_object& obj, const void* who)
100{
101 switch (obj.type) {
102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return;
112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
114 if (b != NULL) {
115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
120 case BINDER_TYPE_WEAK_HANDLE: {
121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
122 if (b != NULL) b.get_refs()->incWeak(who);
123 return;
124 }
125 case BINDER_TYPE_FD: {
126 // intentionally blank -- nothing to do to acquire this, but we do
127 // recognize it as a legitimate object type.
128 return;
129 }
130 }
131
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800132 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133}
134
135void release_object(const sp<ProcessState>& proc,
136 const flat_binder_object& obj, const void* who)
137{
138 switch (obj.type) {
139 case BINDER_TYPE_BINDER:
140 if (obj.binder) {
141 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800142 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 }
144 return;
145 case BINDER_TYPE_WEAK_BINDER:
146 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800147 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 return;
149 case BINDER_TYPE_HANDLE: {
150 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
151 if (b != NULL) {
152 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
153 b->decStrong(who);
154 }
155 return;
156 }
157 case BINDER_TYPE_WEAK_HANDLE: {
158 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
159 if (b != NULL) b.get_refs()->decWeak(who);
160 return;
161 }
162 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800163 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 }
166 }
167
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800168 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
170
171inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800172 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173{
174 return out->writeObject(flat, false);
175}
176
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800177status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 const sp<IBinder>& binder, Parcel* out)
179{
180 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700182 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
183 if (binder != NULL) {
184 IBinder *local = binder->localBinder();
185 if (!local) {
186 BpBinder *proxy = binder->remoteBinder();
187 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000188 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189 }
190 const int32_t handle = proxy ? proxy->handle() : 0;
191 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800192 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800194 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195 } else {
196 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800197 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
198 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199 }
200 } else {
201 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800202 obj.binder = 0;
203 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 return finish_flatten_binder(binder, obj, out);
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 wp<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 sp<IBinder> real = binder.promote();
217 if (real != NULL) {
218 IBinder *local = real->localBinder();
219 if (!local) {
220 BpBinder *proxy = real->remoteBinder();
221 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
225 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
230 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
232 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
234 return finish_flatten_binder(real, obj, out);
235 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 // XXX How to deal? In order to flatten the given binder,
238 // we need to probe it for information, which requires a primary
239 // reference... but we don't have one.
240 //
241 // The OpenBinder implementation uses a dynamic_cast<> here,
242 // but we can't do that with the different reference counting
243 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000244 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800246 obj.binder = 0;
247 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 } else {
251 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800252 obj.binder = 0;
253 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 return finish_flatten_binder(NULL, obj, out);
255 }
256}
257
258inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800259 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
260 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261{
262 return NO_ERROR;
263}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265status_t unflatten_binder(const sp<ProcessState>& proc,
266 const Parcel& in, sp<IBinder>* out)
267{
268 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 if (flat) {
271 switch (flat->type) {
272 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 return finish_unflatten_binder(NULL, *flat, in);
275 case BINDER_TYPE_HANDLE:
276 *out = proc->getStrongProxyForHandle(flat->handle);
277 return finish_unflatten_binder(
278 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700279 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
281 return BAD_TYPE;
282}
283
284status_t unflatten_binder(const sp<ProcessState>& proc,
285 const Parcel& in, wp<IBinder>* out)
286{
287 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 if (flat) {
290 switch (flat->type) {
291 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800292 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 return finish_unflatten_binder(NULL, *flat, in);
294 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800295 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800297 reinterpret_cast<IBinder*>(flat->cookie),
298 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 } else {
300 *out = NULL;
301 }
302 return finish_unflatten_binder(NULL, *flat, in);
303 case BINDER_TYPE_HANDLE:
304 case BINDER_TYPE_WEAK_HANDLE:
305 *out = proc->getWeakProxyForHandle(flat->handle);
306 return finish_unflatten_binder(
307 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
308 }
309 }
310 return BAD_TYPE;
311}
312
313// ---------------------------------------------------------------------------
314
315Parcel::Parcel()
316{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800317 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 initState();
319}
320
321Parcel::~Parcel()
322{
323 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800324 LOG_ALLOC("Parcel %p: destroyed", this);
325}
326
327size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800328 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
329 size_t size = gParcelGlobalAllocSize;
330 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
331 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800332}
333
334size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800335 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
336 size_t count = gParcelGlobalAllocCount;
337 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
338 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339}
340
341const uint8_t* Parcel::data() const
342{
343 return mData;
344}
345
346size_t Parcel::dataSize() const
347{
348 return (mDataSize > mDataPos ? mDataSize : mDataPos);
349}
350
351size_t Parcel::dataAvail() const
352{
353 // TODO: decide what to do about the possibility that this can
354 // report an available-data size that exceeds a Java int's max
355 // positive value, causing havoc. Fortunately this will only
356 // happen if someone constructs a Parcel containing more than two
357 // gigabytes of data, which on typical phone hardware is simply
358 // not possible.
359 return dataSize() - dataPosition();
360}
361
362size_t Parcel::dataPosition() const
363{
364 return mDataPos;
365}
366
367size_t Parcel::dataCapacity() const
368{
369 return mDataCapacity;
370}
371
372status_t Parcel::setDataSize(size_t size)
373{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700374 if (size > INT32_MAX) {
375 // don't accept size_t values which may have come from an
376 // inadvertent conversion from a negative int.
377 return BAD_VALUE;
378 }
379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 status_t err;
381 err = continueWrite(size);
382 if (err == NO_ERROR) {
383 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700384 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385 }
386 return err;
387}
388
389void Parcel::setDataPosition(size_t pos) const
390{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 if (pos > INT32_MAX) {
392 // don't accept size_t values which may have come from an
393 // inadvertent conversion from a negative int.
394 abort();
395 }
396
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397 mDataPos = pos;
398 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -0700399 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700400}
401
402status_t Parcel::setDataCapacity(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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700410 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 return NO_ERROR;
412}
413
414status_t Parcel::setData(const uint8_t* buffer, size_t len)
415{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700416 if (len > INT32_MAX) {
417 // don't accept size_t values which may have come from an
418 // inadvertent conversion from a negative int.
419 return BAD_VALUE;
420 }
421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 status_t err = restartWrite(len);
423 if (err == NO_ERROR) {
424 memcpy(const_cast<uint8_t*>(data()), buffer, len);
425 mDataSize = len;
426 mFdsKnown = false;
427 }
428 return err;
429}
430
Andreas Huber51faf462011-04-13 10:21:56 -0700431status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432{
433 const sp<ProcessState> proc(ProcessState::self());
434 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700435 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800436 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 size_t size = parcel->mObjectsSize;
438 int startPos = mDataPos;
439 int firstIndex = -1, lastIndex = -2;
440
441 if (len == 0) {
442 return NO_ERROR;
443 }
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 // range checks against the source parcel size
452 if ((offset > parcel->mDataSize)
453 || (len > parcel->mDataSize)
454 || (offset + len > parcel->mDataSize)) {
455 return BAD_VALUE;
456 }
457
458 // Count objects in range
459 for (int i = 0; i < (int) size; i++) {
460 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700461 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 if (firstIndex == -1) {
463 firstIndex = i;
464 }
465 lastIndex = i;
466 }
467 }
468 int numObjects = lastIndex - firstIndex + 1;
469
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700470 if ((mDataSize+len) > mDataCapacity) {
471 // grow data
472 err = growData(len);
473 if (err != NO_ERROR) {
474 return err;
475 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 }
477
478 // append data
479 memcpy(mData + mDataPos, data + offset, len);
480 mDataPos += len;
481 mDataSize += len;
482
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400483 err = NO_ERROR;
484
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 if (numObjects > 0) {
486 // grow objects
487 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700488 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate8b643072016-11-03 13:32:41 -0700489 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800490 binder_size_t *objects =
491 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
492 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 return NO_MEMORY;
494 }
495 mObjects = objects;
496 mObjectsCapacity = newSize;
497 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 // append and acquire objects
500 int idx = mObjectsSize;
501 for (int i = firstIndex; i <= lastIndex; i++) {
502 size_t off = objects[i] - offset + startPos;
503 mObjects[idx++] = off;
504 mObjectsSize++;
505
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700506 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 = reinterpret_cast<flat_binder_object*>(mData + off);
508 acquire_object(proc, *flat, this);
509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700511 // If this is a file descriptor, we need to dup it so the
512 // new Parcel now owns its own fd, and can declare that we
513 // officially know we have fds.
514 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800515 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400517 if (!mAllowFds) {
518 err = FDS_NOT_ALLOWED;
519 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 }
521 }
522 }
523
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400524 return err;
525}
526
Jeff Brown13b16042014-11-11 16:44:25 -0800527bool Parcel::allowFds() const
528{
529 return mAllowFds;
530}
531
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400533{
534 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700535 if (!allowFds) {
536 mAllowFds = false;
537 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400538 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539}
540
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700541void Parcel::restoreAllowFds(bool lastValue)
542{
543 mAllowFds = lastValue;
544}
545
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546bool Parcel::hasFileDescriptors() const
547{
548 if (!mFdsKnown) {
549 scanForFds();
550 }
551 return mHasFds;
552}
553
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700554// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555status_t Parcel::writeInterfaceToken(const String16& interface)
556{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700557 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
558 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700559 // currently the interface identification token is just its name as a string
560 return writeString16(interface);
561}
562
Mathias Agopian83c04462009-05-22 19:00:22 -0700563bool Parcel::checkInterface(IBinder* binder) const
564{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700565 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700566}
567
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700568bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700569 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700571 int32_t strictPolicy = readInt32();
572 if (threadState == NULL) {
573 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700574 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700575 if ((threadState->getLastTransactionBinderFlags() &
576 IBinder::FLAG_ONEWAY) != 0) {
577 // For one-way calls, the callee is running entirely
578 // disconnected from the caller, so disable StrictMode entirely.
579 // Not only does disk/network usage not impact the caller, but
580 // there's no way to commuicate back any violations anyway.
581 threadState->setStrictModePolicy(0);
582 } else {
583 threadState->setStrictModePolicy(strictPolicy);
584 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700585 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586 if (str == interface) {
587 return true;
588 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700589 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 String8(interface).string(), String8(str).string());
591 return false;
592 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700593}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800595const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596{
597 return mObjects;
598}
599
600size_t Parcel::objectsCount() const
601{
602 return mObjectsSize;
603}
604
605status_t Parcel::errorCheck() const
606{
607 return mError;
608}
609
610void Parcel::setError(status_t err)
611{
612 mError = err;
613}
614
615status_t Parcel::finishWrite(size_t len)
616{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700617 if (len > INT32_MAX) {
618 // don't accept size_t values which may have come from an
619 // inadvertent conversion from a negative int.
620 return BAD_VALUE;
621 }
622
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 //printf("Finish write of %d\n", len);
624 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 if (mDataPos > mDataSize) {
627 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700628 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629 }
630 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
631 return NO_ERROR;
632}
633
634status_t Parcel::writeUnpadded(const void* data, size_t len)
635{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700636 if (len > INT32_MAX) {
637 // don't accept size_t values which may have come from an
638 // inadvertent conversion from a negative int.
639 return BAD_VALUE;
640 }
641
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642 size_t end = mDataPos + len;
643 if (end < mDataPos) {
644 // integer overflow
645 return BAD_VALUE;
646 }
647
648 if (end <= mDataCapacity) {
649restart_write:
650 memcpy(mData+mDataPos, data, len);
651 return finishWrite(len);
652 }
653
654 status_t err = growData(len);
655 if (err == NO_ERROR) goto restart_write;
656 return err;
657}
658
659status_t Parcel::write(const void* data, size_t len)
660{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700661 if (len > INT32_MAX) {
662 // don't accept size_t values which may have come from an
663 // inadvertent conversion from a negative int.
664 return BAD_VALUE;
665 }
666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700667 void* const d = writeInplace(len);
668 if (d) {
669 memcpy(d, data, len);
670 return NO_ERROR;
671 }
672 return mError;
673}
674
675void* Parcel::writeInplace(size_t len)
676{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700677 if (len > INT32_MAX) {
678 // don't accept size_t values which may have come from an
679 // inadvertent conversion from a negative int.
680 return NULL;
681 }
682
683 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700684
685 // sanity check for integer overflow
686 if (mDataPos+padded < mDataPos) {
687 return NULL;
688 }
689
690 if ((mDataPos+padded) <= mDataCapacity) {
691restart_write:
692 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
693 uint8_t* const data = mData+mDataPos;
694
695 // Need to pad at end?
696 if (padded != len) {
697#if BYTE_ORDER == BIG_ENDIAN
698 static const uint32_t mask[4] = {
699 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
700 };
701#endif
702#if BYTE_ORDER == LITTLE_ENDIAN
703 static const uint32_t mask[4] = {
704 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
705 };
706#endif
707 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
708 // *reinterpret_cast<void**>(data+padded-4));
709 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
710 }
711
712 finishWrite(padded);
713 return data;
714 }
715
716 status_t err = growData(padded);
717 if (err == NO_ERROR) goto restart_write;
718 return NULL;
719}
720
721status_t Parcel::writeInt32(int32_t val)
722{
Andreas Huber84a6d042009-08-17 13:33:27 -0700723 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800725
726status_t Parcel::writeUint32(uint32_t val)
727{
728 return writeAligned(val);
729}
730
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700731status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700732 if (len > INT32_MAX) {
733 // don't accept size_t values which may have come from an
734 // inadvertent conversion from a negative int.
735 return BAD_VALUE;
736 }
737
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700738 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700739 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700740 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700741 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700742 if (ret == NO_ERROR) {
743 ret = write(val, len * sizeof(*val));
744 }
745 return ret;
746}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700747status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700748 if (len > INT32_MAX) {
749 // don't accept size_t values which may have come from an
750 // inadvertent conversion from a negative int.
751 return BAD_VALUE;
752 }
753
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700754 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700755 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700756 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700757 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700758 if (ret == NO_ERROR) {
759 ret = write(val, len * sizeof(*val));
760 }
761 return ret;
762}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700763
764status_t Parcel::writeInt64(int64_t val)
765{
Andreas Huber84a6d042009-08-17 13:33:27 -0700766 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700767}
768
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700769status_t Parcel::writeUint64(uint64_t val)
770{
771 return writeAligned(val);
772}
773
Serban Constantinescuf683e012013-11-05 16:53:55 +0000774status_t Parcel::writePointer(uintptr_t val)
775{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800776 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000777}
778
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779status_t Parcel::writeFloat(float val)
780{
Andreas Huber84a6d042009-08-17 13:33:27 -0700781 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700782}
783
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800784#if defined(__mips__) && defined(__mips_hard_float)
785
786status_t Parcel::writeDouble(double val)
787{
788 union {
789 double d;
790 unsigned long long ll;
791 } u;
792 u.d = val;
793 return writeAligned(u.ll);
794}
795
796#else
797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700798status_t Parcel::writeDouble(double val)
799{
Andreas Huber84a6d042009-08-17 13:33:27 -0700800 return writeAligned(val);
801}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700802
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800803#endif
804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700805status_t Parcel::writeCString(const char* str)
806{
807 return write(str, strlen(str)+1);
808}
809
810status_t Parcel::writeString8(const String8& str)
811{
812 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100813 // only write string if its length is more than zero characters,
814 // as readString8 will only read if the length field is non-zero.
815 // this is slightly different from how writeString16 works.
816 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700817 err = write(str.string(), str.bytes()+1);
818 }
819 return err;
820}
821
822status_t Parcel::writeString16(const String16& str)
823{
824 return writeString16(str.string(), str.size());
825}
826
827status_t Parcel::writeString16(const char16_t* str, size_t len)
828{
829 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700831 status_t err = writeInt32(len);
832 if (err == NO_ERROR) {
833 len *= sizeof(char16_t);
834 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
835 if (data) {
836 memcpy(data, str, len);
837 *reinterpret_cast<char16_t*>(data+len) = 0;
838 return NO_ERROR;
839 }
840 err = mError;
841 }
842 return err;
843}
844
845status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
846{
847 return flatten_binder(ProcessState::self(), val, this);
848}
849
850status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
851{
852 return flatten_binder(ProcessState::self(), val, this);
853}
854
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700855status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800856{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700857 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800858 return BAD_TYPE;
859
860 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700861 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800862 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700864 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800865 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700867 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
868 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869
870 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000871 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 return err;
873 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700874 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800875 return err;
876}
877
Jeff Brown93ff1f92011-11-04 19:01:44 -0700878status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700879{
880 flat_binder_object obj;
881 obj.type = BINDER_TYPE_FD;
882 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800883 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700884 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800885 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700886 return writeObject(obj, true);
887}
888
889status_t Parcel::writeDupFileDescriptor(int fd)
890{
Jeff Brownd341c712011-11-04 20:19:33 -0700891 int dupFd = dup(fd);
892 if (dupFd < 0) {
893 return -errno;
894 }
895 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
896 if (err) {
897 close(dupFd);
898 }
899 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700900}
901
Jeff Brown13b16042014-11-11 16:44:25 -0800902status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700903{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700904 if (len > INT32_MAX) {
905 // don't accept size_t values which may have come from an
906 // inadvertent conversion from a negative int.
907 return BAD_VALUE;
908 }
909
Jeff Brown13b16042014-11-11 16:44:25 -0800910 status_t status;
911 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100912 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800913 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700914 if (status) return status;
915
916 void* ptr = writeInplace(len);
917 if (!ptr) return NO_MEMORY;
918
Jeff Brown13b16042014-11-11 16:44:25 -0800919 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700920 return NO_ERROR;
921 }
922
Steve Block6807e592011-10-20 11:56:00 +0100923 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700924 int fd = ashmem_create_region("Parcel Blob", len);
925 if (fd < 0) return NO_MEMORY;
926
Dan Sandleraa5c2342015-04-10 10:08:45 -0400927 mBlobAshmemSize += len;
928
Jeff Brown5707dbf2011-09-23 21:17:56 -0700929 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
930 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700931 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700932 } else {
933 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
934 if (ptr == MAP_FAILED) {
935 status = -errno;
936 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800937 if (!mutableCopy) {
938 result = ashmem_set_prot_region(fd, PROT_READ);
939 }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700940 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700941 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700942 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800943 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700944 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700945 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700946 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -0800947 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700948 return NO_ERROR;
949 }
950 }
951 }
952 }
953 ::munmap(ptr, len);
954 }
955 ::close(fd);
956 return status;
957}
958
Jeff Brown13b16042014-11-11 16:44:25 -0800959status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
960{
961 // Must match up with what's done in writeBlob.
962 if (!mAllowFds) return FDS_NOT_ALLOWED;
963 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
964 if (status) return status;
965 return writeDupFileDescriptor(fd);
966}
967
Mathias Agopiane1424282013-07-29 21:24:40 -0700968status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800969{
970 status_t err;
971
972 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700973 const size_t len = val.getFlattenedSize();
974 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800975
Nick Kralevichb6b14232015-04-02 09:36:02 -0700976 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
977 // don't accept size_t values which may have come from an
978 // inadvertent conversion from a negative int.
979 return BAD_VALUE;
980 }
981
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800982 err = this->writeInt32(len);
983 if (err) return err;
984
985 err = this->writeInt32(fd_count);
986 if (err) return err;
987
988 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -0700989 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800990 if (buf == NULL)
991 return BAD_VALUE;
992
993 int* fds = NULL;
994 if (fd_count) {
995 fds = new int[fd_count];
996 }
997
998 err = val.flatten(buf, len, fds, fd_count);
999 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1000 err = this->writeDupFileDescriptor( fds[i] );
1001 }
1002
1003 if (fd_count) {
1004 delete [] fds;
1005 }
1006
1007 return err;
1008}
1009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1011{
1012 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1013 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1014 if (enoughData && enoughObjects) {
1015restart_write:
1016 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001017
Christopher Tate98e67d32015-06-03 18:44:15 -07001018 // remember if it's a file descriptor
1019 if (val.type == BINDER_TYPE_FD) {
1020 if (!mAllowFds) {
1021 // fail before modifying our object index
1022 return FDS_NOT_ALLOWED;
1023 }
1024 mHasFds = mFdsKnown = true;
1025 }
1026
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001027 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001028 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029 mObjects[mObjectsSize] = mDataPos;
1030 acquire_object(ProcessState::self(), val, this);
1031 mObjectsSize++;
1032 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034 return finishWrite(sizeof(flat_binder_object));
1035 }
1036
1037 if (!enoughData) {
1038 const status_t err = growData(sizeof(val));
1039 if (err != NO_ERROR) return err;
1040 }
1041 if (!enoughObjects) {
1042 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate8b643072016-11-03 13:32:41 -07001043 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001044 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045 if (objects == NULL) return NO_MEMORY;
1046 mObjects = objects;
1047 mObjectsCapacity = newSize;
1048 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050 goto restart_write;
1051}
1052
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001053status_t Parcel::writeNoException()
1054{
1055 return writeInt32(0);
1056}
1057
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001058void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059{
1060 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1061}
1062
akirilovf7841832018-04-03 12:56:06 -07001063status_t Parcel::validateReadData(size_t upperBound) const
1064{
1065 // Don't allow non-object reads on object data
1066 if (mObjectsSorted || mObjectsSize <= 1) {
1067data_sorted:
1068 // Expect to check only against the next object
1069 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1070 // For some reason the current read position is greater than the next object
1071 // hint. Iterate until we find the right object
1072 size_t nextObject = mNextObjectHint;
1073 do {
1074 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1075 // Requested info overlaps with an object
1076 ALOGE("Attempt to read from protected data in Parcel %p", this);
1077 return PERMISSION_DENIED;
1078 }
1079 nextObject++;
1080 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1081 mNextObjectHint = nextObject;
1082 }
1083 return NO_ERROR;
1084 }
1085 // Quickly determine if mObjects is sorted.
1086 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1087 binder_size_t* prevObj = currObj;
1088 while (currObj > mObjects) {
1089 prevObj--;
1090 if(*prevObj > *currObj) {
1091 goto data_unsorted;
1092 }
1093 currObj--;
1094 }
1095 mObjectsSorted = true;
1096 goto data_sorted;
1097
1098data_unsorted:
1099 // Insertion Sort mObjects
1100 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1101 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1102 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1103 binder_size_t temp = *iter0;
1104 binder_size_t* iter1 = iter0 - 1;
1105 while (iter1 >= mObjects && *iter1 > temp) {
1106 *(iter1 + 1) = *iter1;
1107 iter1--;
1108 }
1109 *(iter1 + 1) = temp;
1110 }
1111 mNextObjectHint = 0;
1112 mObjectsSorted = true;
1113 goto data_sorted;
1114}
1115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116status_t Parcel::read(void* outData, size_t len) const
1117{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001118 if (len > INT32_MAX) {
1119 // don't accept size_t values which may have come from an
1120 // inadvertent conversion from a negative int.
1121 return BAD_VALUE;
1122 }
1123
1124 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1125 && len <= pad_size(len)) {
akirilovf7841832018-04-03 12:56:06 -07001126 if (mObjectsSize > 0) {
1127 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanz6a825e82018-04-17 16:52:40 -07001128 if(err != NO_ERROR) {
1129 // Still increment the data position by the expected length
1130 mDataPos += pad_size(len);
1131 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1132 return err;
1133 }
akirilovf7841832018-04-03 12:56:06 -07001134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001135 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001136 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001137 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001138 return NO_ERROR;
1139 }
1140 return NOT_ENOUGH_DATA;
1141}
1142
1143const void* Parcel::readInplace(size_t len) const
1144{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001145 if (len > INT32_MAX) {
1146 // don't accept size_t values which may have come from an
1147 // inadvertent conversion from a negative int.
1148 return NULL;
1149 }
1150
1151 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1152 && len <= pad_size(len)) {
akirilovf7841832018-04-03 12:56:06 -07001153 if (mObjectsSize > 0) {
1154 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanz6a825e82018-04-17 16:52:40 -07001155 if(err != NO_ERROR) {
1156 // Still increment the data position by the expected length
1157 mDataPos += pad_size(len);
1158 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
1159 return NULL;
1160 }
akirilovf7841832018-04-03 12:56:06 -07001161 }
1162
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001164 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001165 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166 return data;
1167 }
1168 return NULL;
1169}
1170
Andreas Huber84a6d042009-08-17 13:33:27 -07001171template<class T>
1172status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001173 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001174
1175 if ((mDataPos+sizeof(T)) <= mDataSize) {
akirilovf7841832018-04-03 12:56:06 -07001176 if (mObjectsSize > 0) {
1177 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanz6a825e82018-04-17 16:52:40 -07001178 if(err != NO_ERROR) {
1179 // Still increment the data position by the expected length
1180 mDataPos += sizeof(T);
1181 return err;
1182 }
akirilovf7841832018-04-03 12:56:06 -07001183 }
1184
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001186 mDataPos += sizeof(T);
1187 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001188 return NO_ERROR;
1189 } else {
1190 return NOT_ENOUGH_DATA;
1191 }
1192}
1193
Andreas Huber84a6d042009-08-17 13:33:27 -07001194template<class T>
1195T Parcel::readAligned() const {
1196 T result;
1197 if (readAligned(&result) != NO_ERROR) {
1198 result = 0;
1199 }
1200
1201 return result;
1202}
1203
1204template<class T>
1205status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001206 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001207
1208 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1209restart_write:
1210 *reinterpret_cast<T*>(mData+mDataPos) = val;
1211 return finishWrite(sizeof(val));
1212 }
1213
1214 status_t err = growData(sizeof(val));
1215 if (err == NO_ERROR) goto restart_write;
1216 return err;
1217}
1218
1219status_t Parcel::readInt32(int32_t *pArg) const
1220{
1221 return readAligned(pArg);
1222}
1223
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001224int32_t Parcel::readInt32() const
1225{
Andreas Huber84a6d042009-08-17 13:33:27 -07001226 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227}
1228
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001229status_t Parcel::readUint32(uint32_t *pArg) const
1230{
1231 return readAligned(pArg);
1232}
1233
1234uint32_t Parcel::readUint32() const
1235{
1236 return readAligned<uint32_t>();
1237}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238
1239status_t Parcel::readInt64(int64_t *pArg) const
1240{
Andreas Huber84a6d042009-08-17 13:33:27 -07001241 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001242}
1243
1244
1245int64_t Parcel::readInt64() const
1246{
Andreas Huber84a6d042009-08-17 13:33:27 -07001247 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001248}
1249
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001250status_t Parcel::readUint64(uint64_t *pArg) const
1251{
1252 return readAligned(pArg);
1253}
1254
1255uint64_t Parcel::readUint64() const
1256{
1257 return readAligned<uint64_t>();
1258}
1259
Serban Constantinescuf683e012013-11-05 16:53:55 +00001260status_t Parcel::readPointer(uintptr_t *pArg) const
1261{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001262 status_t ret;
1263 binder_uintptr_t ptr;
1264 ret = readAligned(&ptr);
1265 if (!ret)
1266 *pArg = ptr;
1267 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001268}
1269
1270uintptr_t Parcel::readPointer() const
1271{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001272 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001273}
1274
1275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276status_t Parcel::readFloat(float *pArg) const
1277{
Andreas Huber84a6d042009-08-17 13:33:27 -07001278 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001279}
1280
1281
1282float Parcel::readFloat() const
1283{
Andreas Huber84a6d042009-08-17 13:33:27 -07001284 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001285}
1286
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001287#if defined(__mips__) && defined(__mips_hard_float)
1288
1289status_t Parcel::readDouble(double *pArg) const
1290{
1291 union {
1292 double d;
1293 unsigned long long ll;
1294 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001295 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001296 status_t status;
1297 status = readAligned(&u.ll);
1298 *pArg = u.d;
1299 return status;
1300}
1301
1302double Parcel::readDouble() const
1303{
1304 union {
1305 double d;
1306 unsigned long long ll;
1307 } u;
1308 u.ll = readAligned<unsigned long long>();
1309 return u.d;
1310}
1311
1312#else
1313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314status_t Parcel::readDouble(double *pArg) const
1315{
Andreas Huber84a6d042009-08-17 13:33:27 -07001316 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317}
1318
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001319double Parcel::readDouble() const
1320{
Andreas Huber84a6d042009-08-17 13:33:27 -07001321 return readAligned<double>();
1322}
1323
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001324#endif
1325
Andreas Huber84a6d042009-08-17 13:33:27 -07001326status_t Parcel::readIntPtr(intptr_t *pArg) const
1327{
1328 return readAligned(pArg);
1329}
1330
1331
1332intptr_t Parcel::readIntPtr() const
1333{
1334 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001335}
1336
1337
1338const char* Parcel::readCString() const
1339{
1340 const size_t avail = mDataSize-mDataPos;
1341 if (avail > 0) {
1342 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1343 // is the string's trailing NUL within the parcel's valid bounds?
1344 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1345 if (eos) {
1346 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001347 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001348 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 return str;
1350 }
1351 }
1352 return NULL;
1353}
1354
1355String8 Parcel::readString8() const
1356{
1357 int32_t size = readInt32();
1358 // watch for potential int overflow adding 1 for trailing NUL
1359 if (size > 0 && size < INT32_MAX) {
1360 const char* str = (const char*)readInplace(size+1);
1361 if (str) return String8(str, size);
1362 }
1363 return String8();
1364}
1365
1366String16 Parcel::readString16() const
1367{
1368 size_t len;
1369 const char16_t* str = readString16Inplace(&len);
1370 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001371 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372 return String16();
1373}
1374
1375const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1376{
1377 int32_t size = readInt32();
1378 // watch for potential int overflow from size+1
1379 if (size >= 0 && size < INT32_MAX) {
1380 *outLen = size;
1381 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1382 if (str != NULL) {
1383 return str;
1384 }
1385 }
1386 *outLen = 0;
1387 return NULL;
1388}
1389
1390sp<IBinder> Parcel::readStrongBinder() const
1391{
1392 sp<IBinder> val;
1393 unflatten_binder(ProcessState::self(), *this, &val);
1394 return val;
1395}
1396
1397wp<IBinder> Parcel::readWeakBinder() const
1398{
1399 wp<IBinder> val;
1400 unflatten_binder(ProcessState::self(), *this, &val);
1401 return val;
1402}
1403
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001404int32_t Parcel::readExceptionCode() const
1405{
1406 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001407 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001408 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001409 int32_t header_size = readAligned<int32_t>();
1410 // Skip over fat responses headers. Not used (or propagated) in
1411 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001412 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001413 // And fat response headers are currently only used when there are no
1414 // exceptions, so return no error:
1415 return 0;
1416 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001417 return exception_code;
1418}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001419
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001420native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001421{
1422 int numFds, numInts;
1423 status_t err;
1424 err = readInt32(&numFds);
1425 if (err != NO_ERROR) return 0;
1426 err = readInt32(&numInts);
1427 if (err != NO_ERROR) return 0;
1428
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001429 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001430 if (!h) {
1431 return 0;
1432 }
1433
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001434 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001435 h->data[i] = dup(readFileDescriptor());
Marco Nelissen1de79662016-04-26 08:44:09 -07001436 if (h->data[i] < 0) {
1437 for (int j = 0; j < i; j++) {
1438 close(h->data[j]);
1439 }
1440 native_handle_delete(h);
1441 return 0;
1442 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001443 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001444 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001445 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001446 native_handle_close(h);
1447 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001448 h = 0;
1449 }
1450 return h;
1451}
1452
1453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454int Parcel::readFileDescriptor() const
1455{
1456 const flat_binder_object* flat = readObject(true);
1457 if (flat) {
1458 switch (flat->type) {
1459 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001460 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001462 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463 }
1464 return BAD_TYPE;
1465}
1466
Jeff Brown5707dbf2011-09-23 21:17:56 -07001467status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1468{
Jeff Brown13b16042014-11-11 16:44:25 -08001469 int32_t blobType;
1470 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001471 if (status) return status;
1472
Jeff Brown13b16042014-11-11 16:44:25 -08001473 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001474 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001475 const void* ptr = readInplace(len);
1476 if (!ptr) return BAD_VALUE;
1477
Jeff Brown13b16042014-11-11 16:44:25 -08001478 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001479 return NO_ERROR;
1480 }
1481
Steve Block6807e592011-10-20 11:56:00 +01001482 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001483 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001484 int fd = readFileDescriptor();
1485 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1486
Jeff Brown13b16042014-11-11 16:44:25 -08001487 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1488 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001489 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001490
Jeff Brown13b16042014-11-11 16:44:25 -08001491 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001492 return NO_ERROR;
1493}
1494
Mathias Agopiane1424282013-07-29 21:24:40 -07001495status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001496{
1497 // size
1498 const size_t len = this->readInt32();
1499 const size_t fd_count = this->readInt32();
1500
Nick Kralevichb6b14232015-04-02 09:36:02 -07001501 if (len > INT32_MAX) {
1502 // don't accept size_t values which may have come from an
1503 // inadvertent conversion from a negative int.
1504 return BAD_VALUE;
1505 }
1506
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001507 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001508 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001509 if (buf == NULL)
1510 return BAD_VALUE;
1511
1512 int* fds = NULL;
1513 if (fd_count) {
1514 fds = new int[fd_count];
1515 }
1516
1517 status_t err = NO_ERROR;
1518 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001519 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001520 if (fds[i] < 0) {
1521 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001522 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1523 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001524 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001525 }
1526
1527 if (err == NO_ERROR) {
1528 err = val.unflatten(buf, len, fds, fd_count);
1529 }
1530
1531 if (fd_count) {
1532 delete [] fds;
1533 }
1534
1535 return err;
1536}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001537const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1538{
1539 const size_t DPOS = mDataPos;
1540 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1541 const flat_binder_object* obj
1542 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1543 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001544 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001545 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546 // the object list, so we don't want to check for it when
1547 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001548 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549 return obj;
1550 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001553 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554 const size_t N = mObjectsSize;
1555 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001558 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561 // Start at the current hint position, looking for an object at
1562 // the current data position.
1563 if (opos < N) {
1564 while (opos < (N-1) && OBJS[opos] < DPOS) {
1565 opos++;
1566 }
1567 } else {
1568 opos = N-1;
1569 }
1570 if (OBJS[opos] == DPOS) {
1571 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001572 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573 this, DPOS, opos);
1574 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001575 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576 return obj;
1577 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579 // Look backwards for it...
1580 while (opos > 0 && OBJS[opos] > DPOS) {
1581 opos--;
1582 }
1583 if (OBJS[opos] == DPOS) {
1584 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001585 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586 this, DPOS, opos);
1587 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001588 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001589 return obj;
1590 }
1591 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001592 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 -07001593 this, DPOS);
1594 }
1595 return NULL;
1596}
1597
1598void Parcel::closeFileDescriptors()
1599{
1600 size_t i = mObjectsSize;
1601 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001602 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603 }
1604 while (i > 0) {
1605 i--;
1606 const flat_binder_object* flat
1607 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1608 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001609 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 close(flat->handle);
1611 }
1612 }
1613}
1614
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001615uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001617 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001618}
1619
1620size_t Parcel::ipcDataSize() const
1621{
1622 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1623}
1624
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001625uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001626{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001627 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001628}
1629
1630size_t Parcel::ipcObjectsCount() const
1631{
1632 return mObjectsSize;
1633}
1634
1635void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001636 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001638 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639 freeDataNoInit();
1640 mError = NO_ERROR;
1641 mData = const_cast<uint8_t*>(data);
1642 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001643 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001644 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001645 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001646 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647 mObjectsSize = mObjectsCapacity = objectsCount;
1648 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -07001649 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001650 mOwner = relFunc;
1651 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001652 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001653 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001654 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001655 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001656 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001657 mObjectsSize = 0;
1658 break;
1659 }
1660 minOffset = offset + sizeof(flat_binder_object);
1661 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001662 scanForFds();
1663}
1664
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001665void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666{
1667 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001668
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669 if (errorCheck() != NO_ERROR) {
1670 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001671 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001672 } else if (dataSize() > 0) {
1673 const uint8_t* DATA = data();
1674 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001675 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676 const size_t N = objectsCount();
1677 for (size_t i=0; i<N; i++) {
1678 const flat_binder_object* flat
1679 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1680 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1681 << TypeCode(flat->type & 0x7f7f7f00)
1682 << " = " << flat->binder;
1683 }
1684 } else {
1685 to << "NULL";
1686 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688 to << ")";
1689}
1690
1691void Parcel::releaseObjects()
1692{
1693 const sp<ProcessState> proc(ProcessState::self());
1694 size_t i = mObjectsSize;
1695 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001696 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697 while (i > 0) {
1698 i--;
1699 const flat_binder_object* flat
1700 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1701 release_object(proc, *flat, this);
1702 }
1703}
1704
1705void Parcel::acquireObjects()
1706{
1707 const sp<ProcessState> proc(ProcessState::self());
1708 size_t i = mObjectsSize;
1709 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001710 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001711 while (i > 0) {
1712 i--;
1713 const flat_binder_object* flat
1714 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1715 acquire_object(proc, *flat, this);
1716 }
1717}
1718
1719void Parcel::freeData()
1720{
1721 freeDataNoInit();
1722 initState();
1723}
1724
1725void Parcel::freeDataNoInit()
1726{
1727 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001728 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001729 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001730 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1731 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001732 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001733 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001734 if (mData) {
1735 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001736 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001737 gParcelGlobalAllocSize -= mDataCapacity;
1738 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001739 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001740 free(mData);
1741 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742 if (mObjects) free(mObjects);
1743 }
1744}
1745
1746status_t Parcel::growData(size_t len)
1747{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001748 if (len > INT32_MAX) {
1749 // don't accept size_t values which may have come from an
1750 // inadvertent conversion from a negative int.
1751 return BAD_VALUE;
1752 }
1753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754 size_t newSize = ((mDataSize+len)*3)/2;
1755 return (newSize <= mDataSize)
1756 ? (status_t) NO_MEMORY
1757 : continueWrite(newSize);
1758}
1759
1760status_t Parcel::restartWrite(size_t desired)
1761{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001762 if (desired > INT32_MAX) {
1763 // don't accept size_t values which may have come from an
1764 // inadvertent conversion from a negative int.
1765 return BAD_VALUE;
1766 }
1767
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001768 if (mOwner) {
1769 freeData();
1770 return continueWrite(desired);
1771 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001772
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001773 uint8_t* data = (uint8_t*)realloc(mData, desired);
1774 if (!data && desired > mDataCapacity) {
1775 mError = NO_MEMORY;
1776 return NO_MEMORY;
1777 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001778
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001780
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001782 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001783 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001784 gParcelGlobalAllocSize += desired;
1785 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001786 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001787 mData = data;
1788 mDataCapacity = desired;
1789 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001792 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1793 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1794
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001795 free(mObjects);
1796 mObjects = NULL;
1797 mObjectsSize = mObjectsCapacity = 0;
1798 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -07001799 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800 mHasFds = false;
1801 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001802 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804 return NO_ERROR;
1805}
1806
1807status_t Parcel::continueWrite(size_t desired)
1808{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001809 if (desired > INT32_MAX) {
1810 // don't accept size_t values which may have come from an
1811 // inadvertent conversion from a negative int.
1812 return BAD_VALUE;
1813 }
1814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815 // If shrinking, first adjust for any objects that appear
1816 // after the new data size.
1817 size_t objectsSize = mObjectsSize;
1818 if (desired < mDataSize) {
1819 if (desired == 0) {
1820 objectsSize = 0;
1821 } else {
1822 while (objectsSize > 0) {
1823 if (mObjects[objectsSize-1] < desired)
1824 break;
1825 objectsSize--;
1826 }
1827 }
1828 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001829
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001830 if (mOwner) {
1831 // If the size is going to zero, just release the owner's data.
1832 if (desired == 0) {
1833 freeData();
1834 return NO_ERROR;
1835 }
1836
1837 // If there is a different owner, we need to take
1838 // posession.
1839 uint8_t* data = (uint8_t*)malloc(desired);
1840 if (!data) {
1841 mError = NO_MEMORY;
1842 return NO_MEMORY;
1843 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001844 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001845
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001847 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001849 free(data);
1850
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001851 mError = NO_MEMORY;
1852 return NO_MEMORY;
1853 }
1854
1855 // Little hack to only acquire references on objects
1856 // we will be keeping.
1857 size_t oldObjectsSize = mObjectsSize;
1858 mObjectsSize = objectsSize;
1859 acquireObjects();
1860 mObjectsSize = oldObjectsSize;
1861 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863 if (mData) {
1864 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1865 }
1866 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001867 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001869 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1871 mOwner = NULL;
1872
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001873 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001874 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001875 gParcelGlobalAllocSize += desired;
1876 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001877 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001878
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879 mData = data;
1880 mObjects = objects;
1881 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001882 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001883 mDataCapacity = desired;
1884 mObjectsSize = mObjectsCapacity = objectsSize;
1885 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -07001886 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887
1888 } else if (mData) {
1889 if (objectsSize < mObjectsSize) {
1890 // Need to release refs on any objects we are dropping.
1891 const sp<ProcessState> proc(ProcessState::self());
1892 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1893 const flat_binder_object* flat
1894 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1895 if (flat->type == BINDER_TYPE_FD) {
1896 // will need to rescan because we may have lopped off the only FDs
1897 mFdsKnown = false;
1898 }
1899 release_object(proc, *flat, this);
1900 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001901 binder_size_t* objects =
1902 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 if (objects) {
1904 mObjects = objects;
1905 }
1906 mObjectsSize = objectsSize;
1907 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -07001908 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909 }
1910
1911 // We own the data, so we can just do a realloc().
1912 if (desired > mDataCapacity) {
1913 uint8_t* data = (uint8_t*)realloc(mData, desired);
1914 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001915 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1916 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001917 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001918 gParcelGlobalAllocSize += desired;
1919 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001920 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001921 mData = data;
1922 mDataCapacity = desired;
1923 } else if (desired > mDataCapacity) {
1924 mError = NO_MEMORY;
1925 return NO_MEMORY;
1926 }
1927 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001928 if (mDataSize > desired) {
1929 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001930 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001931 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932 if (mDataPos > desired) {
1933 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001934 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 }
1936 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001937
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001938 } else {
1939 // This is the first data. Easy!
1940 uint8_t* data = (uint8_t*)malloc(desired);
1941 if (!data) {
1942 mError = NO_MEMORY;
1943 return NO_MEMORY;
1944 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001946 if(!(mDataCapacity == 0 && mObjects == NULL
1947 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001948 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001950
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001951 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001952 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001953 gParcelGlobalAllocSize += desired;
1954 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001955 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001956
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 mData = data;
1958 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001959 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1960 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961 mDataCapacity = desired;
1962 }
1963
1964 return NO_ERROR;
1965}
1966
1967void Parcel::initState()
1968{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001969 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001970 mError = NO_ERROR;
1971 mData = 0;
1972 mDataSize = 0;
1973 mDataCapacity = 0;
1974 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001975 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1976 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 mObjects = NULL;
1978 mObjectsSize = 0;
1979 mObjectsCapacity = 0;
1980 mNextObjectHint = 0;
akirilovf7841832018-04-03 12:56:06 -07001981 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982 mHasFds = false;
1983 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001984 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001986 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987}
1988
1989void Parcel::scanForFds() const
1990{
1991 bool hasFds = false;
1992 for (size_t i=0; i<mObjectsSize; i++) {
1993 const flat_binder_object* flat
1994 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1995 if (flat->type == BINDER_TYPE_FD) {
1996 hasFds = true;
1997 break;
1998 }
1999 }
2000 mHasFds = hasFds;
2001 mFdsKnown = true;
2002}
2003
Dan Sandleraa5c2342015-04-10 10:08:45 -04002004size_t Parcel::getBlobAshmemSize() const
2005{
2006 return mBlobAshmemSize;
2007}
2008
Jeff Brown5707dbf2011-09-23 21:17:56 -07002009// --- Parcel::Blob ---
2010
2011Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002012 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002013}
2014
2015Parcel::Blob::~Blob() {
2016 release();
2017}
2018
2019void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002020 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002021 ::munmap(mData, mSize);
2022 }
2023 clear();
2024}
2025
Jeff Brown13b16042014-11-11 16:44:25 -08002026void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2027 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002028 mData = data;
2029 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002030 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002031}
2032
2033void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002034 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002035 mData = NULL;
2036 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002037 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002038}
2039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040}; // namespace android