blob: e50e15582d8cf71931ff0c2199ea4f3cf0327b80 [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{
379 // TODO: decide what to do about the possibility that this can
380 // report an available-data size that exceeds a Java int's max
381 // positive value, causing havoc. Fortunately this will only
382 // happen if someone constructs a Parcel containing more than two
383 // gigabytes of data, which on typical phone hardware is simply
384 // not possible.
385 return dataSize() - dataPosition();
386}
387
388size_t Parcel::dataPosition() const
389{
390 return mDataPos;
391}
392
393size_t Parcel::dataCapacity() const
394{
395 return mDataCapacity;
396}
397
398status_t Parcel::setDataSize(size_t size)
399{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700400 if (size > INT32_MAX) {
401 // don't accept size_t values which may have come from an
402 // inadvertent conversion from a negative int.
403 return BAD_VALUE;
404 }
405
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700406 status_t err;
407 err = continueWrite(size);
408 if (err == NO_ERROR) {
409 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700410 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700411 }
412 return err;
413}
414
415void Parcel::setDataPosition(size_t pos) const
416{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700417 if (pos > INT32_MAX) {
418 // don't accept size_t values which may have come from an
419 // inadvertent conversion from a negative int.
420 abort();
421 }
422
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 mDataPos = pos;
424 mNextObjectHint = 0;
425}
426
427status_t Parcel::setDataCapacity(size_t size)
428{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700429 if (size > INT32_MAX) {
430 // don't accept size_t values which may have come from an
431 // inadvertent conversion from a negative int.
432 return BAD_VALUE;
433 }
434
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700435 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 return NO_ERROR;
437}
438
439status_t Parcel::setData(const uint8_t* buffer, size_t len)
440{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700441 if (len > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700447 status_t err = restartWrite(len);
448 if (err == NO_ERROR) {
449 memcpy(const_cast<uint8_t*>(data()), buffer, len);
450 mDataSize = len;
451 mFdsKnown = false;
452 }
453 return err;
454}
455
Andreas Huber51faf462011-04-13 10:21:56 -0700456status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457{
458 const sp<ProcessState> proc(ProcessState::self());
459 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700460 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800461 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 size_t size = parcel->mObjectsSize;
463 int startPos = mDataPos;
464 int firstIndex = -1, lastIndex = -2;
465
466 if (len == 0) {
467 return NO_ERROR;
468 }
469
Nick Kralevichb6b14232015-04-02 09:36:02 -0700470 if (len > INT32_MAX) {
471 // don't accept size_t values which may have come from an
472 // inadvertent conversion from a negative int.
473 return BAD_VALUE;
474 }
475
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 // range checks against the source parcel size
477 if ((offset > parcel->mDataSize)
478 || (len > parcel->mDataSize)
479 || (offset + len > parcel->mDataSize)) {
480 return BAD_VALUE;
481 }
482
483 // Count objects in range
484 for (int i = 0; i < (int) size; i++) {
485 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700486 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 if (firstIndex == -1) {
488 firstIndex = i;
489 }
490 lastIndex = i;
491 }
492 }
493 int numObjects = lastIndex - firstIndex + 1;
494
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700495 if ((mDataSize+len) > mDataCapacity) {
496 // grow data
497 err = growData(len);
498 if (err != NO_ERROR) {
499 return err;
500 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501 }
502
503 // append data
504 memcpy(mData + mDataPos, data + offset, len);
505 mDataPos += len;
506 mDataSize += len;
507
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400508 err = NO_ERROR;
509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700510 if (numObjects > 0) {
511 // grow objects
512 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700513 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
514 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800515 binder_size_t *objects =
516 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
517 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 return NO_MEMORY;
519 }
520 mObjects = objects;
521 mObjectsCapacity = newSize;
522 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700523
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 // append and acquire objects
525 int idx = mObjectsSize;
526 for (int i = firstIndex; i <= lastIndex; i++) {
527 size_t off = objects[i] - offset + startPos;
528 mObjects[idx++] = off;
529 mObjectsSize++;
530
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700531 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700533 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700536 // If this is a file descriptor, we need to dup it so the
537 // new Parcel now owns its own fd, and can declare that we
538 // officially know we have fds.
539 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800540 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400542 if (!mAllowFds) {
543 err = FDS_NOT_ALLOWED;
544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 }
546 }
547 }
548
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549 return err;
550}
551
Jeff Brown13b16042014-11-11 16:44:25 -0800552bool Parcel::allowFds() const
553{
554 return mAllowFds;
555}
556
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700557bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400558{
559 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700560 if (!allowFds) {
561 mAllowFds = false;
562 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400563 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700564}
565
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700566void Parcel::restoreAllowFds(bool lastValue)
567{
568 mAllowFds = lastValue;
569}
570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571bool Parcel::hasFileDescriptors() const
572{
573 if (!mFdsKnown) {
574 scanForFds();
575 }
576 return mHasFds;
577}
578
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700579// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700580status_t Parcel::writeInterfaceToken(const String16& interface)
581{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700582 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
583 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584 // currently the interface identification token is just its name as a string
585 return writeString16(interface);
586}
587
Mathias Agopian83c04462009-05-22 19:00:22 -0700588bool Parcel::checkInterface(IBinder* binder) const
589{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700590 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700591}
592
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700593bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700594 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700596 int32_t strictPolicy = readInt32();
597 if (threadState == NULL) {
598 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700599 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700600 if ((threadState->getLastTransactionBinderFlags() &
601 IBinder::FLAG_ONEWAY) != 0) {
602 // For one-way calls, the callee is running entirely
603 // disconnected from the caller, so disable StrictMode entirely.
604 // Not only does disk/network usage not impact the caller, but
605 // there's no way to commuicate back any violations anyway.
606 threadState->setStrictModePolicy(0);
607 } else {
608 threadState->setStrictModePolicy(strictPolicy);
609 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700610 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611 if (str == interface) {
612 return true;
613 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700614 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 String8(interface).string(), String8(str).string());
616 return false;
617 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700618}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800620const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621{
622 return mObjects;
623}
624
625size_t Parcel::objectsCount() const
626{
627 return mObjectsSize;
628}
629
630status_t Parcel::errorCheck() const
631{
632 return mError;
633}
634
635void Parcel::setError(status_t err)
636{
637 mError = err;
638}
639
640status_t Parcel::finishWrite(size_t len)
641{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700642 if (len > INT32_MAX) {
643 // don't accept size_t values which may have come from an
644 // inadvertent conversion from a negative int.
645 return BAD_VALUE;
646 }
647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700648 //printf("Finish write of %d\n", len);
649 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700650 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700651 if (mDataPos > mDataSize) {
652 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700653 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 }
655 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
656 return NO_ERROR;
657}
658
659status_t Parcel::writeUnpadded(const void* data, size_t len)
660{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700661 if (len > INT32_MAX) {
662 // don't accept size_t values which may have come from an
663 // inadvertent conversion from a negative int.
664 return BAD_VALUE;
665 }
666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700667 size_t end = mDataPos + len;
668 if (end < mDataPos) {
669 // integer overflow
670 return BAD_VALUE;
671 }
672
673 if (end <= mDataCapacity) {
674restart_write:
675 memcpy(mData+mDataPos, data, len);
676 return finishWrite(len);
677 }
678
679 status_t err = growData(len);
680 if (err == NO_ERROR) goto restart_write;
681 return err;
682}
683
684status_t Parcel::write(const void* data, size_t len)
685{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700686 if (len > INT32_MAX) {
687 // don't accept size_t values which may have come from an
688 // inadvertent conversion from a negative int.
689 return BAD_VALUE;
690 }
691
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700692 void* const d = writeInplace(len);
693 if (d) {
694 memcpy(d, data, len);
695 return NO_ERROR;
696 }
697 return mError;
698}
699
700void* Parcel::writeInplace(size_t len)
701{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700702 if (len > INT32_MAX) {
703 // don't accept size_t values which may have come from an
704 // inadvertent conversion from a negative int.
705 return NULL;
706 }
707
708 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700709
710 // sanity check for integer overflow
711 if (mDataPos+padded < mDataPos) {
712 return NULL;
713 }
714
715 if ((mDataPos+padded) <= mDataCapacity) {
716restart_write:
717 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
718 uint8_t* const data = mData+mDataPos;
719
720 // Need to pad at end?
721 if (padded != len) {
722#if BYTE_ORDER == BIG_ENDIAN
723 static const uint32_t mask[4] = {
724 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
725 };
726#endif
727#if BYTE_ORDER == LITTLE_ENDIAN
728 static const uint32_t mask[4] = {
729 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
730 };
731#endif
732 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
733 // *reinterpret_cast<void**>(data+padded-4));
734 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
735 }
736
737 finishWrite(padded);
738 return data;
739 }
740
741 status_t err = growData(padded);
742 if (err == NO_ERROR) goto restart_write;
743 return NULL;
744}
745
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800746status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
747 const uint8_t* strData = (uint8_t*)str.data();
748 const size_t strLen= str.length();
749 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
750 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
751 return BAD_VALUE;
752 }
753
754 status_t err = writeInt32(utf16Len);
755 if (err) {
756 return err;
757 }
758
759 // Allocate enough bytes to hold our converted string and its terminating NULL.
760 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
761 if (!dst) {
762 return NO_MEMORY;
763 }
764
765 utf8_to_utf16(strData, strLen, (char16_t*)dst);
766
767 return NO_ERROR;
768}
769
770status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
771 if (!str) {
772 return writeInt32(-1);
773 }
774 return writeUtf8AsUtf16(*str);
775}
776
Casey Dahlinb9872622015-11-25 15:09:45 -0800777status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
778{
779 if (!val) {
780 return writeInt32(-1);
781 }
782
783 return writeByteVector(*val);
784}
785
Casey Dahlin451ff582015-10-19 18:12:18 -0700786status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
787{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700788 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700789 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700790 status = BAD_VALUE;
791 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700792 }
793
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700794 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700795 if (status != OK) {
796 return status;
797 }
798
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700799 void* data = writeInplace(val.size());
800 if (!data) {
801 status = BAD_VALUE;
802 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700803 }
804
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700805 memcpy(data, val.data(), val.size());
806 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700807}
808
809status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
810{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800811 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700812}
813
Casey Dahlinb9872622015-11-25 15:09:45 -0800814status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
815{
816 return writeNullableTypedVector(val, &Parcel::writeInt32);
817}
818
Casey Dahlin451ff582015-10-19 18:12:18 -0700819status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
820{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800821 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700822}
823
Casey Dahlinb9872622015-11-25 15:09:45 -0800824status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
825{
826 return writeNullableTypedVector(val, &Parcel::writeInt64);
827}
828
Casey Dahlin451ff582015-10-19 18:12:18 -0700829status_t Parcel::writeFloatVector(const std::vector<float>& val)
830{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800831 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700832}
833
Casey Dahlinb9872622015-11-25 15:09:45 -0800834status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
835{
836 return writeNullableTypedVector(val, &Parcel::writeFloat);
837}
838
Casey Dahlin451ff582015-10-19 18:12:18 -0700839status_t Parcel::writeDoubleVector(const std::vector<double>& val)
840{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800841 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700842}
843
Casey Dahlinb9872622015-11-25 15:09:45 -0800844status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
845{
846 return writeNullableTypedVector(val, &Parcel::writeDouble);
847}
848
Casey Dahlin451ff582015-10-19 18:12:18 -0700849status_t Parcel::writeBoolVector(const std::vector<bool>& val)
850{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800851 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700852}
853
Casey Dahlinb9872622015-11-25 15:09:45 -0800854status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
855{
856 return writeNullableTypedVector(val, &Parcel::writeBool);
857}
858
Casey Dahlin451ff582015-10-19 18:12:18 -0700859status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
860{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800861 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700862}
863
Casey Dahlinb9872622015-11-25 15:09:45 -0800864status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
865{
866 return writeNullableTypedVector(val, &Parcel::writeChar);
867}
868
Casey Dahlin451ff582015-10-19 18:12:18 -0700869status_t Parcel::writeString16Vector(const std::vector<String16>& val)
870{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800871 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700872}
873
Casey Dahlinb9872622015-11-25 15:09:45 -0800874status_t Parcel::writeString16Vector(
875 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeString16);
878}
879
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800880status_t Parcel::writeUtf8VectorAsUtf16Vector(
881 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
882 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
883}
884
885status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
886 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
887}
888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700889status_t Parcel::writeInt32(int32_t val)
890{
Andreas Huber84a6d042009-08-17 13:33:27 -0700891 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700892}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800893
894status_t Parcel::writeUint32(uint32_t val)
895{
896 return writeAligned(val);
897}
898
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700899status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700900 if (len > INT32_MAX) {
901 // don't accept size_t values which may have come from an
902 // inadvertent conversion from a negative int.
903 return BAD_VALUE;
904 }
905
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700906 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700907 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700908 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700909 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700910 if (ret == NO_ERROR) {
911 ret = write(val, len * sizeof(*val));
912 }
913 return ret;
914}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700915status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700916 if (len > INT32_MAX) {
917 // don't accept size_t values which may have come from an
918 // inadvertent conversion from a negative int.
919 return BAD_VALUE;
920 }
921
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700922 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700923 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700924 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700925 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700926 if (ret == NO_ERROR) {
927 ret = write(val, len * sizeof(*val));
928 }
929 return ret;
930}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700931
Casey Dahlind6848f52015-10-15 15:44:59 -0700932status_t Parcel::writeBool(bool val)
933{
934 return writeInt32(int32_t(val));
935}
936
937status_t Parcel::writeChar(char16_t val)
938{
939 return writeInt32(int32_t(val));
940}
941
942status_t Parcel::writeByte(int8_t val)
943{
944 return writeInt32(int32_t(val));
945}
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947status_t Parcel::writeInt64(int64_t val)
948{
Andreas Huber84a6d042009-08-17 13:33:27 -0700949 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950}
951
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700952status_t Parcel::writeUint64(uint64_t val)
953{
954 return writeAligned(val);
955}
956
Serban Constantinescuf683e012013-11-05 16:53:55 +0000957status_t Parcel::writePointer(uintptr_t val)
958{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800959 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000960}
961
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700962status_t Parcel::writeFloat(float val)
963{
Andreas Huber84a6d042009-08-17 13:33:27 -0700964 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700965}
966
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800967#if defined(__mips__) && defined(__mips_hard_float)
968
969status_t Parcel::writeDouble(double val)
970{
971 union {
972 double d;
973 unsigned long long ll;
974 } u;
975 u.d = val;
976 return writeAligned(u.ll);
977}
978
979#else
980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700981status_t Parcel::writeDouble(double val)
982{
Andreas Huber84a6d042009-08-17 13:33:27 -0700983 return writeAligned(val);
984}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800986#endif
987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988status_t Parcel::writeCString(const char* str)
989{
990 return write(str, strlen(str)+1);
991}
992
993status_t Parcel::writeString8(const String8& str)
994{
995 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100996 // only write string if its length is more than zero characters,
997 // as readString8 will only read if the length field is non-zero.
998 // this is slightly different from how writeString16 works.
999 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000 err = write(str.string(), str.bytes()+1);
1001 }
1002 return err;
1003}
1004
Casey Dahlinb9872622015-11-25 15:09:45 -08001005status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1006{
1007 if (!str) {
1008 return writeInt32(-1);
1009 }
1010
1011 return writeString16(*str);
1012}
1013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014status_t Parcel::writeString16(const String16& str)
1015{
1016 return writeString16(str.string(), str.size());
1017}
1018
1019status_t Parcel::writeString16(const char16_t* str, size_t len)
1020{
1021 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023 status_t err = writeInt32(len);
1024 if (err == NO_ERROR) {
1025 len *= sizeof(char16_t);
1026 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1027 if (data) {
1028 memcpy(data, str, len);
1029 *reinterpret_cast<char16_t*>(data+len) = 0;
1030 return NO_ERROR;
1031 }
1032 err = mError;
1033 }
1034 return err;
1035}
1036
1037status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1038{
1039 return flatten_binder(ProcessState::self(), val, this);
1040}
1041
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001042status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1043{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001044 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001045}
1046
Casey Dahlinb9872622015-11-25 15:09:45 -08001047status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1048{
1049 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1050}
1051
1052status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1053 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1054}
1055
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001056status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001057 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001058}
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1061{
1062 return flatten_binder(ProcessState::self(), val, this);
1063}
1064
Casey Dahlinb9872622015-11-25 15:09:45 -08001065status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1066 if (!parcelable) {
1067 return writeInt32(0);
1068 }
1069
1070 return writeParcelable(*parcelable);
1071}
1072
Christopher Wiley97f048d2015-11-19 06:49:05 -08001073status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1074 status_t status = writeInt32(1); // parcelable is not null.
1075 if (status != OK) {
1076 return status;
1077 }
1078 return parcelable.writeToParcel(this);
1079}
1080
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001081status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001082{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001083 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001084 return BAD_TYPE;
1085
1086 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001087 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001088 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001090 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001091 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001093 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1094 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095
1096 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001097 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 return err;
1099 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001100 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001101 return err;
1102}
1103
Jeff Brown93ff1f92011-11-04 19:01:44 -07001104status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105{
1106 flat_binder_object obj;
1107 obj.type = BINDER_TYPE_FD;
1108 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001109 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001110 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001111 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001112 return writeObject(obj, true);
1113}
1114
1115status_t Parcel::writeDupFileDescriptor(int fd)
1116{
Jeff Brownd341c712011-11-04 20:19:33 -07001117 int dupFd = dup(fd);
1118 if (dupFd < 0) {
1119 return -errno;
1120 }
1121 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001122 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001123 close(dupFd);
1124 }
1125 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001126}
1127
Casey Dahlin06673e32015-11-23 13:24:23 -08001128status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1129 return writeDupFileDescriptor(fd.get());
1130}
1131
1132status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1133 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1134}
1135
Casey Dahlinb9872622015-11-25 15:09:45 -08001136status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1137 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1138}
1139
Jeff Brown13b16042014-11-11 16:44:25 -08001140status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001141{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001142 if (len > INT32_MAX) {
1143 // don't accept size_t values which may have come from an
1144 // inadvertent conversion from a negative int.
1145 return BAD_VALUE;
1146 }
1147
Jeff Brown13b16042014-11-11 16:44:25 -08001148 status_t status;
1149 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001150 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001151 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001152 if (status) return status;
1153
1154 void* ptr = writeInplace(len);
1155 if (!ptr) return NO_MEMORY;
1156
Jeff Brown13b16042014-11-11 16:44:25 -08001157 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001158 return NO_ERROR;
1159 }
1160
Steve Block6807e592011-10-20 11:56:00 +01001161 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162 int fd = ashmem_create_region("Parcel Blob", len);
1163 if (fd < 0) return NO_MEMORY;
1164
1165 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1166 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001167 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001168 } else {
1169 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1170 if (ptr == MAP_FAILED) {
1171 status = -errno;
1172 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001173 if (!mutableCopy) {
1174 result = ashmem_set_prot_region(fd, PROT_READ);
1175 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001176 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001177 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001178 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001179 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001180 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001181 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001182 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001183 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001184 return NO_ERROR;
1185 }
1186 }
1187 }
1188 }
1189 ::munmap(ptr, len);
1190 }
1191 ::close(fd);
1192 return status;
1193}
1194
Jeff Brown13b16042014-11-11 16:44:25 -08001195status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1196{
1197 // Must match up with what's done in writeBlob.
1198 if (!mAllowFds) return FDS_NOT_ALLOWED;
1199 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1200 if (status) return status;
1201 return writeDupFileDescriptor(fd);
1202}
1203
Mathias Agopiane1424282013-07-29 21:24:40 -07001204status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001205{
1206 status_t err;
1207
1208 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001209 const size_t len = val.getFlattenedSize();
1210 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001211
Nick Kralevichb6b14232015-04-02 09:36:02 -07001212 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1213 // don't accept size_t values which may have come from an
1214 // inadvertent conversion from a negative int.
1215 return BAD_VALUE;
1216 }
1217
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001218 err = this->writeInt32(len);
1219 if (err) return err;
1220
1221 err = this->writeInt32(fd_count);
1222 if (err) return err;
1223
1224 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001225 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001226 if (buf == NULL)
1227 return BAD_VALUE;
1228
1229 int* fds = NULL;
1230 if (fd_count) {
1231 fds = new int[fd_count];
1232 }
1233
1234 err = val.flatten(buf, len, fds, fd_count);
1235 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1236 err = this->writeDupFileDescriptor( fds[i] );
1237 }
1238
1239 if (fd_count) {
1240 delete [] fds;
1241 }
1242
1243 return err;
1244}
1245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001246status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1247{
1248 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1249 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1250 if (enoughData && enoughObjects) {
1251restart_write:
1252 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001253
Christopher Tate98e67d32015-06-03 18:44:15 -07001254 // remember if it's a file descriptor
1255 if (val.type == BINDER_TYPE_FD) {
1256 if (!mAllowFds) {
1257 // fail before modifying our object index
1258 return FDS_NOT_ALLOWED;
1259 }
1260 mHasFds = mFdsKnown = true;
1261 }
1262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001263 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001264 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001265 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001266 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001267 mObjectsSize++;
1268 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001269
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001270 return finishWrite(sizeof(flat_binder_object));
1271 }
1272
1273 if (!enoughData) {
1274 const status_t err = growData(sizeof(val));
1275 if (err != NO_ERROR) return err;
1276 }
1277 if (!enoughObjects) {
1278 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001279 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001280 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 if (objects == NULL) return NO_MEMORY;
1282 mObjects = objects;
1283 mObjectsCapacity = newSize;
1284 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001286 goto restart_write;
1287}
1288
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001289status_t Parcel::writeNoException()
1290{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001291 binder::Status status;
1292 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001293}
1294
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001295void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296{
1297 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1298}
1299
1300status_t Parcel::read(void* outData, size_t len) const
1301{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001302 if (len > INT32_MAX) {
1303 // don't accept size_t values which may have come from an
1304 // inadvertent conversion from a negative int.
1305 return BAD_VALUE;
1306 }
1307
1308 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1309 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001311 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001312 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313 return NO_ERROR;
1314 }
1315 return NOT_ENOUGH_DATA;
1316}
1317
1318const void* Parcel::readInplace(size_t len) const
1319{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001320 if (len > INT32_MAX) {
1321 // don't accept size_t values which may have come from an
1322 // inadvertent conversion from a negative int.
1323 return NULL;
1324 }
1325
1326 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1327 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001329 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001330 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331 return data;
1332 }
1333 return NULL;
1334}
1335
Andreas Huber84a6d042009-08-17 13:33:27 -07001336template<class T>
1337status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001338 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001339
1340 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001342 mDataPos += sizeof(T);
1343 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 return NO_ERROR;
1345 } else {
1346 return NOT_ENOUGH_DATA;
1347 }
1348}
1349
Andreas Huber84a6d042009-08-17 13:33:27 -07001350template<class T>
1351T Parcel::readAligned() const {
1352 T result;
1353 if (readAligned(&result) != NO_ERROR) {
1354 result = 0;
1355 }
1356
1357 return result;
1358}
1359
1360template<class T>
1361status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001362 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001363
1364 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1365restart_write:
1366 *reinterpret_cast<T*>(mData+mDataPos) = val;
1367 return finishWrite(sizeof(val));
1368 }
1369
1370 status_t err = growData(sizeof(val));
1371 if (err == NO_ERROR) goto restart_write;
1372 return err;
1373}
1374
Casey Dahlin451ff582015-10-19 18:12:18 -07001375status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1376 val->clear();
1377
1378 int32_t size;
1379 status_t status = readInt32(&size);
1380
1381 if (status != OK) {
1382 return status;
1383 }
1384
Christopher Wiley4db672d2015-11-10 09:44:30 -08001385 if (size < 0) {
1386 status = UNEXPECTED_NULL;
1387 return status;
1388 }
1389 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001390 status = BAD_VALUE;
1391 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001392 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001393
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001394 const void* data = readInplace(size);
1395 if (!data) {
1396 status = BAD_VALUE;
1397 return status;
1398 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001399 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001400 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001401
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001402 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001403}
1404
Casey Dahlinb9872622015-11-25 15:09:45 -08001405status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1406 const int32_t start = dataPosition();
1407 int32_t size;
1408 status_t status = readInt32(&size);
1409 val->reset();
1410
1411 if (status != OK || size < 0) {
1412 return status;
1413 }
1414
1415 setDataPosition(start);
1416 val->reset(new std::vector<int8_t>());
1417
1418 status = readByteVector(val->get());
1419
1420 if (status != OK) {
1421 val->reset();
1422 }
1423
1424 return status;
1425}
1426
1427status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1428 return readNullableTypedVector(val, &Parcel::readInt32);
1429}
1430
Casey Dahlin451ff582015-10-19 18:12:18 -07001431status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001432 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001433}
1434
Casey Dahlinb9872622015-11-25 15:09:45 -08001435status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1436 return readNullableTypedVector(val, &Parcel::readInt64);
1437}
1438
Casey Dahlin451ff582015-10-19 18:12:18 -07001439status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001440 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001441}
1442
Casey Dahlinb9872622015-11-25 15:09:45 -08001443status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1444 return readNullableTypedVector(val, &Parcel::readFloat);
1445}
1446
Casey Dahlin451ff582015-10-19 18:12:18 -07001447status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001448 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001449}
1450
Casey Dahlinb9872622015-11-25 15:09:45 -08001451status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1452 return readNullableTypedVector(val, &Parcel::readDouble);
1453}
1454
Casey Dahlin451ff582015-10-19 18:12:18 -07001455status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001456 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001457}
1458
Casey Dahlinb9872622015-11-25 15:09:45 -08001459status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1460 const int32_t start = dataPosition();
1461 int32_t size;
1462 status_t status = readInt32(&size);
1463 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001464
Casey Dahlinb9872622015-11-25 15:09:45 -08001465 if (status != OK || size < 0) {
1466 return status;
1467 }
1468
1469 setDataPosition(start);
1470 val->reset(new std::vector<bool>());
1471
1472 status = readBoolVector(val->get());
1473
1474 if (status != OK) {
1475 val->reset();
1476 }
1477
1478 return status;
1479}
1480
1481status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001482 int32_t size;
1483 status_t status = readInt32(&size);
1484
1485 if (status != OK) {
1486 return status;
1487 }
1488
1489 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001490 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001491 }
1492
1493 val->resize(size);
1494
1495 /* C++ bool handling means a vector of bools isn't necessarily addressable
1496 * (we might use individual bits)
1497 */
Christopher Wiley97887982015-10-27 16:33:47 -07001498 bool data;
1499 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001500 status = readBool(&data);
1501 (*val)[i] = data;
1502
1503 if (status != OK) {
1504 return status;
1505 }
1506 }
1507
1508 return OK;
1509}
1510
Casey Dahlinb9872622015-11-25 15:09:45 -08001511status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1512 return readNullableTypedVector(val, &Parcel::readChar);
1513}
1514
Casey Dahlin451ff582015-10-19 18:12:18 -07001515status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001516 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001517}
1518
Casey Dahlinb9872622015-11-25 15:09:45 -08001519status_t Parcel::readString16Vector(
1520 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1521 return readNullableTypedVector(val, &Parcel::readString16);
1522}
1523
Casey Dahlin451ff582015-10-19 18:12:18 -07001524status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001525 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001526}
1527
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001528status_t Parcel::readUtf8VectorFromUtf16Vector(
1529 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1530 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1531}
1532
1533status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1534 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1535}
Casey Dahlin451ff582015-10-19 18:12:18 -07001536
Andreas Huber84a6d042009-08-17 13:33:27 -07001537status_t Parcel::readInt32(int32_t *pArg) const
1538{
1539 return readAligned(pArg);
1540}
1541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542int32_t Parcel::readInt32() const
1543{
Andreas Huber84a6d042009-08-17 13:33:27 -07001544 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545}
1546
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001547status_t Parcel::readUint32(uint32_t *pArg) const
1548{
1549 return readAligned(pArg);
1550}
1551
1552uint32_t Parcel::readUint32() const
1553{
1554 return readAligned<uint32_t>();
1555}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556
1557status_t Parcel::readInt64(int64_t *pArg) const
1558{
Andreas Huber84a6d042009-08-17 13:33:27 -07001559 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001560}
1561
1562
1563int64_t Parcel::readInt64() const
1564{
Andreas Huber84a6d042009-08-17 13:33:27 -07001565 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001566}
1567
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001568status_t Parcel::readUint64(uint64_t *pArg) const
1569{
1570 return readAligned(pArg);
1571}
1572
1573uint64_t Parcel::readUint64() const
1574{
1575 return readAligned<uint64_t>();
1576}
1577
Serban Constantinescuf683e012013-11-05 16:53:55 +00001578status_t Parcel::readPointer(uintptr_t *pArg) const
1579{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001580 status_t ret;
1581 binder_uintptr_t ptr;
1582 ret = readAligned(&ptr);
1583 if (!ret)
1584 *pArg = ptr;
1585 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001586}
1587
1588uintptr_t Parcel::readPointer() const
1589{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001590 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001591}
1592
1593
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594status_t Parcel::readFloat(float *pArg) const
1595{
Andreas Huber84a6d042009-08-17 13:33:27 -07001596 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597}
1598
1599
1600float Parcel::readFloat() const
1601{
Andreas Huber84a6d042009-08-17 13:33:27 -07001602 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603}
1604
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001605#if defined(__mips__) && defined(__mips_hard_float)
1606
1607status_t Parcel::readDouble(double *pArg) const
1608{
1609 union {
1610 double d;
1611 unsigned long long ll;
1612 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001613 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001614 status_t status;
1615 status = readAligned(&u.ll);
1616 *pArg = u.d;
1617 return status;
1618}
1619
1620double Parcel::readDouble() const
1621{
1622 union {
1623 double d;
1624 unsigned long long ll;
1625 } u;
1626 u.ll = readAligned<unsigned long long>();
1627 return u.d;
1628}
1629
1630#else
1631
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001632status_t Parcel::readDouble(double *pArg) const
1633{
Andreas Huber84a6d042009-08-17 13:33:27 -07001634 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001635}
1636
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637double Parcel::readDouble() const
1638{
Andreas Huber84a6d042009-08-17 13:33:27 -07001639 return readAligned<double>();
1640}
1641
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001642#endif
1643
Andreas Huber84a6d042009-08-17 13:33:27 -07001644status_t Parcel::readIntPtr(intptr_t *pArg) const
1645{
1646 return readAligned(pArg);
1647}
1648
1649
1650intptr_t Parcel::readIntPtr() const
1651{
1652 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001653}
1654
Casey Dahlind6848f52015-10-15 15:44:59 -07001655status_t Parcel::readBool(bool *pArg) const
1656{
1657 int32_t tmp;
1658 status_t ret = readInt32(&tmp);
1659 *pArg = (tmp != 0);
1660 return ret;
1661}
1662
1663bool Parcel::readBool() const
1664{
1665 return readInt32() != 0;
1666}
1667
1668status_t Parcel::readChar(char16_t *pArg) const
1669{
1670 int32_t tmp;
1671 status_t ret = readInt32(&tmp);
1672 *pArg = char16_t(tmp);
1673 return ret;
1674}
1675
1676char16_t Parcel::readChar() const
1677{
1678 return char16_t(readInt32());
1679}
1680
1681status_t Parcel::readByte(int8_t *pArg) const
1682{
1683 int32_t tmp;
1684 status_t ret = readInt32(&tmp);
1685 *pArg = int8_t(tmp);
1686 return ret;
1687}
1688
1689int8_t Parcel::readByte() const
1690{
1691 return int8_t(readInt32());
1692}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001693
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001694status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1695 size_t utf16Size = 0;
1696 const char16_t* src = readString16Inplace(&utf16Size);
1697 if (!src) {
1698 return UNEXPECTED_NULL;
1699 }
1700
1701 // Save ourselves the trouble, we're done.
1702 if (utf16Size == 0u) {
1703 str->clear();
1704 return NO_ERROR;
1705 }
1706
1707 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1708 if (utf8Size < 0) {
1709 return BAD_VALUE;
1710 }
1711 // Note that while it is probably safe to assume string::resize keeps a
1712 // spare byte around for the trailing null, we're going to be explicit.
1713 str->resize(utf8Size + 1);
1714 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1715 str->resize(utf8Size);
1716 return NO_ERROR;
1717}
1718
1719status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1720 const int32_t start = dataPosition();
1721 int32_t size;
1722 status_t status = readInt32(&size);
1723 str->reset();
1724
1725 if (status != OK || size < 0) {
1726 return status;
1727 }
1728
1729 setDataPosition(start);
1730 str->reset(new std::string());
1731 return readUtf8FromUtf16(str->get());
1732}
1733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001734const char* Parcel::readCString() const
1735{
1736 const size_t avail = mDataSize-mDataPos;
1737 if (avail > 0) {
1738 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1739 // is the string's trailing NUL within the parcel's valid bounds?
1740 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1741 if (eos) {
1742 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001743 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001744 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745 return str;
1746 }
1747 }
1748 return NULL;
1749}
1750
1751String8 Parcel::readString8() const
1752{
1753 int32_t size = readInt32();
1754 // watch for potential int overflow adding 1 for trailing NUL
1755 if (size > 0 && size < INT32_MAX) {
1756 const char* str = (const char*)readInplace(size+1);
1757 if (str) return String8(str, size);
1758 }
1759 return String8();
1760}
1761
1762String16 Parcel::readString16() const
1763{
1764 size_t len;
1765 const char16_t* str = readString16Inplace(&len);
1766 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001767 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001768 return String16();
1769}
1770
Casey Dahlinb9872622015-11-25 15:09:45 -08001771status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1772{
1773 const int32_t start = dataPosition();
1774 int32_t size;
1775 status_t status = readInt32(&size);
1776 pArg->reset();
1777
1778 if (status != OK || size < 0) {
1779 return status;
1780 }
1781
1782 setDataPosition(start);
1783 pArg->reset(new String16());
1784
1785 status = readString16(pArg->get());
1786
1787 if (status != OK) {
1788 pArg->reset();
1789 }
1790
1791 return status;
1792}
1793
Casey Dahlin451ff582015-10-19 18:12:18 -07001794status_t Parcel::readString16(String16* pArg) const
1795{
1796 size_t len;
1797 const char16_t* str = readString16Inplace(&len);
1798 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001799 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001800 return 0;
1801 } else {
1802 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001803 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001804 }
1805}
1806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1808{
1809 int32_t size = readInt32();
1810 // watch for potential int overflow from size+1
1811 if (size >= 0 && size < INT32_MAX) {
1812 *outLen = size;
1813 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1814 if (str != NULL) {
1815 return str;
1816 }
1817 }
1818 *outLen = 0;
1819 return NULL;
1820}
1821
Casey Dahlinf0c13772015-10-27 18:33:56 -07001822status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1823{
1824 return unflatten_binder(ProcessState::self(), *this, val);
1825}
1826
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827sp<IBinder> Parcel::readStrongBinder() const
1828{
1829 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001830 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831 return val;
1832}
1833
1834wp<IBinder> Parcel::readWeakBinder() const
1835{
1836 wp<IBinder> val;
1837 unflatten_binder(ProcessState::self(), *this, &val);
1838 return val;
1839}
1840
Christopher Wiley97f048d2015-11-19 06:49:05 -08001841status_t Parcel::readParcelable(Parcelable* parcelable) const {
1842 int32_t have_parcelable = 0;
1843 status_t status = readInt32(&have_parcelable);
1844 if (status != OK) {
1845 return status;
1846 }
1847 if (!have_parcelable) {
1848 return UNEXPECTED_NULL;
1849 }
1850 return parcelable->readFromParcel(this);
1851}
1852
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001853int32_t Parcel::readExceptionCode() const
1854{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001855 binder::Status status;
1856 status.readFromParcel(*this);
1857 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001858}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001859
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001860native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001861{
1862 int numFds, numInts;
1863 status_t err;
1864 err = readInt32(&numFds);
1865 if (err != NO_ERROR) return 0;
1866 err = readInt32(&numInts);
1867 if (err != NO_ERROR) return 0;
1868
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001869 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001870 if (!h) {
1871 return 0;
1872 }
1873
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001874 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001875 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001876 if (h->data[i] < 0) err = BAD_VALUE;
1877 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001878 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001879 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001880 native_handle_close(h);
1881 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001882 h = 0;
1883 }
1884 return h;
1885}
1886
1887
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888int Parcel::readFileDescriptor() const
1889{
1890 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001891
1892 if (flat && flat->type == BINDER_TYPE_FD) {
1893 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001894 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001895
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 return BAD_TYPE;
1897}
1898
Casey Dahlin06673e32015-11-23 13:24:23 -08001899status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1900{
1901 int got = readFileDescriptor();
1902
1903 if (got == BAD_TYPE) {
1904 return BAD_TYPE;
1905 }
1906
1907 val->reset(dup(got));
1908
1909 if (val->get() < 0) {
1910 return BAD_VALUE;
1911 }
1912
1913 return OK;
1914}
1915
1916
Casey Dahlinb9872622015-11-25 15:09:45 -08001917status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1918 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1919}
1920
Casey Dahlin06673e32015-11-23 13:24:23 -08001921status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1922 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1923}
1924
Jeff Brown5707dbf2011-09-23 21:17:56 -07001925status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1926{
Jeff Brown13b16042014-11-11 16:44:25 -08001927 int32_t blobType;
1928 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001929 if (status) return status;
1930
Jeff Brown13b16042014-11-11 16:44:25 -08001931 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001932 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001933 const void* ptr = readInplace(len);
1934 if (!ptr) return BAD_VALUE;
1935
Jeff Brown13b16042014-11-11 16:44:25 -08001936 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001937 return NO_ERROR;
1938 }
1939
Steve Block6807e592011-10-20 11:56:00 +01001940 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001941 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001942 int fd = readFileDescriptor();
1943 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1944
Jeff Brown13b16042014-11-11 16:44:25 -08001945 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1946 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001947 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001948
Jeff Brown13b16042014-11-11 16:44:25 -08001949 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001950 return NO_ERROR;
1951}
1952
Mathias Agopiane1424282013-07-29 21:24:40 -07001953status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001954{
1955 // size
1956 const size_t len = this->readInt32();
1957 const size_t fd_count = this->readInt32();
1958
Nick Kralevichb6b14232015-04-02 09:36:02 -07001959 if (len > INT32_MAX) {
1960 // don't accept size_t values which may have come from an
1961 // inadvertent conversion from a negative int.
1962 return BAD_VALUE;
1963 }
1964
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001965 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001966 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001967 if (buf == NULL)
1968 return BAD_VALUE;
1969
1970 int* fds = NULL;
1971 if (fd_count) {
1972 fds = new int[fd_count];
1973 }
1974
1975 status_t err = NO_ERROR;
1976 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001977 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001978 if (fds[i] < 0) {
1979 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001980 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1981 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001982 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001983 }
1984
1985 if (err == NO_ERROR) {
1986 err = val.unflatten(buf, len, fds, fd_count);
1987 }
1988
1989 if (fd_count) {
1990 delete [] fds;
1991 }
1992
1993 return err;
1994}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001995const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1996{
1997 const size_t DPOS = mDataPos;
1998 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1999 const flat_binder_object* obj
2000 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2001 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002002 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002003 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002004 // the object list, so we don't want to check for it when
2005 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002006 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002007 return obj;
2008 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002011 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002012 const size_t N = mObjectsSize;
2013 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002014
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002016 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002018
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019 // Start at the current hint position, looking for an object at
2020 // the current data position.
2021 if (opos < N) {
2022 while (opos < (N-1) && OBJS[opos] < DPOS) {
2023 opos++;
2024 }
2025 } else {
2026 opos = N-1;
2027 }
2028 if (OBJS[opos] == DPOS) {
2029 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002030 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002031 this, DPOS, opos);
2032 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002033 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034 return obj;
2035 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002036
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 // Look backwards for it...
2038 while (opos > 0 && OBJS[opos] > DPOS) {
2039 opos--;
2040 }
2041 if (OBJS[opos] == DPOS) {
2042 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 this, DPOS, opos);
2045 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002046 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047 return obj;
2048 }
2049 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002050 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 -07002051 this, DPOS);
2052 }
2053 return NULL;
2054}
2055
2056void Parcel::closeFileDescriptors()
2057{
2058 size_t i = mObjectsSize;
2059 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002060 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002061 }
2062 while (i > 0) {
2063 i--;
2064 const flat_binder_object* flat
2065 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2066 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 close(flat->handle);
2069 }
2070 }
2071}
2072
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002073uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002075 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076}
2077
2078size_t Parcel::ipcDataSize() const
2079{
2080 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2081}
2082
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002083uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002085 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086}
2087
2088size_t Parcel::ipcObjectsCount() const
2089{
2090 return mObjectsSize;
2091}
2092
2093void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002094 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002096 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 freeDataNoInit();
2098 mError = NO_ERROR;
2099 mData = const_cast<uint8_t*>(data);
2100 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002101 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002102 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002103 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002104 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 mObjectsSize = mObjectsCapacity = objectsCount;
2106 mNextObjectHint = 0;
2107 mOwner = relFunc;
2108 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002109 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002110 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002111 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002112 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002113 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002114 mObjectsSize = 0;
2115 break;
2116 }
2117 minOffset = offset + sizeof(flat_binder_object);
2118 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 scanForFds();
2120}
2121
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002122void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123{
2124 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002125
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002126 if (errorCheck() != NO_ERROR) {
2127 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002128 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129 } else if (dataSize() > 0) {
2130 const uint8_t* DATA = data();
2131 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002132 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 const size_t N = objectsCount();
2134 for (size_t i=0; i<N; i++) {
2135 const flat_binder_object* flat
2136 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2137 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2138 << TypeCode(flat->type & 0x7f7f7f00)
2139 << " = " << flat->binder;
2140 }
2141 } else {
2142 to << "NULL";
2143 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002144
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145 to << ")";
2146}
2147
2148void Parcel::releaseObjects()
2149{
2150 const sp<ProcessState> proc(ProcessState::self());
2151 size_t i = mObjectsSize;
2152 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002153 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 while (i > 0) {
2155 i--;
2156 const flat_binder_object* flat
2157 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002158 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 }
2160}
2161
2162void Parcel::acquireObjects()
2163{
2164 const sp<ProcessState> proc(ProcessState::self());
2165 size_t i = mObjectsSize;
2166 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002167 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 while (i > 0) {
2169 i--;
2170 const flat_binder_object* flat
2171 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002172 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 }
2174}
2175
2176void Parcel::freeData()
2177{
2178 freeDataNoInit();
2179 initState();
2180}
2181
2182void Parcel::freeDataNoInit()
2183{
2184 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002185 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2188 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002189 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002191 if (mData) {
2192 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002193 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002194 if (mDataCapacity <= gParcelGlobalAllocSize) {
2195 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2196 } else {
2197 gParcelGlobalAllocSize = 0;
2198 }
2199 if (gParcelGlobalAllocCount > 0) {
2200 gParcelGlobalAllocCount--;
2201 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002202 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002203 free(mData);
2204 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 if (mObjects) free(mObjects);
2206 }
2207}
2208
2209status_t Parcel::growData(size_t len)
2210{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002211 if (len > INT32_MAX) {
2212 // don't accept size_t values which may have come from an
2213 // inadvertent conversion from a negative int.
2214 return BAD_VALUE;
2215 }
2216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217 size_t newSize = ((mDataSize+len)*3)/2;
2218 return (newSize <= mDataSize)
2219 ? (status_t) NO_MEMORY
2220 : continueWrite(newSize);
2221}
2222
2223status_t Parcel::restartWrite(size_t desired)
2224{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002225 if (desired > INT32_MAX) {
2226 // don't accept size_t values which may have come from an
2227 // inadvertent conversion from a negative int.
2228 return BAD_VALUE;
2229 }
2230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 if (mOwner) {
2232 freeData();
2233 return continueWrite(desired);
2234 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 uint8_t* data = (uint8_t*)realloc(mData, desired);
2237 if (!data && desired > mDataCapacity) {
2238 mError = NO_MEMORY;
2239 return NO_MEMORY;
2240 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002243
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002244 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002245 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002246 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002247 gParcelGlobalAllocSize += desired;
2248 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002249 if (!mData) {
2250 gParcelGlobalAllocCount++;
2251 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002252 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 mData = data;
2254 mDataCapacity = desired;
2255 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002256
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002258 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2259 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2260
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 free(mObjects);
2262 mObjects = NULL;
2263 mObjectsSize = mObjectsCapacity = 0;
2264 mNextObjectHint = 0;
2265 mHasFds = false;
2266 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002267 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002268
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 return NO_ERROR;
2270}
2271
2272status_t Parcel::continueWrite(size_t desired)
2273{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002274 if (desired > INT32_MAX) {
2275 // don't accept size_t values which may have come from an
2276 // inadvertent conversion from a negative int.
2277 return BAD_VALUE;
2278 }
2279
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 // If shrinking, first adjust for any objects that appear
2281 // after the new data size.
2282 size_t objectsSize = mObjectsSize;
2283 if (desired < mDataSize) {
2284 if (desired == 0) {
2285 objectsSize = 0;
2286 } else {
2287 while (objectsSize > 0) {
2288 if (mObjects[objectsSize-1] < desired)
2289 break;
2290 objectsSize--;
2291 }
2292 }
2293 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 if (mOwner) {
2296 // If the size is going to zero, just release the owner's data.
2297 if (desired == 0) {
2298 freeData();
2299 return NO_ERROR;
2300 }
2301
2302 // If there is a different owner, we need to take
2303 // posession.
2304 uint8_t* data = (uint8_t*)malloc(desired);
2305 if (!data) {
2306 mError = NO_MEMORY;
2307 return NO_MEMORY;
2308 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002309 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002310
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002312 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002314 free(data);
2315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 mError = NO_MEMORY;
2317 return NO_MEMORY;
2318 }
2319
2320 // Little hack to only acquire references on objects
2321 // we will be keeping.
2322 size_t oldObjectsSize = mObjectsSize;
2323 mObjectsSize = objectsSize;
2324 acquireObjects();
2325 mObjectsSize = oldObjectsSize;
2326 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 if (mData) {
2329 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2330 }
2331 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002332 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002334 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2336 mOwner = NULL;
2337
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002338 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002339 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002340 gParcelGlobalAllocSize += desired;
2341 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002342 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mData = data;
2345 mObjects = objects;
2346 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002347 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 mDataCapacity = desired;
2349 mObjectsSize = mObjectsCapacity = objectsSize;
2350 mNextObjectHint = 0;
2351
2352 } else if (mData) {
2353 if (objectsSize < mObjectsSize) {
2354 // Need to release refs on any objects we are dropping.
2355 const sp<ProcessState> proc(ProcessState::self());
2356 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2357 const flat_binder_object* flat
2358 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2359 if (flat->type == BINDER_TYPE_FD) {
2360 // will need to rescan because we may have lopped off the only FDs
2361 mFdsKnown = false;
2362 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002363 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002365 binder_size_t* objects =
2366 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 if (objects) {
2368 mObjects = objects;
2369 }
2370 mObjectsSize = objectsSize;
2371 mNextObjectHint = 0;
2372 }
2373
2374 // We own the data, so we can just do a realloc().
2375 if (desired > mDataCapacity) {
2376 uint8_t* data = (uint8_t*)realloc(mData, desired);
2377 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002378 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2379 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002380 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002381 gParcelGlobalAllocSize += desired;
2382 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002383 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 mData = data;
2385 mDataCapacity = desired;
2386 } else if (desired > mDataCapacity) {
2387 mError = NO_MEMORY;
2388 return NO_MEMORY;
2389 }
2390 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002391 if (mDataSize > desired) {
2392 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002393 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002394 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 if (mDataPos > desired) {
2396 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002397 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 }
2399 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002400
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 } else {
2402 // This is the first data. Easy!
2403 uint8_t* data = (uint8_t*)malloc(desired);
2404 if (!data) {
2405 mError = NO_MEMORY;
2406 return NO_MEMORY;
2407 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if(!(mDataCapacity == 0 && mObjects == NULL
2410 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002411 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002413
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002414 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002415 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002416 gParcelGlobalAllocSize += desired;
2417 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002418 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 mData = data;
2421 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002422 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2423 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 mDataCapacity = desired;
2425 }
2426
2427 return NO_ERROR;
2428}
2429
2430void Parcel::initState()
2431{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002432 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 mError = NO_ERROR;
2434 mData = 0;
2435 mDataSize = 0;
2436 mDataCapacity = 0;
2437 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002438 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2439 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 mObjects = NULL;
2441 mObjectsSize = 0;
2442 mObjectsCapacity = 0;
2443 mNextObjectHint = 0;
2444 mHasFds = false;
2445 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002446 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002448 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449}
2450
2451void Parcel::scanForFds() const
2452{
2453 bool hasFds = false;
2454 for (size_t i=0; i<mObjectsSize; i++) {
2455 const flat_binder_object* flat
2456 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2457 if (flat->type == BINDER_TYPE_FD) {
2458 hasFds = true;
2459 break;
2460 }
2461 }
2462 mHasFds = hasFds;
2463 mFdsKnown = true;
2464}
2465
Dan Sandleraa5c2342015-04-10 10:08:45 -04002466size_t Parcel::getBlobAshmemSize() const
2467{
Adrian Roos6bb31142015-10-22 16:46:12 -07002468 // This used to return the size of all blobs that were written to ashmem, now we're returning
2469 // the ashmem currently referenced by this Parcel, which should be equivalent.
2470 // TODO: Remove method once ABI can be changed.
2471 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002472}
2473
Adrian Rooscbf37262015-10-22 16:12:53 -07002474size_t Parcel::getOpenAshmemSize() const
2475{
2476 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002477}
2478
2479// --- Parcel::Blob ---
2480
2481Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002482 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002483}
2484
2485Parcel::Blob::~Blob() {
2486 release();
2487}
2488
2489void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002490 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002491 ::munmap(mData, mSize);
2492 }
2493 clear();
2494}
2495
Jeff Brown13b16042014-11-11 16:44:25 -08002496void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2497 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002498 mData = data;
2499 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002500 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002501}
2502
2503void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002504 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002505 mData = NULL;
2506 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002507 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002508}
2509
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510}; // namespace android