blob: 10cdee6813e9d25ba4d2551081d203029d68a17f [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080026#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070027#include <binder/TextOutput.h>
28
Jun Jiangabf8a2c2014-04-29 14:22:10 +080029#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070030#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/Log.h>
32#include <utils/String8.h>
33#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080035#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070036#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Mathias Agopian208059f2009-05-18 15:08:03 -070038#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080039#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080041#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070045#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47#ifndef INT32_MAX
48#define INT32_MAX ((int32_t)(2147483647))
49#endif
50
51#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010052//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080053#define LOG_ALLOC(...)
54//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
56// ---------------------------------------------------------------------------
57
Nick Kralevichb6b14232015-04-02 09:36:02 -070058// This macro should never be used at runtime, as a too large value
59// of s could cause an integer overflow. Instead, you should always
60// use the wrapper function pad_size()
61#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
62
63static size_t pad_size(size_t s) {
64 if (s > (SIZE_T_MAX - 3)) {
65 abort();
66 }
67 return PAD_SIZE_UNSAFE(s);
68}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070069
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080071#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073// XXX This can be made public if we want to provide
74// support for typed data.
75struct small_flat_data
76{
77 uint32_t type;
78 uint32_t data;
79};
80
81namespace android {
82
Dianne Hackborna4cff882014-11-13 17:07:40 -080083static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
84static size_t gParcelGlobalAllocSize = 0;
85static size_t gParcelGlobalAllocCount = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
99 switch (obj.type) {
100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
112 if (b != NULL) {
113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
120 if (b != NULL) b.get_refs()->incWeak(who);
121 return;
122 }
123 case BINDER_TYPE_FD: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700124 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700125 if (outAshmemSize != NULL) {
126 // If we own an ashmem fd, keep track of how much memory it refers to.
127 int size = ashmem_get_size_region(obj.handle);
128 if (size > 0) {
129 *outAshmemSize += size;
130 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700131 }
132 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 return;
134 }
135 }
136
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800137 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138}
139
Adrian Roos6bb31142015-10-22 16:46:12 -0700140void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 const flat_binder_object& obj, const void* who)
142{
Adrian Roos6bb31142015-10-22 16:46:12 -0700143 acquire_object(proc, obj, who, NULL);
144}
145
146static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700147 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148{
149 switch (obj.type) {
150 case BINDER_TYPE_BINDER:
151 if (obj.binder) {
152 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800153 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 }
155 return;
156 case BINDER_TYPE_WEAK_BINDER:
157 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800158 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 return;
160 case BINDER_TYPE_HANDLE: {
161 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
162 if (b != NULL) {
163 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
164 b->decStrong(who);
165 }
166 return;
167 }
168 case BINDER_TYPE_WEAK_HANDLE: {
169 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
170 if (b != NULL) b.get_refs()->decWeak(who);
171 return;
172 }
173 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700174 if (outAshmemSize != NULL) {
175 if (obj.cookie != 0) {
176 int size = ashmem_get_size_region(obj.handle);
177 if (size > 0) {
178 *outAshmemSize -= size;
179 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700180
Adrian Roos6bb31142015-10-22 16:46:12 -0700181 close(obj.handle);
182 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700183 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 return;
185 }
186 }
187
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800188 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189}
190
Adrian Roos6bb31142015-10-22 16:46:12 -0700191void release_object(const sp<ProcessState>& proc,
192 const flat_binder_object& obj, const void* who)
193{
194 release_object(proc, obj, who, NULL);
195}
196
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800198 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199{
200 return out->writeObject(flat, false);
201}
202
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800203status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 const sp<IBinder>& binder, Parcel* out)
205{
206 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
209 if (binder != NULL) {
210 IBinder *local = binder->localBinder();
211 if (!local) {
212 BpBinder *proxy = binder->remoteBinder();
213 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000214 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 }
216 const int32_t handle = proxy ? proxy->handle() : 0;
217 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800218 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 } else {
222 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800223 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
224 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 }
226 } else {
227 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = 0;
229 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 return finish_flatten_binder(binder, obj, out);
233}
234
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800235status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 const wp<IBinder>& binder, Parcel* out)
237{
238 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
241 if (binder != NULL) {
242 sp<IBinder> real = binder.promote();
243 if (real != NULL) {
244 IBinder *local = real->localBinder();
245 if (!local) {
246 BpBinder *proxy = real->remoteBinder();
247 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000248 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
250 const int32_t handle = proxy ? proxy->handle() : 0;
251 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800252 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800254 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 } else {
256 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
258 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 return finish_flatten_binder(real, obj, out);
261 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 // XXX How to deal? In order to flatten the given binder,
264 // we need to probe it for information, which requires a primary
265 // reference... but we don't have one.
266 //
267 // The OpenBinder implementation uses a dynamic_cast<> here,
268 // but we can't do that with the different reference counting
269 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000270 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 obj.binder = 0;
273 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 } else {
277 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800278 obj.binder = 0;
279 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 return finish_flatten_binder(NULL, obj, out);
281 }
282}
283
284inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800285 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
286 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287{
288 return NO_ERROR;
289}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291status_t unflatten_binder(const sp<ProcessState>& proc,
292 const Parcel& in, sp<IBinder>* out)
293{
294 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700295
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 if (flat) {
297 switch (flat->type) {
298 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800299 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 return finish_unflatten_binder(NULL, *flat, in);
301 case BINDER_TYPE_HANDLE:
302 *out = proc->getStrongProxyForHandle(flat->handle);
303 return finish_unflatten_binder(
304 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700305 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 }
307 return BAD_TYPE;
308}
309
310status_t unflatten_binder(const sp<ProcessState>& proc,
311 const Parcel& in, wp<IBinder>* out)
312{
313 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 if (flat) {
316 switch (flat->type) {
317 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800318 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 return finish_unflatten_binder(NULL, *flat, in);
320 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800321 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 reinterpret_cast<IBinder*>(flat->cookie),
324 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 } else {
326 *out = NULL;
327 }
328 return finish_unflatten_binder(NULL, *flat, in);
329 case BINDER_TYPE_HANDLE:
330 case BINDER_TYPE_WEAK_HANDLE:
331 *out = proc->getWeakProxyForHandle(flat->handle);
332 return finish_unflatten_binder(
333 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
334 }
335 }
336 return BAD_TYPE;
337}
338
339// ---------------------------------------------------------------------------
340
341Parcel::Parcel()
342{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800343 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700344 initState();
345}
346
347Parcel::~Parcel()
348{
349 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800350 LOG_ALLOC("Parcel %p: destroyed", this);
351}
352
353size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800354 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
355 size_t size = gParcelGlobalAllocSize;
356 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
357 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800358}
359
360size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800361 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
362 size_t count = gParcelGlobalAllocCount;
363 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
364 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365}
366
367const uint8_t* Parcel::data() const
368{
369 return mData;
370}
371
372size_t Parcel::dataSize() const
373{
374 return (mDataSize > mDataPos ? mDataSize : mDataPos);
375}
376
377size_t Parcel::dataAvail() const
378{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700379 size_t result = dataSize() - dataPosition();
380 if (result > INT32_MAX) {
381 abort();
382 }
383 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384}
385
386size_t Parcel::dataPosition() const
387{
388 return mDataPos;
389}
390
391size_t Parcel::dataCapacity() const
392{
393 return mDataCapacity;
394}
395
396status_t Parcel::setDataSize(size_t size)
397{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700398 if (size > INT32_MAX) {
399 // don't accept size_t values which may have come from an
400 // inadvertent conversion from a negative int.
401 return BAD_VALUE;
402 }
403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 status_t err;
405 err = continueWrite(size);
406 if (err == NO_ERROR) {
407 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700408 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 }
410 return err;
411}
412
413void Parcel::setDataPosition(size_t pos) const
414{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700415 if (pos > INT32_MAX) {
416 // don't accept size_t values which may have come from an
417 // inadvertent conversion from a negative int.
418 abort();
419 }
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 mDataPos = pos;
422 mNextObjectHint = 0;
423}
424
425status_t Parcel::setDataCapacity(size_t size)
426{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 if (size > INT32_MAX) {
428 // don't accept size_t values which may have come from an
429 // inadvertent conversion from a negative int.
430 return BAD_VALUE;
431 }
432
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700433 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 return NO_ERROR;
435}
436
437status_t Parcel::setData(const uint8_t* buffer, size_t len)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (len > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 status_t err = restartWrite(len);
446 if (err == NO_ERROR) {
447 memcpy(const_cast<uint8_t*>(data()), buffer, len);
448 mDataSize = len;
449 mFdsKnown = false;
450 }
451 return err;
452}
453
Andreas Huber51faf462011-04-13 10:21:56 -0700454status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455{
456 const sp<ProcessState> proc(ProcessState::self());
457 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700458 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800459 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 size_t size = parcel->mObjectsSize;
461 int startPos = mDataPos;
462 int firstIndex = -1, lastIndex = -2;
463
464 if (len == 0) {
465 return NO_ERROR;
466 }
467
Nick Kralevichb6b14232015-04-02 09:36:02 -0700468 if (len > INT32_MAX) {
469 // don't accept size_t values which may have come from an
470 // inadvertent conversion from a negative int.
471 return BAD_VALUE;
472 }
473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 // range checks against the source parcel size
475 if ((offset > parcel->mDataSize)
476 || (len > parcel->mDataSize)
477 || (offset + len > parcel->mDataSize)) {
478 return BAD_VALUE;
479 }
480
481 // Count objects in range
482 for (int i = 0; i < (int) size; i++) {
483 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700484 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 if (firstIndex == -1) {
486 firstIndex = i;
487 }
488 lastIndex = i;
489 }
490 }
491 int numObjects = lastIndex - firstIndex + 1;
492
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700493 if ((mDataSize+len) > mDataCapacity) {
494 // grow data
495 err = growData(len);
496 if (err != NO_ERROR) {
497 return err;
498 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 }
500
501 // append data
502 memcpy(mData + mDataPos, data + offset, len);
503 mDataPos += len;
504 mDataSize += len;
505
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400506 err = NO_ERROR;
507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 if (numObjects > 0) {
509 // grow objects
510 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700511 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
512 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800513 binder_size_t *objects =
514 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
515 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 return NO_MEMORY;
517 }
518 mObjects = objects;
519 mObjectsCapacity = newSize;
520 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 // append and acquire objects
523 int idx = mObjectsSize;
524 for (int i = firstIndex; i <= lastIndex; i++) {
525 size_t off = objects[i] - offset + startPos;
526 mObjects[idx++] = off;
527 mObjectsSize++;
528
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700529 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700531 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700534 // If this is a file descriptor, we need to dup it so the
535 // new Parcel now owns its own fd, and can declare that we
536 // officially know we have fds.
537 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800538 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400540 if (!mAllowFds) {
541 err = FDS_NOT_ALLOWED;
542 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 }
545 }
546
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 return err;
548}
549
Jeff Brown13b16042014-11-11 16:44:25 -0800550bool Parcel::allowFds() const
551{
552 return mAllowFds;
553}
554
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700555bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400556{
557 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700558 if (!allowFds) {
559 mAllowFds = false;
560 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562}
563
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700564void Parcel::restoreAllowFds(bool lastValue)
565{
566 mAllowFds = lastValue;
567}
568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569bool Parcel::hasFileDescriptors() const
570{
571 if (!mFdsKnown) {
572 scanForFds();
573 }
574 return mHasFds;
575}
576
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700577// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578status_t Parcel::writeInterfaceToken(const String16& interface)
579{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700580 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
581 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700582 // currently the interface identification token is just its name as a string
583 return writeString16(interface);
584}
585
Mathias Agopian83c04462009-05-22 19:00:22 -0700586bool Parcel::checkInterface(IBinder* binder) const
587{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700588 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700589}
590
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700591bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700592 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700594 int32_t strictPolicy = readInt32();
595 if (threadState == NULL) {
596 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700597 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700598 if ((threadState->getLastTransactionBinderFlags() &
599 IBinder::FLAG_ONEWAY) != 0) {
600 // For one-way calls, the callee is running entirely
601 // disconnected from the caller, so disable StrictMode entirely.
602 // Not only does disk/network usage not impact the caller, but
603 // there's no way to commuicate back any violations anyway.
604 threadState->setStrictModePolicy(0);
605 } else {
606 threadState->setStrictModePolicy(strictPolicy);
607 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700608 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 if (str == interface) {
610 return true;
611 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700612 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613 String8(interface).string(), String8(str).string());
614 return false;
615 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700616}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800618const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619{
620 return mObjects;
621}
622
623size_t Parcel::objectsCount() const
624{
625 return mObjectsSize;
626}
627
628status_t Parcel::errorCheck() const
629{
630 return mError;
631}
632
633void Parcel::setError(status_t err)
634{
635 mError = err;
636}
637
638status_t Parcel::finishWrite(size_t len)
639{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700640 if (len > INT32_MAX) {
641 // don't accept size_t values which may have come from an
642 // inadvertent conversion from a negative int.
643 return BAD_VALUE;
644 }
645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646 //printf("Finish write of %d\n", len);
647 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700648 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 if (mDataPos > mDataSize) {
650 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700651 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 }
653 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
654 return NO_ERROR;
655}
656
657status_t Parcel::writeUnpadded(const void* data, size_t len)
658{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700659 if (len > INT32_MAX) {
660 // don't accept size_t values which may have come from an
661 // inadvertent conversion from a negative int.
662 return BAD_VALUE;
663 }
664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700665 size_t end = mDataPos + len;
666 if (end < mDataPos) {
667 // integer overflow
668 return BAD_VALUE;
669 }
670
671 if (end <= mDataCapacity) {
672restart_write:
673 memcpy(mData+mDataPos, data, len);
674 return finishWrite(len);
675 }
676
677 status_t err = growData(len);
678 if (err == NO_ERROR) goto restart_write;
679 return err;
680}
681
682status_t Parcel::write(const void* data, size_t len)
683{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700684 if (len > INT32_MAX) {
685 // don't accept size_t values which may have come from an
686 // inadvertent conversion from a negative int.
687 return BAD_VALUE;
688 }
689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690 void* const d = writeInplace(len);
691 if (d) {
692 memcpy(d, data, len);
693 return NO_ERROR;
694 }
695 return mError;
696}
697
698void* Parcel::writeInplace(size_t len)
699{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700700 if (len > INT32_MAX) {
701 // don't accept size_t values which may have come from an
702 // inadvertent conversion from a negative int.
703 return NULL;
704 }
705
706 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700707
708 // sanity check for integer overflow
709 if (mDataPos+padded < mDataPos) {
710 return NULL;
711 }
712
713 if ((mDataPos+padded) <= mDataCapacity) {
714restart_write:
715 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
716 uint8_t* const data = mData+mDataPos;
717
718 // Need to pad at end?
719 if (padded != len) {
720#if BYTE_ORDER == BIG_ENDIAN
721 static const uint32_t mask[4] = {
722 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
723 };
724#endif
725#if BYTE_ORDER == LITTLE_ENDIAN
726 static const uint32_t mask[4] = {
727 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
728 };
729#endif
730 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
731 // *reinterpret_cast<void**>(data+padded-4));
732 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
733 }
734
735 finishWrite(padded);
736 return data;
737 }
738
739 status_t err = growData(padded);
740 if (err == NO_ERROR) goto restart_write;
741 return NULL;
742}
743
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800744status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
745{
746 if (!val) {
747 return writeInt32(-1);
748 }
749
750 return writeByteVector(*val);
751}
752
Casey Dahlin451ff582015-10-19 18:12:18 -0700753status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
754{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700755 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700756 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700757 status = BAD_VALUE;
758 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700759 }
760
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700761 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700762 if (status != OK) {
763 return status;
764 }
765
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700766 void* data = writeInplace(val.size());
767 if (!data) {
768 status = BAD_VALUE;
769 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700770 }
771
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700772 memcpy(data, val.data(), val.size());
773 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700774}
775
776status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
777{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800778 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700779}
780
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800781status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
782{
783 return writeNullableTypedVector(val, &Parcel::writeInt32);
784}
785
Casey Dahlin451ff582015-10-19 18:12:18 -0700786status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
787{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800788 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700789}
790
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800791status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
792{
793 return writeNullableTypedVector(val, &Parcel::writeInt64);
794}
795
Casey Dahlin451ff582015-10-19 18:12:18 -0700796status_t Parcel::writeFloatVector(const std::vector<float>& val)
797{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800798 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700799}
800
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800801status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
802{
803 return writeNullableTypedVector(val, &Parcel::writeFloat);
804}
805
Casey Dahlin451ff582015-10-19 18:12:18 -0700806status_t Parcel::writeDoubleVector(const std::vector<double>& val)
807{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800808 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700809}
810
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800811status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
812{
813 return writeNullableTypedVector(val, &Parcel::writeDouble);
814}
815
Casey Dahlin451ff582015-10-19 18:12:18 -0700816status_t Parcel::writeBoolVector(const std::vector<bool>& val)
817{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800818 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800821status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
822{
823 return writeNullableTypedVector(val, &Parcel::writeBool);
824}
825
Casey Dahlin451ff582015-10-19 18:12:18 -0700826status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
827{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800828 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700829}
830
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800831status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
832{
833 return writeNullableTypedVector(val, &Parcel::writeChar);
834}
835
Casey Dahlin451ff582015-10-19 18:12:18 -0700836status_t Parcel::writeString16Vector(const std::vector<String16>& val)
837{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800838 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700839}
840
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800841status_t Parcel::writeString16Vector(
842 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
843{
844 return writeNullableTypedVector(val, &Parcel::writeString16);
845}
846
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700847status_t Parcel::writeInt32(int32_t val)
848{
Andreas Huber84a6d042009-08-17 13:33:27 -0700849 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700850}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800851
852status_t Parcel::writeUint32(uint32_t val)
853{
854 return writeAligned(val);
855}
856
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700857status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700858 if (len > INT32_MAX) {
859 // don't accept size_t values which may have come from an
860 // inadvertent conversion from a negative int.
861 return BAD_VALUE;
862 }
863
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700864 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700865 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700866 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700867 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700868 if (ret == NO_ERROR) {
869 ret = write(val, len * sizeof(*val));
870 }
871 return ret;
872}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700873status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700874 if (len > INT32_MAX) {
875 // don't accept size_t values which may have come from an
876 // inadvertent conversion from a negative int.
877 return BAD_VALUE;
878 }
879
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700880 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700881 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700882 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700883 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700884 if (ret == NO_ERROR) {
885 ret = write(val, len * sizeof(*val));
886 }
887 return ret;
888}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700889
Casey Dahlind6848f52015-10-15 15:44:59 -0700890status_t Parcel::writeBool(bool val)
891{
892 return writeInt32(int32_t(val));
893}
894
895status_t Parcel::writeChar(char16_t val)
896{
897 return writeInt32(int32_t(val));
898}
899
900status_t Parcel::writeByte(int8_t val)
901{
902 return writeInt32(int32_t(val));
903}
904
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700905status_t Parcel::writeInt64(int64_t val)
906{
Andreas Huber84a6d042009-08-17 13:33:27 -0700907 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700908}
909
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700910status_t Parcel::writeUint64(uint64_t val)
911{
912 return writeAligned(val);
913}
914
Serban Constantinescuf683e012013-11-05 16:53:55 +0000915status_t Parcel::writePointer(uintptr_t val)
916{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800917 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000918}
919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700920status_t Parcel::writeFloat(float val)
921{
Andreas Huber84a6d042009-08-17 13:33:27 -0700922 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923}
924
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800925#if defined(__mips__) && defined(__mips_hard_float)
926
927status_t Parcel::writeDouble(double val)
928{
929 union {
930 double d;
931 unsigned long long ll;
932 } u;
933 u.d = val;
934 return writeAligned(u.ll);
935}
936
937#else
938
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700939status_t Parcel::writeDouble(double val)
940{
Andreas Huber84a6d042009-08-17 13:33:27 -0700941 return writeAligned(val);
942}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800944#endif
945
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700946status_t Parcel::writeCString(const char* str)
947{
948 return write(str, strlen(str)+1);
949}
950
951status_t Parcel::writeString8(const String8& str)
952{
953 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100954 // only write string if its length is more than zero characters,
955 // as readString8 will only read if the length field is non-zero.
956 // this is slightly different from how writeString16 works.
957 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700958 err = write(str.string(), str.bytes()+1);
959 }
960 return err;
961}
962
Casey Dahlina3b9b1d2015-11-25 15:09:45 -0800963status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
964{
965 if (!str) {
966 return writeInt32(-1);
967 }
968
969 return writeString16(*str);
970}
971
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700972status_t Parcel::writeString16(const String16& str)
973{
974 return writeString16(str.string(), str.size());
975}
976
977status_t Parcel::writeString16(const char16_t* str, size_t len)
978{
979 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981 status_t err = writeInt32(len);
982 if (err == NO_ERROR) {
983 len *= sizeof(char16_t);
984 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
985 if (data) {
986 memcpy(data, str, len);
987 *reinterpret_cast<char16_t*>(data+len) = 0;
988 return NO_ERROR;
989 }
990 err = mError;
991 }
992 return err;
993}
994
995status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
996{
997 return flatten_binder(ProcessState::self(), val, this);
998}
999
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001000status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1001{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001002 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001003}
1004
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001005status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1006{
1007 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1008}
1009
1010status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1011 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1012}
1013
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001014status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001015 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001016}
1017
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1019{
1020 return flatten_binder(ProcessState::self(), val, this);
1021}
1022
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001023status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1024 if (!parcelable) {
1025 return writeInt32(0);
1026 }
1027
1028 return writeParcelable(*parcelable);
1029}
1030
Christopher Wiley97f048d2015-11-19 06:49:05 -08001031status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1032 status_t status = writeInt32(1); // parcelable is not null.
1033 if (status != OK) {
1034 return status;
1035 }
1036 return parcelable.writeToParcel(this);
1037}
1038
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001039status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001040{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001041 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001042 return BAD_TYPE;
1043
1044 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001045 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001046 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001048 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001049 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001051 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1052 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053
1054 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001055 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 return err;
1057 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001058 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001059 return err;
1060}
1061
Jeff Brown93ff1f92011-11-04 19:01:44 -07001062status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001063{
1064 flat_binder_object obj;
1065 obj.type = BINDER_TYPE_FD;
1066 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001067 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001069 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070 return writeObject(obj, true);
1071}
1072
1073status_t Parcel::writeDupFileDescriptor(int fd)
1074{
Jeff Brownd341c712011-11-04 20:19:33 -07001075 int dupFd = dup(fd);
1076 if (dupFd < 0) {
1077 return -errno;
1078 }
1079 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001080 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001081 close(dupFd);
1082 }
1083 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084}
1085
Casey Dahlin06673e32015-11-23 13:24:23 -08001086status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1087 return writeDupFileDescriptor(fd.get());
1088}
1089
1090status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1091 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1092}
1093
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001094status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1095 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1096}
1097
Jeff Brown13b16042014-11-11 16:44:25 -08001098status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001099{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001100 if (len > INT32_MAX) {
1101 // don't accept size_t values which may have come from an
1102 // inadvertent conversion from a negative int.
1103 return BAD_VALUE;
1104 }
1105
Jeff Brown13b16042014-11-11 16:44:25 -08001106 status_t status;
1107 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001108 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001109 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001110 if (status) return status;
1111
1112 void* ptr = writeInplace(len);
1113 if (!ptr) return NO_MEMORY;
1114
Jeff Brown13b16042014-11-11 16:44:25 -08001115 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001116 return NO_ERROR;
1117 }
1118
Steve Block6807e592011-10-20 11:56:00 +01001119 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001120 int fd = ashmem_create_region("Parcel Blob", len);
1121 if (fd < 0) return NO_MEMORY;
1122
1123 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1124 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001125 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 } else {
1127 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1128 if (ptr == MAP_FAILED) {
1129 status = -errno;
1130 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001131 if (!mutableCopy) {
1132 result = ashmem_set_prot_region(fd, PROT_READ);
1133 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001135 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001136 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001137 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001139 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001140 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001141 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142 return NO_ERROR;
1143 }
1144 }
1145 }
1146 }
1147 ::munmap(ptr, len);
1148 }
1149 ::close(fd);
1150 return status;
1151}
1152
Jeff Brown13b16042014-11-11 16:44:25 -08001153status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1154{
1155 // Must match up with what's done in writeBlob.
1156 if (!mAllowFds) return FDS_NOT_ALLOWED;
1157 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1158 if (status) return status;
1159 return writeDupFileDescriptor(fd);
1160}
1161
Mathias Agopiane1424282013-07-29 21:24:40 -07001162status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001163{
1164 status_t err;
1165
1166 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001167 const size_t len = val.getFlattenedSize();
1168 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001169
Nick Kralevichb6b14232015-04-02 09:36:02 -07001170 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1171 // don't accept size_t values which may have come from an
1172 // inadvertent conversion from a negative int.
1173 return BAD_VALUE;
1174 }
1175
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001176 err = this->writeInt32(len);
1177 if (err) return err;
1178
1179 err = this->writeInt32(fd_count);
1180 if (err) return err;
1181
1182 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001183 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001184 if (buf == NULL)
1185 return BAD_VALUE;
1186
1187 int* fds = NULL;
1188 if (fd_count) {
1189 fds = new int[fd_count];
1190 }
1191
1192 err = val.flatten(buf, len, fds, fd_count);
1193 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1194 err = this->writeDupFileDescriptor( fds[i] );
1195 }
1196
1197 if (fd_count) {
1198 delete [] fds;
1199 }
1200
1201 return err;
1202}
1203
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1205{
1206 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1207 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1208 if (enoughData && enoughObjects) {
1209restart_write:
1210 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001211
Christopher Tate98e67d32015-06-03 18:44:15 -07001212 // remember if it's a file descriptor
1213 if (val.type == BINDER_TYPE_FD) {
1214 if (!mAllowFds) {
1215 // fail before modifying our object index
1216 return FDS_NOT_ALLOWED;
1217 }
1218 mHasFds = mFdsKnown = true;
1219 }
1220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001221 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001222 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001223 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001224 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001225 mObjectsSize++;
1226 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228 return finishWrite(sizeof(flat_binder_object));
1229 }
1230
1231 if (!enoughData) {
1232 const status_t err = growData(sizeof(val));
1233 if (err != NO_ERROR) return err;
1234 }
1235 if (!enoughObjects) {
1236 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001237 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001238 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001239 if (objects == NULL) return NO_MEMORY;
1240 mObjects = objects;
1241 mObjectsCapacity = newSize;
1242 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001244 goto restart_write;
1245}
1246
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001247status_t Parcel::writeNoException()
1248{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001249 binder::Status status;
1250 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001251}
1252
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001253void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001254{
1255 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1256}
1257
1258status_t Parcel::read(void* outData, size_t len) const
1259{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001260 if (len > INT32_MAX) {
1261 // don't accept size_t values which may have come from an
1262 // inadvertent conversion from a negative int.
1263 return BAD_VALUE;
1264 }
1265
1266 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1267 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001269 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001270 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 return NO_ERROR;
1272 }
1273 return NOT_ENOUGH_DATA;
1274}
1275
1276const void* Parcel::readInplace(size_t len) const
1277{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001278 if (len > INT32_MAX) {
1279 // don't accept size_t values which may have come from an
1280 // inadvertent conversion from a negative int.
1281 return NULL;
1282 }
1283
1284 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1285 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001286 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001287 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001288 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001289 return data;
1290 }
1291 return NULL;
1292}
1293
Andreas Huber84a6d042009-08-17 13:33:27 -07001294template<class T>
1295status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001296 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001297
1298 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001300 mDataPos += sizeof(T);
1301 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302 return NO_ERROR;
1303 } else {
1304 return NOT_ENOUGH_DATA;
1305 }
1306}
1307
Andreas Huber84a6d042009-08-17 13:33:27 -07001308template<class T>
1309T Parcel::readAligned() const {
1310 T result;
1311 if (readAligned(&result) != NO_ERROR) {
1312 result = 0;
1313 }
1314
1315 return result;
1316}
1317
1318template<class T>
1319status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001320 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001321
1322 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1323restart_write:
1324 *reinterpret_cast<T*>(mData+mDataPos) = val;
1325 return finishWrite(sizeof(val));
1326 }
1327
1328 status_t err = growData(sizeof(val));
1329 if (err == NO_ERROR) goto restart_write;
1330 return err;
1331}
1332
Casey Dahlin451ff582015-10-19 18:12:18 -07001333status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1334 val->clear();
1335
1336 int32_t size;
1337 status_t status = readInt32(&size);
1338
1339 if (status != OK) {
1340 return status;
1341 }
1342
Christopher Wiley4db672d2015-11-10 09:44:30 -08001343 if (size < 0) {
1344 status = UNEXPECTED_NULL;
1345 return status;
1346 }
1347 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001348 status = BAD_VALUE;
1349 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001350 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001351
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001352 const void* data = readInplace(size);
1353 if (!data) {
1354 status = BAD_VALUE;
1355 return status;
1356 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001357 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001358 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001359
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001360 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001361}
1362
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001363status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1364 const int32_t start = dataPosition();
1365 int32_t size;
1366 status_t status = readInt32(&size);
1367 val->reset();
1368
1369 if (status != OK || size < 0) {
1370 return status;
1371 }
1372
1373 setDataPosition(start);
1374 val->reset(new std::vector<int8_t>());
1375
1376 status = readByteVector(val->get());
1377
1378 if (status != OK) {
1379 val->reset();
1380 }
1381
1382 return status;
1383}
1384
1385status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1386 return readNullableTypedVector(val, &Parcel::readInt32);
1387}
1388
Casey Dahlin451ff582015-10-19 18:12:18 -07001389status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001390 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001391}
1392
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001393status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1394 return readNullableTypedVector(val, &Parcel::readInt64);
1395}
1396
Casey Dahlin451ff582015-10-19 18:12:18 -07001397status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001398 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001399}
1400
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001401status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1402 return readNullableTypedVector(val, &Parcel::readFloat);
1403}
1404
Casey Dahlin451ff582015-10-19 18:12:18 -07001405status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001406 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001407}
1408
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001409status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1410 return readNullableTypedVector(val, &Parcel::readDouble);
1411}
1412
Casey Dahlin451ff582015-10-19 18:12:18 -07001413status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001414 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001415}
1416
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001417status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1418 const int32_t start = dataPosition();
1419 int32_t size;
1420 status_t status = readInt32(&size);
1421 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001422
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001423 if (status != OK || size < 0) {
1424 return status;
1425 }
1426
1427 setDataPosition(start);
1428 val->reset(new std::vector<bool>());
1429
1430 status = readBoolVector(val->get());
1431
1432 if (status != OK) {
1433 val->reset();
1434 }
1435
1436 return status;
1437}
1438
1439status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001440 int32_t size;
1441 status_t status = readInt32(&size);
1442
1443 if (status != OK) {
1444 return status;
1445 }
1446
1447 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001448 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001449 }
1450
1451 val->resize(size);
1452
1453 /* C++ bool handling means a vector of bools isn't necessarily addressable
1454 * (we might use individual bits)
1455 */
Christopher Wiley97887982015-10-27 16:33:47 -07001456 bool data;
1457 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001458 status = readBool(&data);
1459 (*val)[i] = data;
1460
1461 if (status != OK) {
1462 return status;
1463 }
1464 }
1465
1466 return OK;
1467}
1468
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001469status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1470 return readNullableTypedVector(val, &Parcel::readChar);
1471}
1472
Casey Dahlin451ff582015-10-19 18:12:18 -07001473status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001474 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001475}
1476
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001477status_t Parcel::readString16Vector(
1478 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1479 return readNullableTypedVector(val, &Parcel::readString16);
1480}
1481
Casey Dahlin451ff582015-10-19 18:12:18 -07001482status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001483 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001484}
1485
1486
Andreas Huber84a6d042009-08-17 13:33:27 -07001487status_t Parcel::readInt32(int32_t *pArg) const
1488{
1489 return readAligned(pArg);
1490}
1491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001492int32_t Parcel::readInt32() const
1493{
Andreas Huber84a6d042009-08-17 13:33:27 -07001494 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495}
1496
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001497status_t Parcel::readUint32(uint32_t *pArg) const
1498{
1499 return readAligned(pArg);
1500}
1501
1502uint32_t Parcel::readUint32() const
1503{
1504 return readAligned<uint32_t>();
1505}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001506
1507status_t Parcel::readInt64(int64_t *pArg) const
1508{
Andreas Huber84a6d042009-08-17 13:33:27 -07001509 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510}
1511
1512
1513int64_t Parcel::readInt64() const
1514{
Andreas Huber84a6d042009-08-17 13:33:27 -07001515 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516}
1517
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001518status_t Parcel::readUint64(uint64_t *pArg) const
1519{
1520 return readAligned(pArg);
1521}
1522
1523uint64_t Parcel::readUint64() const
1524{
1525 return readAligned<uint64_t>();
1526}
1527
Serban Constantinescuf683e012013-11-05 16:53:55 +00001528status_t Parcel::readPointer(uintptr_t *pArg) const
1529{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001530 status_t ret;
1531 binder_uintptr_t ptr;
1532 ret = readAligned(&ptr);
1533 if (!ret)
1534 *pArg = ptr;
1535 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001536}
1537
1538uintptr_t Parcel::readPointer() const
1539{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001540 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001541}
1542
1543
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544status_t Parcel::readFloat(float *pArg) const
1545{
Andreas Huber84a6d042009-08-17 13:33:27 -07001546 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547}
1548
1549
1550float Parcel::readFloat() const
1551{
Andreas Huber84a6d042009-08-17 13:33:27 -07001552 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553}
1554
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001555#if defined(__mips__) && defined(__mips_hard_float)
1556
1557status_t Parcel::readDouble(double *pArg) const
1558{
1559 union {
1560 double d;
1561 unsigned long long ll;
1562 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001563 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001564 status_t status;
1565 status = readAligned(&u.ll);
1566 *pArg = u.d;
1567 return status;
1568}
1569
1570double Parcel::readDouble() const
1571{
1572 union {
1573 double d;
1574 unsigned long long ll;
1575 } u;
1576 u.ll = readAligned<unsigned long long>();
1577 return u.d;
1578}
1579
1580#else
1581
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001582status_t Parcel::readDouble(double *pArg) const
1583{
Andreas Huber84a6d042009-08-17 13:33:27 -07001584 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585}
1586
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001587double Parcel::readDouble() const
1588{
Andreas Huber84a6d042009-08-17 13:33:27 -07001589 return readAligned<double>();
1590}
1591
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001592#endif
1593
Andreas Huber84a6d042009-08-17 13:33:27 -07001594status_t Parcel::readIntPtr(intptr_t *pArg) const
1595{
1596 return readAligned(pArg);
1597}
1598
1599
1600intptr_t Parcel::readIntPtr() const
1601{
1602 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603}
1604
Casey Dahlind6848f52015-10-15 15:44:59 -07001605status_t Parcel::readBool(bool *pArg) const
1606{
1607 int32_t tmp;
1608 status_t ret = readInt32(&tmp);
1609 *pArg = (tmp != 0);
1610 return ret;
1611}
1612
1613bool Parcel::readBool() const
1614{
1615 return readInt32() != 0;
1616}
1617
1618status_t Parcel::readChar(char16_t *pArg) const
1619{
1620 int32_t tmp;
1621 status_t ret = readInt32(&tmp);
1622 *pArg = char16_t(tmp);
1623 return ret;
1624}
1625
1626char16_t Parcel::readChar() const
1627{
1628 return char16_t(readInt32());
1629}
1630
1631status_t Parcel::readByte(int8_t *pArg) const
1632{
1633 int32_t tmp;
1634 status_t ret = readInt32(&tmp);
1635 *pArg = int8_t(tmp);
1636 return ret;
1637}
1638
1639int8_t Parcel::readByte() const
1640{
1641 return int8_t(readInt32());
1642}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643
1644const char* Parcel::readCString() const
1645{
1646 const size_t avail = mDataSize-mDataPos;
1647 if (avail > 0) {
1648 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1649 // is the string's trailing NUL within the parcel's valid bounds?
1650 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1651 if (eos) {
1652 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001653 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001654 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001655 return str;
1656 }
1657 }
1658 return NULL;
1659}
1660
1661String8 Parcel::readString8() const
1662{
1663 int32_t size = readInt32();
1664 // watch for potential int overflow adding 1 for trailing NUL
1665 if (size > 0 && size < INT32_MAX) {
1666 const char* str = (const char*)readInplace(size+1);
1667 if (str) return String8(str, size);
1668 }
1669 return String8();
1670}
1671
1672String16 Parcel::readString16() const
1673{
1674 size_t len;
1675 const char16_t* str = readString16Inplace(&len);
1676 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001677 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678 return String16();
1679}
1680
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001681status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1682{
1683 const int32_t start = dataPosition();
1684 int32_t size;
1685 status_t status = readInt32(&size);
1686 pArg->reset();
1687
1688 if (status != OK || size < 0) {
1689 return status;
1690 }
1691
1692 setDataPosition(start);
1693 pArg->reset(new String16());
1694
1695 status = readString16(pArg->get());
1696
1697 if (status != OK) {
1698 pArg->reset();
1699 }
1700
1701 return status;
1702}
1703
Casey Dahlin451ff582015-10-19 18:12:18 -07001704status_t Parcel::readString16(String16* pArg) const
1705{
1706 size_t len;
1707 const char16_t* str = readString16Inplace(&len);
1708 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001709 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001710 return 0;
1711 } else {
1712 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001713 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001714 }
1715}
1716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001717const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1718{
1719 int32_t size = readInt32();
1720 // watch for potential int overflow from size+1
1721 if (size >= 0 && size < INT32_MAX) {
1722 *outLen = size;
1723 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1724 if (str != NULL) {
1725 return str;
1726 }
1727 }
1728 *outLen = 0;
1729 return NULL;
1730}
1731
Casey Dahlinf0c13772015-10-27 18:33:56 -07001732status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1733{
1734 return unflatten_binder(ProcessState::self(), *this, val);
1735}
1736
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001737sp<IBinder> Parcel::readStrongBinder() const
1738{
1739 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001740 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001741 return val;
1742}
1743
1744wp<IBinder> Parcel::readWeakBinder() const
1745{
1746 wp<IBinder> val;
1747 unflatten_binder(ProcessState::self(), *this, &val);
1748 return val;
1749}
1750
Christopher Wiley97f048d2015-11-19 06:49:05 -08001751status_t Parcel::readParcelable(Parcelable* parcelable) const {
1752 int32_t have_parcelable = 0;
1753 status_t status = readInt32(&have_parcelable);
1754 if (status != OK) {
1755 return status;
1756 }
1757 if (!have_parcelable) {
1758 return UNEXPECTED_NULL;
1759 }
1760 return parcelable->readFromParcel(this);
1761}
1762
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001763int32_t Parcel::readExceptionCode() const
1764{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001765 binder::Status status;
1766 status.readFromParcel(*this);
1767 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001768}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001769
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001770native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001771{
1772 int numFds, numInts;
1773 status_t err;
1774 err = readInt32(&numFds);
1775 if (err != NO_ERROR) return 0;
1776 err = readInt32(&numInts);
1777 if (err != NO_ERROR) return 0;
1778
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001779 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001780 if (!h) {
1781 return 0;
1782 }
1783
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001784 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001785 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001786 if (h->data[i] < 0) err = BAD_VALUE;
1787 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001788 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001789 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001790 native_handle_close(h);
1791 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001792 h = 0;
1793 }
1794 return h;
1795}
1796
1797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798int Parcel::readFileDescriptor() const
1799{
1800 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001801
1802 if (flat && flat->type == BINDER_TYPE_FD) {
1803 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806 return BAD_TYPE;
1807}
1808
Casey Dahlin06673e32015-11-23 13:24:23 -08001809status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1810{
1811 int got = readFileDescriptor();
1812
1813 if (got == BAD_TYPE) {
1814 return BAD_TYPE;
1815 }
1816
1817 val->reset(dup(got));
1818
1819 if (val->get() < 0) {
1820 return BAD_VALUE;
1821 }
1822
1823 return OK;
1824}
1825
1826
Casey Dahlina3b9b1d2015-11-25 15:09:45 -08001827status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1828 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1829}
1830
Casey Dahlin06673e32015-11-23 13:24:23 -08001831status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1832 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1833}
1834
Jeff Brown5707dbf2011-09-23 21:17:56 -07001835status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1836{
Jeff Brown13b16042014-11-11 16:44:25 -08001837 int32_t blobType;
1838 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001839 if (status) return status;
1840
Jeff Brown13b16042014-11-11 16:44:25 -08001841 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001842 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001843 const void* ptr = readInplace(len);
1844 if (!ptr) return BAD_VALUE;
1845
Jeff Brown13b16042014-11-11 16:44:25 -08001846 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001847 return NO_ERROR;
1848 }
1849
Steve Block6807e592011-10-20 11:56:00 +01001850 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001851 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001852 int fd = readFileDescriptor();
1853 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1854
Jeff Brown13b16042014-11-11 16:44:25 -08001855 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1856 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001857 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001858
Jeff Brown13b16042014-11-11 16:44:25 -08001859 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001860 return NO_ERROR;
1861}
1862
Mathias Agopiane1424282013-07-29 21:24:40 -07001863status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001864{
1865 // size
1866 const size_t len = this->readInt32();
1867 const size_t fd_count = this->readInt32();
1868
Nick Kralevichb6b14232015-04-02 09:36:02 -07001869 if (len > INT32_MAX) {
1870 // don't accept size_t values which may have come from an
1871 // inadvertent conversion from a negative int.
1872 return BAD_VALUE;
1873 }
1874
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001875 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001876 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001877 if (buf == NULL)
1878 return BAD_VALUE;
1879
1880 int* fds = NULL;
1881 if (fd_count) {
1882 fds = new int[fd_count];
1883 }
1884
1885 status_t err = NO_ERROR;
1886 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001887 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001888 if (fds[i] < 0) {
1889 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001890 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1891 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001892 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001893 }
1894
1895 if (err == NO_ERROR) {
1896 err = val.unflatten(buf, len, fds, fd_count);
1897 }
1898
1899 if (fd_count) {
1900 delete [] fds;
1901 }
1902
1903 return err;
1904}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1906{
1907 const size_t DPOS = mDataPos;
1908 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1909 const flat_binder_object* obj
1910 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1911 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001912 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001913 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914 // the object list, so we don't want to check for it when
1915 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001916 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917 return obj;
1918 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001921 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922 const size_t N = mObjectsSize;
1923 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001924
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001926 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001928
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001929 // Start at the current hint position, looking for an object at
1930 // the current data position.
1931 if (opos < N) {
1932 while (opos < (N-1) && OBJS[opos] < DPOS) {
1933 opos++;
1934 }
1935 } else {
1936 opos = N-1;
1937 }
1938 if (OBJS[opos] == DPOS) {
1939 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001940 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941 this, DPOS, opos);
1942 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001943 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001944 return obj;
1945 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001947 // Look backwards for it...
1948 while (opos > 0 && OBJS[opos] > DPOS) {
1949 opos--;
1950 }
1951 if (OBJS[opos] == DPOS) {
1952 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001953 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954 this, DPOS, opos);
1955 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001956 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 return obj;
1958 }
1959 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001960 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 -07001961 this, DPOS);
1962 }
1963 return NULL;
1964}
1965
1966void Parcel::closeFileDescriptors()
1967{
1968 size_t i = mObjectsSize;
1969 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001970 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971 }
1972 while (i > 0) {
1973 i--;
1974 const flat_binder_object* flat
1975 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1976 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001977 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978 close(flat->handle);
1979 }
1980 }
1981}
1982
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001983uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001985 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986}
1987
1988size_t Parcel::ipcDataSize() const
1989{
1990 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1991}
1992
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001993uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001995 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996}
1997
1998size_t Parcel::ipcObjectsCount() const
1999{
2000 return mObjectsSize;
2001}
2002
2003void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002004 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002006 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007 freeDataNoInit();
2008 mError = NO_ERROR;
2009 mData = const_cast<uint8_t*>(data);
2010 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002011 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002012 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002013 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002014 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 mObjectsSize = mObjectsCapacity = objectsCount;
2016 mNextObjectHint = 0;
2017 mOwner = relFunc;
2018 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002019 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002020 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002021 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002022 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002023 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002024 mObjectsSize = 0;
2025 break;
2026 }
2027 minOffset = offset + sizeof(flat_binder_object);
2028 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029 scanForFds();
2030}
2031
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002032void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033{
2034 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002036 if (errorCheck() != NO_ERROR) {
2037 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002038 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002039 } else if (dataSize() > 0) {
2040 const uint8_t* DATA = data();
2041 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002042 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043 const size_t N = objectsCount();
2044 for (size_t i=0; i<N; i++) {
2045 const flat_binder_object* flat
2046 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2047 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2048 << TypeCode(flat->type & 0x7f7f7f00)
2049 << " = " << flat->binder;
2050 }
2051 } else {
2052 to << "NULL";
2053 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055 to << ")";
2056}
2057
2058void Parcel::releaseObjects()
2059{
2060 const sp<ProcessState> proc(ProcessState::self());
2061 size_t i = mObjectsSize;
2062 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002063 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 while (i > 0) {
2065 i--;
2066 const flat_binder_object* flat
2067 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002068 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 }
2070}
2071
2072void Parcel::acquireObjects()
2073{
2074 const sp<ProcessState> proc(ProcessState::self());
2075 size_t i = mObjectsSize;
2076 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002077 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 while (i > 0) {
2079 i--;
2080 const flat_binder_object* flat
2081 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002082 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 }
2084}
2085
2086void Parcel::freeData()
2087{
2088 freeDataNoInit();
2089 initState();
2090}
2091
2092void Parcel::freeDataNoInit()
2093{
2094 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002095 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002096 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2098 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002099 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002101 if (mData) {
2102 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002103 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002104 if (mDataCapacity <= gParcelGlobalAllocSize) {
2105 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2106 } else {
2107 gParcelGlobalAllocSize = 0;
2108 }
2109 if (gParcelGlobalAllocCount > 0) {
2110 gParcelGlobalAllocCount--;
2111 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002112 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002113 free(mData);
2114 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002115 if (mObjects) free(mObjects);
2116 }
2117}
2118
2119status_t Parcel::growData(size_t len)
2120{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002121 if (len > INT32_MAX) {
2122 // don't accept size_t values which may have come from an
2123 // inadvertent conversion from a negative int.
2124 return BAD_VALUE;
2125 }
2126
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 size_t newSize = ((mDataSize+len)*3)/2;
2128 return (newSize <= mDataSize)
2129 ? (status_t) NO_MEMORY
2130 : continueWrite(newSize);
2131}
2132
2133status_t Parcel::restartWrite(size_t desired)
2134{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002135 if (desired > INT32_MAX) {
2136 // don't accept size_t values which may have come from an
2137 // inadvertent conversion from a negative int.
2138 return BAD_VALUE;
2139 }
2140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 if (mOwner) {
2142 freeData();
2143 return continueWrite(desired);
2144 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002145
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146 uint8_t* data = (uint8_t*)realloc(mData, desired);
2147 if (!data && desired > mDataCapacity) {
2148 mError = NO_MEMORY;
2149 return NO_MEMORY;
2150 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002155 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002156 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002157 gParcelGlobalAllocSize += desired;
2158 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002159 if (!mData) {
2160 gParcelGlobalAllocCount++;
2161 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002162 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002163 mData = data;
2164 mDataCapacity = desired;
2165 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002166
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002168 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2169 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2170
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 free(mObjects);
2172 mObjects = NULL;
2173 mObjectsSize = mObjectsCapacity = 0;
2174 mNextObjectHint = 0;
2175 mHasFds = false;
2176 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002177 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002178
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 return NO_ERROR;
2180}
2181
2182status_t Parcel::continueWrite(size_t desired)
2183{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002184 if (desired > INT32_MAX) {
2185 // don't accept size_t values which may have come from an
2186 // inadvertent conversion from a negative int.
2187 return BAD_VALUE;
2188 }
2189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 // If shrinking, first adjust for any objects that appear
2191 // after the new data size.
2192 size_t objectsSize = mObjectsSize;
2193 if (desired < mDataSize) {
2194 if (desired == 0) {
2195 objectsSize = 0;
2196 } else {
2197 while (objectsSize > 0) {
2198 if (mObjects[objectsSize-1] < desired)
2199 break;
2200 objectsSize--;
2201 }
2202 }
2203 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 if (mOwner) {
2206 // If the size is going to zero, just release the owner's data.
2207 if (desired == 0) {
2208 freeData();
2209 return NO_ERROR;
2210 }
2211
2212 // If there is a different owner, we need to take
2213 // posession.
2214 uint8_t* data = (uint8_t*)malloc(desired);
2215 if (!data) {
2216 mError = NO_MEMORY;
2217 return NO_MEMORY;
2218 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002219 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002222 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002224 free(data);
2225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 mError = NO_MEMORY;
2227 return NO_MEMORY;
2228 }
2229
2230 // Little hack to only acquire references on objects
2231 // we will be keeping.
2232 size_t oldObjectsSize = mObjectsSize;
2233 mObjectsSize = objectsSize;
2234 acquireObjects();
2235 mObjectsSize = oldObjectsSize;
2236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 if (mData) {
2239 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2240 }
2241 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002242 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002244 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2246 mOwner = NULL;
2247
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002248 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002249 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002250 gParcelGlobalAllocSize += desired;
2251 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002252 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254 mData = data;
2255 mObjects = objects;
2256 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002257 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002258 mDataCapacity = desired;
2259 mObjectsSize = mObjectsCapacity = objectsSize;
2260 mNextObjectHint = 0;
2261
2262 } else if (mData) {
2263 if (objectsSize < mObjectsSize) {
2264 // Need to release refs on any objects we are dropping.
2265 const sp<ProcessState> proc(ProcessState::self());
2266 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2267 const flat_binder_object* flat
2268 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2269 if (flat->type == BINDER_TYPE_FD) {
2270 // will need to rescan because we may have lopped off the only FDs
2271 mFdsKnown = false;
2272 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002273 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002275 binder_size_t* objects =
2276 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 if (objects) {
2278 mObjects = objects;
2279 }
2280 mObjectsSize = objectsSize;
2281 mNextObjectHint = 0;
2282 }
2283
2284 // We own the data, so we can just do a realloc().
2285 if (desired > mDataCapacity) {
2286 uint8_t* data = (uint8_t*)realloc(mData, desired);
2287 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002288 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2289 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002290 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002291 gParcelGlobalAllocSize += desired;
2292 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002293 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 mData = data;
2295 mDataCapacity = desired;
2296 } else if (desired > mDataCapacity) {
2297 mError = NO_MEMORY;
2298 return NO_MEMORY;
2299 }
2300 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002301 if (mDataSize > desired) {
2302 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002303 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002304 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305 if (mDataPos > desired) {
2306 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002307 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 }
2309 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002310
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 } else {
2312 // This is the first data. Easy!
2313 uint8_t* data = (uint8_t*)malloc(desired);
2314 if (!data) {
2315 mError = NO_MEMORY;
2316 return NO_MEMORY;
2317 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002318
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 if(!(mDataCapacity == 0 && mObjects == NULL
2320 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002321 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002323
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002324 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002325 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002326 gParcelGlobalAllocSize += desired;
2327 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002328 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330 mData = data;
2331 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002332 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2333 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 mDataCapacity = desired;
2335 }
2336
2337 return NO_ERROR;
2338}
2339
2340void Parcel::initState()
2341{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002342 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002343 mError = NO_ERROR;
2344 mData = 0;
2345 mDataSize = 0;
2346 mDataCapacity = 0;
2347 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2349 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mObjects = NULL;
2351 mObjectsSize = 0;
2352 mObjectsCapacity = 0;
2353 mNextObjectHint = 0;
2354 mHasFds = false;
2355 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002356 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002358 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359}
2360
2361void Parcel::scanForFds() const
2362{
2363 bool hasFds = false;
2364 for (size_t i=0; i<mObjectsSize; i++) {
2365 const flat_binder_object* flat
2366 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2367 if (flat->type == BINDER_TYPE_FD) {
2368 hasFds = true;
2369 break;
2370 }
2371 }
2372 mHasFds = hasFds;
2373 mFdsKnown = true;
2374}
2375
Dan Sandleraa5c2342015-04-10 10:08:45 -04002376size_t Parcel::getBlobAshmemSize() const
2377{
Adrian Roos6bb31142015-10-22 16:46:12 -07002378 // This used to return the size of all blobs that were written to ashmem, now we're returning
2379 // the ashmem currently referenced by this Parcel, which should be equivalent.
2380 // TODO: Remove method once ABI can be changed.
2381 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002382}
2383
Adrian Rooscbf37262015-10-22 16:12:53 -07002384size_t Parcel::getOpenAshmemSize() const
2385{
2386 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002387}
2388
2389// --- Parcel::Blob ---
2390
2391Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002392 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002393}
2394
2395Parcel::Blob::~Blob() {
2396 release();
2397}
2398
2399void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002400 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002401 ::munmap(mData, mSize);
2402 }
2403 clear();
2404}
2405
Jeff Brown13b16042014-11-11 16:44:25 -08002406void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2407 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002408 mData = data;
2409 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002410 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002411}
2412
2413void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002414 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002415 mData = NULL;
2416 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002417 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002418}
2419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420}; // namespace android