blob: 066b4d130690aa48d6295ab65232da5aef4a3150 [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
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
21#include <inttypes.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070026
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/Binder.h>
28#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080029#include <binder/IPCThreadState.h>
30#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080032#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070033#include <binder/TextOutput.h>
34
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070036#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080037#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070038#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080039#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040#include <utils/String8.h>
41#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042
Mathias Agopian208059f2009-05-18 15:08:03 -070043#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080044#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#ifndef INT32_MAX
47#define INT32_MAX ((int32_t)(2147483647))
48#endif
49
50#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080051//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080052#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080053//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
55// ---------------------------------------------------------------------------
56
Nick Kralevichb6b14232015-04-02 09:36:02 -070057// This macro should never be used at runtime, as a too large value
58// of s could cause an integer overflow. Instead, you should always
59// use the wrapper function pad_size()
60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
61
62static size_t pad_size(size_t s) {
63 if (s > (SIZE_T_MAX - 3)) {
64 abort();
65 }
66 return PAD_SIZE_UNSAFE(s);
67}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080070#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072// XXX This can be made public if we want to provide
73// support for typed data.
74struct small_flat_data
75{
76 uint32_t type;
77 uint32_t data;
78};
79
80namespace android {
81
Dianne Hackborna4cff882014-11-13 17:07:40 -080082static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
83static size_t gParcelGlobalAllocSize = 0;
84static size_t gParcelGlobalAllocCount = 0;
85
Jeff Brown13b16042014-11-11 16:44:25 -080086// Maximum size of a blob to transfer in-place.
87static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
88
89enum {
90 BLOB_INPLACE = 0,
91 BLOB_ASHMEM_IMMUTABLE = 1,
92 BLOB_ASHMEM_MUTABLE = 2,
93};
94
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070095void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070096 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097{
98 switch (obj.type) {
99 case BINDER_TYPE_BINDER:
100 if (obj.binder) {
101 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800102 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700103 }
104 return;
105 case BINDER_TYPE_WEAK_BINDER:
106 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800107 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 return;
109 case BINDER_TYPE_HANDLE: {
110 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
111 if (b != NULL) {
112 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
113 b->incStrong(who);
114 }
115 return;
116 }
117 case BINDER_TYPE_WEAK_HANDLE: {
118 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
119 if (b != NULL) b.get_refs()->incWeak(who);
120 return;
121 }
122 case BINDER_TYPE_FD: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700123 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700124 if (outAshmemSize != NULL) {
125 // If we own an ashmem fd, keep track of how much memory it refers to.
126 int size = ashmem_get_size_region(obj.handle);
127 if (size > 0) {
128 *outAshmemSize += size;
129 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700130 }
131 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700132 return;
133 }
134 }
135
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800136 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137}
138
Adrian Roos6bb31142015-10-22 16:46:12 -0700139void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 const flat_binder_object& obj, const void* who)
141{
Adrian Roos6bb31142015-10-22 16:46:12 -0700142 acquire_object(proc, obj, who, NULL);
143}
144
145static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700146 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147{
148 switch (obj.type) {
149 case BINDER_TYPE_BINDER:
150 if (obj.binder) {
151 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800152 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153 }
154 return;
155 case BINDER_TYPE_WEAK_BINDER:
156 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800157 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700158 return;
159 case BINDER_TYPE_HANDLE: {
160 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
161 if (b != NULL) {
162 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
163 b->decStrong(who);
164 }
165 return;
166 }
167 case BINDER_TYPE_WEAK_HANDLE: {
168 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
169 if (b != NULL) b.get_refs()->decWeak(who);
170 return;
171 }
172 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700173 if (outAshmemSize != NULL) {
174 if (obj.cookie != 0) {
175 int size = ashmem_get_size_region(obj.handle);
176 if (size > 0) {
177 *outAshmemSize -= size;
178 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700179
Adrian Roos6bb31142015-10-22 16:46:12 -0700180 close(obj.handle);
181 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700182 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183 return;
184 }
185 }
186
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800187 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188}
189
Adrian Roos6bb31142015-10-22 16:46:12 -0700190void release_object(const sp<ProcessState>& proc,
191 const flat_binder_object& obj, const void* who)
192{
193 release_object(proc, obj, who, NULL);
194}
195
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700196inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800197 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198{
199 return out->writeObject(flat, false);
200}
201
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800202status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203 const sp<IBinder>& binder, Parcel* out)
204{
205 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700206
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
208 if (binder != NULL) {
209 IBinder *local = binder->localBinder();
210 if (!local) {
211 BpBinder *proxy = binder->remoteBinder();
212 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000213 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 }
215 const int32_t handle = proxy ? proxy->handle() : 0;
216 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800217 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800219 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 } else {
221 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
223 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 }
225 } else {
226 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800227 obj.binder = 0;
228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 return finish_flatten_binder(binder, obj, out);
232}
233
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800234status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 const wp<IBinder>& binder, Parcel* out)
236{
237 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
240 if (binder != NULL) {
241 sp<IBinder> real = binder.promote();
242 if (real != NULL) {
243 IBinder *local = real->localBinder();
244 if (!local) {
245 BpBinder *proxy = real->remoteBinder();
246 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000247 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
249 const int32_t handle = proxy ? proxy->handle() : 0;
250 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800251 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 } else {
255 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
257 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 }
259 return finish_flatten_binder(real, obj, out);
260 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 // XXX How to deal? In order to flatten the given binder,
263 // we need to probe it for information, which requires a primary
264 // reference... but we don't have one.
265 //
266 // The OpenBinder implementation uses a dynamic_cast<> here,
267 // but we can't do that with the different reference counting
268 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000269 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700270 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800271 obj.binder = 0;
272 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 } else {
276 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800277 obj.binder = 0;
278 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 return finish_flatten_binder(NULL, obj, out);
280 }
281}
282
283inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800284 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
285 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286{
287 return NO_ERROR;
288}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290status_t unflatten_binder(const sp<ProcessState>& proc,
291 const Parcel& in, sp<IBinder>* out)
292{
293 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295 if (flat) {
296 switch (flat->type) {
297 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800298 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299 return finish_unflatten_binder(NULL, *flat, in);
300 case BINDER_TYPE_HANDLE:
301 *out = proc->getStrongProxyForHandle(flat->handle);
302 return finish_unflatten_binder(
303 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700305 }
306 return BAD_TYPE;
307}
308
309status_t unflatten_binder(const sp<ProcessState>& proc,
310 const Parcel& in, wp<IBinder>* out)
311{
312 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 if (flat) {
315 switch (flat->type) {
316 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800317 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 return finish_unflatten_binder(NULL, *flat, in);
319 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 reinterpret_cast<IBinder*>(flat->cookie),
323 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 } else {
325 *out = NULL;
326 }
327 return finish_unflatten_binder(NULL, *flat, in);
328 case BINDER_TYPE_HANDLE:
329 case BINDER_TYPE_WEAK_HANDLE:
330 *out = proc->getWeakProxyForHandle(flat->handle);
331 return finish_unflatten_binder(
332 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
333 }
334 }
335 return BAD_TYPE;
336}
337
338// ---------------------------------------------------------------------------
339
340Parcel::Parcel()
341{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800342 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343 initState();
344}
345
346Parcel::~Parcel()
347{
348 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: destroyed", this);
350}
351
352size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800353 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
354 size_t size = gParcelGlobalAllocSize;
355 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
356 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800357}
358
359size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800360 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
361 size_t count = gParcelGlobalAllocCount;
362 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
363 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700364}
365
366const uint8_t* Parcel::data() const
367{
368 return mData;
369}
370
371size_t Parcel::dataSize() const
372{
373 return (mDataSize > mDataPos ? mDataSize : mDataPos);
374}
375
376size_t Parcel::dataAvail() const
377{
378 // TODO: decide what to do about the possibility that this can
379 // report an available-data size that exceeds a Java int's max
380 // positive value, causing havoc. Fortunately this will only
381 // happen if someone constructs a Parcel containing more than two
382 // gigabytes of data, which on typical phone hardware is simply
383 // not possible.
384 return dataSize() - dataPosition();
385}
386
387size_t Parcel::dataPosition() const
388{
389 return mDataPos;
390}
391
392size_t Parcel::dataCapacity() const
393{
394 return mDataCapacity;
395}
396
397status_t Parcel::setDataSize(size_t size)
398{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700399 if (size > INT32_MAX) {
400 // don't accept size_t values which may have come from an
401 // inadvertent conversion from a negative int.
402 return BAD_VALUE;
403 }
404
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700405 status_t err;
406 err = continueWrite(size);
407 if (err == NO_ERROR) {
408 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700409 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700410 }
411 return err;
412}
413
414void Parcel::setDataPosition(size_t pos) const
415{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700416 if (pos > INT32_MAX) {
417 // don't accept size_t values which may have come from an
418 // inadvertent conversion from a negative int.
419 abort();
420 }
421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 mDataPos = pos;
423 mNextObjectHint = 0;
424}
425
426status_t Parcel::setDataCapacity(size_t size)
427{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700428 if (size > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
431 return BAD_VALUE;
432 }
433
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700434 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700435 return NO_ERROR;
436}
437
438status_t Parcel::setData(const uint8_t* buffer, size_t len)
439{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700440 if (len > INT32_MAX) {
441 // don't accept size_t values which may have come from an
442 // inadvertent conversion from a negative int.
443 return BAD_VALUE;
444 }
445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 status_t err = restartWrite(len);
447 if (err == NO_ERROR) {
448 memcpy(const_cast<uint8_t*>(data()), buffer, len);
449 mDataSize = len;
450 mFdsKnown = false;
451 }
452 return err;
453}
454
Andreas Huber51faf462011-04-13 10:21:56 -0700455status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456{
457 const sp<ProcessState> proc(ProcessState::self());
458 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700459 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800460 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461 size_t size = parcel->mObjectsSize;
462 int startPos = mDataPos;
463 int firstIndex = -1, lastIndex = -2;
464
465 if (len == 0) {
466 return NO_ERROR;
467 }
468
Nick Kralevichb6b14232015-04-02 09:36:02 -0700469 if (len > INT32_MAX) {
470 // don't accept size_t values which may have come from an
471 // inadvertent conversion from a negative int.
472 return BAD_VALUE;
473 }
474
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475 // range checks against the source parcel size
476 if ((offset > parcel->mDataSize)
477 || (len > parcel->mDataSize)
478 || (offset + len > parcel->mDataSize)) {
479 return BAD_VALUE;
480 }
481
482 // Count objects in range
483 for (int i = 0; i < (int) size; i++) {
484 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700485 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486 if (firstIndex == -1) {
487 firstIndex = i;
488 }
489 lastIndex = i;
490 }
491 }
492 int numObjects = lastIndex - firstIndex + 1;
493
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700494 if ((mDataSize+len) > mDataCapacity) {
495 // grow data
496 err = growData(len);
497 if (err != NO_ERROR) {
498 return err;
499 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500 }
501
502 // append data
503 memcpy(mData + mDataPos, data + offset, len);
504 mDataPos += len;
505 mDataSize += len;
506
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400507 err = NO_ERROR;
508
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 if (numObjects > 0) {
510 // grow objects
511 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700512 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
513 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800514 binder_size_t *objects =
515 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
516 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700517 return NO_MEMORY;
518 }
519 mObjects = objects;
520 mObjectsCapacity = newSize;
521 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 // append and acquire objects
524 int idx = mObjectsSize;
525 for (int i = firstIndex; i <= lastIndex; i++) {
526 size_t off = objects[i] - offset + startPos;
527 mObjects[idx++] = off;
528 mObjectsSize++;
529
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700530 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700532 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700535 // If this is a file descriptor, we need to dup it so the
536 // new Parcel now owns its own fd, and can declare that we
537 // officially know we have fds.
538 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800539 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400541 if (!mAllowFds) {
542 err = FDS_NOT_ALLOWED;
543 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 }
545 }
546 }
547
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400548 return err;
549}
550
Jeff Brown13b16042014-11-11 16:44:25 -0800551bool Parcel::allowFds() const
552{
553 return mAllowFds;
554}
555
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700556bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400557{
558 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700559 if (!allowFds) {
560 mAllowFds = false;
561 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400562 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700563}
564
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700565void Parcel::restoreAllowFds(bool lastValue)
566{
567 mAllowFds = lastValue;
568}
569
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570bool Parcel::hasFileDescriptors() const
571{
572 if (!mFdsKnown) {
573 scanForFds();
574 }
575 return mHasFds;
576}
577
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700578// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579status_t Parcel::writeInterfaceToken(const String16& interface)
580{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700581 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
582 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583 // currently the interface identification token is just its name as a string
584 return writeString16(interface);
585}
586
Mathias Agopian83c04462009-05-22 19:00:22 -0700587bool Parcel::checkInterface(IBinder* binder) const
588{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700589 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700590}
591
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700592bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700593 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700595 int32_t strictPolicy = readInt32();
596 if (threadState == NULL) {
597 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700598 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700599 if ((threadState->getLastTransactionBinderFlags() &
600 IBinder::FLAG_ONEWAY) != 0) {
601 // For one-way calls, the callee is running entirely
602 // disconnected from the caller, so disable StrictMode entirely.
603 // Not only does disk/network usage not impact the caller, but
604 // there's no way to commuicate back any violations anyway.
605 threadState->setStrictModePolicy(0);
606 } else {
607 threadState->setStrictModePolicy(strictPolicy);
608 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700609 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610 if (str == interface) {
611 return true;
612 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700613 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 String8(interface).string(), String8(str).string());
615 return false;
616 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700617}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800619const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620{
621 return mObjects;
622}
623
624size_t Parcel::objectsCount() const
625{
626 return mObjectsSize;
627}
628
629status_t Parcel::errorCheck() const
630{
631 return mError;
632}
633
634void Parcel::setError(status_t err)
635{
636 mError = err;
637}
638
639status_t Parcel::finishWrite(size_t len)
640{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700641 if (len > INT32_MAX) {
642 // don't accept size_t values which may have come from an
643 // inadvertent conversion from a negative int.
644 return BAD_VALUE;
645 }
646
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647 //printf("Finish write of %d\n", len);
648 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700649 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650 if (mDataPos > mDataSize) {
651 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700652 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 }
654 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
655 return NO_ERROR;
656}
657
658status_t Parcel::writeUnpadded(const void* data, size_t len)
659{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700660 if (len > INT32_MAX) {
661 // don't accept size_t values which may have come from an
662 // inadvertent conversion from a negative int.
663 return BAD_VALUE;
664 }
665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 size_t end = mDataPos + len;
667 if (end < mDataPos) {
668 // integer overflow
669 return BAD_VALUE;
670 }
671
672 if (end <= mDataCapacity) {
673restart_write:
674 memcpy(mData+mDataPos, data, len);
675 return finishWrite(len);
676 }
677
678 status_t err = growData(len);
679 if (err == NO_ERROR) goto restart_write;
680 return err;
681}
682
683status_t Parcel::write(const void* data, size_t len)
684{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700685 if (len > INT32_MAX) {
686 // don't accept size_t values which may have come from an
687 // inadvertent conversion from a negative int.
688 return BAD_VALUE;
689 }
690
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700691 void* const d = writeInplace(len);
692 if (d) {
693 memcpy(d, data, len);
694 return NO_ERROR;
695 }
696 return mError;
697}
698
699void* Parcel::writeInplace(size_t len)
700{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700701 if (len > INT32_MAX) {
702 // don't accept size_t values which may have come from an
703 // inadvertent conversion from a negative int.
704 return NULL;
705 }
706
707 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700708
709 // sanity check for integer overflow
710 if (mDataPos+padded < mDataPos) {
711 return NULL;
712 }
713
714 if ((mDataPos+padded) <= mDataCapacity) {
715restart_write:
716 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
717 uint8_t* const data = mData+mDataPos;
718
719 // Need to pad at end?
720 if (padded != len) {
721#if BYTE_ORDER == BIG_ENDIAN
722 static const uint32_t mask[4] = {
723 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
724 };
725#endif
726#if BYTE_ORDER == LITTLE_ENDIAN
727 static const uint32_t mask[4] = {
728 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
729 };
730#endif
731 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
732 // *reinterpret_cast<void**>(data+padded-4));
733 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
734 }
735
736 finishWrite(padded);
737 return data;
738 }
739
740 status_t err = growData(padded);
741 if (err == NO_ERROR) goto restart_write;
742 return NULL;
743}
744
Casey Dahlinb9872622015-11-25 15:09:45 -0800745status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
746{
747 if (!val) {
748 return writeInt32(-1);
749 }
750
751 return writeByteVector(*val);
752}
753
Casey Dahlin451ff582015-10-19 18:12:18 -0700754status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
755{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700756 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700757 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700758 status = BAD_VALUE;
759 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700760 }
761
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700762 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700763 if (status != OK) {
764 return status;
765 }
766
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700767 void* data = writeInplace(val.size());
768 if (!data) {
769 status = BAD_VALUE;
770 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700771 }
772
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700773 memcpy(data, val.data(), val.size());
774 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700775}
776
777status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
778{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800779 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700780}
781
Casey Dahlinb9872622015-11-25 15:09:45 -0800782status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
783{
784 return writeNullableTypedVector(val, &Parcel::writeInt32);
785}
786
Casey Dahlin451ff582015-10-19 18:12:18 -0700787status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
788{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800789 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700790}
791
Casey Dahlinb9872622015-11-25 15:09:45 -0800792status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
793{
794 return writeNullableTypedVector(val, &Parcel::writeInt64);
795}
796
Casey Dahlin451ff582015-10-19 18:12:18 -0700797status_t Parcel::writeFloatVector(const std::vector<float>& val)
798{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800799 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700800}
801
Casey Dahlinb9872622015-11-25 15:09:45 -0800802status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
803{
804 return writeNullableTypedVector(val, &Parcel::writeFloat);
805}
806
Casey Dahlin451ff582015-10-19 18:12:18 -0700807status_t Parcel::writeDoubleVector(const std::vector<double>& val)
808{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800809 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700810}
811
Casey Dahlinb9872622015-11-25 15:09:45 -0800812status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
813{
814 return writeNullableTypedVector(val, &Parcel::writeDouble);
815}
816
Casey Dahlin451ff582015-10-19 18:12:18 -0700817status_t Parcel::writeBoolVector(const std::vector<bool>& val)
818{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800819 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700820}
821
Casey Dahlinb9872622015-11-25 15:09:45 -0800822status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
823{
824 return writeNullableTypedVector(val, &Parcel::writeBool);
825}
826
Casey Dahlin451ff582015-10-19 18:12:18 -0700827status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
828{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800829 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700830}
831
Casey Dahlinb9872622015-11-25 15:09:45 -0800832status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
833{
834 return writeNullableTypedVector(val, &Parcel::writeChar);
835}
836
Casey Dahlin451ff582015-10-19 18:12:18 -0700837status_t Parcel::writeString16Vector(const std::vector<String16>& val)
838{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800839 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700840}
841
Casey Dahlinb9872622015-11-25 15:09:45 -0800842status_t Parcel::writeString16Vector(
843 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
844{
845 return writeNullableTypedVector(val, &Parcel::writeString16);
846}
847
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700848status_t Parcel::writeInt32(int32_t val)
849{
Andreas Huber84a6d042009-08-17 13:33:27 -0700850 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700851}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800852
853status_t Parcel::writeUint32(uint32_t val)
854{
855 return writeAligned(val);
856}
857
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700858status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700859 if (len > INT32_MAX) {
860 // don't accept size_t values which may have come from an
861 // inadvertent conversion from a negative int.
862 return BAD_VALUE;
863 }
864
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700865 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700866 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700867 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700868 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700869 if (ret == NO_ERROR) {
870 ret = write(val, len * sizeof(*val));
871 }
872 return ret;
873}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700874status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700875 if (len > INT32_MAX) {
876 // don't accept size_t values which may have come from an
877 // inadvertent conversion from a negative int.
878 return BAD_VALUE;
879 }
880
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700881 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700882 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700883 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700884 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700885 if (ret == NO_ERROR) {
886 ret = write(val, len * sizeof(*val));
887 }
888 return ret;
889}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700890
Casey Dahlind6848f52015-10-15 15:44:59 -0700891status_t Parcel::writeBool(bool val)
892{
893 return writeInt32(int32_t(val));
894}
895
896status_t Parcel::writeChar(char16_t val)
897{
898 return writeInt32(int32_t(val));
899}
900
901status_t Parcel::writeByte(int8_t val)
902{
903 return writeInt32(int32_t(val));
904}
905
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700906status_t Parcel::writeInt64(int64_t val)
907{
Andreas Huber84a6d042009-08-17 13:33:27 -0700908 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909}
910
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700911status_t Parcel::writeUint64(uint64_t val)
912{
913 return writeAligned(val);
914}
915
Serban Constantinescuf683e012013-11-05 16:53:55 +0000916status_t Parcel::writePointer(uintptr_t val)
917{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800918 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000919}
920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921status_t Parcel::writeFloat(float val)
922{
Andreas Huber84a6d042009-08-17 13:33:27 -0700923 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700924}
925
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800926#if defined(__mips__) && defined(__mips_hard_float)
927
928status_t Parcel::writeDouble(double val)
929{
930 union {
931 double d;
932 unsigned long long ll;
933 } u;
934 u.d = val;
935 return writeAligned(u.ll);
936}
937
938#else
939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940status_t Parcel::writeDouble(double val)
941{
Andreas Huber84a6d042009-08-17 13:33:27 -0700942 return writeAligned(val);
943}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700944
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800945#endif
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947status_t Parcel::writeCString(const char* str)
948{
949 return write(str, strlen(str)+1);
950}
951
952status_t Parcel::writeString8(const String8& str)
953{
954 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100955 // only write string if its length is more than zero characters,
956 // as readString8 will only read if the length field is non-zero.
957 // this is slightly different from how writeString16 works.
958 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959 err = write(str.string(), str.bytes()+1);
960 }
961 return err;
962}
963
Casey Dahlinb9872622015-11-25 15:09:45 -0800964status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
965{
966 if (!str) {
967 return writeInt32(-1);
968 }
969
970 return writeString16(*str);
971}
972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973status_t Parcel::writeString16(const String16& str)
974{
975 return writeString16(str.string(), str.size());
976}
977
978status_t Parcel::writeString16(const char16_t* str, size_t len)
979{
980 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982 status_t err = writeInt32(len);
983 if (err == NO_ERROR) {
984 len *= sizeof(char16_t);
985 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
986 if (data) {
987 memcpy(data, str, len);
988 *reinterpret_cast<char16_t*>(data+len) = 0;
989 return NO_ERROR;
990 }
991 err = mError;
992 }
993 return err;
994}
995
996status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
997{
998 return flatten_binder(ProcessState::self(), val, this);
999}
1000
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001001status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1002{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001003 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001004}
1005
Casey Dahlinb9872622015-11-25 15:09:45 -08001006status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1007{
1008 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1009}
1010
1011status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1012 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1013}
1014
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001015status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001016 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001017}
1018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1020{
1021 return flatten_binder(ProcessState::self(), val, this);
1022}
1023
Casey Dahlinb9872622015-11-25 15:09:45 -08001024status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1025 if (!parcelable) {
1026 return writeInt32(0);
1027 }
1028
1029 return writeParcelable(*parcelable);
1030}
1031
Christopher Wiley97f048d2015-11-19 06:49:05 -08001032status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1033 status_t status = writeInt32(1); // parcelable is not null.
1034 if (status != OK) {
1035 return status;
1036 }
1037 return parcelable.writeToParcel(this);
1038}
1039
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001040status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001041{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001042 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001043 return BAD_TYPE;
1044
1045 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001046 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001047 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001049 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001050 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001052 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1053 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054
1055 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001056 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 return err;
1058 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001060 return err;
1061}
1062
Jeff Brown93ff1f92011-11-04 19:01:44 -07001063status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064{
1065 flat_binder_object obj;
1066 obj.type = BINDER_TYPE_FD;
1067 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001068 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001070 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071 return writeObject(obj, true);
1072}
1073
1074status_t Parcel::writeDupFileDescriptor(int fd)
1075{
Jeff Brownd341c712011-11-04 20:19:33 -07001076 int dupFd = dup(fd);
1077 if (dupFd < 0) {
1078 return -errno;
1079 }
1080 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001081 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001082 close(dupFd);
1083 }
1084 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085}
1086
Casey Dahlin06673e32015-11-23 13:24:23 -08001087status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1088 return writeDupFileDescriptor(fd.get());
1089}
1090
1091status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1092 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1093}
1094
Casey Dahlinb9872622015-11-25 15:09:45 -08001095status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1096 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1097}
1098
Jeff Brown13b16042014-11-11 16:44:25 -08001099status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001100{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001101 if (len > INT32_MAX) {
1102 // don't accept size_t values which may have come from an
1103 // inadvertent conversion from a negative int.
1104 return BAD_VALUE;
1105 }
1106
Jeff Brown13b16042014-11-11 16:44:25 -08001107 status_t status;
1108 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001109 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001110 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001111 if (status) return status;
1112
1113 void* ptr = writeInplace(len);
1114 if (!ptr) return NO_MEMORY;
1115
Jeff Brown13b16042014-11-11 16:44:25 -08001116 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001117 return NO_ERROR;
1118 }
1119
Steve Block6807e592011-10-20 11:56:00 +01001120 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001121 int fd = ashmem_create_region("Parcel Blob", len);
1122 if (fd < 0) return NO_MEMORY;
1123
1124 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1125 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001126 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001127 } else {
1128 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1129 if (ptr == MAP_FAILED) {
1130 status = -errno;
1131 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001132 if (!mutableCopy) {
1133 result = ashmem_set_prot_region(fd, PROT_READ);
1134 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001135 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001136 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001137 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001138 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001139 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001140 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001141 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001142 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001143 return NO_ERROR;
1144 }
1145 }
1146 }
1147 }
1148 ::munmap(ptr, len);
1149 }
1150 ::close(fd);
1151 return status;
1152}
1153
Jeff Brown13b16042014-11-11 16:44:25 -08001154status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1155{
1156 // Must match up with what's done in writeBlob.
1157 if (!mAllowFds) return FDS_NOT_ALLOWED;
1158 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1159 if (status) return status;
1160 return writeDupFileDescriptor(fd);
1161}
1162
Mathias Agopiane1424282013-07-29 21:24:40 -07001163status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001164{
1165 status_t err;
1166
1167 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001168 const size_t len = val.getFlattenedSize();
1169 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001170
Nick Kralevichb6b14232015-04-02 09:36:02 -07001171 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1172 // don't accept size_t values which may have come from an
1173 // inadvertent conversion from a negative int.
1174 return BAD_VALUE;
1175 }
1176
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001177 err = this->writeInt32(len);
1178 if (err) return err;
1179
1180 err = this->writeInt32(fd_count);
1181 if (err) return err;
1182
1183 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001184 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001185 if (buf == NULL)
1186 return BAD_VALUE;
1187
1188 int* fds = NULL;
1189 if (fd_count) {
1190 fds = new int[fd_count];
1191 }
1192
1193 err = val.flatten(buf, len, fds, fd_count);
1194 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1195 err = this->writeDupFileDescriptor( fds[i] );
1196 }
1197
1198 if (fd_count) {
1199 delete [] fds;
1200 }
1201
1202 return err;
1203}
1204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1206{
1207 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1208 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1209 if (enoughData && enoughObjects) {
1210restart_write:
1211 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001212
Christopher Tate98e67d32015-06-03 18:44:15 -07001213 // remember if it's a file descriptor
1214 if (val.type == BINDER_TYPE_FD) {
1215 if (!mAllowFds) {
1216 // fail before modifying our object index
1217 return FDS_NOT_ALLOWED;
1218 }
1219 mHasFds = mFdsKnown = true;
1220 }
1221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001222 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001223 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001224 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001225 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001226 mObjectsSize++;
1227 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 return finishWrite(sizeof(flat_binder_object));
1230 }
1231
1232 if (!enoughData) {
1233 const status_t err = growData(sizeof(val));
1234 if (err != NO_ERROR) return err;
1235 }
1236 if (!enoughObjects) {
1237 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001238 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001239 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001240 if (objects == NULL) return NO_MEMORY;
1241 mObjects = objects;
1242 mObjectsCapacity = newSize;
1243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001245 goto restart_write;
1246}
1247
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001248status_t Parcel::writeNoException()
1249{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001250 binder::Status status;
1251 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001252}
1253
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001254void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001255{
1256 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1257}
1258
1259status_t Parcel::read(void* outData, size_t len) const
1260{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001261 if (len > INT32_MAX) {
1262 // don't accept size_t values which may have come from an
1263 // inadvertent conversion from a negative int.
1264 return BAD_VALUE;
1265 }
1266
1267 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1268 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001269 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001270 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001271 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 return NO_ERROR;
1273 }
1274 return NOT_ENOUGH_DATA;
1275}
1276
1277const void* Parcel::readInplace(size_t len) const
1278{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001279 if (len > INT32_MAX) {
1280 // don't accept size_t values which may have come from an
1281 // inadvertent conversion from a negative int.
1282 return NULL;
1283 }
1284
1285 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1286 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001288 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001289 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001290 return data;
1291 }
1292 return NULL;
1293}
1294
Andreas Huber84a6d042009-08-17 13:33:27 -07001295template<class T>
1296status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001297 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001298
1299 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001300 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001301 mDataPos += sizeof(T);
1302 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 return NO_ERROR;
1304 } else {
1305 return NOT_ENOUGH_DATA;
1306 }
1307}
1308
Andreas Huber84a6d042009-08-17 13:33:27 -07001309template<class T>
1310T Parcel::readAligned() const {
1311 T result;
1312 if (readAligned(&result) != NO_ERROR) {
1313 result = 0;
1314 }
1315
1316 return result;
1317}
1318
1319template<class T>
1320status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001321 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001322
1323 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1324restart_write:
1325 *reinterpret_cast<T*>(mData+mDataPos) = val;
1326 return finishWrite(sizeof(val));
1327 }
1328
1329 status_t err = growData(sizeof(val));
1330 if (err == NO_ERROR) goto restart_write;
1331 return err;
1332}
1333
Casey Dahlin451ff582015-10-19 18:12:18 -07001334status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1335 val->clear();
1336
1337 int32_t size;
1338 status_t status = readInt32(&size);
1339
1340 if (status != OK) {
1341 return status;
1342 }
1343
Christopher Wiley4db672d2015-11-10 09:44:30 -08001344 if (size < 0) {
1345 status = UNEXPECTED_NULL;
1346 return status;
1347 }
1348 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001349 status = BAD_VALUE;
1350 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001351 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001352
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001353 const void* data = readInplace(size);
1354 if (!data) {
1355 status = BAD_VALUE;
1356 return status;
1357 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001358 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001359 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001360
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001361 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001362}
1363
Casey Dahlinb9872622015-11-25 15:09:45 -08001364status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1365 const int32_t start = dataPosition();
1366 int32_t size;
1367 status_t status = readInt32(&size);
1368 val->reset();
1369
1370 if (status != OK || size < 0) {
1371 return status;
1372 }
1373
1374 setDataPosition(start);
1375 val->reset(new std::vector<int8_t>());
1376
1377 status = readByteVector(val->get());
1378
1379 if (status != OK) {
1380 val->reset();
1381 }
1382
1383 return status;
1384}
1385
1386status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1387 return readNullableTypedVector(val, &Parcel::readInt32);
1388}
1389
Casey Dahlin451ff582015-10-19 18:12:18 -07001390status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001391 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001392}
1393
Casey Dahlinb9872622015-11-25 15:09:45 -08001394status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1395 return readNullableTypedVector(val, &Parcel::readInt64);
1396}
1397
Casey Dahlin451ff582015-10-19 18:12:18 -07001398status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001399 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001400}
1401
Casey Dahlinb9872622015-11-25 15:09:45 -08001402status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1403 return readNullableTypedVector(val, &Parcel::readFloat);
1404}
1405
Casey Dahlin451ff582015-10-19 18:12:18 -07001406status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001407 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001408}
1409
Casey Dahlinb9872622015-11-25 15:09:45 -08001410status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1411 return readNullableTypedVector(val, &Parcel::readDouble);
1412}
1413
Casey Dahlin451ff582015-10-19 18:12:18 -07001414status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001415 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001416}
1417
Casey Dahlinb9872622015-11-25 15:09:45 -08001418status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1419 const int32_t start = dataPosition();
1420 int32_t size;
1421 status_t status = readInt32(&size);
1422 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001423
Casey Dahlinb9872622015-11-25 15:09:45 -08001424 if (status != OK || size < 0) {
1425 return status;
1426 }
1427
1428 setDataPosition(start);
1429 val->reset(new std::vector<bool>());
1430
1431 status = readBoolVector(val->get());
1432
1433 if (status != OK) {
1434 val->reset();
1435 }
1436
1437 return status;
1438}
1439
1440status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001441 int32_t size;
1442 status_t status = readInt32(&size);
1443
1444 if (status != OK) {
1445 return status;
1446 }
1447
1448 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001449 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001450 }
1451
1452 val->resize(size);
1453
1454 /* C++ bool handling means a vector of bools isn't necessarily addressable
1455 * (we might use individual bits)
1456 */
Christopher Wiley97887982015-10-27 16:33:47 -07001457 bool data;
1458 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001459 status = readBool(&data);
1460 (*val)[i] = data;
1461
1462 if (status != OK) {
1463 return status;
1464 }
1465 }
1466
1467 return OK;
1468}
1469
Casey Dahlinb9872622015-11-25 15:09:45 -08001470status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1471 return readNullableTypedVector(val, &Parcel::readChar);
1472}
1473
Casey Dahlin451ff582015-10-19 18:12:18 -07001474status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001475 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001476}
1477
Casey Dahlinb9872622015-11-25 15:09:45 -08001478status_t Parcel::readString16Vector(
1479 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1480 return readNullableTypedVector(val, &Parcel::readString16);
1481}
1482
Casey Dahlin451ff582015-10-19 18:12:18 -07001483status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001484 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001485}
1486
1487
Andreas Huber84a6d042009-08-17 13:33:27 -07001488status_t Parcel::readInt32(int32_t *pArg) const
1489{
1490 return readAligned(pArg);
1491}
1492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001493int32_t Parcel::readInt32() const
1494{
Andreas Huber84a6d042009-08-17 13:33:27 -07001495 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496}
1497
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001498status_t Parcel::readUint32(uint32_t *pArg) const
1499{
1500 return readAligned(pArg);
1501}
1502
1503uint32_t Parcel::readUint32() const
1504{
1505 return readAligned<uint32_t>();
1506}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001507
1508status_t Parcel::readInt64(int64_t *pArg) const
1509{
Andreas Huber84a6d042009-08-17 13:33:27 -07001510 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511}
1512
1513
1514int64_t Parcel::readInt64() const
1515{
Andreas Huber84a6d042009-08-17 13:33:27 -07001516 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001517}
1518
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001519status_t Parcel::readUint64(uint64_t *pArg) const
1520{
1521 return readAligned(pArg);
1522}
1523
1524uint64_t Parcel::readUint64() const
1525{
1526 return readAligned<uint64_t>();
1527}
1528
Serban Constantinescuf683e012013-11-05 16:53:55 +00001529status_t Parcel::readPointer(uintptr_t *pArg) const
1530{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001531 status_t ret;
1532 binder_uintptr_t ptr;
1533 ret = readAligned(&ptr);
1534 if (!ret)
1535 *pArg = ptr;
1536 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001537}
1538
1539uintptr_t Parcel::readPointer() const
1540{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001541 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001542}
1543
1544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545status_t Parcel::readFloat(float *pArg) const
1546{
Andreas Huber84a6d042009-08-17 13:33:27 -07001547 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548}
1549
1550
1551float Parcel::readFloat() const
1552{
Andreas Huber84a6d042009-08-17 13:33:27 -07001553 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554}
1555
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001556#if defined(__mips__) && defined(__mips_hard_float)
1557
1558status_t Parcel::readDouble(double *pArg) const
1559{
1560 union {
1561 double d;
1562 unsigned long long ll;
1563 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001564 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001565 status_t status;
1566 status = readAligned(&u.ll);
1567 *pArg = u.d;
1568 return status;
1569}
1570
1571double Parcel::readDouble() const
1572{
1573 union {
1574 double d;
1575 unsigned long long ll;
1576 } u;
1577 u.ll = readAligned<unsigned long long>();
1578 return u.d;
1579}
1580
1581#else
1582
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583status_t Parcel::readDouble(double *pArg) const
1584{
Andreas Huber84a6d042009-08-17 13:33:27 -07001585 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586}
1587
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001588double Parcel::readDouble() const
1589{
Andreas Huber84a6d042009-08-17 13:33:27 -07001590 return readAligned<double>();
1591}
1592
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001593#endif
1594
Andreas Huber84a6d042009-08-17 13:33:27 -07001595status_t Parcel::readIntPtr(intptr_t *pArg) const
1596{
1597 return readAligned(pArg);
1598}
1599
1600
1601intptr_t Parcel::readIntPtr() const
1602{
1603 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001604}
1605
Casey Dahlind6848f52015-10-15 15:44:59 -07001606status_t Parcel::readBool(bool *pArg) const
1607{
1608 int32_t tmp;
1609 status_t ret = readInt32(&tmp);
1610 *pArg = (tmp != 0);
1611 return ret;
1612}
1613
1614bool Parcel::readBool() const
1615{
1616 return readInt32() != 0;
1617}
1618
1619status_t Parcel::readChar(char16_t *pArg) const
1620{
1621 int32_t tmp;
1622 status_t ret = readInt32(&tmp);
1623 *pArg = char16_t(tmp);
1624 return ret;
1625}
1626
1627char16_t Parcel::readChar() const
1628{
1629 return char16_t(readInt32());
1630}
1631
1632status_t Parcel::readByte(int8_t *pArg) const
1633{
1634 int32_t tmp;
1635 status_t ret = readInt32(&tmp);
1636 *pArg = int8_t(tmp);
1637 return ret;
1638}
1639
1640int8_t Parcel::readByte() const
1641{
1642 return int8_t(readInt32());
1643}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001644
1645const char* Parcel::readCString() const
1646{
1647 const size_t avail = mDataSize-mDataPos;
1648 if (avail > 0) {
1649 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1650 // is the string's trailing NUL within the parcel's valid bounds?
1651 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1652 if (eos) {
1653 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001654 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001655 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001656 return str;
1657 }
1658 }
1659 return NULL;
1660}
1661
1662String8 Parcel::readString8() const
1663{
1664 int32_t size = readInt32();
1665 // watch for potential int overflow adding 1 for trailing NUL
1666 if (size > 0 && size < INT32_MAX) {
1667 const char* str = (const char*)readInplace(size+1);
1668 if (str) return String8(str, size);
1669 }
1670 return String8();
1671}
1672
1673String16 Parcel::readString16() const
1674{
1675 size_t len;
1676 const char16_t* str = readString16Inplace(&len);
1677 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001678 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001679 return String16();
1680}
1681
Casey Dahlinb9872622015-11-25 15:09:45 -08001682status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1683{
1684 const int32_t start = dataPosition();
1685 int32_t size;
1686 status_t status = readInt32(&size);
1687 pArg->reset();
1688
1689 if (status != OK || size < 0) {
1690 return status;
1691 }
1692
1693 setDataPosition(start);
1694 pArg->reset(new String16());
1695
1696 status = readString16(pArg->get());
1697
1698 if (status != OK) {
1699 pArg->reset();
1700 }
1701
1702 return status;
1703}
1704
Casey Dahlin451ff582015-10-19 18:12:18 -07001705status_t Parcel::readString16(String16* pArg) const
1706{
1707 size_t len;
1708 const char16_t* str = readString16Inplace(&len);
1709 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001710 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001711 return 0;
1712 } else {
1713 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001714 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001715 }
1716}
1717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1719{
1720 int32_t size = readInt32();
1721 // watch for potential int overflow from size+1
1722 if (size >= 0 && size < INT32_MAX) {
1723 *outLen = size;
1724 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1725 if (str != NULL) {
1726 return str;
1727 }
1728 }
1729 *outLen = 0;
1730 return NULL;
1731}
1732
Casey Dahlinf0c13772015-10-27 18:33:56 -07001733status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1734{
1735 return unflatten_binder(ProcessState::self(), *this, val);
1736}
1737
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738sp<IBinder> Parcel::readStrongBinder() const
1739{
1740 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001741 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742 return val;
1743}
1744
1745wp<IBinder> Parcel::readWeakBinder() const
1746{
1747 wp<IBinder> val;
1748 unflatten_binder(ProcessState::self(), *this, &val);
1749 return val;
1750}
1751
Christopher Wiley97f048d2015-11-19 06:49:05 -08001752status_t Parcel::readParcelable(Parcelable* parcelable) const {
1753 int32_t have_parcelable = 0;
1754 status_t status = readInt32(&have_parcelable);
1755 if (status != OK) {
1756 return status;
1757 }
1758 if (!have_parcelable) {
1759 return UNEXPECTED_NULL;
1760 }
1761 return parcelable->readFromParcel(this);
1762}
1763
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001764int32_t Parcel::readExceptionCode() const
1765{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001766 binder::Status status;
1767 status.readFromParcel(*this);
1768 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001769}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001770
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001771native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001772{
1773 int numFds, numInts;
1774 status_t err;
1775 err = readInt32(&numFds);
1776 if (err != NO_ERROR) return 0;
1777 err = readInt32(&numInts);
1778 if (err != NO_ERROR) return 0;
1779
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001780 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001781 if (!h) {
1782 return 0;
1783 }
1784
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001785 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001786 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001787 if (h->data[i] < 0) err = BAD_VALUE;
1788 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001789 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001790 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001791 native_handle_close(h);
1792 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001793 h = 0;
1794 }
1795 return h;
1796}
1797
1798
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001799int Parcel::readFileDescriptor() const
1800{
1801 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001802
1803 if (flat && flat->type == BINDER_TYPE_FD) {
1804 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807 return BAD_TYPE;
1808}
1809
Casey Dahlin06673e32015-11-23 13:24:23 -08001810status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1811{
1812 int got = readFileDescriptor();
1813
1814 if (got == BAD_TYPE) {
1815 return BAD_TYPE;
1816 }
1817
1818 val->reset(dup(got));
1819
1820 if (val->get() < 0) {
1821 return BAD_VALUE;
1822 }
1823
1824 return OK;
1825}
1826
1827
Casey Dahlinb9872622015-11-25 15:09:45 -08001828status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1829 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1830}
1831
Casey Dahlin06673e32015-11-23 13:24:23 -08001832status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1833 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1834}
1835
Jeff Brown5707dbf2011-09-23 21:17:56 -07001836status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1837{
Jeff Brown13b16042014-11-11 16:44:25 -08001838 int32_t blobType;
1839 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001840 if (status) return status;
1841
Jeff Brown13b16042014-11-11 16:44:25 -08001842 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001843 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001844 const void* ptr = readInplace(len);
1845 if (!ptr) return BAD_VALUE;
1846
Jeff Brown13b16042014-11-11 16:44:25 -08001847 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001848 return NO_ERROR;
1849 }
1850
Steve Block6807e592011-10-20 11:56:00 +01001851 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001852 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001853 int fd = readFileDescriptor();
1854 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1855
Jeff Brown13b16042014-11-11 16:44:25 -08001856 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1857 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001858 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001859
Jeff Brown13b16042014-11-11 16:44:25 -08001860 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001861 return NO_ERROR;
1862}
1863
Mathias Agopiane1424282013-07-29 21:24:40 -07001864status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001865{
1866 // size
1867 const size_t len = this->readInt32();
1868 const size_t fd_count = this->readInt32();
1869
Nick Kralevichb6b14232015-04-02 09:36:02 -07001870 if (len > INT32_MAX) {
1871 // don't accept size_t values which may have come from an
1872 // inadvertent conversion from a negative int.
1873 return BAD_VALUE;
1874 }
1875
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001876 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001877 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001878 if (buf == NULL)
1879 return BAD_VALUE;
1880
1881 int* fds = NULL;
1882 if (fd_count) {
1883 fds = new int[fd_count];
1884 }
1885
1886 status_t err = NO_ERROR;
1887 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001888 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001889 if (fds[i] < 0) {
1890 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001891 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1892 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001893 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001894 }
1895
1896 if (err == NO_ERROR) {
1897 err = val.unflatten(buf, len, fds, fd_count);
1898 }
1899
1900 if (fd_count) {
1901 delete [] fds;
1902 }
1903
1904 return err;
1905}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001906const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1907{
1908 const size_t DPOS = mDataPos;
1909 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1910 const flat_binder_object* obj
1911 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1912 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001913 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001914 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001915 // the object list, so we don't want to check for it when
1916 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001917 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001918 return obj;
1919 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001921 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001922 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923 const size_t N = mObjectsSize;
1924 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001925
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001927 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 // Start at the current hint position, looking for an object at
1931 // the current data position.
1932 if (opos < N) {
1933 while (opos < (N-1) && OBJS[opos] < DPOS) {
1934 opos++;
1935 }
1936 } else {
1937 opos = N-1;
1938 }
1939 if (OBJS[opos] == DPOS) {
1940 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001941 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001942 this, DPOS, opos);
1943 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001944 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001945 return obj;
1946 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001948 // Look backwards for it...
1949 while (opos > 0 && OBJS[opos] > DPOS) {
1950 opos--;
1951 }
1952 if (OBJS[opos] == DPOS) {
1953 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001954 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955 this, DPOS, opos);
1956 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001957 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958 return obj;
1959 }
1960 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001961 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 -07001962 this, DPOS);
1963 }
1964 return NULL;
1965}
1966
1967void Parcel::closeFileDescriptors()
1968{
1969 size_t i = mObjectsSize;
1970 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001971 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972 }
1973 while (i > 0) {
1974 i--;
1975 const flat_binder_object* flat
1976 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1977 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001978 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979 close(flat->handle);
1980 }
1981 }
1982}
1983
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001984uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001986 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987}
1988
1989size_t Parcel::ipcDataSize() const
1990{
1991 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1992}
1993
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001994uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001995{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001996 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997}
1998
1999size_t Parcel::ipcObjectsCount() const
2000{
2001 return mObjectsSize;
2002}
2003
2004void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002005 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002007 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008 freeDataNoInit();
2009 mError = NO_ERROR;
2010 mData = const_cast<uint8_t*>(data);
2011 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002012 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002014 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002015 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016 mObjectsSize = mObjectsCapacity = objectsCount;
2017 mNextObjectHint = 0;
2018 mOwner = relFunc;
2019 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002020 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002021 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002022 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002023 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002024 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002025 mObjectsSize = 0;
2026 break;
2027 }
2028 minOffset = offset + sizeof(flat_binder_object);
2029 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030 scanForFds();
2031}
2032
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002033void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034{
2035 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002036
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 if (errorCheck() != NO_ERROR) {
2038 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002039 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040 } else if (dataSize() > 0) {
2041 const uint8_t* DATA = data();
2042 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002043 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 const size_t N = objectsCount();
2045 for (size_t i=0; i<N; i++) {
2046 const flat_binder_object* flat
2047 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2048 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2049 << TypeCode(flat->type & 0x7f7f7f00)
2050 << " = " << flat->binder;
2051 }
2052 } else {
2053 to << "NULL";
2054 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 to << ")";
2057}
2058
2059void Parcel::releaseObjects()
2060{
2061 const sp<ProcessState> proc(ProcessState::self());
2062 size_t i = mObjectsSize;
2063 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002064 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 while (i > 0) {
2066 i--;
2067 const flat_binder_object* flat
2068 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002069 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070 }
2071}
2072
2073void Parcel::acquireObjects()
2074{
2075 const sp<ProcessState> proc(ProcessState::self());
2076 size_t i = mObjectsSize;
2077 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002078 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 while (i > 0) {
2080 i--;
2081 const flat_binder_object* flat
2082 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002083 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084 }
2085}
2086
2087void Parcel::freeData()
2088{
2089 freeDataNoInit();
2090 initState();
2091}
2092
2093void Parcel::freeDataNoInit()
2094{
2095 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002096 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002097 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2099 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002100 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002101 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002102 if (mData) {
2103 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002104 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002105 if (mDataCapacity <= gParcelGlobalAllocSize) {
2106 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2107 } else {
2108 gParcelGlobalAllocSize = 0;
2109 }
2110 if (gParcelGlobalAllocCount > 0) {
2111 gParcelGlobalAllocCount--;
2112 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002113 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002114 free(mData);
2115 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002116 if (mObjects) free(mObjects);
2117 }
2118}
2119
2120status_t Parcel::growData(size_t len)
2121{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002122 if (len > INT32_MAX) {
2123 // don't accept size_t values which may have come from an
2124 // inadvertent conversion from a negative int.
2125 return BAD_VALUE;
2126 }
2127
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128 size_t newSize = ((mDataSize+len)*3)/2;
2129 return (newSize <= mDataSize)
2130 ? (status_t) NO_MEMORY
2131 : continueWrite(newSize);
2132}
2133
2134status_t Parcel::restartWrite(size_t desired)
2135{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002136 if (desired > INT32_MAX) {
2137 // don't accept size_t values which may have come from an
2138 // inadvertent conversion from a negative int.
2139 return BAD_VALUE;
2140 }
2141
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142 if (mOwner) {
2143 freeData();
2144 return continueWrite(desired);
2145 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002146
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 uint8_t* data = (uint8_t*)realloc(mData, desired);
2148 if (!data && desired > mDataCapacity) {
2149 mError = NO_MEMORY;
2150 return NO_MEMORY;
2151 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002152
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002154
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002156 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002157 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002158 gParcelGlobalAllocSize += desired;
2159 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002160 if (!mData) {
2161 gParcelGlobalAllocCount++;
2162 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002163 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 mData = data;
2165 mDataCapacity = desired;
2166 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002169 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2170 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2171
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 free(mObjects);
2173 mObjects = NULL;
2174 mObjectsSize = mObjectsCapacity = 0;
2175 mNextObjectHint = 0;
2176 mHasFds = false;
2177 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002178 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002180 return NO_ERROR;
2181}
2182
2183status_t Parcel::continueWrite(size_t desired)
2184{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002185 if (desired > INT32_MAX) {
2186 // don't accept size_t values which may have come from an
2187 // inadvertent conversion from a negative int.
2188 return BAD_VALUE;
2189 }
2190
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191 // If shrinking, first adjust for any objects that appear
2192 // after the new data size.
2193 size_t objectsSize = mObjectsSize;
2194 if (desired < mDataSize) {
2195 if (desired == 0) {
2196 objectsSize = 0;
2197 } else {
2198 while (objectsSize > 0) {
2199 if (mObjects[objectsSize-1] < desired)
2200 break;
2201 objectsSize--;
2202 }
2203 }
2204 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002205
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002206 if (mOwner) {
2207 // If the size is going to zero, just release the owner's data.
2208 if (desired == 0) {
2209 freeData();
2210 return NO_ERROR;
2211 }
2212
2213 // If there is a different owner, we need to take
2214 // posession.
2215 uint8_t* data = (uint8_t*)malloc(desired);
2216 if (!data) {
2217 mError = NO_MEMORY;
2218 return NO_MEMORY;
2219 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002220 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002223 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002225 free(data);
2226
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 mError = NO_MEMORY;
2228 return NO_MEMORY;
2229 }
2230
2231 // Little hack to only acquire references on objects
2232 // we will be keeping.
2233 size_t oldObjectsSize = mObjectsSize;
2234 mObjectsSize = objectsSize;
2235 acquireObjects();
2236 mObjectsSize = oldObjectsSize;
2237 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002238
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002239 if (mData) {
2240 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2241 }
2242 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002243 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002244 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2247 mOwner = NULL;
2248
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002249 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002250 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002251 gParcelGlobalAllocSize += desired;
2252 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002253 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002254
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 mData = data;
2256 mObjects = objects;
2257 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002258 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 mDataCapacity = desired;
2260 mObjectsSize = mObjectsCapacity = objectsSize;
2261 mNextObjectHint = 0;
2262
2263 } else if (mData) {
2264 if (objectsSize < mObjectsSize) {
2265 // Need to release refs on any objects we are dropping.
2266 const sp<ProcessState> proc(ProcessState::self());
2267 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2268 const flat_binder_object* flat
2269 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2270 if (flat->type == BINDER_TYPE_FD) {
2271 // will need to rescan because we may have lopped off the only FDs
2272 mFdsKnown = false;
2273 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002274 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002275 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002276 binder_size_t* objects =
2277 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002278 if (objects) {
2279 mObjects = objects;
2280 }
2281 mObjectsSize = objectsSize;
2282 mNextObjectHint = 0;
2283 }
2284
2285 // We own the data, so we can just do a realloc().
2286 if (desired > mDataCapacity) {
2287 uint8_t* data = (uint8_t*)realloc(mData, desired);
2288 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002289 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2290 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002291 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002292 gParcelGlobalAllocSize += desired;
2293 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002294 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 mData = data;
2296 mDataCapacity = desired;
2297 } else if (desired > mDataCapacity) {
2298 mError = NO_MEMORY;
2299 return NO_MEMORY;
2300 }
2301 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002302 if (mDataSize > desired) {
2303 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002304 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002305 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306 if (mDataPos > desired) {
2307 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002308 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 }
2310 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002311
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 } else {
2313 // This is the first data. Easy!
2314 uint8_t* data = (uint8_t*)malloc(desired);
2315 if (!data) {
2316 mError = NO_MEMORY;
2317 return NO_MEMORY;
2318 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002319
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002320 if(!(mDataCapacity == 0 && mObjects == NULL
2321 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002322 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002324
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002325 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002326 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002327 gParcelGlobalAllocSize += desired;
2328 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002329 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 mData = data;
2332 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2334 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 mDataCapacity = desired;
2336 }
2337
2338 return NO_ERROR;
2339}
2340
2341void Parcel::initState()
2342{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002343 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mError = NO_ERROR;
2345 mData = 0;
2346 mDataSize = 0;
2347 mDataCapacity = 0;
2348 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002349 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2350 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 mObjects = NULL;
2352 mObjectsSize = 0;
2353 mObjectsCapacity = 0;
2354 mNextObjectHint = 0;
2355 mHasFds = false;
2356 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002357 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002359 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360}
2361
2362void Parcel::scanForFds() const
2363{
2364 bool hasFds = false;
2365 for (size_t i=0; i<mObjectsSize; i++) {
2366 const flat_binder_object* flat
2367 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2368 if (flat->type == BINDER_TYPE_FD) {
2369 hasFds = true;
2370 break;
2371 }
2372 }
2373 mHasFds = hasFds;
2374 mFdsKnown = true;
2375}
2376
Dan Sandleraa5c2342015-04-10 10:08:45 -04002377size_t Parcel::getBlobAshmemSize() const
2378{
Adrian Roos6bb31142015-10-22 16:46:12 -07002379 // This used to return the size of all blobs that were written to ashmem, now we're returning
2380 // the ashmem currently referenced by this Parcel, which should be equivalent.
2381 // TODO: Remove method once ABI can be changed.
2382 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002383}
2384
Adrian Rooscbf37262015-10-22 16:12:53 -07002385size_t Parcel::getOpenAshmemSize() const
2386{
2387 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002388}
2389
2390// --- Parcel::Blob ---
2391
2392Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002393 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002394}
2395
2396Parcel::Blob::~Blob() {
2397 release();
2398}
2399
2400void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002401 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002402 ::munmap(mData, mSize);
2403 }
2404 clear();
2405}
2406
Jeff Brown13b16042014-11-11 16:44:25 -08002407void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2408 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002409 mData = data;
2410 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002411 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002412}
2413
2414void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002415 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002416 mData = NULL;
2417 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002418 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002419}
2420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421}; // namespace android