blob: 94968218e0784205da25715c455f1ec3701e1fe8 [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*/);
Casey Dahlin06673e32015-11-23 13:24:23 -0800983 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -0700984 close(dupFd);
985 }
986 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987}
988
Casey Dahlin06673e32015-11-23 13:24:23 -0800989status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
990 return writeDupFileDescriptor(fd.get());
991}
992
993status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
994 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
995}
996
Jeff Brown13b16042014-11-11 16:44:25 -0800997status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700998{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700999 if (len > INT32_MAX) {
1000 // don't accept size_t values which may have come from an
1001 // inadvertent conversion from a negative int.
1002 return BAD_VALUE;
1003 }
1004
Jeff Brown13b16042014-11-11 16:44:25 -08001005 status_t status;
1006 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001007 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001008 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001009 if (status) return status;
1010
1011 void* ptr = writeInplace(len);
1012 if (!ptr) return NO_MEMORY;
1013
Jeff Brown13b16042014-11-11 16:44:25 -08001014 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001015 return NO_ERROR;
1016 }
1017
Steve Block6807e592011-10-20 11:56:00 +01001018 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001019 int fd = ashmem_create_region("Parcel Blob", len);
1020 if (fd < 0) return NO_MEMORY;
1021
Dan Sandleraa5c2342015-04-10 10:08:45 -04001022 mBlobAshmemSize += len;
1023
Jeff Brown5707dbf2011-09-23 21:17:56 -07001024 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1025 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001026 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001027 } else {
1028 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1029 if (ptr == MAP_FAILED) {
1030 status = -errno;
1031 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001032 if (!mutableCopy) {
1033 result = ashmem_set_prot_region(fd, PROT_READ);
1034 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001035 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001036 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001037 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001038 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001039 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001040 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001041 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001042 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001043 return NO_ERROR;
1044 }
1045 }
1046 }
1047 }
1048 ::munmap(ptr, len);
1049 }
1050 ::close(fd);
1051 return status;
1052}
1053
Jeff Brown13b16042014-11-11 16:44:25 -08001054status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1055{
1056 // Must match up with what's done in writeBlob.
1057 if (!mAllowFds) return FDS_NOT_ALLOWED;
1058 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1059 if (status) return status;
1060 return writeDupFileDescriptor(fd);
1061}
1062
Mathias Agopiane1424282013-07-29 21:24:40 -07001063status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001064{
1065 status_t err;
1066
1067 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001068 const size_t len = val.getFlattenedSize();
1069 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001070
Nick Kralevichb6b14232015-04-02 09:36:02 -07001071 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1072 // don't accept size_t values which may have come from an
1073 // inadvertent conversion from a negative int.
1074 return BAD_VALUE;
1075 }
1076
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001077 err = this->writeInt32(len);
1078 if (err) return err;
1079
1080 err = this->writeInt32(fd_count);
1081 if (err) return err;
1082
1083 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001084 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001085 if (buf == NULL)
1086 return BAD_VALUE;
1087
1088 int* fds = NULL;
1089 if (fd_count) {
1090 fds = new int[fd_count];
1091 }
1092
1093 err = val.flatten(buf, len, fds, fd_count);
1094 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1095 err = this->writeDupFileDescriptor( fds[i] );
1096 }
1097
1098 if (fd_count) {
1099 delete [] fds;
1100 }
1101
1102 return err;
1103}
1104
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1106{
1107 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1108 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1109 if (enoughData && enoughObjects) {
1110restart_write:
1111 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001112
Christopher Tate98e67d32015-06-03 18:44:15 -07001113 // remember if it's a file descriptor
1114 if (val.type == BINDER_TYPE_FD) {
1115 if (!mAllowFds) {
1116 // fail before modifying our object index
1117 return FDS_NOT_ALLOWED;
1118 }
1119 mHasFds = mFdsKnown = true;
1120 }
1121
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001122 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001123 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124 mObjects[mObjectsSize] = mDataPos;
1125 acquire_object(ProcessState::self(), val, this);
1126 mObjectsSize++;
1127 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001128
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001129 return finishWrite(sizeof(flat_binder_object));
1130 }
1131
1132 if (!enoughData) {
1133 const status_t err = growData(sizeof(val));
1134 if (err != NO_ERROR) return err;
1135 }
1136 if (!enoughObjects) {
1137 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001138 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001139 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140 if (objects == NULL) return NO_MEMORY;
1141 mObjects = objects;
1142 mObjectsCapacity = newSize;
1143 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001144
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145 goto restart_write;
1146}
1147
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001148status_t Parcel::writeNoException()
1149{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001150 binder::Status status;
1151 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001152}
1153
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001154void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001155{
1156 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1157}
1158
1159status_t Parcel::read(void* outData, size_t len) const
1160{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001161 if (len > INT32_MAX) {
1162 // don't accept size_t values which may have come from an
1163 // inadvertent conversion from a negative int.
1164 return BAD_VALUE;
1165 }
1166
1167 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1168 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001170 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001171 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001172 return NO_ERROR;
1173 }
1174 return NOT_ENOUGH_DATA;
1175}
1176
1177const void* Parcel::readInplace(size_t len) const
1178{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001179 if (len > INT32_MAX) {
1180 // don't accept size_t values which may have come from an
1181 // inadvertent conversion from a negative int.
1182 return NULL;
1183 }
1184
1185 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1186 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001187 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001188 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001189 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001190 return data;
1191 }
1192 return NULL;
1193}
1194
Andreas Huber84a6d042009-08-17 13:33:27 -07001195template<class T>
1196status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001197 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001198
1199 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001201 mDataPos += sizeof(T);
1202 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203 return NO_ERROR;
1204 } else {
1205 return NOT_ENOUGH_DATA;
1206 }
1207}
1208
Andreas Huber84a6d042009-08-17 13:33:27 -07001209template<class T>
1210T Parcel::readAligned() const {
1211 T result;
1212 if (readAligned(&result) != NO_ERROR) {
1213 result = 0;
1214 }
1215
1216 return result;
1217}
1218
1219template<class T>
1220status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001221 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001222
1223 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1224restart_write:
1225 *reinterpret_cast<T*>(mData+mDataPos) = val;
1226 return finishWrite(sizeof(val));
1227 }
1228
1229 status_t err = growData(sizeof(val));
1230 if (err == NO_ERROR) goto restart_write;
1231 return err;
1232}
1233
Casey Dahlin451ff582015-10-19 18:12:18 -07001234status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1235 val->clear();
1236
1237 int32_t size;
1238 status_t status = readInt32(&size);
1239
1240 if (status != OK) {
1241 return status;
1242 }
1243
Christopher Wiley4db672d2015-11-10 09:44:30 -08001244 if (size < 0) {
1245 status = UNEXPECTED_NULL;
1246 return status;
1247 }
1248 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001249 status = BAD_VALUE;
1250 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001251 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001252
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001253 const void* data = readInplace(size);
1254 if (!data) {
1255 status = BAD_VALUE;
1256 return status;
1257 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001258 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001259 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001260
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001261 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001262}
1263
1264status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001265 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001266}
1267
1268status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001269 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001270}
1271
1272status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001273 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001274}
1275
1276status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001277 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001278}
1279
1280status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1281 val->clear();
1282
1283 int32_t size;
1284 status_t status = readInt32(&size);
1285
1286 if (status != OK) {
1287 return status;
1288 }
1289
1290 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001291 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001292 }
1293
1294 val->resize(size);
1295
1296 /* C++ bool handling means a vector of bools isn't necessarily addressable
1297 * (we might use individual bits)
1298 */
Christopher Wiley97887982015-10-27 16:33:47 -07001299 bool data;
1300 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001301 status = readBool(&data);
1302 (*val)[i] = data;
1303
1304 if (status != OK) {
1305 return status;
1306 }
1307 }
1308
1309 return OK;
1310}
1311
1312status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001313 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001314}
1315
1316status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001317 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001318}
1319
1320
Andreas Huber84a6d042009-08-17 13:33:27 -07001321status_t Parcel::readInt32(int32_t *pArg) const
1322{
1323 return readAligned(pArg);
1324}
1325
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326int32_t Parcel::readInt32() const
1327{
Andreas Huber84a6d042009-08-17 13:33:27 -07001328 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329}
1330
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001331status_t Parcel::readUint32(uint32_t *pArg) const
1332{
1333 return readAligned(pArg);
1334}
1335
1336uint32_t Parcel::readUint32() const
1337{
1338 return readAligned<uint32_t>();
1339}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001340
1341status_t Parcel::readInt64(int64_t *pArg) const
1342{
Andreas Huber84a6d042009-08-17 13:33:27 -07001343 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344}
1345
1346
1347int64_t Parcel::readInt64() const
1348{
Andreas Huber84a6d042009-08-17 13:33:27 -07001349 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350}
1351
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001352status_t Parcel::readUint64(uint64_t *pArg) const
1353{
1354 return readAligned(pArg);
1355}
1356
1357uint64_t Parcel::readUint64() const
1358{
1359 return readAligned<uint64_t>();
1360}
1361
Serban Constantinescuf683e012013-11-05 16:53:55 +00001362status_t Parcel::readPointer(uintptr_t *pArg) const
1363{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001364 status_t ret;
1365 binder_uintptr_t ptr;
1366 ret = readAligned(&ptr);
1367 if (!ret)
1368 *pArg = ptr;
1369 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001370}
1371
1372uintptr_t Parcel::readPointer() const
1373{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001374 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001375}
1376
1377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001378status_t Parcel::readFloat(float *pArg) const
1379{
Andreas Huber84a6d042009-08-17 13:33:27 -07001380 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001381}
1382
1383
1384float Parcel::readFloat() const
1385{
Andreas Huber84a6d042009-08-17 13:33:27 -07001386 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001387}
1388
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001389#if defined(__mips__) && defined(__mips_hard_float)
1390
1391status_t Parcel::readDouble(double *pArg) const
1392{
1393 union {
1394 double d;
1395 unsigned long long ll;
1396 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001397 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001398 status_t status;
1399 status = readAligned(&u.ll);
1400 *pArg = u.d;
1401 return status;
1402}
1403
1404double Parcel::readDouble() const
1405{
1406 union {
1407 double d;
1408 unsigned long long ll;
1409 } u;
1410 u.ll = readAligned<unsigned long long>();
1411 return u.d;
1412}
1413
1414#else
1415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001416status_t Parcel::readDouble(double *pArg) const
1417{
Andreas Huber84a6d042009-08-17 13:33:27 -07001418 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001419}
1420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421double Parcel::readDouble() const
1422{
Andreas Huber84a6d042009-08-17 13:33:27 -07001423 return readAligned<double>();
1424}
1425
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001426#endif
1427
Andreas Huber84a6d042009-08-17 13:33:27 -07001428status_t Parcel::readIntPtr(intptr_t *pArg) const
1429{
1430 return readAligned(pArg);
1431}
1432
1433
1434intptr_t Parcel::readIntPtr() const
1435{
1436 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001437}
1438
Casey Dahlind6848f52015-10-15 15:44:59 -07001439status_t Parcel::readBool(bool *pArg) const
1440{
1441 int32_t tmp;
1442 status_t ret = readInt32(&tmp);
1443 *pArg = (tmp != 0);
1444 return ret;
1445}
1446
1447bool Parcel::readBool() const
1448{
1449 return readInt32() != 0;
1450}
1451
1452status_t Parcel::readChar(char16_t *pArg) const
1453{
1454 int32_t tmp;
1455 status_t ret = readInt32(&tmp);
1456 *pArg = char16_t(tmp);
1457 return ret;
1458}
1459
1460char16_t Parcel::readChar() const
1461{
1462 return char16_t(readInt32());
1463}
1464
1465status_t Parcel::readByte(int8_t *pArg) const
1466{
1467 int32_t tmp;
1468 status_t ret = readInt32(&tmp);
1469 *pArg = int8_t(tmp);
1470 return ret;
1471}
1472
1473int8_t Parcel::readByte() const
1474{
1475 return int8_t(readInt32());
1476}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477
1478const char* Parcel::readCString() const
1479{
1480 const size_t avail = mDataSize-mDataPos;
1481 if (avail > 0) {
1482 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1483 // is the string's trailing NUL within the parcel's valid bounds?
1484 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1485 if (eos) {
1486 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001487 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001488 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489 return str;
1490 }
1491 }
1492 return NULL;
1493}
1494
1495String8 Parcel::readString8() const
1496{
1497 int32_t size = readInt32();
1498 // watch for potential int overflow adding 1 for trailing NUL
1499 if (size > 0 && size < INT32_MAX) {
1500 const char* str = (const char*)readInplace(size+1);
1501 if (str) return String8(str, size);
1502 }
1503 return String8();
1504}
1505
1506String16 Parcel::readString16() const
1507{
1508 size_t len;
1509 const char16_t* str = readString16Inplace(&len);
1510 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001511 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512 return String16();
1513}
1514
Casey Dahlin451ff582015-10-19 18:12:18 -07001515status_t Parcel::readString16(String16* pArg) const
1516{
1517 size_t len;
1518 const char16_t* str = readString16Inplace(&len);
1519 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001520 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001521 return 0;
1522 } else {
1523 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001524 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001525 }
1526}
1527
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001528const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1529{
1530 int32_t size = readInt32();
1531 // watch for potential int overflow from size+1
1532 if (size >= 0 && size < INT32_MAX) {
1533 *outLen = size;
1534 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1535 if (str != NULL) {
1536 return str;
1537 }
1538 }
1539 *outLen = 0;
1540 return NULL;
1541}
1542
Casey Dahlinf0c13772015-10-27 18:33:56 -07001543status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1544{
1545 return unflatten_binder(ProcessState::self(), *this, val);
1546}
1547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548sp<IBinder> Parcel::readStrongBinder() const
1549{
1550 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001551 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552 return val;
1553}
1554
1555wp<IBinder> Parcel::readWeakBinder() const
1556{
1557 wp<IBinder> val;
1558 unflatten_binder(ProcessState::self(), *this, &val);
1559 return val;
1560}
1561
Christopher Wiley97f048d2015-11-19 06:49:05 -08001562status_t Parcel::readParcelable(Parcelable* parcelable) const {
1563 int32_t have_parcelable = 0;
1564 status_t status = readInt32(&have_parcelable);
1565 if (status != OK) {
1566 return status;
1567 }
1568 if (!have_parcelable) {
1569 return UNEXPECTED_NULL;
1570 }
1571 return parcelable->readFromParcel(this);
1572}
1573
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001574int32_t Parcel::readExceptionCode() const
1575{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001576 binder::Status status;
1577 status.readFromParcel(*this);
1578 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001579}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001580
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001581native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001582{
1583 int numFds, numInts;
1584 status_t err;
1585 err = readInt32(&numFds);
1586 if (err != NO_ERROR) return 0;
1587 err = readInt32(&numInts);
1588 if (err != NO_ERROR) return 0;
1589
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001590 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001591 if (!h) {
1592 return 0;
1593 }
1594
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001595 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001596 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001597 if (h->data[i] < 0) err = BAD_VALUE;
1598 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001599 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001600 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001601 native_handle_close(h);
1602 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001603 h = 0;
1604 }
1605 return h;
1606}
1607
1608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609int Parcel::readFileDescriptor() const
1610{
1611 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001612
1613 if (flat && flat->type == BINDER_TYPE_FD) {
1614 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001616
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617 return BAD_TYPE;
1618}
1619
Casey Dahlin06673e32015-11-23 13:24:23 -08001620status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1621{
1622 int got = readFileDescriptor();
1623
1624 if (got == BAD_TYPE) {
1625 return BAD_TYPE;
1626 }
1627
1628 val->reset(dup(got));
1629
1630 if (val->get() < 0) {
1631 return BAD_VALUE;
1632 }
1633
1634 return OK;
1635}
1636
1637
1638status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1639 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1640}
1641
Jeff Brown5707dbf2011-09-23 21:17:56 -07001642status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1643{
Jeff Brown13b16042014-11-11 16:44:25 -08001644 int32_t blobType;
1645 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001646 if (status) return status;
1647
Jeff Brown13b16042014-11-11 16:44:25 -08001648 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001649 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001650 const void* ptr = readInplace(len);
1651 if (!ptr) return BAD_VALUE;
1652
Jeff Brown13b16042014-11-11 16:44:25 -08001653 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001654 return NO_ERROR;
1655 }
1656
Steve Block6807e592011-10-20 11:56:00 +01001657 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001658 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001659 int fd = readFileDescriptor();
1660 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1661
Jeff Brown13b16042014-11-11 16:44:25 -08001662 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1663 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001664 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001665
Jeff Brown13b16042014-11-11 16:44:25 -08001666 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001667 return NO_ERROR;
1668}
1669
Mathias Agopiane1424282013-07-29 21:24:40 -07001670status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001671{
1672 // size
1673 const size_t len = this->readInt32();
1674 const size_t fd_count = this->readInt32();
1675
Nick Kralevichb6b14232015-04-02 09:36:02 -07001676 if (len > INT32_MAX) {
1677 // don't accept size_t values which may have come from an
1678 // inadvertent conversion from a negative int.
1679 return BAD_VALUE;
1680 }
1681
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001682 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001683 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001684 if (buf == NULL)
1685 return BAD_VALUE;
1686
1687 int* fds = NULL;
1688 if (fd_count) {
1689 fds = new int[fd_count];
1690 }
1691
1692 status_t err = NO_ERROR;
1693 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001694 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001695 if (fds[i] < 0) {
1696 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001697 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1698 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001699 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001700 }
1701
1702 if (err == NO_ERROR) {
1703 err = val.unflatten(buf, len, fds, fd_count);
1704 }
1705
1706 if (fd_count) {
1707 delete [] fds;
1708 }
1709
1710 return err;
1711}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001712const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1713{
1714 const size_t DPOS = mDataPos;
1715 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1716 const flat_binder_object* obj
1717 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1718 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001719 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001720 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001721 // the object list, so we don't want to check for it when
1722 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001723 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724 return obj;
1725 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001728 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729 const size_t N = mObjectsSize;
1730 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001733 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736 // Start at the current hint position, looking for an object at
1737 // the current data position.
1738 if (opos < N) {
1739 while (opos < (N-1) && OBJS[opos] < DPOS) {
1740 opos++;
1741 }
1742 } else {
1743 opos = N-1;
1744 }
1745 if (OBJS[opos] == DPOS) {
1746 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001747 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 this, DPOS, opos);
1749 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001750 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751 return obj;
1752 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754 // Look backwards for it...
1755 while (opos > 0 && OBJS[opos] > DPOS) {
1756 opos--;
1757 }
1758 if (OBJS[opos] == DPOS) {
1759 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001760 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001761 this, DPOS, opos);
1762 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001763 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764 return obj;
1765 }
1766 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001767 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 -07001768 this, DPOS);
1769 }
1770 return NULL;
1771}
1772
1773void Parcel::closeFileDescriptors()
1774{
1775 size_t i = mObjectsSize;
1776 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001777 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001778 }
1779 while (i > 0) {
1780 i--;
1781 const flat_binder_object* flat
1782 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1783 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001784 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785 close(flat->handle);
1786 }
1787 }
1788}
1789
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001790uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001792 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793}
1794
1795size_t Parcel::ipcDataSize() const
1796{
1797 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1798}
1799
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001800uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001801{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001802 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803}
1804
1805size_t Parcel::ipcObjectsCount() const
1806{
1807 return mObjectsSize;
1808}
1809
1810void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001811 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001813 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 freeDataNoInit();
1815 mError = NO_ERROR;
1816 mData = const_cast<uint8_t*>(data);
1817 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001818 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001819 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001820 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001821 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001822 mObjectsSize = mObjectsCapacity = objectsCount;
1823 mNextObjectHint = 0;
1824 mOwner = relFunc;
1825 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001826 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001827 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001828 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001829 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001830 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001831 mObjectsSize = 0;
1832 break;
1833 }
1834 minOffset = offset + sizeof(flat_binder_object);
1835 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836 scanForFds();
1837}
1838
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001839void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840{
1841 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001842
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843 if (errorCheck() != NO_ERROR) {
1844 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001845 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846 } else if (dataSize() > 0) {
1847 const uint8_t* DATA = data();
1848 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001849 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850 const size_t N = objectsCount();
1851 for (size_t i=0; i<N; i++) {
1852 const flat_binder_object* flat
1853 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1854 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1855 << TypeCode(flat->type & 0x7f7f7f00)
1856 << " = " << flat->binder;
1857 }
1858 } else {
1859 to << "NULL";
1860 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001861
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001862 to << ")";
1863}
1864
1865void Parcel::releaseObjects()
1866{
1867 const sp<ProcessState> proc(ProcessState::self());
1868 size_t i = mObjectsSize;
1869 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001870 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 while (i > 0) {
1872 i--;
1873 const flat_binder_object* flat
1874 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1875 release_object(proc, *flat, this);
1876 }
1877}
1878
1879void Parcel::acquireObjects()
1880{
1881 const sp<ProcessState> proc(ProcessState::self());
1882 size_t i = mObjectsSize;
1883 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001884 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 while (i > 0) {
1886 i--;
1887 const flat_binder_object* flat
1888 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1889 acquire_object(proc, *flat, this);
1890 }
1891}
1892
1893void Parcel::freeData()
1894{
1895 freeDataNoInit();
1896 initState();
1897}
1898
1899void Parcel::freeDataNoInit()
1900{
1901 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001902 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001903 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001904 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1905 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001906 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001907 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001908 if (mData) {
1909 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001910 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001911 if (mDataCapacity <= gParcelGlobalAllocSize) {
1912 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1913 } else {
1914 gParcelGlobalAllocSize = 0;
1915 }
1916 if (gParcelGlobalAllocCount > 0) {
1917 gParcelGlobalAllocCount--;
1918 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001919 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001920 free(mData);
1921 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922 if (mObjects) free(mObjects);
1923 }
1924}
1925
1926status_t Parcel::growData(size_t len)
1927{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001928 if (len > INT32_MAX) {
1929 // don't accept size_t values which may have come from an
1930 // inadvertent conversion from a negative int.
1931 return BAD_VALUE;
1932 }
1933
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001934 size_t newSize = ((mDataSize+len)*3)/2;
1935 return (newSize <= mDataSize)
1936 ? (status_t) NO_MEMORY
1937 : continueWrite(newSize);
1938}
1939
1940status_t Parcel::restartWrite(size_t desired)
1941{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001942 if (desired > INT32_MAX) {
1943 // don't accept size_t values which may have come from an
1944 // inadvertent conversion from a negative int.
1945 return BAD_VALUE;
1946 }
1947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001948 if (mOwner) {
1949 freeData();
1950 return continueWrite(desired);
1951 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001952
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953 uint8_t* data = (uint8_t*)realloc(mData, desired);
1954 if (!data && desired > mDataCapacity) {
1955 mError = NO_MEMORY;
1956 return NO_MEMORY;
1957 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001958
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001959 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001960
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001962 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001963 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001964 gParcelGlobalAllocSize += desired;
1965 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001966 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967 mData = data;
1968 mDataCapacity = desired;
1969 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001970
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001972 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1973 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975 free(mObjects);
1976 mObjects = NULL;
1977 mObjectsSize = mObjectsCapacity = 0;
1978 mNextObjectHint = 0;
1979 mHasFds = false;
1980 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001981 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001982
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 return NO_ERROR;
1984}
1985
1986status_t Parcel::continueWrite(size_t desired)
1987{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001988 if (desired > INT32_MAX) {
1989 // don't accept size_t values which may have come from an
1990 // inadvertent conversion from a negative int.
1991 return BAD_VALUE;
1992 }
1993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 // If shrinking, first adjust for any objects that appear
1995 // after the new data size.
1996 size_t objectsSize = mObjectsSize;
1997 if (desired < mDataSize) {
1998 if (desired == 0) {
1999 objectsSize = 0;
2000 } else {
2001 while (objectsSize > 0) {
2002 if (mObjects[objectsSize-1] < desired)
2003 break;
2004 objectsSize--;
2005 }
2006 }
2007 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002008
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002009 if (mOwner) {
2010 // If the size is going to zero, just release the owner's data.
2011 if (desired == 0) {
2012 freeData();
2013 return NO_ERROR;
2014 }
2015
2016 // If there is a different owner, we need to take
2017 // posession.
2018 uint8_t* data = (uint8_t*)malloc(desired);
2019 if (!data) {
2020 mError = NO_MEMORY;
2021 return NO_MEMORY;
2022 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002023 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002026 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002027 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002028 free(data);
2029
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030 mError = NO_MEMORY;
2031 return NO_MEMORY;
2032 }
2033
2034 // Little hack to only acquire references on objects
2035 // we will be keeping.
2036 size_t oldObjectsSize = mObjectsSize;
2037 mObjectsSize = objectsSize;
2038 acquireObjects();
2039 mObjectsSize = oldObjectsSize;
2040 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042 if (mData) {
2043 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2044 }
2045 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002046 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002048 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2050 mOwner = NULL;
2051
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002052 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002053 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002054 gParcelGlobalAllocSize += desired;
2055 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002056 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 mData = data;
2059 mObjects = objects;
2060 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002061 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062 mDataCapacity = desired;
2063 mObjectsSize = mObjectsCapacity = objectsSize;
2064 mNextObjectHint = 0;
2065
2066 } else if (mData) {
2067 if (objectsSize < mObjectsSize) {
2068 // Need to release refs on any objects we are dropping.
2069 const sp<ProcessState> proc(ProcessState::self());
2070 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2071 const flat_binder_object* flat
2072 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2073 if (flat->type == BINDER_TYPE_FD) {
2074 // will need to rescan because we may have lopped off the only FDs
2075 mFdsKnown = false;
2076 }
2077 release_object(proc, *flat, this);
2078 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002079 binder_size_t* objects =
2080 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002081 if (objects) {
2082 mObjects = objects;
2083 }
2084 mObjectsSize = objectsSize;
2085 mNextObjectHint = 0;
2086 }
2087
2088 // We own the data, so we can just do a realloc().
2089 if (desired > mDataCapacity) {
2090 uint8_t* data = (uint8_t*)realloc(mData, desired);
2091 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002092 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2093 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002094 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002095 gParcelGlobalAllocSize += desired;
2096 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002097 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002098 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 mData = data;
2100 mDataCapacity = desired;
2101 } else if (desired > mDataCapacity) {
2102 mError = NO_MEMORY;
2103 return NO_MEMORY;
2104 }
2105 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002106 if (mDataSize > desired) {
2107 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002108 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002109 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 if (mDataPos > desired) {
2111 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002112 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 }
2114 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 } else {
2117 // This is the first data. Easy!
2118 uint8_t* data = (uint8_t*)malloc(desired);
2119 if (!data) {
2120 mError = NO_MEMORY;
2121 return NO_MEMORY;
2122 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 if(!(mDataCapacity == 0 && mObjects == NULL
2125 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002126 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002128
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002129 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002130 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002131 gParcelGlobalAllocSize += desired;
2132 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002133 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 mData = data;
2136 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2138 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 mDataCapacity = desired;
2140 }
2141
2142 return NO_ERROR;
2143}
2144
2145void Parcel::initState()
2146{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002147 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 mError = NO_ERROR;
2149 mData = 0;
2150 mDataSize = 0;
2151 mDataCapacity = 0;
2152 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2154 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 mObjects = NULL;
2156 mObjectsSize = 0;
2157 mObjectsCapacity = 0;
2158 mNextObjectHint = 0;
2159 mHasFds = false;
2160 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002161 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002163 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164}
2165
2166void Parcel::scanForFds() const
2167{
2168 bool hasFds = false;
2169 for (size_t i=0; i<mObjectsSize; i++) {
2170 const flat_binder_object* flat
2171 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2172 if (flat->type == BINDER_TYPE_FD) {
2173 hasFds = true;
2174 break;
2175 }
2176 }
2177 mHasFds = hasFds;
2178 mFdsKnown = true;
2179}
2180
Dan Sandleraa5c2342015-04-10 10:08:45 -04002181size_t Parcel::getBlobAshmemSize() const
2182{
2183 return mBlobAshmemSize;
2184}
2185
Jeff Brown5707dbf2011-09-23 21:17:56 -07002186// --- Parcel::Blob ---
2187
2188Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002189 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002190}
2191
2192Parcel::Blob::~Blob() {
2193 release();
2194}
2195
2196void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002197 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002198 ::munmap(mData, mSize);
2199 }
2200 clear();
2201}
2202
Jeff Brown13b16042014-11-11 16:44:25 -08002203void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2204 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002205 mData = data;
2206 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002207 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002208}
2209
2210void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002211 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002212 mData = NULL;
2213 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002214 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002215}
2216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217}; // namespace android