blob: c1e6f0e949d49b0308aabdbe433a3054376ee325 [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
Christopher Wiley4db672d2015-11-10 09:44:30 -0800311namespace {
312
313template<typename T>
314status_t readTypedVector(std::vector<T>* val, const Parcel* p,
315 status_t(Parcel::*read_func)(T*) const) {
316 val->clear();
317
318 int32_t size;
319 status_t status = p->readInt32(&size);
320
321 if (status != OK) {
322 return status;
323 }
324
325 if (size < 0) {
326 return UNEXPECTED_NULL;
327 }
328
329 val->resize(size);
330
331 for (auto& v: *val) {
332 status = (p->*read_func)(&v);
333
334 if (status != OK) {
335 return status;
336 }
337 }
338
339 return OK;
340}
341
342} // namespace
343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700344// ---------------------------------------------------------------------------
345
346Parcel::Parcel()
347{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800348 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349 initState();
350}
351
352Parcel::~Parcel()
353{
354 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800355 LOG_ALLOC("Parcel %p: destroyed", this);
356}
357
358size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800359 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
360 size_t size = gParcelGlobalAllocSize;
361 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
362 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363}
364
365size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800366 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
367 size_t count = gParcelGlobalAllocCount;
368 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
369 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700370}
371
372const uint8_t* Parcel::data() const
373{
374 return mData;
375}
376
377size_t Parcel::dataSize() const
378{
379 return (mDataSize > mDataPos ? mDataSize : mDataPos);
380}
381
382size_t Parcel::dataAvail() const
383{
384 // TODO: decide what to do about the possibility that this can
385 // report an available-data size that exceeds a Java int's max
386 // positive value, causing havoc. Fortunately this will only
387 // happen if someone constructs a Parcel containing more than two
388 // gigabytes of data, which on typical phone hardware is simply
389 // not possible.
390 return dataSize() - dataPosition();
391}
392
393size_t Parcel::dataPosition() const
394{
395 return mDataPos;
396}
397
398size_t Parcel::dataCapacity() const
399{
400 return mDataCapacity;
401}
402
403status_t Parcel::setDataSize(size_t size)
404{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700405 if (size > INT32_MAX) {
406 // don't accept size_t values which may have come from an
407 // inadvertent conversion from a negative int.
408 return BAD_VALUE;
409 }
410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 status_t err;
412 err = continueWrite(size);
413 if (err == NO_ERROR) {
414 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700415 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 }
417 return err;
418}
419
420void Parcel::setDataPosition(size_t pos) const
421{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700422 if (pos > INT32_MAX) {
423 // don't accept size_t values which may have come from an
424 // inadvertent conversion from a negative int.
425 abort();
426 }
427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700428 mDataPos = pos;
429 mNextObjectHint = 0;
430}
431
432status_t Parcel::setDataCapacity(size_t size)
433{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700434 if (size > INT32_MAX) {
435 // don't accept size_t values which may have come from an
436 // inadvertent conversion from a negative int.
437 return BAD_VALUE;
438 }
439
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700440 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700441 return NO_ERROR;
442}
443
444status_t Parcel::setData(const uint8_t* buffer, size_t len)
445{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700446 if (len > INT32_MAX) {
447 // don't accept size_t values which may have come from an
448 // inadvertent conversion from a negative int.
449 return BAD_VALUE;
450 }
451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 status_t err = restartWrite(len);
453 if (err == NO_ERROR) {
454 memcpy(const_cast<uint8_t*>(data()), buffer, len);
455 mDataSize = len;
456 mFdsKnown = false;
457 }
458 return err;
459}
460
Andreas Huber51faf462011-04-13 10:21:56 -0700461status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462{
463 const sp<ProcessState> proc(ProcessState::self());
464 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700465 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800466 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 size_t size = parcel->mObjectsSize;
468 int startPos = mDataPos;
469 int firstIndex = -1, lastIndex = -2;
470
471 if (len == 0) {
472 return NO_ERROR;
473 }
474
Nick Kralevichb6b14232015-04-02 09:36:02 -0700475 if (len > INT32_MAX) {
476 // don't accept size_t values which may have come from an
477 // inadvertent conversion from a negative int.
478 return BAD_VALUE;
479 }
480
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481 // range checks against the source parcel size
482 if ((offset > parcel->mDataSize)
483 || (len > parcel->mDataSize)
484 || (offset + len > parcel->mDataSize)) {
485 return BAD_VALUE;
486 }
487
488 // Count objects in range
489 for (int i = 0; i < (int) size; i++) {
490 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700491 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700492 if (firstIndex == -1) {
493 firstIndex = i;
494 }
495 lastIndex = i;
496 }
497 }
498 int numObjects = lastIndex - firstIndex + 1;
499
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700500 if ((mDataSize+len) > mDataCapacity) {
501 // grow data
502 err = growData(len);
503 if (err != NO_ERROR) {
504 return err;
505 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700506 }
507
508 // append data
509 memcpy(mData + mDataPos, data + offset, len);
510 mDataPos += len;
511 mDataSize += len;
512
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400513 err = NO_ERROR;
514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700515 if (numObjects > 0) {
516 // grow objects
517 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700518 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
519 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800520 binder_size_t *objects =
521 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
522 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 return NO_MEMORY;
524 }
525 mObjects = objects;
526 mObjectsCapacity = newSize;
527 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700528
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 // append and acquire objects
530 int idx = mObjectsSize;
531 for (int i = firstIndex; i <= lastIndex; i++) {
532 size_t off = objects[i] - offset + startPos;
533 mObjects[idx++] = off;
534 mObjectsSize++;
535
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700536 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 = reinterpret_cast<flat_binder_object*>(mData + off);
538 acquire_object(proc, *flat, this);
539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700541 // If this is a file descriptor, we need to dup it so the
542 // new Parcel now owns its own fd, and can declare that we
543 // officially know we have fds.
544 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800545 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 if (!mAllowFds) {
548 err = FDS_NOT_ALLOWED;
549 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550 }
551 }
552 }
553
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 return err;
555}
556
Jeff Brown13b16042014-11-11 16:44:25 -0800557bool Parcel::allowFds() const
558{
559 return mAllowFds;
560}
561
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700562bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400563{
564 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700565 if (!allowFds) {
566 mAllowFds = false;
567 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400568 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569}
570
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700571void Parcel::restoreAllowFds(bool lastValue)
572{
573 mAllowFds = lastValue;
574}
575
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576bool Parcel::hasFileDescriptors() const
577{
578 if (!mFdsKnown) {
579 scanForFds();
580 }
581 return mHasFds;
582}
583
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700584// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700585status_t Parcel::writeInterfaceToken(const String16& interface)
586{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700587 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
588 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700589 // currently the interface identification token is just its name as a string
590 return writeString16(interface);
591}
592
Mathias Agopian83c04462009-05-22 19:00:22 -0700593bool Parcel::checkInterface(IBinder* binder) const
594{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700595 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700596}
597
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700598bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700599 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700601 int32_t strictPolicy = readInt32();
602 if (threadState == NULL) {
603 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700604 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700605 if ((threadState->getLastTransactionBinderFlags() &
606 IBinder::FLAG_ONEWAY) != 0) {
607 // For one-way calls, the callee is running entirely
608 // disconnected from the caller, so disable StrictMode entirely.
609 // Not only does disk/network usage not impact the caller, but
610 // there's no way to commuicate back any violations anyway.
611 threadState->setStrictModePolicy(0);
612 } else {
613 threadState->setStrictModePolicy(strictPolicy);
614 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700615 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 if (str == interface) {
617 return true;
618 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700619 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 String8(interface).string(), String8(str).string());
621 return false;
622 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700623}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700624
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800625const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626{
627 return mObjects;
628}
629
630size_t Parcel::objectsCount() const
631{
632 return mObjectsSize;
633}
634
635status_t Parcel::errorCheck() const
636{
637 return mError;
638}
639
640void Parcel::setError(status_t err)
641{
642 mError = err;
643}
644
645status_t Parcel::finishWrite(size_t len)
646{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700647 if (len > INT32_MAX) {
648 // don't accept size_t values which may have come from an
649 // inadvertent conversion from a negative int.
650 return BAD_VALUE;
651 }
652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 //printf("Finish write of %d\n", len);
654 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700655 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656 if (mDataPos > mDataSize) {
657 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700658 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659 }
660 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
661 return NO_ERROR;
662}
663
664status_t Parcel::writeUnpadded(const void* data, size_t len)
665{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700666 if (len > INT32_MAX) {
667 // don't accept size_t values which may have come from an
668 // inadvertent conversion from a negative int.
669 return BAD_VALUE;
670 }
671
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700672 size_t end = mDataPos + len;
673 if (end < mDataPos) {
674 // integer overflow
675 return BAD_VALUE;
676 }
677
678 if (end <= mDataCapacity) {
679restart_write:
680 memcpy(mData+mDataPos, data, len);
681 return finishWrite(len);
682 }
683
684 status_t err = growData(len);
685 if (err == NO_ERROR) goto restart_write;
686 return err;
687}
688
689status_t Parcel::write(const void* data, size_t len)
690{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700691 if (len > INT32_MAX) {
692 // don't accept size_t values which may have come from an
693 // inadvertent conversion from a negative int.
694 return BAD_VALUE;
695 }
696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 void* const d = writeInplace(len);
698 if (d) {
699 memcpy(d, data, len);
700 return NO_ERROR;
701 }
702 return mError;
703}
704
705void* Parcel::writeInplace(size_t len)
706{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700707 if (len > INT32_MAX) {
708 // don't accept size_t values which may have come from an
709 // inadvertent conversion from a negative int.
710 return NULL;
711 }
712
713 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700714
715 // sanity check for integer overflow
716 if (mDataPos+padded < mDataPos) {
717 return NULL;
718 }
719
720 if ((mDataPos+padded) <= mDataCapacity) {
721restart_write:
722 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
723 uint8_t* const data = mData+mDataPos;
724
725 // Need to pad at end?
726 if (padded != len) {
727#if BYTE_ORDER == BIG_ENDIAN
728 static const uint32_t mask[4] = {
729 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
730 };
731#endif
732#if BYTE_ORDER == LITTLE_ENDIAN
733 static const uint32_t mask[4] = {
734 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
735 };
736#endif
737 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
738 // *reinterpret_cast<void**>(data+padded-4));
739 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
740 }
741
742 finishWrite(padded);
743 return data;
744 }
745
746 status_t err = growData(padded);
747 if (err == NO_ERROR) goto restart_write;
748 return NULL;
749}
750
Casey Dahlin5f062562015-11-13 13:46:29 -0800751namespace {
752
753template<typename T, typename U>
754status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p,
755 status_t(Parcel::*write_func)(U)) {
756 if (val.size() > std::numeric_limits<int32_t>::max()) {
757 return BAD_VALUE;
758 }
759
760 status_t status = p->writeInt32(val.size());
761
762 if (status != OK) {
763 return status;
764 }
765
766 for (const auto& item : val) {
767 status = (p->*write_func)(item);
768
769 if (status != OK) {
770 return status;
771 }
772 }
773
774 return OK;
775}
776
777template<typename T>
778status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
779 status_t(Parcel::*write_func)(const T&)) {
780 return unsafeWriteTypedVector(val, p, write_func);
781}
782
783template<typename T>
784status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
785 status_t(Parcel::*write_func)(T)) {
786 return unsafeWriteTypedVector(val, p, write_func);
787}
788
789} // namespace
790
Casey Dahlin451ff582015-10-19 18:12:18 -0700791status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
792{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700793 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700794 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700795 status = BAD_VALUE;
796 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700797 }
798
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700799 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700800 if (status != OK) {
801 return status;
802 }
803
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700804 void* data = writeInplace(val.size());
805 if (!data) {
806 status = BAD_VALUE;
807 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700808 }
809
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700810 memcpy(data, val.data(), val.size());
811 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700812}
813
814status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
815{
Casey Dahlin5f062562015-11-13 13:46:29 -0800816 return writeTypedVector(val, this, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700817}
818
819status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
820{
Casey Dahlin5f062562015-11-13 13:46:29 -0800821 return writeTypedVector(val, this, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700822}
823
824status_t Parcel::writeFloatVector(const std::vector<float>& val)
825{
Casey Dahlin5f062562015-11-13 13:46:29 -0800826 return writeTypedVector(val, this, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700827}
828
829status_t Parcel::writeDoubleVector(const std::vector<double>& val)
830{
Casey Dahlin5f062562015-11-13 13:46:29 -0800831 return writeTypedVector(val, this, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700832}
833
834status_t Parcel::writeBoolVector(const std::vector<bool>& val)
835{
Casey Dahlin5f062562015-11-13 13:46:29 -0800836 return writeTypedVector(val, this, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700837}
838
839status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
840{
Casey Dahlin5f062562015-11-13 13:46:29 -0800841 return writeTypedVector(val, this, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700842}
843
844status_t Parcel::writeString16Vector(const std::vector<String16>& val)
845{
Casey Dahlin5f062562015-11-13 13:46:29 -0800846 return writeTypedVector(val, this, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700847}
848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700849status_t Parcel::writeInt32(int32_t val)
850{
Andreas Huber84a6d042009-08-17 13:33:27 -0700851 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700852}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800853
854status_t Parcel::writeUint32(uint32_t val)
855{
856 return writeAligned(val);
857}
858
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700859status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700860 if (len > INT32_MAX) {
861 // don't accept size_t values which may have come from an
862 // inadvertent conversion from a negative int.
863 return BAD_VALUE;
864 }
865
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700866 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700867 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700868 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700869 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700870 if (ret == NO_ERROR) {
871 ret = write(val, len * sizeof(*val));
872 }
873 return ret;
874}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700875status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700876 if (len > INT32_MAX) {
877 // don't accept size_t values which may have come from an
878 // inadvertent conversion from a negative int.
879 return BAD_VALUE;
880 }
881
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700882 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700883 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700884 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700885 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700886 if (ret == NO_ERROR) {
887 ret = write(val, len * sizeof(*val));
888 }
889 return ret;
890}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700891
Casey Dahlind6848f52015-10-15 15:44:59 -0700892status_t Parcel::writeBool(bool val)
893{
894 return writeInt32(int32_t(val));
895}
896
897status_t Parcel::writeChar(char16_t val)
898{
899 return writeInt32(int32_t(val));
900}
901
902status_t Parcel::writeByte(int8_t val)
903{
904 return writeInt32(int32_t(val));
905}
906
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700907status_t Parcel::writeInt64(int64_t val)
908{
Andreas Huber84a6d042009-08-17 13:33:27 -0700909 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910}
911
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700912status_t Parcel::writeUint64(uint64_t val)
913{
914 return writeAligned(val);
915}
916
Serban Constantinescuf683e012013-11-05 16:53:55 +0000917status_t Parcel::writePointer(uintptr_t val)
918{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800919 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000920}
921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700922status_t Parcel::writeFloat(float val)
923{
Andreas Huber84a6d042009-08-17 13:33:27 -0700924 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925}
926
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800927#if defined(__mips__) && defined(__mips_hard_float)
928
929status_t Parcel::writeDouble(double val)
930{
931 union {
932 double d;
933 unsigned long long ll;
934 } u;
935 u.d = val;
936 return writeAligned(u.ll);
937}
938
939#else
940
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700941status_t Parcel::writeDouble(double val)
942{
Andreas Huber84a6d042009-08-17 13:33:27 -0700943 return writeAligned(val);
944}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700945
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800946#endif
947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948status_t Parcel::writeCString(const char* str)
949{
950 return write(str, strlen(str)+1);
951}
952
953status_t Parcel::writeString8(const String8& str)
954{
955 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100956 // only write string if its length is more than zero characters,
957 // as readString8 will only read if the length field is non-zero.
958 // this is slightly different from how writeString16 works.
959 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700960 err = write(str.string(), str.bytes()+1);
961 }
962 return err;
963}
964
965status_t Parcel::writeString16(const String16& str)
966{
967 return writeString16(str.string(), str.size());
968}
969
970status_t Parcel::writeString16(const char16_t* str, size_t len)
971{
972 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974 status_t err = writeInt32(len);
975 if (err == NO_ERROR) {
976 len *= sizeof(char16_t);
977 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
978 if (data) {
979 memcpy(data, str, len);
980 *reinterpret_cast<char16_t*>(data+len) = 0;
981 return NO_ERROR;
982 }
983 err = mError;
984 }
985 return err;
986}
987
988status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
989{
990 return flatten_binder(ProcessState::self(), val, this);
991}
992
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800993status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
994{
Casey Dahlin5f062562015-11-13 13:46:29 -0800995 return writeTypedVector(val, this, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800996}
997
998status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -0800999 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001000}
1001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1003{
1004 return flatten_binder(ProcessState::self(), val, this);
1005}
1006
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001007status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001008{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001009 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001010 return BAD_TYPE;
1011
1012 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001013 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001014 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001016 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001017 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001019 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1020 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021
1022 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001023 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 return err;
1025 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001026 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001027 return err;
1028}
1029
Jeff Brown93ff1f92011-11-04 19:01:44 -07001030status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001031{
1032 flat_binder_object obj;
1033 obj.type = BINDER_TYPE_FD;
1034 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001035 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001037 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038 return writeObject(obj, true);
1039}
1040
1041status_t Parcel::writeDupFileDescriptor(int fd)
1042{
Jeff Brownd341c712011-11-04 20:19:33 -07001043 int dupFd = dup(fd);
1044 if (dupFd < 0) {
1045 return -errno;
1046 }
1047 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1048 if (err) {
1049 close(dupFd);
1050 }
1051 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052}
1053
Jeff Brown13b16042014-11-11 16:44:25 -08001054status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001055{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001056 if (len > INT32_MAX) {
1057 // don't accept size_t values which may have come from an
1058 // inadvertent conversion from a negative int.
1059 return BAD_VALUE;
1060 }
1061
Jeff Brown13b16042014-11-11 16:44:25 -08001062 status_t status;
1063 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001064 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001065 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001066 if (status) return status;
1067
1068 void* ptr = writeInplace(len);
1069 if (!ptr) return NO_MEMORY;
1070
Jeff Brown13b16042014-11-11 16:44:25 -08001071 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001072 return NO_ERROR;
1073 }
1074
Steve Block6807e592011-10-20 11:56:00 +01001075 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001076 int fd = ashmem_create_region("Parcel Blob", len);
1077 if (fd < 0) return NO_MEMORY;
1078
Dan Sandleraa5c2342015-04-10 10:08:45 -04001079 mBlobAshmemSize += len;
1080
Jeff Brown5707dbf2011-09-23 21:17:56 -07001081 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1082 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001083 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001084 } else {
1085 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1086 if (ptr == MAP_FAILED) {
1087 status = -errno;
1088 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001089 if (!mutableCopy) {
1090 result = ashmem_set_prot_region(fd, PROT_READ);
1091 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001092 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001093 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001094 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001095 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001096 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001097 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001098 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001099 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001100 return NO_ERROR;
1101 }
1102 }
1103 }
1104 }
1105 ::munmap(ptr, len);
1106 }
1107 ::close(fd);
1108 return status;
1109}
1110
Jeff Brown13b16042014-11-11 16:44:25 -08001111status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1112{
1113 // Must match up with what's done in writeBlob.
1114 if (!mAllowFds) return FDS_NOT_ALLOWED;
1115 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1116 if (status) return status;
1117 return writeDupFileDescriptor(fd);
1118}
1119
Mathias Agopiane1424282013-07-29 21:24:40 -07001120status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001121{
1122 status_t err;
1123
1124 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001125 const size_t len = val.getFlattenedSize();
1126 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001127
Nick Kralevichb6b14232015-04-02 09:36:02 -07001128 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1129 // don't accept size_t values which may have come from an
1130 // inadvertent conversion from a negative int.
1131 return BAD_VALUE;
1132 }
1133
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001134 err = this->writeInt32(len);
1135 if (err) return err;
1136
1137 err = this->writeInt32(fd_count);
1138 if (err) return err;
1139
1140 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001141 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001142 if (buf == NULL)
1143 return BAD_VALUE;
1144
1145 int* fds = NULL;
1146 if (fd_count) {
1147 fds = new int[fd_count];
1148 }
1149
1150 err = val.flatten(buf, len, fds, fd_count);
1151 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1152 err = this->writeDupFileDescriptor( fds[i] );
1153 }
1154
1155 if (fd_count) {
1156 delete [] fds;
1157 }
1158
1159 return err;
1160}
1161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001162status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1163{
1164 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1165 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1166 if (enoughData && enoughObjects) {
1167restart_write:
1168 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001169
Christopher Tate98e67d32015-06-03 18:44:15 -07001170 // remember if it's a file descriptor
1171 if (val.type == BINDER_TYPE_FD) {
1172 if (!mAllowFds) {
1173 // fail before modifying our object index
1174 return FDS_NOT_ALLOWED;
1175 }
1176 mHasFds = mFdsKnown = true;
1177 }
1178
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001180 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181 mObjects[mObjectsSize] = mDataPos;
1182 acquire_object(ProcessState::self(), val, this);
1183 mObjectsSize++;
1184 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001186 return finishWrite(sizeof(flat_binder_object));
1187 }
1188
1189 if (!enoughData) {
1190 const status_t err = growData(sizeof(val));
1191 if (err != NO_ERROR) return err;
1192 }
1193 if (!enoughObjects) {
1194 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001195 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001196 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197 if (objects == NULL) return NO_MEMORY;
1198 mObjects = objects;
1199 mObjectsCapacity = newSize;
1200 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001201
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001202 goto restart_write;
1203}
1204
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001205status_t Parcel::writeNoException()
1206{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001207 binder::Status status;
1208 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001209}
1210
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001211void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212{
1213 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1214}
1215
1216status_t Parcel::read(void* outData, size_t len) const
1217{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001218 if (len > INT32_MAX) {
1219 // don't accept size_t values which may have come from an
1220 // inadvertent conversion from a negative int.
1221 return BAD_VALUE;
1222 }
1223
1224 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1225 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001226 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001227 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001228 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 return NO_ERROR;
1230 }
1231 return NOT_ENOUGH_DATA;
1232}
1233
1234const void* Parcel::readInplace(size_t len) const
1235{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001236 if (len > INT32_MAX) {
1237 // don't accept size_t values which may have come from an
1238 // inadvertent conversion from a negative int.
1239 return NULL;
1240 }
1241
1242 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1243 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001244 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001245 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001246 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247 return data;
1248 }
1249 return NULL;
1250}
1251
Andreas Huber84a6d042009-08-17 13:33:27 -07001252template<class T>
1253status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001254 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001255
1256 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001257 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001258 mDataPos += sizeof(T);
1259 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001260 return NO_ERROR;
1261 } else {
1262 return NOT_ENOUGH_DATA;
1263 }
1264}
1265
Andreas Huber84a6d042009-08-17 13:33:27 -07001266template<class T>
1267T Parcel::readAligned() const {
1268 T result;
1269 if (readAligned(&result) != NO_ERROR) {
1270 result = 0;
1271 }
1272
1273 return result;
1274}
1275
1276template<class T>
1277status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001278 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001279
1280 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1281restart_write:
1282 *reinterpret_cast<T*>(mData+mDataPos) = val;
1283 return finishWrite(sizeof(val));
1284 }
1285
1286 status_t err = growData(sizeof(val));
1287 if (err == NO_ERROR) goto restart_write;
1288 return err;
1289}
1290
Casey Dahlin451ff582015-10-19 18:12:18 -07001291status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1292 val->clear();
1293
1294 int32_t size;
1295 status_t status = readInt32(&size);
1296
1297 if (status != OK) {
1298 return status;
1299 }
1300
Christopher Wiley4db672d2015-11-10 09:44:30 -08001301 if (size < 0) {
1302 status = UNEXPECTED_NULL;
1303 return status;
1304 }
1305 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001306 status = BAD_VALUE;
1307 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001308 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001309
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001310 const void* data = readInplace(size);
1311 if (!data) {
1312 status = BAD_VALUE;
1313 return status;
1314 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001315 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001316 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001317
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001318 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001319}
1320
1321status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001322 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001323}
1324
1325status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001326 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001327}
1328
1329status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001330 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001331}
1332
1333status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001334 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001335}
1336
1337status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1338 val->clear();
1339
1340 int32_t size;
1341 status_t status = readInt32(&size);
1342
1343 if (status != OK) {
1344 return status;
1345 }
1346
1347 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001348 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001349 }
1350
1351 val->resize(size);
1352
1353 /* C++ bool handling means a vector of bools isn't necessarily addressable
1354 * (we might use individual bits)
1355 */
Christopher Wiley97887982015-10-27 16:33:47 -07001356 bool data;
1357 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001358 status = readBool(&data);
1359 (*val)[i] = data;
1360
1361 if (status != OK) {
1362 return status;
1363 }
1364 }
1365
1366 return OK;
1367}
1368
1369status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001370 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001371}
1372
1373status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001374 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001375}
1376
1377
Andreas Huber84a6d042009-08-17 13:33:27 -07001378status_t Parcel::readInt32(int32_t *pArg) const
1379{
1380 return readAligned(pArg);
1381}
1382
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001383int32_t Parcel::readInt32() const
1384{
Andreas Huber84a6d042009-08-17 13:33:27 -07001385 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386}
1387
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001388status_t Parcel::readUint32(uint32_t *pArg) const
1389{
1390 return readAligned(pArg);
1391}
1392
1393uint32_t Parcel::readUint32() const
1394{
1395 return readAligned<uint32_t>();
1396}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397
1398status_t Parcel::readInt64(int64_t *pArg) const
1399{
Andreas Huber84a6d042009-08-17 13:33:27 -07001400 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001401}
1402
1403
1404int64_t Parcel::readInt64() const
1405{
Andreas Huber84a6d042009-08-17 13:33:27 -07001406 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407}
1408
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001409status_t Parcel::readUint64(uint64_t *pArg) const
1410{
1411 return readAligned(pArg);
1412}
1413
1414uint64_t Parcel::readUint64() const
1415{
1416 return readAligned<uint64_t>();
1417}
1418
Serban Constantinescuf683e012013-11-05 16:53:55 +00001419status_t Parcel::readPointer(uintptr_t *pArg) const
1420{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001421 status_t ret;
1422 binder_uintptr_t ptr;
1423 ret = readAligned(&ptr);
1424 if (!ret)
1425 *pArg = ptr;
1426 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001427}
1428
1429uintptr_t Parcel::readPointer() const
1430{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001431 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001432}
1433
1434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001435status_t Parcel::readFloat(float *pArg) const
1436{
Andreas Huber84a6d042009-08-17 13:33:27 -07001437 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438}
1439
1440
1441float Parcel::readFloat() const
1442{
Andreas Huber84a6d042009-08-17 13:33:27 -07001443 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444}
1445
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001446#if defined(__mips__) && defined(__mips_hard_float)
1447
1448status_t Parcel::readDouble(double *pArg) const
1449{
1450 union {
1451 double d;
1452 unsigned long long ll;
1453 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001454 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001455 status_t status;
1456 status = readAligned(&u.ll);
1457 *pArg = u.d;
1458 return status;
1459}
1460
1461double Parcel::readDouble() const
1462{
1463 union {
1464 double d;
1465 unsigned long long ll;
1466 } u;
1467 u.ll = readAligned<unsigned long long>();
1468 return u.d;
1469}
1470
1471#else
1472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001473status_t Parcel::readDouble(double *pArg) const
1474{
Andreas Huber84a6d042009-08-17 13:33:27 -07001475 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001476}
1477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478double Parcel::readDouble() const
1479{
Andreas Huber84a6d042009-08-17 13:33:27 -07001480 return readAligned<double>();
1481}
1482
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001483#endif
1484
Andreas Huber84a6d042009-08-17 13:33:27 -07001485status_t Parcel::readIntPtr(intptr_t *pArg) const
1486{
1487 return readAligned(pArg);
1488}
1489
1490
1491intptr_t Parcel::readIntPtr() const
1492{
1493 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001494}
1495
Casey Dahlind6848f52015-10-15 15:44:59 -07001496status_t Parcel::readBool(bool *pArg) const
1497{
1498 int32_t tmp;
1499 status_t ret = readInt32(&tmp);
1500 *pArg = (tmp != 0);
1501 return ret;
1502}
1503
1504bool Parcel::readBool() const
1505{
1506 return readInt32() != 0;
1507}
1508
1509status_t Parcel::readChar(char16_t *pArg) const
1510{
1511 int32_t tmp;
1512 status_t ret = readInt32(&tmp);
1513 *pArg = char16_t(tmp);
1514 return ret;
1515}
1516
1517char16_t Parcel::readChar() const
1518{
1519 return char16_t(readInt32());
1520}
1521
1522status_t Parcel::readByte(int8_t *pArg) const
1523{
1524 int32_t tmp;
1525 status_t ret = readInt32(&tmp);
1526 *pArg = int8_t(tmp);
1527 return ret;
1528}
1529
1530int8_t Parcel::readByte() const
1531{
1532 return int8_t(readInt32());
1533}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001534
1535const char* Parcel::readCString() const
1536{
1537 const size_t avail = mDataSize-mDataPos;
1538 if (avail > 0) {
1539 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1540 // is the string's trailing NUL within the parcel's valid bounds?
1541 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1542 if (eos) {
1543 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001544 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001545 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546 return str;
1547 }
1548 }
1549 return NULL;
1550}
1551
1552String8 Parcel::readString8() const
1553{
1554 int32_t size = readInt32();
1555 // watch for potential int overflow adding 1 for trailing NUL
1556 if (size > 0 && size < INT32_MAX) {
1557 const char* str = (const char*)readInplace(size+1);
1558 if (str) return String8(str, size);
1559 }
1560 return String8();
1561}
1562
1563String16 Parcel::readString16() const
1564{
1565 size_t len;
1566 const char16_t* str = readString16Inplace(&len);
1567 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001568 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001569 return String16();
1570}
1571
Casey Dahlin451ff582015-10-19 18:12:18 -07001572status_t Parcel::readString16(String16* pArg) const
1573{
1574 size_t len;
1575 const char16_t* str = readString16Inplace(&len);
1576 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001577 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001578 return 0;
1579 } else {
1580 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001581 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001582 }
1583}
1584
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1586{
1587 int32_t size = readInt32();
1588 // watch for potential int overflow from size+1
1589 if (size >= 0 && size < INT32_MAX) {
1590 *outLen = size;
1591 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1592 if (str != NULL) {
1593 return str;
1594 }
1595 }
1596 *outLen = 0;
1597 return NULL;
1598}
1599
Casey Dahlinf0c13772015-10-27 18:33:56 -07001600status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1601{
1602 return unflatten_binder(ProcessState::self(), *this, val);
1603}
1604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605sp<IBinder> Parcel::readStrongBinder() const
1606{
1607 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001608 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609 return val;
1610}
1611
1612wp<IBinder> Parcel::readWeakBinder() const
1613{
1614 wp<IBinder> val;
1615 unflatten_binder(ProcessState::self(), *this, &val);
1616 return val;
1617}
1618
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001619int32_t Parcel::readExceptionCode() const
1620{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001621 binder::Status status;
1622 status.readFromParcel(*this);
1623 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001624}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001625
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001626native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001627{
1628 int numFds, numInts;
1629 status_t err;
1630 err = readInt32(&numFds);
1631 if (err != NO_ERROR) return 0;
1632 err = readInt32(&numInts);
1633 if (err != NO_ERROR) return 0;
1634
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001635 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001636 if (!h) {
1637 return 0;
1638 }
1639
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001640 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001641 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001642 if (h->data[i] < 0) err = BAD_VALUE;
1643 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001644 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001645 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001646 native_handle_close(h);
1647 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001648 h = 0;
1649 }
1650 return h;
1651}
1652
1653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001654int Parcel::readFileDescriptor() const
1655{
1656 const flat_binder_object* flat = readObject(true);
1657 if (flat) {
1658 switch (flat->type) {
1659 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001660 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001661 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001662 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663 }
1664 return BAD_TYPE;
1665}
1666
Jeff Brown5707dbf2011-09-23 21:17:56 -07001667status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1668{
Jeff Brown13b16042014-11-11 16:44:25 -08001669 int32_t blobType;
1670 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001671 if (status) return status;
1672
Jeff Brown13b16042014-11-11 16:44:25 -08001673 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001674 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001675 const void* ptr = readInplace(len);
1676 if (!ptr) return BAD_VALUE;
1677
Jeff Brown13b16042014-11-11 16:44:25 -08001678 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001679 return NO_ERROR;
1680 }
1681
Steve Block6807e592011-10-20 11:56:00 +01001682 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001683 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001684 int fd = readFileDescriptor();
1685 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1686
Jeff Brown13b16042014-11-11 16:44:25 -08001687 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1688 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001689 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001690
Jeff Brown13b16042014-11-11 16:44:25 -08001691 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001692 return NO_ERROR;
1693}
1694
Mathias Agopiane1424282013-07-29 21:24:40 -07001695status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001696{
1697 // size
1698 const size_t len = this->readInt32();
1699 const size_t fd_count = this->readInt32();
1700
Nick Kralevichb6b14232015-04-02 09:36:02 -07001701 if (len > INT32_MAX) {
1702 // don't accept size_t values which may have come from an
1703 // inadvertent conversion from a negative int.
1704 return BAD_VALUE;
1705 }
1706
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001707 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001708 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001709 if (buf == NULL)
1710 return BAD_VALUE;
1711
1712 int* fds = NULL;
1713 if (fd_count) {
1714 fds = new int[fd_count];
1715 }
1716
1717 status_t err = NO_ERROR;
1718 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001719 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001720 if (fds[i] < 0) {
1721 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001722 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1723 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001724 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001725 }
1726
1727 if (err == NO_ERROR) {
1728 err = val.unflatten(buf, len, fds, fd_count);
1729 }
1730
1731 if (fd_count) {
1732 delete [] fds;
1733 }
1734
1735 return err;
1736}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001737const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1738{
1739 const size_t DPOS = mDataPos;
1740 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1741 const flat_binder_object* obj
1742 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1743 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001744 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001745 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001746 // the object list, so we don't want to check for it when
1747 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001748 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001749 return obj;
1750 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001751
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001752 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001753 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754 const size_t N = mObjectsSize;
1755 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001756
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001757 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001758 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001759 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001760
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001761 // Start at the current hint position, looking for an object at
1762 // the current data position.
1763 if (opos < N) {
1764 while (opos < (N-1) && OBJS[opos] < DPOS) {
1765 opos++;
1766 }
1767 } else {
1768 opos = N-1;
1769 }
1770 if (OBJS[opos] == DPOS) {
1771 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001772 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001773 this, DPOS, opos);
1774 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001775 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001776 return obj;
1777 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001778
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001779 // Look backwards for it...
1780 while (opos > 0 && OBJS[opos] > DPOS) {
1781 opos--;
1782 }
1783 if (OBJS[opos] == DPOS) {
1784 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001785 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 this, DPOS, opos);
1787 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001788 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001789 return obj;
1790 }
1791 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001792 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 -07001793 this, DPOS);
1794 }
1795 return NULL;
1796}
1797
1798void Parcel::closeFileDescriptors()
1799{
1800 size_t i = mObjectsSize;
1801 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001802 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 }
1804 while (i > 0) {
1805 i--;
1806 const flat_binder_object* flat
1807 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1808 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001809 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 close(flat->handle);
1811 }
1812 }
1813}
1814
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001815uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001817 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818}
1819
1820size_t Parcel::ipcDataSize() const
1821{
1822 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1823}
1824
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001825uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001827 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828}
1829
1830size_t Parcel::ipcObjectsCount() const
1831{
1832 return mObjectsSize;
1833}
1834
1835void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001836 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001838 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839 freeDataNoInit();
1840 mError = NO_ERROR;
1841 mData = const_cast<uint8_t*>(data);
1842 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001843 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001845 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001846 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847 mObjectsSize = mObjectsCapacity = objectsCount;
1848 mNextObjectHint = 0;
1849 mOwner = relFunc;
1850 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001851 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001852 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001853 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001854 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001855 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001856 mObjectsSize = 0;
1857 break;
1858 }
1859 minOffset = offset + sizeof(flat_binder_object);
1860 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861 scanForFds();
1862}
1863
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001864void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865{
1866 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001867
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 if (errorCheck() != NO_ERROR) {
1869 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001870 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 } else if (dataSize() > 0) {
1872 const uint8_t* DATA = data();
1873 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001874 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875 const size_t N = objectsCount();
1876 for (size_t i=0; i<N; i++) {
1877 const flat_binder_object* flat
1878 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1879 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1880 << TypeCode(flat->type & 0x7f7f7f00)
1881 << " = " << flat->binder;
1882 }
1883 } else {
1884 to << "NULL";
1885 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001886
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001887 to << ")";
1888}
1889
1890void Parcel::releaseObjects()
1891{
1892 const sp<ProcessState> proc(ProcessState::self());
1893 size_t i = mObjectsSize;
1894 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001895 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 while (i > 0) {
1897 i--;
1898 const flat_binder_object* flat
1899 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1900 release_object(proc, *flat, this);
1901 }
1902}
1903
1904void Parcel::acquireObjects()
1905{
1906 const sp<ProcessState> proc(ProcessState::self());
1907 size_t i = mObjectsSize;
1908 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001909 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910 while (i > 0) {
1911 i--;
1912 const flat_binder_object* flat
1913 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1914 acquire_object(proc, *flat, this);
1915 }
1916}
1917
1918void Parcel::freeData()
1919{
1920 freeDataNoInit();
1921 initState();
1922}
1923
1924void Parcel::freeDataNoInit()
1925{
1926 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001927 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001928 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001929 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1930 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001931 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001933 if (mData) {
1934 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001935 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001936 if (mDataCapacity <= gParcelGlobalAllocSize) {
1937 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1938 } else {
1939 gParcelGlobalAllocSize = 0;
1940 }
1941 if (gParcelGlobalAllocCount > 0) {
1942 gParcelGlobalAllocCount--;
1943 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001944 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001945 free(mData);
1946 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001947 if (mObjects) free(mObjects);
1948 }
1949}
1950
1951status_t Parcel::growData(size_t len)
1952{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001953 if (len > INT32_MAX) {
1954 // don't accept size_t values which may have come from an
1955 // inadvertent conversion from a negative int.
1956 return BAD_VALUE;
1957 }
1958
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001959 size_t newSize = ((mDataSize+len)*3)/2;
1960 return (newSize <= mDataSize)
1961 ? (status_t) NO_MEMORY
1962 : continueWrite(newSize);
1963}
1964
1965status_t Parcel::restartWrite(size_t desired)
1966{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001967 if (desired > INT32_MAX) {
1968 // don't accept size_t values which may have come from an
1969 // inadvertent conversion from a negative int.
1970 return BAD_VALUE;
1971 }
1972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973 if (mOwner) {
1974 freeData();
1975 return continueWrite(desired);
1976 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001977
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978 uint8_t* data = (uint8_t*)realloc(mData, desired);
1979 if (!data && desired > mDataCapacity) {
1980 mError = NO_MEMORY;
1981 return NO_MEMORY;
1982 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001983
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001985
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001987 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001988 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001989 gParcelGlobalAllocSize += desired;
1990 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001991 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992 mData = data;
1993 mDataCapacity = desired;
1994 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001997 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1998 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002000 free(mObjects);
2001 mObjects = NULL;
2002 mObjectsSize = mObjectsCapacity = 0;
2003 mNextObjectHint = 0;
2004 mHasFds = false;
2005 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002006 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008 return NO_ERROR;
2009}
2010
2011status_t Parcel::continueWrite(size_t desired)
2012{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002013 if (desired > INT32_MAX) {
2014 // don't accept size_t values which may have come from an
2015 // inadvertent conversion from a negative int.
2016 return BAD_VALUE;
2017 }
2018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019 // If shrinking, first adjust for any objects that appear
2020 // after the new data size.
2021 size_t objectsSize = mObjectsSize;
2022 if (desired < mDataSize) {
2023 if (desired == 0) {
2024 objectsSize = 0;
2025 } else {
2026 while (objectsSize > 0) {
2027 if (mObjects[objectsSize-1] < desired)
2028 break;
2029 objectsSize--;
2030 }
2031 }
2032 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 if (mOwner) {
2035 // If the size is going to zero, just release the owner's data.
2036 if (desired == 0) {
2037 freeData();
2038 return NO_ERROR;
2039 }
2040
2041 // If there is a different owner, we need to take
2042 // posession.
2043 uint8_t* data = (uint8_t*)malloc(desired);
2044 if (!data) {
2045 mError = NO_MEMORY;
2046 return NO_MEMORY;
2047 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002048 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002051 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002053 free(data);
2054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055 mError = NO_MEMORY;
2056 return NO_MEMORY;
2057 }
2058
2059 // Little hack to only acquire references on objects
2060 // we will be keeping.
2061 size_t oldObjectsSize = mObjectsSize;
2062 mObjectsSize = objectsSize;
2063 acquireObjects();
2064 mObjectsSize = oldObjectsSize;
2065 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 if (mData) {
2068 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2069 }
2070 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002071 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002073 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2075 mOwner = NULL;
2076
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002077 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002078 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002079 gParcelGlobalAllocSize += desired;
2080 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002081 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 mData = data;
2084 mObjects = objects;
2085 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002086 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087 mDataCapacity = desired;
2088 mObjectsSize = mObjectsCapacity = objectsSize;
2089 mNextObjectHint = 0;
2090
2091 } else if (mData) {
2092 if (objectsSize < mObjectsSize) {
2093 // Need to release refs on any objects we are dropping.
2094 const sp<ProcessState> proc(ProcessState::self());
2095 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2096 const flat_binder_object* flat
2097 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2098 if (flat->type == BINDER_TYPE_FD) {
2099 // will need to rescan because we may have lopped off the only FDs
2100 mFdsKnown = false;
2101 }
2102 release_object(proc, *flat, this);
2103 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002104 binder_size_t* objects =
2105 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106 if (objects) {
2107 mObjects = objects;
2108 }
2109 mObjectsSize = objectsSize;
2110 mNextObjectHint = 0;
2111 }
2112
2113 // We own the data, so we can just do a realloc().
2114 if (desired > mDataCapacity) {
2115 uint8_t* data = (uint8_t*)realloc(mData, desired);
2116 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002117 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2118 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002119 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002120 gParcelGlobalAllocSize += desired;
2121 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002122 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002123 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 mData = data;
2125 mDataCapacity = desired;
2126 } else if (desired > mDataCapacity) {
2127 mError = NO_MEMORY;
2128 return NO_MEMORY;
2129 }
2130 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002131 if (mDataSize > desired) {
2132 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002133 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 if (mDataPos > desired) {
2136 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002137 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138 }
2139 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 } else {
2142 // This is the first data. Easy!
2143 uint8_t* data = (uint8_t*)malloc(desired);
2144 if (!data) {
2145 mError = NO_MEMORY;
2146 return NO_MEMORY;
2147 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 if(!(mDataCapacity == 0 && mObjects == NULL
2150 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002151 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002154 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002155 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002156 gParcelGlobalAllocSize += desired;
2157 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002158 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 mData = data;
2161 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002162 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2163 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 mDataCapacity = desired;
2165 }
2166
2167 return NO_ERROR;
2168}
2169
2170void Parcel::initState()
2171{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002172 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 mError = NO_ERROR;
2174 mData = 0;
2175 mDataSize = 0;
2176 mDataCapacity = 0;
2177 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002178 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2179 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 mObjects = NULL;
2181 mObjectsSize = 0;
2182 mObjectsCapacity = 0;
2183 mNextObjectHint = 0;
2184 mHasFds = false;
2185 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002186 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002188 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189}
2190
2191void Parcel::scanForFds() const
2192{
2193 bool hasFds = false;
2194 for (size_t i=0; i<mObjectsSize; i++) {
2195 const flat_binder_object* flat
2196 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2197 if (flat->type == BINDER_TYPE_FD) {
2198 hasFds = true;
2199 break;
2200 }
2201 }
2202 mHasFds = hasFds;
2203 mFdsKnown = true;
2204}
2205
Dan Sandleraa5c2342015-04-10 10:08:45 -04002206size_t Parcel::getBlobAshmemSize() const
2207{
2208 return mBlobAshmemSize;
2209}
2210
Jeff Brown5707dbf2011-09-23 21:17:56 -07002211// --- Parcel::Blob ---
2212
2213Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002214 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002215}
2216
2217Parcel::Blob::~Blob() {
2218 release();
2219}
2220
2221void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002222 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002223 ::munmap(mData, mSize);
2224 }
2225 clear();
2226}
2227
Jeff Brown13b16042014-11-11 16:44:25 -08002228void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2229 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002230 mData = data;
2231 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002232 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002233}
2234
2235void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002236 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002237 mData = NULL;
2238 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002239 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002240}
2241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242}; // namespace android