blob: 03348da773ec2ee9ea24cc225c67f7b3719e4c1a [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>
Christopher Wiley09eb7492015-11-09 15:06:15 -080026#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070027#include <binder/TextOutput.h>
28
Jun Jiangabf8a2c2014-04-29 14:22:10 +080029#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070030#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/Log.h>
32#include <utils/String8.h>
33#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080035#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070036#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Mathias Agopian208059f2009-05-18 15:08:03 -070038#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080039#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080041#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070045#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47#ifndef INT32_MAX
48#define INT32_MAX ((int32_t)(2147483647))
49#endif
50
51#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010052//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080053#define LOG_ALLOC(...)
54//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
56// ---------------------------------------------------------------------------
57
Nick Kralevichb6b14232015-04-02 09:36:02 -070058// This macro should never be used at runtime, as a too large value
59// of s could cause an integer overflow. Instead, you should always
60// use the wrapper function pad_size()
61#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
62
63static size_t pad_size(size_t s) {
64 if (s > (SIZE_T_MAX - 3)) {
65 abort();
66 }
67 return PAD_SIZE_UNSAFE(s);
68}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070069
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080071#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073// XXX This can be made public if we want to provide
74// support for typed data.
75struct small_flat_data
76{
77 uint32_t type;
78 uint32_t data;
79};
80
81namespace android {
82
Dianne Hackborna4cff882014-11-13 17:07:40 -080083static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
84static size_t gParcelGlobalAllocSize = 0;
85static size_t gParcelGlobalAllocCount = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void acquire_object(const sp<ProcessState>& proc,
97 const flat_binder_object& obj, const void* who)
98{
99 switch (obj.type) {
100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
112 if (b != NULL) {
113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
120 if (b != NULL) b.get_refs()->incWeak(who);
121 return;
122 }
123 case BINDER_TYPE_FD: {
124 // intentionally blank -- nothing to do to acquire this, but we do
125 // recognize it as a legitimate object type.
126 return;
127 }
128 }
129
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800130 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131}
132
133void release_object(const sp<ProcessState>& proc,
134 const flat_binder_object& obj, const void* who)
135{
136 switch (obj.type) {
137 case BINDER_TYPE_BINDER:
138 if (obj.binder) {
139 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800140 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 }
142 return;
143 case BINDER_TYPE_WEAK_BINDER:
144 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800145 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 return;
147 case BINDER_TYPE_HANDLE: {
148 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
149 if (b != NULL) {
150 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
151 b->decStrong(who);
152 }
153 return;
154 }
155 case BINDER_TYPE_WEAK_HANDLE: {
156 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
157 if (b != NULL) b.get_refs()->decWeak(who);
158 return;
159 }
160 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800161 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 return;
163 }
164 }
165
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800166 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167}
168
169inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800170 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171{
172 return out->writeObject(flat, false);
173}
174
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800175status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176 const sp<IBinder>& binder, Parcel* out)
177{
178 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
181 if (binder != NULL) {
182 IBinder *local = binder->localBinder();
183 if (!local) {
184 BpBinder *proxy = binder->remoteBinder();
185 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000186 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187 }
188 const int32_t handle = proxy ? proxy->handle() : 0;
189 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800190 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800192 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 } else {
194 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800195 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
196 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 }
198 } else {
199 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800200 obj.binder = 0;
201 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700203
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 return finish_flatten_binder(binder, obj, out);
205}
206
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 const wp<IBinder>& binder, Parcel* out)
209{
210 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
213 if (binder != NULL) {
214 sp<IBinder> real = binder.promote();
215 if (real != NULL) {
216 IBinder *local = real->localBinder();
217 if (!local) {
218 BpBinder *proxy = real->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_WEAK_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_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
230 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 return finish_flatten_binder(real, obj, out);
233 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 // XXX How to deal? In order to flatten the given binder,
236 // we need to probe it for information, which requires a primary
237 // reference... but we don't have one.
238 //
239 // The OpenBinder implementation uses a dynamic_cast<> here,
240 // but we can't do that with the different reference counting
241 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000242 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.binder = 0;
245 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 } else {
249 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800250 obj.binder = 0;
251 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 return finish_flatten_binder(NULL, obj, out);
253 }
254}
255
256inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800257 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
258 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259{
260 return NO_ERROR;
261}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263status_t unflatten_binder(const sp<ProcessState>& proc,
264 const Parcel& in, sp<IBinder>* out)
265{
266 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 if (flat) {
269 switch (flat->type) {
270 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 return finish_unflatten_binder(NULL, *flat, in);
273 case BINDER_TYPE_HANDLE:
274 *out = proc->getStrongProxyForHandle(flat->handle);
275 return finish_unflatten_binder(
276 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 }
279 return BAD_TYPE;
280}
281
282status_t unflatten_binder(const sp<ProcessState>& proc,
283 const Parcel& in, wp<IBinder>* out)
284{
285 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 if (flat) {
288 switch (flat->type) {
289 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800290 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 return finish_unflatten_binder(NULL, *flat, in);
292 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800293 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800295 reinterpret_cast<IBinder*>(flat->cookie),
296 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 } else {
298 *out = NULL;
299 }
300 return finish_unflatten_binder(NULL, *flat, in);
301 case BINDER_TYPE_HANDLE:
302 case BINDER_TYPE_WEAK_HANDLE:
303 *out = proc->getWeakProxyForHandle(flat->handle);
304 return finish_unflatten_binder(
305 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
306 }
307 }
308 return BAD_TYPE;
309}
310
311// ---------------------------------------------------------------------------
312
313Parcel::Parcel()
314{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800315 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 initState();
317}
318
319Parcel::~Parcel()
320{
321 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800322 LOG_ALLOC("Parcel %p: destroyed", this);
323}
324
325size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800326 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
327 size_t size = gParcelGlobalAllocSize;
328 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
329 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800330}
331
332size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800333 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
334 size_t count = gParcelGlobalAllocCount;
335 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
336 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339const uint8_t* Parcel::data() const
340{
341 return mData;
342}
343
344size_t Parcel::dataSize() const
345{
346 return (mDataSize > mDataPos ? mDataSize : mDataPos);
347}
348
349size_t Parcel::dataAvail() const
350{
351 // TODO: decide what to do about the possibility that this can
352 // report an available-data size that exceeds a Java int's max
353 // positive value, causing havoc. Fortunately this will only
354 // happen if someone constructs a Parcel containing more than two
355 // gigabytes of data, which on typical phone hardware is simply
356 // not possible.
357 return dataSize() - dataPosition();
358}
359
360size_t Parcel::dataPosition() const
361{
362 return mDataPos;
363}
364
365size_t Parcel::dataCapacity() const
366{
367 return mDataCapacity;
368}
369
370status_t Parcel::setDataSize(size_t size)
371{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700372 if (size > INT32_MAX) {
373 // don't accept size_t values which may have come from an
374 // inadvertent conversion from a negative int.
375 return BAD_VALUE;
376 }
377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 status_t err;
379 err = continueWrite(size);
380 if (err == NO_ERROR) {
381 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700382 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383 }
384 return err;
385}
386
387void Parcel::setDataPosition(size_t pos) const
388{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700389 if (pos > INT32_MAX) {
390 // don't accept size_t values which may have come from an
391 // inadvertent conversion from a negative int.
392 abort();
393 }
394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 mDataPos = pos;
396 mNextObjectHint = 0;
397}
398
399status_t Parcel::setDataCapacity(size_t size)
400{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700401 if (size > INT32_MAX) {
402 // don't accept size_t values which may have come from an
403 // inadvertent conversion from a negative int.
404 return BAD_VALUE;
405 }
406
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700407 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 return NO_ERROR;
409}
410
411status_t Parcel::setData(const uint8_t* buffer, size_t len)
412{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700413 if (len > INT32_MAX) {
414 // don't accept size_t values which may have come from an
415 // inadvertent conversion from a negative int.
416 return BAD_VALUE;
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 status_t err = restartWrite(len);
420 if (err == NO_ERROR) {
421 memcpy(const_cast<uint8_t*>(data()), buffer, len);
422 mDataSize = len;
423 mFdsKnown = false;
424 }
425 return err;
426}
427
Andreas Huber51faf462011-04-13 10:21:56 -0700428status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429{
430 const sp<ProcessState> proc(ProcessState::self());
431 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700432 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 size_t size = parcel->mObjectsSize;
435 int startPos = mDataPos;
436 int firstIndex = -1, lastIndex = -2;
437
438 if (len == 0) {
439 return NO_ERROR;
440 }
441
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 // range checks against the source parcel size
449 if ((offset > parcel->mDataSize)
450 || (len > parcel->mDataSize)
451 || (offset + len > parcel->mDataSize)) {
452 return BAD_VALUE;
453 }
454
455 // Count objects in range
456 for (int i = 0; i < (int) size; i++) {
457 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700458 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 if (firstIndex == -1) {
460 firstIndex = i;
461 }
462 lastIndex = i;
463 }
464 }
465 int numObjects = lastIndex - firstIndex + 1;
466
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700467 if ((mDataSize+len) > mDataCapacity) {
468 // grow data
469 err = growData(len);
470 if (err != NO_ERROR) {
471 return err;
472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 }
474
475 // append data
476 memcpy(mData + mDataPos, data + offset, len);
477 mDataPos += len;
478 mDataSize += len;
479
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480 err = NO_ERROR;
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 if (numObjects > 0) {
483 // grow objects
484 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700485 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
486 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800487 binder_size_t *objects =
488 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
489 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 return NO_MEMORY;
491 }
492 mObjects = objects;
493 mObjectsCapacity = newSize;
494 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 // append and acquire objects
497 int idx = mObjectsSize;
498 for (int i = firstIndex; i <= lastIndex; i++) {
499 size_t off = objects[i] - offset + startPos;
500 mObjects[idx++] = off;
501 mObjectsSize++;
502
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700503 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 = reinterpret_cast<flat_binder_object*>(mData + off);
505 acquire_object(proc, *flat, this);
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700508 // If this is a file descriptor, we need to dup it so the
509 // new Parcel now owns its own fd, and can declare that we
510 // officially know we have fds.
511 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800512 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 if (!mAllowFds) {
515 err = FDS_NOT_ALLOWED;
516 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 }
518 }
519 }
520
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400521 return err;
522}
523
Jeff Brown13b16042014-11-11 16:44:25 -0800524bool Parcel::allowFds() const
525{
526 return mAllowFds;
527}
528
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700529bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530{
531 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532 if (!allowFds) {
533 mAllowFds = false;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538void Parcel::restoreAllowFds(bool lastValue)
539{
540 mAllowFds = lastValue;
541}
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543bool Parcel::hasFileDescriptors() const
544{
545 if (!mFdsKnown) {
546 scanForFds();
547 }
548 return mHasFds;
549}
550
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700551// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552status_t Parcel::writeInterfaceToken(const String16& interface)
553{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700554 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
555 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 // currently the interface identification token is just its name as a string
557 return writeString16(interface);
558}
559
Mathias Agopian83c04462009-05-22 19:00:22 -0700560bool Parcel::checkInterface(IBinder* binder) const
561{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700562 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700563}
564
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700568 int32_t strictPolicy = readInt32();
569 if (threadState == NULL) {
570 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700571 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700572 if ((threadState->getLastTransactionBinderFlags() &
573 IBinder::FLAG_ONEWAY) != 0) {
574 // For one-way calls, the callee is running entirely
575 // disconnected from the caller, so disable StrictMode entirely.
576 // Not only does disk/network usage not impact the caller, but
577 // there's no way to commuicate back any violations anyway.
578 threadState->setStrictModePolicy(0);
579 } else {
580 threadState->setStrictModePolicy(strictPolicy);
581 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700582 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583 if (str == interface) {
584 return true;
585 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700586 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587 String8(interface).string(), String8(str).string());
588 return false;
589 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700590}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800592const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593{
594 return mObjects;
595}
596
597size_t Parcel::objectsCount() const
598{
599 return mObjectsSize;
600}
601
602status_t Parcel::errorCheck() const
603{
604 return mError;
605}
606
607void Parcel::setError(status_t err)
608{
609 mError = err;
610}
611
612status_t Parcel::finishWrite(size_t len)
613{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700614 if (len > INT32_MAX) {
615 // don't accept size_t values which may have come from an
616 // inadvertent conversion from a negative int.
617 return BAD_VALUE;
618 }
619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 //printf("Finish write of %d\n", len);
621 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (mDataPos > mDataSize) {
624 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 }
627 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
628 return NO_ERROR;
629}
630
631status_t Parcel::writeUnpadded(const void* data, size_t len)
632{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 if (len > INT32_MAX) {
634 // don't accept size_t values which may have come from an
635 // inadvertent conversion from a negative int.
636 return BAD_VALUE;
637 }
638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 size_t end = mDataPos + len;
640 if (end < mDataPos) {
641 // integer overflow
642 return BAD_VALUE;
643 }
644
645 if (end <= mDataCapacity) {
646restart_write:
647 memcpy(mData+mDataPos, data, len);
648 return finishWrite(len);
649 }
650
651 status_t err = growData(len);
652 if (err == NO_ERROR) goto restart_write;
653 return err;
654}
655
656status_t Parcel::write(const void* data, size_t len)
657{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700658 if (len > INT32_MAX) {
659 // don't accept size_t values which may have come from an
660 // inadvertent conversion from a negative int.
661 return BAD_VALUE;
662 }
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 void* const d = writeInplace(len);
665 if (d) {
666 memcpy(d, data, len);
667 return NO_ERROR;
668 }
669 return mError;
670}
671
672void* Parcel::writeInplace(size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
677 return NULL;
678 }
679
680 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
682 // sanity check for integer overflow
683 if (mDataPos+padded < mDataPos) {
684 return NULL;
685 }
686
687 if ((mDataPos+padded) <= mDataCapacity) {
688restart_write:
689 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
690 uint8_t* const data = mData+mDataPos;
691
692 // Need to pad at end?
693 if (padded != len) {
694#if BYTE_ORDER == BIG_ENDIAN
695 static const uint32_t mask[4] = {
696 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
697 };
698#endif
699#if BYTE_ORDER == LITTLE_ENDIAN
700 static const uint32_t mask[4] = {
701 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
702 };
703#endif
704 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
705 // *reinterpret_cast<void**>(data+padded-4));
706 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
707 }
708
709 finishWrite(padded);
710 return data;
711 }
712
713 status_t err = growData(padded);
714 if (err == NO_ERROR) goto restart_write;
715 return NULL;
716}
717
Casey Dahlin451ff582015-10-19 18:12:18 -0700718status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
719{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700720 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700721 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700722 status = BAD_VALUE;
723 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700724 }
725
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700726 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700727 if (status != OK) {
728 return status;
729 }
730
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700731 void* data = writeInplace(val.size());
732 if (!data) {
733 status = BAD_VALUE;
734 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700735 }
736
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700737 memcpy(data, val.data(), val.size());
738 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700739}
740
741status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
742{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800743 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700744}
745
746status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
747{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800748 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700749}
750
751status_t Parcel::writeFloatVector(const std::vector<float>& val)
752{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800753 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700754}
755
756status_t Parcel::writeDoubleVector(const std::vector<double>& val)
757{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800758 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700759}
760
761status_t Parcel::writeBoolVector(const std::vector<bool>& val)
762{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800763 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700764}
765
766status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
767{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800768 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700769}
770
771status_t Parcel::writeString16Vector(const std::vector<String16>& val)
772{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800773 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700774}
775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776status_t Parcel::writeInt32(int32_t val)
777{
Andreas Huber84a6d042009-08-17 13:33:27 -0700778 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800780
781status_t Parcel::writeUint32(uint32_t val)
782{
783 return writeAligned(val);
784}
785
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700786status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700787 if (len > INT32_MAX) {
788 // don't accept size_t values which may have come from an
789 // inadvertent conversion from a negative int.
790 return BAD_VALUE;
791 }
792
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700793 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700794 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700795 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700796 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700797 if (ret == NO_ERROR) {
798 ret = write(val, len * sizeof(*val));
799 }
800 return ret;
801}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700802status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700803 if (len > INT32_MAX) {
804 // don't accept size_t values which may have come from an
805 // inadvertent conversion from a negative int.
806 return BAD_VALUE;
807 }
808
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700809 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700810 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700811 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700812 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700813 if (ret == NO_ERROR) {
814 ret = write(val, len * sizeof(*val));
815 }
816 return ret;
817}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700818
Casey Dahlind6848f52015-10-15 15:44:59 -0700819status_t Parcel::writeBool(bool val)
820{
821 return writeInt32(int32_t(val));
822}
823
824status_t Parcel::writeChar(char16_t val)
825{
826 return writeInt32(int32_t(val));
827}
828
829status_t Parcel::writeByte(int8_t val)
830{
831 return writeInt32(int32_t(val));
832}
833
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700834status_t Parcel::writeInt64(int64_t val)
835{
Andreas Huber84a6d042009-08-17 13:33:27 -0700836 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700837}
838
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700839status_t Parcel::writeUint64(uint64_t val)
840{
841 return writeAligned(val);
842}
843
Serban Constantinescuf683e012013-11-05 16:53:55 +0000844status_t Parcel::writePointer(uintptr_t val)
845{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800846 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000847}
848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700849status_t Parcel::writeFloat(float val)
850{
Andreas Huber84a6d042009-08-17 13:33:27 -0700851 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700852}
853
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800854#if defined(__mips__) && defined(__mips_hard_float)
855
856status_t Parcel::writeDouble(double val)
857{
858 union {
859 double d;
860 unsigned long long ll;
861 } u;
862 u.d = val;
863 return writeAligned(u.ll);
864}
865
866#else
867
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700868status_t Parcel::writeDouble(double val)
869{
Andreas Huber84a6d042009-08-17 13:33:27 -0700870 return writeAligned(val);
871}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700872
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800873#endif
874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875status_t Parcel::writeCString(const char* str)
876{
877 return write(str, strlen(str)+1);
878}
879
880status_t Parcel::writeString8(const String8& str)
881{
882 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100883 // only write string if its length is more than zero characters,
884 // as readString8 will only read if the length field is non-zero.
885 // this is slightly different from how writeString16 works.
886 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700887 err = write(str.string(), str.bytes()+1);
888 }
889 return err;
890}
891
892status_t Parcel::writeString16(const String16& str)
893{
894 return writeString16(str.string(), str.size());
895}
896
897status_t Parcel::writeString16(const char16_t* str, size_t len)
898{
899 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700900
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901 status_t err = writeInt32(len);
902 if (err == NO_ERROR) {
903 len *= sizeof(char16_t);
904 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
905 if (data) {
906 memcpy(data, str, len);
907 *reinterpret_cast<char16_t*>(data+len) = 0;
908 return NO_ERROR;
909 }
910 err = mError;
911 }
912 return err;
913}
914
915status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
916{
917 return flatten_binder(ProcessState::self(), val, this);
918}
919
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800920status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
921{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800922 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800923}
924
925status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800926 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800927}
928
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
930{
931 return flatten_binder(ProcessState::self(), val, this);
932}
933
Christopher Wiley97f048d2015-11-19 06:49:05 -0800934status_t Parcel::writeParcelable(const Parcelable& parcelable) {
935 status_t status = writeInt32(1); // parcelable is not null.
936 if (status != OK) {
937 return status;
938 }
939 return parcelable.writeToParcel(this);
940}
941
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700942status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800943{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700944 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800945 return BAD_TYPE;
946
947 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700948 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800949 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700951 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800952 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700954 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
955 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956
957 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000958 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 return err;
960 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700961 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800962 return err;
963}
964
Jeff Brown93ff1f92011-11-04 19:01:44 -0700965status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966{
967 flat_binder_object obj;
968 obj.type = BINDER_TYPE_FD;
969 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800970 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800972 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973 return writeObject(obj, true);
974}
975
976status_t Parcel::writeDupFileDescriptor(int fd)
977{
Jeff Brownd341c712011-11-04 20:19:33 -0700978 int dupFd = dup(fd);
979 if (dupFd < 0) {
980 return -errno;
981 }
982 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
983 if (err) {
984 close(dupFd);
985 }
986 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987}
988
Jeff Brown13b16042014-11-11 16:44:25 -0800989status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700990{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700991 if (len > INT32_MAX) {
992 // don't accept size_t values which may have come from an
993 // inadvertent conversion from a negative int.
994 return BAD_VALUE;
995 }
996
Jeff Brown13b16042014-11-11 16:44:25 -0800997 status_t status;
998 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100999 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001000 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001001 if (status) return status;
1002
1003 void* ptr = writeInplace(len);
1004 if (!ptr) return NO_MEMORY;
1005
Jeff Brown13b16042014-11-11 16:44:25 -08001006 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001007 return NO_ERROR;
1008 }
1009
Steve Block6807e592011-10-20 11:56:00 +01001010 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001011 int fd = ashmem_create_region("Parcel Blob", len);
1012 if (fd < 0) return NO_MEMORY;
1013
Dan Sandleraa5c2342015-04-10 10:08:45 -04001014 mBlobAshmemSize += len;
1015
Jeff Brown5707dbf2011-09-23 21:17:56 -07001016 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1017 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001018 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001019 } else {
1020 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1021 if (ptr == MAP_FAILED) {
1022 status = -errno;
1023 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001024 if (!mutableCopy) {
1025 result = ashmem_set_prot_region(fd, PROT_READ);
1026 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001027 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001028 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001029 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001030 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001031 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001032 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001033 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001034 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001035 return NO_ERROR;
1036 }
1037 }
1038 }
1039 }
1040 ::munmap(ptr, len);
1041 }
1042 ::close(fd);
1043 return status;
1044}
1045
Jeff Brown13b16042014-11-11 16:44:25 -08001046status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1047{
1048 // Must match up with what's done in writeBlob.
1049 if (!mAllowFds) return FDS_NOT_ALLOWED;
1050 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1051 if (status) return status;
1052 return writeDupFileDescriptor(fd);
1053}
1054
Mathias Agopiane1424282013-07-29 21:24:40 -07001055status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001056{
1057 status_t err;
1058
1059 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001060 const size_t len = val.getFlattenedSize();
1061 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001062
Nick Kralevichb6b14232015-04-02 09:36:02 -07001063 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1064 // don't accept size_t values which may have come from an
1065 // inadvertent conversion from a negative int.
1066 return BAD_VALUE;
1067 }
1068
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001069 err = this->writeInt32(len);
1070 if (err) return err;
1071
1072 err = this->writeInt32(fd_count);
1073 if (err) return err;
1074
1075 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001076 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001077 if (buf == NULL)
1078 return BAD_VALUE;
1079
1080 int* fds = NULL;
1081 if (fd_count) {
1082 fds = new int[fd_count];
1083 }
1084
1085 err = val.flatten(buf, len, fds, fd_count);
1086 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1087 err = this->writeDupFileDescriptor( fds[i] );
1088 }
1089
1090 if (fd_count) {
1091 delete [] fds;
1092 }
1093
1094 return err;
1095}
1096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1098{
1099 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1100 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1101 if (enoughData && enoughObjects) {
1102restart_write:
1103 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001104
Christopher Tate98e67d32015-06-03 18:44:15 -07001105 // remember if it's a file descriptor
1106 if (val.type == BINDER_TYPE_FD) {
1107 if (!mAllowFds) {
1108 // fail before modifying our object index
1109 return FDS_NOT_ALLOWED;
1110 }
1111 mHasFds = mFdsKnown = true;
1112 }
1113
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001115 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116 mObjects[mObjectsSize] = mDataPos;
1117 acquire_object(ProcessState::self(), val, this);
1118 mObjectsSize++;
1119 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001120
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001121 return finishWrite(sizeof(flat_binder_object));
1122 }
1123
1124 if (!enoughData) {
1125 const status_t err = growData(sizeof(val));
1126 if (err != NO_ERROR) return err;
1127 }
1128 if (!enoughObjects) {
1129 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001130 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001131 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001132 if (objects == NULL) return NO_MEMORY;
1133 mObjects = objects;
1134 mObjectsCapacity = newSize;
1135 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001136
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001137 goto restart_write;
1138}
1139
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001140status_t Parcel::writeNoException()
1141{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001142 binder::Status status;
1143 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001144}
1145
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001146void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001147{
1148 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1149}
1150
1151status_t Parcel::read(void* outData, size_t len) const
1152{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001153 if (len > INT32_MAX) {
1154 // don't accept size_t values which may have come from an
1155 // inadvertent conversion from a negative int.
1156 return BAD_VALUE;
1157 }
1158
1159 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1160 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001162 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001163 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 return NO_ERROR;
1165 }
1166 return NOT_ENOUGH_DATA;
1167}
1168
1169const void* Parcel::readInplace(size_t len) const
1170{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001171 if (len > INT32_MAX) {
1172 // don't accept size_t values which may have come from an
1173 // inadvertent conversion from a negative int.
1174 return NULL;
1175 }
1176
1177 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1178 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001180 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001181 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001182 return data;
1183 }
1184 return NULL;
1185}
1186
Andreas Huber84a6d042009-08-17 13:33:27 -07001187template<class T>
1188status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001189 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001190
1191 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001193 mDataPos += sizeof(T);
1194 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195 return NO_ERROR;
1196 } else {
1197 return NOT_ENOUGH_DATA;
1198 }
1199}
1200
Andreas Huber84a6d042009-08-17 13:33:27 -07001201template<class T>
1202T Parcel::readAligned() const {
1203 T result;
1204 if (readAligned(&result) != NO_ERROR) {
1205 result = 0;
1206 }
1207
1208 return result;
1209}
1210
1211template<class T>
1212status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001213 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001214
1215 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1216restart_write:
1217 *reinterpret_cast<T*>(mData+mDataPos) = val;
1218 return finishWrite(sizeof(val));
1219 }
1220
1221 status_t err = growData(sizeof(val));
1222 if (err == NO_ERROR) goto restart_write;
1223 return err;
1224}
1225
Casey Dahlin451ff582015-10-19 18:12:18 -07001226status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1227 val->clear();
1228
1229 int32_t size;
1230 status_t status = readInt32(&size);
1231
1232 if (status != OK) {
1233 return status;
1234 }
1235
Christopher Wiley4db672d2015-11-10 09:44:30 -08001236 if (size < 0) {
1237 status = UNEXPECTED_NULL;
1238 return status;
1239 }
1240 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001241 status = BAD_VALUE;
1242 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001243 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001244
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001245 const void* data = readInplace(size);
1246 if (!data) {
1247 status = BAD_VALUE;
1248 return status;
1249 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001250 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001251 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001252
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001253 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001254}
1255
1256status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001257 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001258}
1259
1260status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001261 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001262}
1263
1264status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001265 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001266}
1267
1268status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001269 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001270}
1271
1272status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1273 val->clear();
1274
1275 int32_t size;
1276 status_t status = readInt32(&size);
1277
1278 if (status != OK) {
1279 return status;
1280 }
1281
1282 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001283 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001284 }
1285
1286 val->resize(size);
1287
1288 /* C++ bool handling means a vector of bools isn't necessarily addressable
1289 * (we might use individual bits)
1290 */
Christopher Wiley97887982015-10-27 16:33:47 -07001291 bool data;
1292 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001293 status = readBool(&data);
1294 (*val)[i] = data;
1295
1296 if (status != OK) {
1297 return status;
1298 }
1299 }
1300
1301 return OK;
1302}
1303
1304status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001305 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001306}
1307
1308status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001309 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001310}
1311
1312
Andreas Huber84a6d042009-08-17 13:33:27 -07001313status_t Parcel::readInt32(int32_t *pArg) const
1314{
1315 return readAligned(pArg);
1316}
1317
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001318int32_t Parcel::readInt32() const
1319{
Andreas Huber84a6d042009-08-17 13:33:27 -07001320 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321}
1322
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001323status_t Parcel::readUint32(uint32_t *pArg) const
1324{
1325 return readAligned(pArg);
1326}
1327
1328uint32_t Parcel::readUint32() const
1329{
1330 return readAligned<uint32_t>();
1331}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332
1333status_t Parcel::readInt64(int64_t *pArg) const
1334{
Andreas Huber84a6d042009-08-17 13:33:27 -07001335 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336}
1337
1338
1339int64_t Parcel::readInt64() const
1340{
Andreas Huber84a6d042009-08-17 13:33:27 -07001341 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342}
1343
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001344status_t Parcel::readUint64(uint64_t *pArg) const
1345{
1346 return readAligned(pArg);
1347}
1348
1349uint64_t Parcel::readUint64() const
1350{
1351 return readAligned<uint64_t>();
1352}
1353
Serban Constantinescuf683e012013-11-05 16:53:55 +00001354status_t Parcel::readPointer(uintptr_t *pArg) const
1355{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001356 status_t ret;
1357 binder_uintptr_t ptr;
1358 ret = readAligned(&ptr);
1359 if (!ret)
1360 *pArg = ptr;
1361 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001362}
1363
1364uintptr_t Parcel::readPointer() const
1365{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001366 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001367}
1368
1369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370status_t Parcel::readFloat(float *pArg) const
1371{
Andreas Huber84a6d042009-08-17 13:33:27 -07001372 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373}
1374
1375
1376float Parcel::readFloat() const
1377{
Andreas Huber84a6d042009-08-17 13:33:27 -07001378 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001379}
1380
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001381#if defined(__mips__) && defined(__mips_hard_float)
1382
1383status_t Parcel::readDouble(double *pArg) const
1384{
1385 union {
1386 double d;
1387 unsigned long long ll;
1388 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001389 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001390 status_t status;
1391 status = readAligned(&u.ll);
1392 *pArg = u.d;
1393 return status;
1394}
1395
1396double Parcel::readDouble() const
1397{
1398 union {
1399 double d;
1400 unsigned long long ll;
1401 } u;
1402 u.ll = readAligned<unsigned long long>();
1403 return u.d;
1404}
1405
1406#else
1407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001408status_t Parcel::readDouble(double *pArg) const
1409{
Andreas Huber84a6d042009-08-17 13:33:27 -07001410 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411}
1412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413double Parcel::readDouble() const
1414{
Andreas Huber84a6d042009-08-17 13:33:27 -07001415 return readAligned<double>();
1416}
1417
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001418#endif
1419
Andreas Huber84a6d042009-08-17 13:33:27 -07001420status_t Parcel::readIntPtr(intptr_t *pArg) const
1421{
1422 return readAligned(pArg);
1423}
1424
1425
1426intptr_t Parcel::readIntPtr() const
1427{
1428 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001429}
1430
Casey Dahlind6848f52015-10-15 15:44:59 -07001431status_t Parcel::readBool(bool *pArg) const
1432{
1433 int32_t tmp;
1434 status_t ret = readInt32(&tmp);
1435 *pArg = (tmp != 0);
1436 return ret;
1437}
1438
1439bool Parcel::readBool() const
1440{
1441 return readInt32() != 0;
1442}
1443
1444status_t Parcel::readChar(char16_t *pArg) const
1445{
1446 int32_t tmp;
1447 status_t ret = readInt32(&tmp);
1448 *pArg = char16_t(tmp);
1449 return ret;
1450}
1451
1452char16_t Parcel::readChar() const
1453{
1454 return char16_t(readInt32());
1455}
1456
1457status_t Parcel::readByte(int8_t *pArg) const
1458{
1459 int32_t tmp;
1460 status_t ret = readInt32(&tmp);
1461 *pArg = int8_t(tmp);
1462 return ret;
1463}
1464
1465int8_t Parcel::readByte() const
1466{
1467 return int8_t(readInt32());
1468}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469
1470const char* Parcel::readCString() const
1471{
1472 const size_t avail = mDataSize-mDataPos;
1473 if (avail > 0) {
1474 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1475 // is the string's trailing NUL within the parcel's valid bounds?
1476 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1477 if (eos) {
1478 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001480 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481 return str;
1482 }
1483 }
1484 return NULL;
1485}
1486
1487String8 Parcel::readString8() const
1488{
1489 int32_t size = readInt32();
1490 // watch for potential int overflow adding 1 for trailing NUL
1491 if (size > 0 && size < INT32_MAX) {
1492 const char* str = (const char*)readInplace(size+1);
1493 if (str) return String8(str, size);
1494 }
1495 return String8();
1496}
1497
1498String16 Parcel::readString16() const
1499{
1500 size_t len;
1501 const char16_t* str = readString16Inplace(&len);
1502 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001503 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001504 return String16();
1505}
1506
Casey Dahlin451ff582015-10-19 18:12:18 -07001507status_t Parcel::readString16(String16* pArg) const
1508{
1509 size_t len;
1510 const char16_t* str = readString16Inplace(&len);
1511 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001512 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001513 return 0;
1514 } else {
1515 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001516 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001517 }
1518}
1519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1521{
1522 int32_t size = readInt32();
1523 // watch for potential int overflow from size+1
1524 if (size >= 0 && size < INT32_MAX) {
1525 *outLen = size;
1526 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1527 if (str != NULL) {
1528 return str;
1529 }
1530 }
1531 *outLen = 0;
1532 return NULL;
1533}
1534
Casey Dahlinf0c13772015-10-27 18:33:56 -07001535status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1536{
1537 return unflatten_binder(ProcessState::self(), *this, val);
1538}
1539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540sp<IBinder> Parcel::readStrongBinder() const
1541{
1542 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001543 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544 return val;
1545}
1546
1547wp<IBinder> Parcel::readWeakBinder() const
1548{
1549 wp<IBinder> val;
1550 unflatten_binder(ProcessState::self(), *this, &val);
1551 return val;
1552}
1553
Christopher Wiley97f048d2015-11-19 06:49:05 -08001554status_t Parcel::readParcelable(Parcelable* parcelable) const {
1555 int32_t have_parcelable = 0;
1556 status_t status = readInt32(&have_parcelable);
1557 if (status != OK) {
1558 return status;
1559 }
1560 if (!have_parcelable) {
1561 return UNEXPECTED_NULL;
1562 }
1563 return parcelable->readFromParcel(this);
1564}
1565
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001566int32_t Parcel::readExceptionCode() const
1567{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001568 binder::Status status;
1569 status.readFromParcel(*this);
1570 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001571}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001572
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001573native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001574{
1575 int numFds, numInts;
1576 status_t err;
1577 err = readInt32(&numFds);
1578 if (err != NO_ERROR) return 0;
1579 err = readInt32(&numInts);
1580 if (err != NO_ERROR) return 0;
1581
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001582 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001583 if (!h) {
1584 return 0;
1585 }
1586
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001587 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001588 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001589 if (h->data[i] < 0) err = BAD_VALUE;
1590 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001591 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001592 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001593 native_handle_close(h);
1594 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001595 h = 0;
1596 }
1597 return h;
1598}
1599
1600
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601int Parcel::readFileDescriptor() const
1602{
1603 const flat_binder_object* flat = readObject(true);
1604 if (flat) {
1605 switch (flat->type) {
1606 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001607 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001609 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001610 }
1611 return BAD_TYPE;
1612}
1613
Jeff Brown5707dbf2011-09-23 21:17:56 -07001614status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1615{
Jeff Brown13b16042014-11-11 16:44:25 -08001616 int32_t blobType;
1617 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001618 if (status) return status;
1619
Jeff Brown13b16042014-11-11 16:44:25 -08001620 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001621 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001622 const void* ptr = readInplace(len);
1623 if (!ptr) return BAD_VALUE;
1624
Jeff Brown13b16042014-11-11 16:44:25 -08001625 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001626 return NO_ERROR;
1627 }
1628
Steve Block6807e592011-10-20 11:56:00 +01001629 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001630 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001631 int fd = readFileDescriptor();
1632 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1633
Jeff Brown13b16042014-11-11 16:44:25 -08001634 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1635 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001636 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001637
Jeff Brown13b16042014-11-11 16:44:25 -08001638 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001639 return NO_ERROR;
1640}
1641
Mathias Agopiane1424282013-07-29 21:24:40 -07001642status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001643{
1644 // size
1645 const size_t len = this->readInt32();
1646 const size_t fd_count = this->readInt32();
1647
Nick Kralevichb6b14232015-04-02 09:36:02 -07001648 if (len > INT32_MAX) {
1649 // don't accept size_t values which may have come from an
1650 // inadvertent conversion from a negative int.
1651 return BAD_VALUE;
1652 }
1653
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001654 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001655 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001656 if (buf == NULL)
1657 return BAD_VALUE;
1658
1659 int* fds = NULL;
1660 if (fd_count) {
1661 fds = new int[fd_count];
1662 }
1663
1664 status_t err = NO_ERROR;
1665 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001666 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001667 if (fds[i] < 0) {
1668 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001669 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1670 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001671 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001672 }
1673
1674 if (err == NO_ERROR) {
1675 err = val.unflatten(buf, len, fds, fd_count);
1676 }
1677
1678 if (fd_count) {
1679 delete [] fds;
1680 }
1681
1682 return err;
1683}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001684const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1685{
1686 const size_t DPOS = mDataPos;
1687 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1688 const flat_binder_object* obj
1689 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1690 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001691 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001692 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001693 // the object list, so we don't want to check for it when
1694 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001695 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001696 return obj;
1697 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001699 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001700 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701 const size_t N = mObjectsSize;
1702 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001705 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001707
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001708 // Start at the current hint position, looking for an object at
1709 // the current data position.
1710 if (opos < N) {
1711 while (opos < (N-1) && OBJS[opos] < DPOS) {
1712 opos++;
1713 }
1714 } else {
1715 opos = N-1;
1716 }
1717 if (OBJS[opos] == DPOS) {
1718 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001719 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720 this, DPOS, opos);
1721 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001722 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723 return obj;
1724 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726 // Look backwards for it...
1727 while (opos > 0 && OBJS[opos] > DPOS) {
1728 opos--;
1729 }
1730 if (OBJS[opos] == DPOS) {
1731 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001732 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001733 this, DPOS, opos);
1734 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001735 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736 return obj;
1737 }
1738 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001739 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 -07001740 this, DPOS);
1741 }
1742 return NULL;
1743}
1744
1745void Parcel::closeFileDescriptors()
1746{
1747 size_t i = mObjectsSize;
1748 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001749 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001750 }
1751 while (i > 0) {
1752 i--;
1753 const flat_binder_object* flat
1754 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1755 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001756 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001757 close(flat->handle);
1758 }
1759 }
1760}
1761
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001762uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001764 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765}
1766
1767size_t Parcel::ipcDataSize() const
1768{
1769 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1770}
1771
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001772uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001773{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001774 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775}
1776
1777size_t Parcel::ipcObjectsCount() const
1778{
1779 return mObjectsSize;
1780}
1781
1782void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001783 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001785 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 freeDataNoInit();
1787 mError = NO_ERROR;
1788 mData = const_cast<uint8_t*>(data);
1789 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001790 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001792 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001793 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001794 mObjectsSize = mObjectsCapacity = objectsCount;
1795 mNextObjectHint = 0;
1796 mOwner = relFunc;
1797 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001798 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001799 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001800 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001801 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001802 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001803 mObjectsSize = 0;
1804 break;
1805 }
1806 minOffset = offset + sizeof(flat_binder_object);
1807 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808 scanForFds();
1809}
1810
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001811void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812{
1813 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815 if (errorCheck() != NO_ERROR) {
1816 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001817 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818 } else if (dataSize() > 0) {
1819 const uint8_t* DATA = data();
1820 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001821 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822 const size_t N = objectsCount();
1823 for (size_t i=0; i<N; i++) {
1824 const flat_binder_object* flat
1825 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1826 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1827 << TypeCode(flat->type & 0x7f7f7f00)
1828 << " = " << flat->binder;
1829 }
1830 } else {
1831 to << "NULL";
1832 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001833
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834 to << ")";
1835}
1836
1837void Parcel::releaseObjects()
1838{
1839 const sp<ProcessState> proc(ProcessState::self());
1840 size_t i = mObjectsSize;
1841 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001842 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843 while (i > 0) {
1844 i--;
1845 const flat_binder_object* flat
1846 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1847 release_object(proc, *flat, this);
1848 }
1849}
1850
1851void Parcel::acquireObjects()
1852{
1853 const sp<ProcessState> proc(ProcessState::self());
1854 size_t i = mObjectsSize;
1855 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001856 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001857 while (i > 0) {
1858 i--;
1859 const flat_binder_object* flat
1860 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1861 acquire_object(proc, *flat, this);
1862 }
1863}
1864
1865void Parcel::freeData()
1866{
1867 freeDataNoInit();
1868 initState();
1869}
1870
1871void Parcel::freeDataNoInit()
1872{
1873 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001874 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001875 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1877 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001878 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001880 if (mData) {
1881 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001882 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001883 if (mDataCapacity <= gParcelGlobalAllocSize) {
1884 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1885 } else {
1886 gParcelGlobalAllocSize = 0;
1887 }
1888 if (gParcelGlobalAllocCount > 0) {
1889 gParcelGlobalAllocCount--;
1890 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001891 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001892 free(mData);
1893 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001894 if (mObjects) free(mObjects);
1895 }
1896}
1897
1898status_t Parcel::growData(size_t len)
1899{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001900 if (len > INT32_MAX) {
1901 // don't accept size_t values which may have come from an
1902 // inadvertent conversion from a negative int.
1903 return BAD_VALUE;
1904 }
1905
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001906 size_t newSize = ((mDataSize+len)*3)/2;
1907 return (newSize <= mDataSize)
1908 ? (status_t) NO_MEMORY
1909 : continueWrite(newSize);
1910}
1911
1912status_t Parcel::restartWrite(size_t desired)
1913{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001914 if (desired > INT32_MAX) {
1915 // don't accept size_t values which may have come from an
1916 // inadvertent conversion from a negative int.
1917 return BAD_VALUE;
1918 }
1919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920 if (mOwner) {
1921 freeData();
1922 return continueWrite(desired);
1923 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001924
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925 uint8_t* data = (uint8_t*)realloc(mData, desired);
1926 if (!data && desired > mDataCapacity) {
1927 mError = NO_MEMORY;
1928 return NO_MEMORY;
1929 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001932
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001933 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001934 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001935 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001936 gParcelGlobalAllocSize += desired;
1937 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001938 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 mData = data;
1940 mDataCapacity = desired;
1941 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001943 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001944 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1945 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001947 free(mObjects);
1948 mObjects = NULL;
1949 mObjectsSize = mObjectsCapacity = 0;
1950 mNextObjectHint = 0;
1951 mHasFds = false;
1952 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001953 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001954
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955 return NO_ERROR;
1956}
1957
1958status_t Parcel::continueWrite(size_t desired)
1959{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001960 if (desired > INT32_MAX) {
1961 // don't accept size_t values which may have come from an
1962 // inadvertent conversion from a negative int.
1963 return BAD_VALUE;
1964 }
1965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966 // If shrinking, first adjust for any objects that appear
1967 // after the new data size.
1968 size_t objectsSize = mObjectsSize;
1969 if (desired < mDataSize) {
1970 if (desired == 0) {
1971 objectsSize = 0;
1972 } else {
1973 while (objectsSize > 0) {
1974 if (mObjects[objectsSize-1] < desired)
1975 break;
1976 objectsSize--;
1977 }
1978 }
1979 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001981 if (mOwner) {
1982 // If the size is going to zero, just release the owner's data.
1983 if (desired == 0) {
1984 freeData();
1985 return NO_ERROR;
1986 }
1987
1988 // If there is a different owner, we need to take
1989 // posession.
1990 uint8_t* data = (uint8_t*)malloc(desired);
1991 if (!data) {
1992 mError = NO_MEMORY;
1993 return NO_MEMORY;
1994 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001995 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001998 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002000 free(data);
2001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 mError = NO_MEMORY;
2003 return NO_MEMORY;
2004 }
2005
2006 // Little hack to only acquire references on objects
2007 // we will be keeping.
2008 size_t oldObjectsSize = mObjectsSize;
2009 mObjectsSize = objectsSize;
2010 acquireObjects();
2011 mObjectsSize = oldObjectsSize;
2012 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 if (mData) {
2015 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2016 }
2017 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002018 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002020 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2022 mOwner = NULL;
2023
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002024 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002025 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002026 gParcelGlobalAllocSize += desired;
2027 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002028 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030 mData = data;
2031 mObjects = objects;
2032 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002033 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 mDataCapacity = desired;
2035 mObjectsSize = mObjectsCapacity = objectsSize;
2036 mNextObjectHint = 0;
2037
2038 } else if (mData) {
2039 if (objectsSize < mObjectsSize) {
2040 // Need to release refs on any objects we are dropping.
2041 const sp<ProcessState> proc(ProcessState::self());
2042 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2043 const flat_binder_object* flat
2044 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2045 if (flat->type == BINDER_TYPE_FD) {
2046 // will need to rescan because we may have lopped off the only FDs
2047 mFdsKnown = false;
2048 }
2049 release_object(proc, *flat, this);
2050 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002051 binder_size_t* objects =
2052 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 if (objects) {
2054 mObjects = objects;
2055 }
2056 mObjectsSize = objectsSize;
2057 mNextObjectHint = 0;
2058 }
2059
2060 // We own the data, so we can just do a realloc().
2061 if (desired > mDataCapacity) {
2062 uint8_t* data = (uint8_t*)realloc(mData, desired);
2063 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002064 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2065 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002066 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002067 gParcelGlobalAllocSize += desired;
2068 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002069 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002070 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 mData = data;
2072 mDataCapacity = desired;
2073 } else if (desired > mDataCapacity) {
2074 mError = NO_MEMORY;
2075 return NO_MEMORY;
2076 }
2077 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002078 if (mDataSize > desired) {
2079 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002080 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002081 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002082 if (mDataPos > desired) {
2083 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002084 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 }
2086 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002087
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002088 } else {
2089 // This is the first data. Easy!
2090 uint8_t* data = (uint8_t*)malloc(desired);
2091 if (!data) {
2092 mError = NO_MEMORY;
2093 return NO_MEMORY;
2094 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002095
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096 if(!(mDataCapacity == 0 && mObjects == NULL
2097 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002098 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002100
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002101 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002102 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002103 gParcelGlobalAllocSize += desired;
2104 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002105 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002106
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 mData = data;
2108 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002109 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2110 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 mDataCapacity = desired;
2112 }
2113
2114 return NO_ERROR;
2115}
2116
2117void Parcel::initState()
2118{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002119 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120 mError = NO_ERROR;
2121 mData = 0;
2122 mDataSize = 0;
2123 mDataCapacity = 0;
2124 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002125 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2126 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 mObjects = NULL;
2128 mObjectsSize = 0;
2129 mObjectsCapacity = 0;
2130 mNextObjectHint = 0;
2131 mHasFds = false;
2132 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002133 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002135 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136}
2137
2138void Parcel::scanForFds() const
2139{
2140 bool hasFds = false;
2141 for (size_t i=0; i<mObjectsSize; i++) {
2142 const flat_binder_object* flat
2143 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2144 if (flat->type == BINDER_TYPE_FD) {
2145 hasFds = true;
2146 break;
2147 }
2148 }
2149 mHasFds = hasFds;
2150 mFdsKnown = true;
2151}
2152
Dan Sandleraa5c2342015-04-10 10:08:45 -04002153size_t Parcel::getBlobAshmemSize() const
2154{
2155 return mBlobAshmemSize;
2156}
2157
Jeff Brown5707dbf2011-09-23 21:17:56 -07002158// --- Parcel::Blob ---
2159
2160Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002161 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002162}
2163
2164Parcel::Blob::~Blob() {
2165 release();
2166}
2167
2168void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002169 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002170 ::munmap(mData, mSize);
2171 }
2172 clear();
2173}
2174
Jeff Brown13b16042014-11-11 16:44:25 -08002175void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2176 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002177 mData = data;
2178 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002179 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002180}
2181
2182void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002183 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002184 mData = NULL;
2185 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002186 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002187}
2188
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189}; // namespace android