blob: a5f7d89bd840f3e33fdc6ebbc9ac3c633e47abe2 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080026#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070027#include <binder/TextOutput.h>
28
Jun Jiangabf8a2c2014-04-29 14:22:10 +080029#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070030#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/Log.h>
32#include <utils/String8.h>
33#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080035#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070036#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Mathias Agopian208059f2009-05-18 15:08:03 -070038#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080039#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080041#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070045#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47#ifndef INT32_MAX
48#define INT32_MAX ((int32_t)(2147483647))
49#endif
50
51#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010052//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080053#define LOG_ALLOC(...)
54//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
56// ---------------------------------------------------------------------------
57
Nick Kralevichb6b14232015-04-02 09:36:02 -070058// This macro should never be used at runtime, as a too large value
59// of s could cause an integer overflow. Instead, you should always
60// use the wrapper function pad_size()
61#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
62
63static size_t pad_size(size_t s) {
64 if (s > (SIZE_T_MAX - 3)) {
65 abort();
66 }
67 return PAD_SIZE_UNSAFE(s);
68}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070069
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080071#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073// XXX This can be made public if we want to provide
74// support for typed data.
75struct small_flat_data
76{
77 uint32_t type;
78 uint32_t data;
79};
80
81namespace android {
82
Dianne Hackborna4cff882014-11-13 17:07:40 -080083static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
84static size_t gParcelGlobalAllocSize = 0;
85static size_t gParcelGlobalAllocCount = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void acquire_object(const sp<ProcessState>& proc,
97 const flat_binder_object& obj, const void* who)
98{
99 switch (obj.type) {
100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
112 if (b != NULL) {
113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
120 if (b != NULL) b.get_refs()->incWeak(who);
121 return;
122 }
123 case BINDER_TYPE_FD: {
124 // intentionally blank -- nothing to do to acquire this, but we do
125 // recognize it as a legitimate object type.
126 return;
127 }
128 }
129
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800130 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131}
132
133void release_object(const sp<ProcessState>& proc,
134 const flat_binder_object& obj, const void* who)
135{
136 switch (obj.type) {
137 case BINDER_TYPE_BINDER:
138 if (obj.binder) {
139 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800140 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 }
142 return;
143 case BINDER_TYPE_WEAK_BINDER:
144 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800145 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 return;
147 case BINDER_TYPE_HANDLE: {
148 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
149 if (b != NULL) {
150 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
151 b->decStrong(who);
152 }
153 return;
154 }
155 case BINDER_TYPE_WEAK_HANDLE: {
156 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
157 if (b != NULL) b.get_refs()->decWeak(who);
158 return;
159 }
160 case BINDER_TYPE_FD: {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800161 if (obj.cookie != 0) close(obj.handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 return;
163 }
164 }
165
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800166 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700167}
168
169inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800170 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171{
172 return out->writeObject(flat, false);
173}
174
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800175status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700176 const sp<IBinder>& binder, Parcel* out)
177{
178 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700180 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
181 if (binder != NULL) {
182 IBinder *local = binder->localBinder();
183 if (!local) {
184 BpBinder *proxy = binder->remoteBinder();
185 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000186 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700187 }
188 const int32_t handle = proxy ? proxy->handle() : 0;
189 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800190 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800192 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700193 } else {
194 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800195 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
196 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 }
198 } else {
199 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800200 obj.binder = 0;
201 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700203
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 return finish_flatten_binder(binder, obj, out);
205}
206
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 const wp<IBinder>& binder, Parcel* out)
209{
210 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
213 if (binder != NULL) {
214 sp<IBinder> real = binder.promote();
215 if (real != NULL) {
216 IBinder *local = real->localBinder();
217 if (!local) {
218 BpBinder *proxy = real->remoteBinder();
219 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000220 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 }
222 const int32_t handle = proxy ? proxy->handle() : 0;
223 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800224 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800226 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 } else {
228 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
230 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
232 return finish_flatten_binder(real, obj, out);
233 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700234
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 // XXX How to deal? In order to flatten the given binder,
236 // we need to probe it for information, which requires a primary
237 // reference... but we don't have one.
238 //
239 // The OpenBinder implementation uses a dynamic_cast<> here,
240 // but we can't do that with the different reference counting
241 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000242 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800244 obj.binder = 0;
245 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 } else {
249 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800250 obj.binder = 0;
251 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 return finish_flatten_binder(NULL, obj, out);
253 }
254}
255
256inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800257 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
258 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259{
260 return NO_ERROR;
261}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263status_t unflatten_binder(const sp<ProcessState>& proc,
264 const Parcel& in, sp<IBinder>* out)
265{
266 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700267
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 if (flat) {
269 switch (flat->type) {
270 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 return finish_unflatten_binder(NULL, *flat, in);
273 case BINDER_TYPE_HANDLE:
274 *out = proc->getStrongProxyForHandle(flat->handle);
275 return finish_unflatten_binder(
276 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 }
279 return BAD_TYPE;
280}
281
282status_t unflatten_binder(const sp<ProcessState>& proc,
283 const Parcel& in, wp<IBinder>* out)
284{
285 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 if (flat) {
288 switch (flat->type) {
289 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800290 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291 return finish_unflatten_binder(NULL, *flat, in);
292 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800293 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800295 reinterpret_cast<IBinder*>(flat->cookie),
296 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 } else {
298 *out = NULL;
299 }
300 return finish_unflatten_binder(NULL, *flat, in);
301 case BINDER_TYPE_HANDLE:
302 case BINDER_TYPE_WEAK_HANDLE:
303 *out = proc->getWeakProxyForHandle(flat->handle);
304 return finish_unflatten_binder(
305 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
306 }
307 }
308 return BAD_TYPE;
309}
310
311// ---------------------------------------------------------------------------
312
313Parcel::Parcel()
314{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800315 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 initState();
317}
318
319Parcel::~Parcel()
320{
321 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800322 LOG_ALLOC("Parcel %p: destroyed", this);
323}
324
325size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800326 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
327 size_t size = gParcelGlobalAllocSize;
328 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
329 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800330}
331
332size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800333 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
334 size_t count = gParcelGlobalAllocCount;
335 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
336 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339const uint8_t* Parcel::data() const
340{
341 return mData;
342}
343
344size_t Parcel::dataSize() const
345{
346 return (mDataSize > mDataPos ? mDataSize : mDataPos);
347}
348
349size_t Parcel::dataAvail() const
350{
351 // TODO: decide what to do about the possibility that this can
352 // report an available-data size that exceeds a Java int's max
353 // positive value, causing havoc. Fortunately this will only
354 // happen if someone constructs a Parcel containing more than two
355 // gigabytes of data, which on typical phone hardware is simply
356 // not possible.
357 return dataSize() - dataPosition();
358}
359
360size_t Parcel::dataPosition() const
361{
362 return mDataPos;
363}
364
365size_t Parcel::dataCapacity() const
366{
367 return mDataCapacity;
368}
369
370status_t Parcel::setDataSize(size_t size)
371{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700372 if (size > INT32_MAX) {
373 // don't accept size_t values which may have come from an
374 // inadvertent conversion from a negative int.
375 return BAD_VALUE;
376 }
377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 status_t err;
379 err = continueWrite(size);
380 if (err == NO_ERROR) {
381 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700382 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383 }
384 return err;
385}
386
387void Parcel::setDataPosition(size_t pos) const
388{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700389 if (pos > INT32_MAX) {
390 // don't accept size_t values which may have come from an
391 // inadvertent conversion from a negative int.
392 abort();
393 }
394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700395 mDataPos = pos;
396 mNextObjectHint = 0;
397}
398
399status_t Parcel::setDataCapacity(size_t size)
400{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700401 if (size > INT32_MAX) {
402 // don't accept size_t values which may have come from an
403 // inadvertent conversion from a negative int.
404 return BAD_VALUE;
405 }
406
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700407 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 return NO_ERROR;
409}
410
411status_t Parcel::setData(const uint8_t* buffer, size_t len)
412{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700413 if (len > INT32_MAX) {
414 // don't accept size_t values which may have come from an
415 // inadvertent conversion from a negative int.
416 return BAD_VALUE;
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 status_t err = restartWrite(len);
420 if (err == NO_ERROR) {
421 memcpy(const_cast<uint8_t*>(data()), buffer, len);
422 mDataSize = len;
423 mFdsKnown = false;
424 }
425 return err;
426}
427
Andreas Huber51faf462011-04-13 10:21:56 -0700428status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429{
430 const sp<ProcessState> proc(ProcessState::self());
431 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700432 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800433 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 size_t size = parcel->mObjectsSize;
435 int startPos = mDataPos;
436 int firstIndex = -1, lastIndex = -2;
437
438 if (len == 0) {
439 return NO_ERROR;
440 }
441
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 // range checks against the source parcel size
449 if ((offset > parcel->mDataSize)
450 || (len > parcel->mDataSize)
451 || (offset + len > parcel->mDataSize)) {
452 return BAD_VALUE;
453 }
454
455 // Count objects in range
456 for (int i = 0; i < (int) size; i++) {
457 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700458 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 if (firstIndex == -1) {
460 firstIndex = i;
461 }
462 lastIndex = i;
463 }
464 }
465 int numObjects = lastIndex - firstIndex + 1;
466
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700467 if ((mDataSize+len) > mDataCapacity) {
468 // grow data
469 err = growData(len);
470 if (err != NO_ERROR) {
471 return err;
472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 }
474
475 // append data
476 memcpy(mData + mDataPos, data + offset, len);
477 mDataPos += len;
478 mDataSize += len;
479
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400480 err = NO_ERROR;
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 if (numObjects > 0) {
483 // grow objects
484 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700485 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
486 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800487 binder_size_t *objects =
488 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
489 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 return NO_MEMORY;
491 }
492 mObjects = objects;
493 mObjectsCapacity = newSize;
494 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 // append and acquire objects
497 int idx = mObjectsSize;
498 for (int i = firstIndex; i <= lastIndex; i++) {
499 size_t off = objects[i] - offset + startPos;
500 mObjects[idx++] = off;
501 mObjectsSize++;
502
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700503 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 = reinterpret_cast<flat_binder_object*>(mData + off);
505 acquire_object(proc, *flat, this);
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700508 // If this is a file descriptor, we need to dup it so the
509 // new Parcel now owns its own fd, and can declare that we
510 // officially know we have fds.
511 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800512 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 if (!mAllowFds) {
515 err = FDS_NOT_ALLOWED;
516 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 }
518 }
519 }
520
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400521 return err;
522}
523
Jeff Brown13b16042014-11-11 16:44:25 -0800524bool Parcel::allowFds() const
525{
526 return mAllowFds;
527}
528
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700529bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530{
531 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532 if (!allowFds) {
533 mAllowFds = false;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538void Parcel::restoreAllowFds(bool lastValue)
539{
540 mAllowFds = lastValue;
541}
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543bool Parcel::hasFileDescriptors() const
544{
545 if (!mFdsKnown) {
546 scanForFds();
547 }
548 return mHasFds;
549}
550
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700551// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552status_t Parcel::writeInterfaceToken(const String16& interface)
553{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700554 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
555 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 // currently the interface identification token is just its name as a string
557 return writeString16(interface);
558}
559
Mathias Agopian83c04462009-05-22 19:00:22 -0700560bool Parcel::checkInterface(IBinder* binder) const
561{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700562 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700563}
564
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700565bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700566 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700567{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700568 int32_t strictPolicy = readInt32();
569 if (threadState == NULL) {
570 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700571 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700572 if ((threadState->getLastTransactionBinderFlags() &
573 IBinder::FLAG_ONEWAY) != 0) {
574 // For one-way calls, the callee is running entirely
575 // disconnected from the caller, so disable StrictMode entirely.
576 // Not only does disk/network usage not impact the caller, but
577 // there's no way to commuicate back any violations anyway.
578 threadState->setStrictModePolicy(0);
579 } else {
580 threadState->setStrictModePolicy(strictPolicy);
581 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700582 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583 if (str == interface) {
584 return true;
585 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700586 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587 String8(interface).string(), String8(str).string());
588 return false;
589 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700590}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800592const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593{
594 return mObjects;
595}
596
597size_t Parcel::objectsCount() const
598{
599 return mObjectsSize;
600}
601
602status_t Parcel::errorCheck() const
603{
604 return mError;
605}
606
607void Parcel::setError(status_t err)
608{
609 mError = err;
610}
611
612status_t Parcel::finishWrite(size_t len)
613{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700614 if (len > INT32_MAX) {
615 // don't accept size_t values which may have come from an
616 // inadvertent conversion from a negative int.
617 return BAD_VALUE;
618 }
619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620 //printf("Finish write of %d\n", len);
621 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 if (mDataPos > mDataSize) {
624 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700625 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 }
627 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
628 return NO_ERROR;
629}
630
631status_t Parcel::writeUnpadded(const void* data, size_t len)
632{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700633 if (len > INT32_MAX) {
634 // don't accept size_t values which may have come from an
635 // inadvertent conversion from a negative int.
636 return BAD_VALUE;
637 }
638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 size_t end = mDataPos + len;
640 if (end < mDataPos) {
641 // integer overflow
642 return BAD_VALUE;
643 }
644
645 if (end <= mDataCapacity) {
646restart_write:
647 memcpy(mData+mDataPos, data, len);
648 return finishWrite(len);
649 }
650
651 status_t err = growData(len);
652 if (err == NO_ERROR) goto restart_write;
653 return err;
654}
655
656status_t Parcel::write(const void* data, size_t len)
657{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700658 if (len > INT32_MAX) {
659 // don't accept size_t values which may have come from an
660 // inadvertent conversion from a negative int.
661 return BAD_VALUE;
662 }
663
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 void* const d = writeInplace(len);
665 if (d) {
666 memcpy(d, data, len);
667 return NO_ERROR;
668 }
669 return mError;
670}
671
672void* Parcel::writeInplace(size_t len)
673{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700674 if (len > INT32_MAX) {
675 // don't accept size_t values which may have come from an
676 // inadvertent conversion from a negative int.
677 return NULL;
678 }
679
680 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700681
682 // sanity check for integer overflow
683 if (mDataPos+padded < mDataPos) {
684 return NULL;
685 }
686
687 if ((mDataPos+padded) <= mDataCapacity) {
688restart_write:
689 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
690 uint8_t* const data = mData+mDataPos;
691
692 // Need to pad at end?
693 if (padded != len) {
694#if BYTE_ORDER == BIG_ENDIAN
695 static const uint32_t mask[4] = {
696 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
697 };
698#endif
699#if BYTE_ORDER == LITTLE_ENDIAN
700 static const uint32_t mask[4] = {
701 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
702 };
703#endif
704 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
705 // *reinterpret_cast<void**>(data+padded-4));
706 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
707 }
708
709 finishWrite(padded);
710 return data;
711 }
712
713 status_t err = growData(padded);
714 if (err == NO_ERROR) goto restart_write;
715 return NULL;
716}
717
Casey Dahlin451ff582015-10-19 18:12:18 -0700718status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
719{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700720 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700721 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700722 status = BAD_VALUE;
723 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700724 }
725
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700726 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700727 if (status != OK) {
728 return status;
729 }
730
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700731 void* data = writeInplace(val.size());
732 if (!data) {
733 status = BAD_VALUE;
734 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700735 }
736
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700737 memcpy(data, val.data(), val.size());
738 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700739}
740
741status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
742{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800743 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700744}
745
746status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
747{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800748 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700749}
750
751status_t Parcel::writeFloatVector(const std::vector<float>& val)
752{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800753 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700754}
755
756status_t Parcel::writeDoubleVector(const std::vector<double>& val)
757{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800758 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700759}
760
761status_t Parcel::writeBoolVector(const std::vector<bool>& val)
762{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800763 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700764}
765
766status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
767{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800768 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700769}
770
771status_t Parcel::writeString16Vector(const std::vector<String16>& val)
772{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800773 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700774}
775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776status_t Parcel::writeInt32(int32_t val)
777{
Andreas Huber84a6d042009-08-17 13:33:27 -0700778 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700779}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800780
781status_t Parcel::writeUint32(uint32_t val)
782{
783 return writeAligned(val);
784}
785
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700786status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700787 if (len > INT32_MAX) {
788 // don't accept size_t values which may have come from an
789 // inadvertent conversion from a negative int.
790 return BAD_VALUE;
791 }
792
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700793 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700794 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700795 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700796 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700797 if (ret == NO_ERROR) {
798 ret = write(val, len * sizeof(*val));
799 }
800 return ret;
801}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700802status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700803 if (len > INT32_MAX) {
804 // don't accept size_t values which may have come from an
805 // inadvertent conversion from a negative int.
806 return BAD_VALUE;
807 }
808
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700809 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700810 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700811 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700812 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700813 if (ret == NO_ERROR) {
814 ret = write(val, len * sizeof(*val));
815 }
816 return ret;
817}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700818
Casey Dahlind6848f52015-10-15 15:44:59 -0700819status_t Parcel::writeBool(bool val)
820{
821 return writeInt32(int32_t(val));
822}
823
824status_t Parcel::writeChar(char16_t val)
825{
826 return writeInt32(int32_t(val));
827}
828
829status_t Parcel::writeByte(int8_t val)
830{
831 return writeInt32(int32_t(val));
832}
833
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700834status_t Parcel::writeInt64(int64_t val)
835{
Andreas Huber84a6d042009-08-17 13:33:27 -0700836 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700837}
838
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700839status_t Parcel::writeUint64(uint64_t val)
840{
841 return writeAligned(val);
842}
843
Serban Constantinescuf683e012013-11-05 16:53:55 +0000844status_t Parcel::writePointer(uintptr_t val)
845{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800846 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000847}
848
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700849status_t Parcel::writeFloat(float val)
850{
Andreas Huber84a6d042009-08-17 13:33:27 -0700851 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700852}
853
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800854#if defined(__mips__) && defined(__mips_hard_float)
855
856status_t Parcel::writeDouble(double val)
857{
858 union {
859 double d;
860 unsigned long long ll;
861 } u;
862 u.d = val;
863 return writeAligned(u.ll);
864}
865
866#else
867
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700868status_t Parcel::writeDouble(double val)
869{
Andreas Huber84a6d042009-08-17 13:33:27 -0700870 return writeAligned(val);
871}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700872
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800873#endif
874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875status_t Parcel::writeCString(const char* str)
876{
877 return write(str, strlen(str)+1);
878}
879
880status_t Parcel::writeString8(const String8& str)
881{
882 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100883 // only write string if its length is more than zero characters,
884 // as readString8 will only read if the length field is non-zero.
885 // this is slightly different from how writeString16 works.
886 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700887 err = write(str.string(), str.bytes()+1);
888 }
889 return err;
890}
891
892status_t Parcel::writeString16(const String16& str)
893{
894 return writeString16(str.string(), str.size());
895}
896
897status_t Parcel::writeString16(const char16_t* str, size_t len)
898{
899 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700900
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901 status_t err = writeInt32(len);
902 if (err == NO_ERROR) {
903 len *= sizeof(char16_t);
904 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
905 if (data) {
906 memcpy(data, str, len);
907 *reinterpret_cast<char16_t*>(data+len) = 0;
908 return NO_ERROR;
909 }
910 err = mError;
911 }
912 return err;
913}
914
915status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
916{
917 return flatten_binder(ProcessState::self(), val, this);
918}
919
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800920status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
921{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800922 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800923}
924
925status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800926 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800927}
928
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
930{
931 return flatten_binder(ProcessState::self(), val, this);
932}
933
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700934status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800935{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700936 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800937 return BAD_TYPE;
938
939 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700940 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800941 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700943 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800944 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700946 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
947 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948
949 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000950 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 return err;
952 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700953 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800954 return err;
955}
956
Jeff Brown93ff1f92011-11-04 19:01:44 -0700957status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958{
959 flat_binder_object obj;
960 obj.type = BINDER_TYPE_FD;
961 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800962 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800964 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965 return writeObject(obj, true);
966}
967
968status_t Parcel::writeDupFileDescriptor(int fd)
969{
Jeff Brownd341c712011-11-04 20:19:33 -0700970 int dupFd = dup(fd);
971 if (dupFd < 0) {
972 return -errno;
973 }
974 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
975 if (err) {
976 close(dupFd);
977 }
978 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
980
Jeff Brown13b16042014-11-11 16:44:25 -0800981status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700982{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700983 if (len > INT32_MAX) {
984 // don't accept size_t values which may have come from an
985 // inadvertent conversion from a negative int.
986 return BAD_VALUE;
987 }
988
Jeff Brown13b16042014-11-11 16:44:25 -0800989 status_t status;
990 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100991 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800992 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700993 if (status) return status;
994
995 void* ptr = writeInplace(len);
996 if (!ptr) return NO_MEMORY;
997
Jeff Brown13b16042014-11-11 16:44:25 -0800998 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700999 return NO_ERROR;
1000 }
1001
Steve Block6807e592011-10-20 11:56:00 +01001002 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001003 int fd = ashmem_create_region("Parcel Blob", len);
1004 if (fd < 0) return NO_MEMORY;
1005
Dan Sandleraa5c2342015-04-10 10:08:45 -04001006 mBlobAshmemSize += len;
1007
Jeff Brown5707dbf2011-09-23 21:17:56 -07001008 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1009 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001010 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001011 } else {
1012 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1013 if (ptr == MAP_FAILED) {
1014 status = -errno;
1015 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001016 if (!mutableCopy) {
1017 result = ashmem_set_prot_region(fd, PROT_READ);
1018 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001019 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001020 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001021 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001022 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001023 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001024 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001025 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001026 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001027 return NO_ERROR;
1028 }
1029 }
1030 }
1031 }
1032 ::munmap(ptr, len);
1033 }
1034 ::close(fd);
1035 return status;
1036}
1037
Jeff Brown13b16042014-11-11 16:44:25 -08001038status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1039{
1040 // Must match up with what's done in writeBlob.
1041 if (!mAllowFds) return FDS_NOT_ALLOWED;
1042 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1043 if (status) return status;
1044 return writeDupFileDescriptor(fd);
1045}
1046
Mathias Agopiane1424282013-07-29 21:24:40 -07001047status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001048{
1049 status_t err;
1050
1051 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001052 const size_t len = val.getFlattenedSize();
1053 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001054
Nick Kralevichb6b14232015-04-02 09:36:02 -07001055 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1056 // don't accept size_t values which may have come from an
1057 // inadvertent conversion from a negative int.
1058 return BAD_VALUE;
1059 }
1060
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001061 err = this->writeInt32(len);
1062 if (err) return err;
1063
1064 err = this->writeInt32(fd_count);
1065 if (err) return err;
1066
1067 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001068 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001069 if (buf == NULL)
1070 return BAD_VALUE;
1071
1072 int* fds = NULL;
1073 if (fd_count) {
1074 fds = new int[fd_count];
1075 }
1076
1077 err = val.flatten(buf, len, fds, fd_count);
1078 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1079 err = this->writeDupFileDescriptor( fds[i] );
1080 }
1081
1082 if (fd_count) {
1083 delete [] fds;
1084 }
1085
1086 return err;
1087}
1088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001089status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1090{
1091 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1092 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1093 if (enoughData && enoughObjects) {
1094restart_write:
1095 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001096
Christopher Tate98e67d32015-06-03 18:44:15 -07001097 // remember if it's a file descriptor
1098 if (val.type == BINDER_TYPE_FD) {
1099 if (!mAllowFds) {
1100 // fail before modifying our object index
1101 return FDS_NOT_ALLOWED;
1102 }
1103 mHasFds = mFdsKnown = true;
1104 }
1105
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001107 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108 mObjects[mObjectsSize] = mDataPos;
1109 acquire_object(ProcessState::self(), val, this);
1110 mObjectsSize++;
1111 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001112
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001113 return finishWrite(sizeof(flat_binder_object));
1114 }
1115
1116 if (!enoughData) {
1117 const status_t err = growData(sizeof(val));
1118 if (err != NO_ERROR) return err;
1119 }
1120 if (!enoughObjects) {
1121 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001122 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001123 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124 if (objects == NULL) return NO_MEMORY;
1125 mObjects = objects;
1126 mObjectsCapacity = newSize;
1127 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001128
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001129 goto restart_write;
1130}
1131
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001132status_t Parcel::writeNoException()
1133{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001134 binder::Status status;
1135 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001136}
1137
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001138void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139{
1140 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1141}
1142
1143status_t Parcel::read(void* outData, size_t len) const
1144{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001145 if (len > INT32_MAX) {
1146 // don't accept size_t values which may have come from an
1147 // inadvertent conversion from a negative int.
1148 return BAD_VALUE;
1149 }
1150
1151 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1152 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001154 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001155 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156 return NO_ERROR;
1157 }
1158 return NOT_ENOUGH_DATA;
1159}
1160
1161const void* Parcel::readInplace(size_t len) const
1162{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001163 if (len > INT32_MAX) {
1164 // don't accept size_t values which may have come from an
1165 // inadvertent conversion from a negative int.
1166 return NULL;
1167 }
1168
1169 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1170 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001172 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001173 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174 return data;
1175 }
1176 return NULL;
1177}
1178
Andreas Huber84a6d042009-08-17 13:33:27 -07001179template<class T>
1180status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001181 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001182
1183 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001184 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001185 mDataPos += sizeof(T);
1186 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001187 return NO_ERROR;
1188 } else {
1189 return NOT_ENOUGH_DATA;
1190 }
1191}
1192
Andreas Huber84a6d042009-08-17 13:33:27 -07001193template<class T>
1194T Parcel::readAligned() const {
1195 T result;
1196 if (readAligned(&result) != NO_ERROR) {
1197 result = 0;
1198 }
1199
1200 return result;
1201}
1202
1203template<class T>
1204status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001205 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001206
1207 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1208restart_write:
1209 *reinterpret_cast<T*>(mData+mDataPos) = val;
1210 return finishWrite(sizeof(val));
1211 }
1212
1213 status_t err = growData(sizeof(val));
1214 if (err == NO_ERROR) goto restart_write;
1215 return err;
1216}
1217
Casey Dahlin451ff582015-10-19 18:12:18 -07001218status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1219 val->clear();
1220
1221 int32_t size;
1222 status_t status = readInt32(&size);
1223
1224 if (status != OK) {
1225 return status;
1226 }
1227
Christopher Wiley4db672d2015-11-10 09:44:30 -08001228 if (size < 0) {
1229 status = UNEXPECTED_NULL;
1230 return status;
1231 }
1232 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001233 status = BAD_VALUE;
1234 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001235 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001236
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001237 const void* data = readInplace(size);
1238 if (!data) {
1239 status = BAD_VALUE;
1240 return status;
1241 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001242 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001243 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001244
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001245 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001246}
1247
1248status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001249 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001250}
1251
1252status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001253 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001254}
1255
1256status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001257 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001258}
1259
1260status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001261 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001262}
1263
1264status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1265 val->clear();
1266
1267 int32_t size;
1268 status_t status = readInt32(&size);
1269
1270 if (status != OK) {
1271 return status;
1272 }
1273
1274 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001275 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001276 }
1277
1278 val->resize(size);
1279
1280 /* C++ bool handling means a vector of bools isn't necessarily addressable
1281 * (we might use individual bits)
1282 */
Christopher Wiley97887982015-10-27 16:33:47 -07001283 bool data;
1284 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001285 status = readBool(&data);
1286 (*val)[i] = data;
1287
1288 if (status != OK) {
1289 return status;
1290 }
1291 }
1292
1293 return OK;
1294}
1295
1296status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001297 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001298}
1299
1300status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001301 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001302}
1303
1304
Andreas Huber84a6d042009-08-17 13:33:27 -07001305status_t Parcel::readInt32(int32_t *pArg) const
1306{
1307 return readAligned(pArg);
1308}
1309
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310int32_t Parcel::readInt32() const
1311{
Andreas Huber84a6d042009-08-17 13:33:27 -07001312 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313}
1314
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001315status_t Parcel::readUint32(uint32_t *pArg) const
1316{
1317 return readAligned(pArg);
1318}
1319
1320uint32_t Parcel::readUint32() const
1321{
1322 return readAligned<uint32_t>();
1323}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324
1325status_t Parcel::readInt64(int64_t *pArg) const
1326{
Andreas Huber84a6d042009-08-17 13:33:27 -07001327 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328}
1329
1330
1331int64_t Parcel::readInt64() const
1332{
Andreas Huber84a6d042009-08-17 13:33:27 -07001333 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334}
1335
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001336status_t Parcel::readUint64(uint64_t *pArg) const
1337{
1338 return readAligned(pArg);
1339}
1340
1341uint64_t Parcel::readUint64() const
1342{
1343 return readAligned<uint64_t>();
1344}
1345
Serban Constantinescuf683e012013-11-05 16:53:55 +00001346status_t Parcel::readPointer(uintptr_t *pArg) const
1347{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001348 status_t ret;
1349 binder_uintptr_t ptr;
1350 ret = readAligned(&ptr);
1351 if (!ret)
1352 *pArg = ptr;
1353 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001354}
1355
1356uintptr_t Parcel::readPointer() const
1357{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001358 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001359}
1360
1361
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362status_t Parcel::readFloat(float *pArg) const
1363{
Andreas Huber84a6d042009-08-17 13:33:27 -07001364 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365}
1366
1367
1368float Parcel::readFloat() const
1369{
Andreas Huber84a6d042009-08-17 13:33:27 -07001370 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371}
1372
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001373#if defined(__mips__) && defined(__mips_hard_float)
1374
1375status_t Parcel::readDouble(double *pArg) const
1376{
1377 union {
1378 double d;
1379 unsigned long long ll;
1380 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001381 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001382 status_t status;
1383 status = readAligned(&u.ll);
1384 *pArg = u.d;
1385 return status;
1386}
1387
1388double Parcel::readDouble() const
1389{
1390 union {
1391 double d;
1392 unsigned long long ll;
1393 } u;
1394 u.ll = readAligned<unsigned long long>();
1395 return u.d;
1396}
1397
1398#else
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400status_t Parcel::readDouble(double *pArg) const
1401{
Andreas Huber84a6d042009-08-17 13:33:27 -07001402 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403}
1404
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405double Parcel::readDouble() const
1406{
Andreas Huber84a6d042009-08-17 13:33:27 -07001407 return readAligned<double>();
1408}
1409
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001410#endif
1411
Andreas Huber84a6d042009-08-17 13:33:27 -07001412status_t Parcel::readIntPtr(intptr_t *pArg) const
1413{
1414 return readAligned(pArg);
1415}
1416
1417
1418intptr_t Parcel::readIntPtr() const
1419{
1420 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421}
1422
Casey Dahlind6848f52015-10-15 15:44:59 -07001423status_t Parcel::readBool(bool *pArg) const
1424{
1425 int32_t tmp;
1426 status_t ret = readInt32(&tmp);
1427 *pArg = (tmp != 0);
1428 return ret;
1429}
1430
1431bool Parcel::readBool() const
1432{
1433 return readInt32() != 0;
1434}
1435
1436status_t Parcel::readChar(char16_t *pArg) const
1437{
1438 int32_t tmp;
1439 status_t ret = readInt32(&tmp);
1440 *pArg = char16_t(tmp);
1441 return ret;
1442}
1443
1444char16_t Parcel::readChar() const
1445{
1446 return char16_t(readInt32());
1447}
1448
1449status_t Parcel::readByte(int8_t *pArg) const
1450{
1451 int32_t tmp;
1452 status_t ret = readInt32(&tmp);
1453 *pArg = int8_t(tmp);
1454 return ret;
1455}
1456
1457int8_t Parcel::readByte() const
1458{
1459 return int8_t(readInt32());
1460}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461
1462const char* Parcel::readCString() const
1463{
1464 const size_t avail = mDataSize-mDataPos;
1465 if (avail > 0) {
1466 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1467 // is the string's trailing NUL within the parcel's valid bounds?
1468 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1469 if (eos) {
1470 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001471 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001472 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001473 return str;
1474 }
1475 }
1476 return NULL;
1477}
1478
1479String8 Parcel::readString8() const
1480{
1481 int32_t size = readInt32();
1482 // watch for potential int overflow adding 1 for trailing NUL
1483 if (size > 0 && size < INT32_MAX) {
1484 const char* str = (const char*)readInplace(size+1);
1485 if (str) return String8(str, size);
1486 }
1487 return String8();
1488}
1489
1490String16 Parcel::readString16() const
1491{
1492 size_t len;
1493 const char16_t* str = readString16Inplace(&len);
1494 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001495 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 return String16();
1497}
1498
Casey Dahlin451ff582015-10-19 18:12:18 -07001499status_t Parcel::readString16(String16* pArg) const
1500{
1501 size_t len;
1502 const char16_t* str = readString16Inplace(&len);
1503 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001504 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001505 return 0;
1506 } else {
1507 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001508 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001509 }
1510}
1511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1513{
1514 int32_t size = readInt32();
1515 // watch for potential int overflow from size+1
1516 if (size >= 0 && size < INT32_MAX) {
1517 *outLen = size;
1518 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1519 if (str != NULL) {
1520 return str;
1521 }
1522 }
1523 *outLen = 0;
1524 return NULL;
1525}
1526
Casey Dahlinf0c13772015-10-27 18:33:56 -07001527status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1528{
1529 return unflatten_binder(ProcessState::self(), *this, val);
1530}
1531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001532sp<IBinder> Parcel::readStrongBinder() const
1533{
1534 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001535 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536 return val;
1537}
1538
1539wp<IBinder> Parcel::readWeakBinder() const
1540{
1541 wp<IBinder> val;
1542 unflatten_binder(ProcessState::self(), *this, &val);
1543 return val;
1544}
1545
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001546int32_t Parcel::readExceptionCode() const
1547{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001548 binder::Status status;
1549 status.readFromParcel(*this);
1550 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001551}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001552
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001553native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001554{
1555 int numFds, numInts;
1556 status_t err;
1557 err = readInt32(&numFds);
1558 if (err != NO_ERROR) return 0;
1559 err = readInt32(&numInts);
1560 if (err != NO_ERROR) return 0;
1561
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001562 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001563 if (!h) {
1564 return 0;
1565 }
1566
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001567 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001568 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001569 if (h->data[i] < 0) err = BAD_VALUE;
1570 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001571 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001572 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001573 native_handle_close(h);
1574 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001575 h = 0;
1576 }
1577 return h;
1578}
1579
1580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581int Parcel::readFileDescriptor() const
1582{
1583 const flat_binder_object* flat = readObject(true);
1584 if (flat) {
1585 switch (flat->type) {
1586 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001587 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001588 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001589 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590 }
1591 return BAD_TYPE;
1592}
1593
Jeff Brown5707dbf2011-09-23 21:17:56 -07001594status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1595{
Jeff Brown13b16042014-11-11 16:44:25 -08001596 int32_t blobType;
1597 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001598 if (status) return status;
1599
Jeff Brown13b16042014-11-11 16:44:25 -08001600 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001601 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001602 const void* ptr = readInplace(len);
1603 if (!ptr) return BAD_VALUE;
1604
Jeff Brown13b16042014-11-11 16:44:25 -08001605 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001606 return NO_ERROR;
1607 }
1608
Steve Block6807e592011-10-20 11:56:00 +01001609 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001610 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001611 int fd = readFileDescriptor();
1612 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1613
Jeff Brown13b16042014-11-11 16:44:25 -08001614 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1615 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001616 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001617
Jeff Brown13b16042014-11-11 16:44:25 -08001618 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001619 return NO_ERROR;
1620}
1621
Mathias Agopiane1424282013-07-29 21:24:40 -07001622status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001623{
1624 // size
1625 const size_t len = this->readInt32();
1626 const size_t fd_count = this->readInt32();
1627
Nick Kralevichb6b14232015-04-02 09:36:02 -07001628 if (len > INT32_MAX) {
1629 // don't accept size_t values which may have come from an
1630 // inadvertent conversion from a negative int.
1631 return BAD_VALUE;
1632 }
1633
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001634 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001635 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001636 if (buf == NULL)
1637 return BAD_VALUE;
1638
1639 int* fds = NULL;
1640 if (fd_count) {
1641 fds = new int[fd_count];
1642 }
1643
1644 status_t err = NO_ERROR;
1645 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001646 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001647 if (fds[i] < 0) {
1648 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001649 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1650 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001651 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001652 }
1653
1654 if (err == NO_ERROR) {
1655 err = val.unflatten(buf, len, fds, fd_count);
1656 }
1657
1658 if (fd_count) {
1659 delete [] fds;
1660 }
1661
1662 return err;
1663}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001664const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1665{
1666 const size_t DPOS = mDataPos;
1667 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1668 const flat_binder_object* obj
1669 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1670 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001671 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001672 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001673 // the object list, so we don't want to check for it when
1674 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001675 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676 return obj;
1677 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001679 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001680 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681 const size_t N = mObjectsSize;
1682 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001683
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001684 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001685 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688 // Start at the current hint position, looking for an object at
1689 // the current data position.
1690 if (opos < N) {
1691 while (opos < (N-1) && OBJS[opos] < DPOS) {
1692 opos++;
1693 }
1694 } else {
1695 opos = N-1;
1696 }
1697 if (OBJS[opos] == DPOS) {
1698 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001699 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700 this, DPOS, opos);
1701 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001702 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 return obj;
1704 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706 // Look backwards for it...
1707 while (opos > 0 && OBJS[opos] > DPOS) {
1708 opos--;
1709 }
1710 if (OBJS[opos] == DPOS) {
1711 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001712 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713 this, DPOS, opos);
1714 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001715 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001716 return obj;
1717 }
1718 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001719 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 -07001720 this, DPOS);
1721 }
1722 return NULL;
1723}
1724
1725void Parcel::closeFileDescriptors()
1726{
1727 size_t i = mObjectsSize;
1728 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001729 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001730 }
1731 while (i > 0) {
1732 i--;
1733 const flat_binder_object* flat
1734 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1735 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001736 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001737 close(flat->handle);
1738 }
1739 }
1740}
1741
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001742uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001743{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001744 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745}
1746
1747size_t Parcel::ipcDataSize() const
1748{
1749 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1750}
1751
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001752uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001754 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755}
1756
1757size_t Parcel::ipcObjectsCount() const
1758{
1759 return mObjectsSize;
1760}
1761
1762void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001763 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001765 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766 freeDataNoInit();
1767 mError = NO_ERROR;
1768 mData = const_cast<uint8_t*>(data);
1769 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001770 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001771 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001772 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001773 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001774 mObjectsSize = mObjectsCapacity = objectsCount;
1775 mNextObjectHint = 0;
1776 mOwner = relFunc;
1777 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001778 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001779 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001780 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001781 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001782 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001783 mObjectsSize = 0;
1784 break;
1785 }
1786 minOffset = offset + sizeof(flat_binder_object);
1787 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788 scanForFds();
1789}
1790
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001791void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792{
1793 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001794
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001795 if (errorCheck() != NO_ERROR) {
1796 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001797 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798 } else if (dataSize() > 0) {
1799 const uint8_t* DATA = data();
1800 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001801 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802 const size_t N = objectsCount();
1803 for (size_t i=0; i<N; i++) {
1804 const flat_binder_object* flat
1805 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1806 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1807 << TypeCode(flat->type & 0x7f7f7f00)
1808 << " = " << flat->binder;
1809 }
1810 } else {
1811 to << "NULL";
1812 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 to << ")";
1815}
1816
1817void Parcel::releaseObjects()
1818{
1819 const sp<ProcessState> proc(ProcessState::self());
1820 size_t i = mObjectsSize;
1821 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001822 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 while (i > 0) {
1824 i--;
1825 const flat_binder_object* flat
1826 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1827 release_object(proc, *flat, this);
1828 }
1829}
1830
1831void Parcel::acquireObjects()
1832{
1833 const sp<ProcessState> proc(ProcessState::self());
1834 size_t i = mObjectsSize;
1835 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001836 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 while (i > 0) {
1838 i--;
1839 const flat_binder_object* flat
1840 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1841 acquire_object(proc, *flat, this);
1842 }
1843}
1844
1845void Parcel::freeData()
1846{
1847 freeDataNoInit();
1848 initState();
1849}
1850
1851void Parcel::freeDataNoInit()
1852{
1853 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001854 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001855 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001856 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1857 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001858 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001860 if (mData) {
1861 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001862 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001863 if (mDataCapacity <= gParcelGlobalAllocSize) {
1864 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1865 } else {
1866 gParcelGlobalAllocSize = 0;
1867 }
1868 if (gParcelGlobalAllocCount > 0) {
1869 gParcelGlobalAllocCount--;
1870 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001871 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001872 free(mData);
1873 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874 if (mObjects) free(mObjects);
1875 }
1876}
1877
1878status_t Parcel::growData(size_t len)
1879{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001880 if (len > INT32_MAX) {
1881 // don't accept size_t values which may have come from an
1882 // inadvertent conversion from a negative int.
1883 return BAD_VALUE;
1884 }
1885
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001886 size_t newSize = ((mDataSize+len)*3)/2;
1887 return (newSize <= mDataSize)
1888 ? (status_t) NO_MEMORY
1889 : continueWrite(newSize);
1890}
1891
1892status_t Parcel::restartWrite(size_t desired)
1893{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001894 if (desired > INT32_MAX) {
1895 // don't accept size_t values which may have come from an
1896 // inadvertent conversion from a negative int.
1897 return BAD_VALUE;
1898 }
1899
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900 if (mOwner) {
1901 freeData();
1902 return continueWrite(desired);
1903 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905 uint8_t* data = (uint8_t*)realloc(mData, desired);
1906 if (!data && desired > mDataCapacity) {
1907 mError = NO_MEMORY;
1908 return NO_MEMORY;
1909 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001910
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001912
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001914 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001915 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001916 gParcelGlobalAllocSize += desired;
1917 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001918 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919 mData = data;
1920 mDataCapacity = desired;
1921 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001922
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001924 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1925 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 free(mObjects);
1928 mObjects = NULL;
1929 mObjectsSize = mObjectsCapacity = 0;
1930 mNextObjectHint = 0;
1931 mHasFds = false;
1932 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001933 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 return NO_ERROR;
1936}
1937
1938status_t Parcel::continueWrite(size_t desired)
1939{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001940 if (desired > INT32_MAX) {
1941 // don't accept size_t values which may have come from an
1942 // inadvertent conversion from a negative int.
1943 return BAD_VALUE;
1944 }
1945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001946 // If shrinking, first adjust for any objects that appear
1947 // after the new data size.
1948 size_t objectsSize = mObjectsSize;
1949 if (desired < mDataSize) {
1950 if (desired == 0) {
1951 objectsSize = 0;
1952 } else {
1953 while (objectsSize > 0) {
1954 if (mObjects[objectsSize-1] < desired)
1955 break;
1956 objectsSize--;
1957 }
1958 }
1959 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001960
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961 if (mOwner) {
1962 // If the size is going to zero, just release the owner's data.
1963 if (desired == 0) {
1964 freeData();
1965 return NO_ERROR;
1966 }
1967
1968 // If there is a different owner, we need to take
1969 // posession.
1970 uint8_t* data = (uint8_t*)malloc(desired);
1971 if (!data) {
1972 mError = NO_MEMORY;
1973 return NO_MEMORY;
1974 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001975 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001978 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001980 free(data);
1981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982 mError = NO_MEMORY;
1983 return NO_MEMORY;
1984 }
1985
1986 // Little hack to only acquire references on objects
1987 // we will be keeping.
1988 size_t oldObjectsSize = mObjectsSize;
1989 mObjectsSize = objectsSize;
1990 acquireObjects();
1991 mObjectsSize = oldObjectsSize;
1992 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 if (mData) {
1995 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1996 }
1997 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001998 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001999 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002000 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2002 mOwner = NULL;
2003
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002004 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002005 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002006 gParcelGlobalAllocSize += desired;
2007 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002008 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 mData = data;
2011 mObjects = objects;
2012 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002013 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 mDataCapacity = desired;
2015 mObjectsSize = mObjectsCapacity = objectsSize;
2016 mNextObjectHint = 0;
2017
2018 } else if (mData) {
2019 if (objectsSize < mObjectsSize) {
2020 // Need to release refs on any objects we are dropping.
2021 const sp<ProcessState> proc(ProcessState::self());
2022 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2023 const flat_binder_object* flat
2024 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2025 if (flat->type == BINDER_TYPE_FD) {
2026 // will need to rescan because we may have lopped off the only FDs
2027 mFdsKnown = false;
2028 }
2029 release_object(proc, *flat, this);
2030 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002031 binder_size_t* objects =
2032 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033 if (objects) {
2034 mObjects = objects;
2035 }
2036 mObjectsSize = objectsSize;
2037 mNextObjectHint = 0;
2038 }
2039
2040 // We own the data, so we can just do a realloc().
2041 if (desired > mDataCapacity) {
2042 uint8_t* data = (uint8_t*)realloc(mData, desired);
2043 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002044 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2045 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002046 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002047 gParcelGlobalAllocSize += desired;
2048 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin48fd7b42015-09-10 13:46:02 -07002049 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002050 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 mData = data;
2052 mDataCapacity = desired;
2053 } else if (desired > mDataCapacity) {
2054 mError = NO_MEMORY;
2055 return NO_MEMORY;
2056 }
2057 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002058 if (mDataSize > desired) {
2059 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002060 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002061 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002062 if (mDataPos > desired) {
2063 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002064 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 }
2066 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 } else {
2069 // This is the first data. Easy!
2070 uint8_t* data = (uint8_t*)malloc(desired);
2071 if (!data) {
2072 mError = NO_MEMORY;
2073 return NO_MEMORY;
2074 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076 if(!(mDataCapacity == 0 && mObjects == NULL
2077 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002078 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002080
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002081 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002082 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002083 gParcelGlobalAllocSize += desired;
2084 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002085 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002086
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087 mData = data;
2088 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002089 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2090 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091 mDataCapacity = desired;
2092 }
2093
2094 return NO_ERROR;
2095}
2096
2097void Parcel::initState()
2098{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002099 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100 mError = NO_ERROR;
2101 mData = 0;
2102 mDataSize = 0;
2103 mDataCapacity = 0;
2104 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002105 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2106 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 mObjects = NULL;
2108 mObjectsSize = 0;
2109 mObjectsCapacity = 0;
2110 mNextObjectHint = 0;
2111 mHasFds = false;
2112 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002113 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114 mOwner = NULL;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002115 mBlobAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116}
2117
2118void Parcel::scanForFds() const
2119{
2120 bool hasFds = false;
2121 for (size_t i=0; i<mObjectsSize; i++) {
2122 const flat_binder_object* flat
2123 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2124 if (flat->type == BINDER_TYPE_FD) {
2125 hasFds = true;
2126 break;
2127 }
2128 }
2129 mHasFds = hasFds;
2130 mFdsKnown = true;
2131}
2132
Dan Sandleraa5c2342015-04-10 10:08:45 -04002133size_t Parcel::getBlobAshmemSize() const
2134{
2135 return mBlobAshmemSize;
2136}
2137
Jeff Brown5707dbf2011-09-23 21:17:56 -07002138// --- Parcel::Blob ---
2139
2140Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002141 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002142}
2143
2144Parcel::Blob::~Blob() {
2145 release();
2146}
2147
2148void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002149 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002150 ::munmap(mData, mSize);
2151 }
2152 clear();
2153}
2154
Jeff Brown13b16042014-11-11 16:44:25 -08002155void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2156 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002157 mData = data;
2158 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002159 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002160}
2161
2162void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002163 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002164 mData = NULL;
2165 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002166 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002167}
2168
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169}; // namespace android