blob: 16a20cf87de1b4e3d4d40dc79bc5504d2fb07590 [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>
Mathias Agopian002e1e52013-05-06 20:20:50 -070026#include <binder/TextOutput.h>
27
Jun Jiangabf8a2c2014-04-29 14:22:10 +080028#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070029#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080034#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070035#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036
Mathias Agopian208059f2009-05-18 15:08:03 -070037#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080038#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080040#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070044#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
46#ifndef INT32_MAX
47#define INT32_MAX ((int32_t)(2147483647))
48#endif
49
50#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010051//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080052#define LOG_ALLOC(...)
53//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070054
55// ---------------------------------------------------------------------------
56
Nick Kralevichb6b14232015-04-02 09:36:02 -070057// This macro should never be used at runtime, as a too large value
58// of s could cause an integer overflow. Instead, you should always
59// use the wrapper function pad_size()
60#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
61
62static size_t pad_size(size_t s) {
63 if (s > (SIZE_T_MAX - 3)) {
64 abort();
65 }
66 return PAD_SIZE_UNSAFE(s);
67}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070068
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070069// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080070#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070072// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
73#define EX_HAS_REPLY_HEADER -128
74
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborna4cff882014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100{
101 switch (obj.type) {
102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return;
112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
114 if (b != NULL) {
115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
120 case BINDER_TYPE_WEAK_HANDLE: {
121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
122 if (b != NULL) b.get_refs()->incWeak(who);
123 return;
124 }
125 case BINDER_TYPE_FD: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700126 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700127 if (outAshmemSize != NULL) {
128 // If we own an ashmem fd, keep track of how much memory it refers to.
129 int size = ashmem_get_size_region(obj.handle);
130 if (size > 0) {
131 *outAshmemSize += size;
132 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700133 }
134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 return;
136 }
137 }
138
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800139 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140}
141
Adrian Roos6bb31142015-10-22 16:46:12 -0700142void acquire_object(const sp<ProcessState>& proc,
143 const flat_binder_object& obj, const void* who)
144{
145 acquire_object(proc, obj, who, NULL);
146}
147
148static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700149 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150{
151 switch (obj.type) {
152 case BINDER_TYPE_BINDER:
153 if (obj.binder) {
154 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800155 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 }
157 return;
158 case BINDER_TYPE_WEAK_BINDER:
159 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800160 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 return;
162 case BINDER_TYPE_HANDLE: {
163 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
164 if (b != NULL) {
165 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
166 b->decStrong(who);
167 }
168 return;
169 }
170 case BINDER_TYPE_WEAK_HANDLE: {
171 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
172 if (b != NULL) b.get_refs()->decWeak(who);
173 return;
174 }
175 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700176 if (outAshmemSize != NULL) {
177 if (obj.cookie != 0) {
178 int size = ashmem_get_size_region(obj.handle);
179 if (size > 0) {
180 *outAshmemSize -= size;
181 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700182
Adrian Roos6bb31142015-10-22 16:46:12 -0700183 close(obj.handle);
184 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700185 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 return;
187 }
188 }
189
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800190 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700191}
192
Adrian Roos6bb31142015-10-22 16:46:12 -0700193void release_object(const sp<ProcessState>& proc,
194 const flat_binder_object& obj, const void* who)
195{
196 release_object(proc, obj, who, NULL);
197}
198
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800200 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700201{
202 return out->writeObject(flat, false);
203}
204
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800205status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700206 const sp<IBinder>& binder, Parcel* out)
207{
208 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700209
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
211 if (binder != NULL) {
212 IBinder *local = binder->localBinder();
213 if (!local) {
214 BpBinder *proxy = binder->remoteBinder();
215 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700217 }
218 const int32_t handle = proxy ? proxy->handle() : 0;
219 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800220 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800222 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 } else {
224 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
226 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 }
228 } else {
229 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.binder = 0;
231 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 return finish_flatten_binder(binder, obj, out);
235}
236
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800237status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 const wp<IBinder>& binder, Parcel* out)
239{
240 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
243 if (binder != NULL) {
244 sp<IBinder> real = binder.promote();
245 if (real != NULL) {
246 IBinder *local = real->localBinder();
247 if (!local) {
248 BpBinder *proxy = real->remoteBinder();
249 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700251 }
252 const int32_t handle = proxy ? proxy->handle() : 0;
253 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800254 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 } else {
258 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
260 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
262 return finish_flatten_binder(real, obj, out);
263 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 // XXX How to deal? In order to flatten the given binder,
266 // we need to probe it for information, which requires a primary
267 // reference... but we don't have one.
268 //
269 // The OpenBinder implementation uses a dynamic_cast<> here,
270 // but we can't do that with the different reference counting
271 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000272 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800274 obj.binder = 0;
275 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 } else {
279 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 return finish_flatten_binder(NULL, obj, out);
283 }
284}
285
286inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800287 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
288 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289{
290 return NO_ERROR;
291}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700292
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293status_t unflatten_binder(const sp<ProcessState>& proc,
294 const Parcel& in, sp<IBinder>* out)
295{
296 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700297
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 if (flat) {
299 switch (flat->type) {
300 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800301 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302 return finish_unflatten_binder(NULL, *flat, in);
303 case BINDER_TYPE_HANDLE:
304 *out = proc->getStrongProxyForHandle(flat->handle);
305 return finish_unflatten_binder(
306 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700307 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308 }
309 return BAD_TYPE;
310}
311
312status_t unflatten_binder(const sp<ProcessState>& proc,
313 const Parcel& in, wp<IBinder>* out)
314{
315 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317 if (flat) {
318 switch (flat->type) {
319 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800320 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700321 return finish_unflatten_binder(NULL, *flat, in);
322 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800325 reinterpret_cast<IBinder*>(flat->cookie),
326 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 } else {
328 *out = NULL;
329 }
330 return finish_unflatten_binder(NULL, *flat, in);
331 case BINDER_TYPE_HANDLE:
332 case BINDER_TYPE_WEAK_HANDLE:
333 *out = proc->getWeakProxyForHandle(flat->handle);
334 return finish_unflatten_binder(
335 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
336 }
337 }
338 return BAD_TYPE;
339}
340
341// ---------------------------------------------------------------------------
342
343Parcel::Parcel()
344{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800345 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346 initState();
347}
348
349Parcel::~Parcel()
350{
351 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800352 LOG_ALLOC("Parcel %p: destroyed", this);
353}
354
355size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800356 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
357 size_t size = gParcelGlobalAllocSize;
358 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
359 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800360}
361
362size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800363 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
364 size_t count = gParcelGlobalAllocCount;
365 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
366 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700367}
368
369const uint8_t* Parcel::data() const
370{
371 return mData;
372}
373
374size_t Parcel::dataSize() const
375{
376 return (mDataSize > mDataPos ? mDataSize : mDataPos);
377}
378
379size_t Parcel::dataAvail() const
380{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700381 size_t result = dataSize() - dataPosition();
382 if (result > INT32_MAX) {
383 abort();
384 }
385 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700386}
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
Casey Dahlin08bb1452015-10-19 18:12:18 -0700746status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
747{
748 if (val.size() > std::numeric_limits<int32_t>::max()) {
749 return BAD_VALUE;
750 }
751
752 status_t status = writeInt32(val.size());
753
754 if (status != OK) {
755 return status;
756 }
757
758 for (const auto& item : val) {
759 status = writeByte(item);
760
761 if (status != OK) {
762 return status;
763 }
764 }
765
766 return OK;
767}
768
769status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
770{
771 if (val.size() > std::numeric_limits<int32_t>::max()) {
772 return BAD_VALUE;
773 }
774
775 status_t status = writeInt32(val.size());
776
777 if (status != OK) {
778 return status;
779 }
780
781 for (const auto& item : val) {
782 status = writeInt32(item);
783
784 if (status != OK) {
785 return status;
786 }
787 }
788
789 return OK;
790}
791
792status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
793{
794 if (val.size() > std::numeric_limits<int32_t>::max()) {
795 return BAD_VALUE;
796 }
797
798 status_t status = writeInt32(val.size());
799
800 if (status != OK) {
801 return status;
802 }
803
804 for (const auto& item : val) {
805 status = writeInt64(item);
806
807 if (status != OK) {
808 return status;
809 }
810 }
811
812 return OK;
813}
814
815status_t Parcel::writeFloatVector(const std::vector<float>& val)
816{
817 if (val.size() > std::numeric_limits<int32_t>::max()) {
818 return BAD_VALUE;
819 }
820
821 status_t status = writeInt32(val.size());
822
823 if (status != OK) {
824 return status;
825 }
826
827 for (const auto& item : val) {
828 status = writeFloat(item);
829
830 if (status != OK) {
831 return status;
832 }
833 }
834
835 return OK;
836}
837
838status_t Parcel::writeDoubleVector(const std::vector<double>& val)
839{
840 if (val.size() > std::numeric_limits<int32_t>::max()) {
841 return BAD_VALUE;
842 }
843
844 status_t status = writeInt32(val.size());
845
846 if (status != OK) {
847 return status;
848 }
849
850 for (const auto& item : val) {
851 status = writeDouble(item);
852
853 if (status != OK) {
854 return status;
855 }
856 }
857
858 return OK;
859}
860
861status_t Parcel::writeBoolVector(const std::vector<bool>& val)
862{
863 if (val.size() > std::numeric_limits<int32_t>::max()) {
864 return BAD_VALUE;
865 }
866
867 status_t status = writeInt32(val.size());
868
869 if (status != OK) {
870 return status;
871 }
872
873 for (const auto& item : val) {
874 status = writeBool(item);
875
876 if (status != OK) {
877 return status;
878 }
879 }
880
881 return OK;
882}
883
884status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
885{
886 if (val.size() > std::numeric_limits<int32_t>::max()) {
887 return BAD_VALUE;
888 }
889
890 status_t status = writeInt32(val.size());
891
892 if (status != OK) {
893 return status;
894 }
895
896 for (const auto& item : val) {
897 status = writeChar(item);
898
899 if (status != OK) {
900 return status;
901 }
902 }
903
904 return OK;
905}
906
907status_t Parcel::writeString16Vector(const std::vector<String16>& val)
908{
909 if (val.size() > std::numeric_limits<int32_t>::max()) {
910 return BAD_VALUE;
911 }
912
913 status_t status = writeInt32(val.size());
914
915 if (status != OK) {
916 return status;
917 }
918
919 for (const auto& item : val) {
920 status = writeString16(item);
921
922 if (status != OK) {
923 return status;
924 }
925 }
926
927 return OK;
928}
929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930status_t Parcel::writeInt32(int32_t val)
931{
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800934
935status_t Parcel::writeUint32(uint32_t val)
936{
937 return writeAligned(val);
938}
939
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700940status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700941 if (len > INT32_MAX) {
942 // don't accept size_t values which may have come from an
943 // inadvertent conversion from a negative int.
944 return BAD_VALUE;
945 }
946
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700947 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700948 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700949 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700950 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700951 if (ret == NO_ERROR) {
952 ret = write(val, len * sizeof(*val));
953 }
954 return ret;
955}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700956status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700957 if (len > INT32_MAX) {
958 // don't accept size_t values which may have come from an
959 // inadvertent conversion from a negative int.
960 return BAD_VALUE;
961 }
962
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700963 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700964 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700965 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700966 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700967 if (ret == NO_ERROR) {
968 ret = write(val, len * sizeof(*val));
969 }
970 return ret;
971}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700972
Casey Dahlina3774912015-10-15 15:44:59 -0700973status_t Parcel::writeBool(bool val)
974{
975 return writeInt32(int32_t(val));
976}
977
978status_t Parcel::writeChar(char16_t val)
979{
980 return writeInt32(int32_t(val));
981}
982
983status_t Parcel::writeByte(int8_t val)
984{
985 return writeInt32(int32_t(val));
986}
987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988status_t Parcel::writeInt64(int64_t val)
989{
Andreas Huber84a6d042009-08-17 13:33:27 -0700990 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991}
992
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700993status_t Parcel::writeUint64(uint64_t val)
994{
995 return writeAligned(val);
996}
997
Serban Constantinescuf683e012013-11-05 16:53:55 +0000998status_t Parcel::writePointer(uintptr_t val)
999{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001000 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001001}
1002
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001003status_t Parcel::writeFloat(float val)
1004{
Andreas Huber84a6d042009-08-17 13:33:27 -07001005 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006}
1007
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001008#if defined(__mips__) && defined(__mips_hard_float)
1009
1010status_t Parcel::writeDouble(double val)
1011{
1012 union {
1013 double d;
1014 unsigned long long ll;
1015 } u;
1016 u.d = val;
1017 return writeAligned(u.ll);
1018}
1019
1020#else
1021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001022status_t Parcel::writeDouble(double val)
1023{
Andreas Huber84a6d042009-08-17 13:33:27 -07001024 return writeAligned(val);
1025}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001027#endif
1028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029status_t Parcel::writeCString(const char* str)
1030{
1031 return write(str, strlen(str)+1);
1032}
1033
1034status_t Parcel::writeString8(const String8& str)
1035{
1036 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001037 // only write string if its length is more than zero characters,
1038 // as readString8 will only read if the length field is non-zero.
1039 // this is slightly different from how writeString16 works.
1040 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041 err = write(str.string(), str.bytes()+1);
1042 }
1043 return err;
1044}
1045
1046status_t Parcel::writeString16(const String16& str)
1047{
1048 return writeString16(str.string(), str.size());
1049}
1050
1051status_t Parcel::writeString16(const char16_t* str, size_t len)
1052{
1053 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001054
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055 status_t err = writeInt32(len);
1056 if (err == NO_ERROR) {
1057 len *= sizeof(char16_t);
1058 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1059 if (data) {
1060 memcpy(data, str, len);
1061 *reinterpret_cast<char16_t*>(data+len) = 0;
1062 return NO_ERROR;
1063 }
1064 err = mError;
1065 }
1066 return err;
1067}
1068
1069status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1070{
1071 return flatten_binder(ProcessState::self(), val, this);
1072}
1073
1074status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1075{
1076 return flatten_binder(ProcessState::self(), val, this);
1077}
1078
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001079status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001080{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001081 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001082 return BAD_TYPE;
1083
1084 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001085 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001086 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001088 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001089 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001091 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1092 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093
1094 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001095 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 return err;
1097 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001098 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001099 return err;
1100}
1101
Jeff Brown93ff1f92011-11-04 19:01:44 -07001102status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103{
1104 flat_binder_object obj;
1105 obj.type = BINDER_TYPE_FD;
1106 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001107 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001109 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001110 return writeObject(obj, true);
1111}
1112
1113status_t Parcel::writeDupFileDescriptor(int fd)
1114{
Jeff Brownd341c712011-11-04 20:19:33 -07001115 int dupFd = dup(fd);
1116 if (dupFd < 0) {
1117 return -errno;
1118 }
1119 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1120 if (err) {
1121 close(dupFd);
1122 }
1123 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124}
1125
Jeff Brown13b16042014-11-11 16:44:25 -08001126status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001127{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001128 if (len > INT32_MAX) {
1129 // don't accept size_t values which may have come from an
1130 // inadvertent conversion from a negative int.
1131 return BAD_VALUE;
1132 }
1133
Jeff Brown13b16042014-11-11 16:44:25 -08001134 status_t status;
1135 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001136 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001137 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001138 if (status) return status;
1139
1140 void* ptr = writeInplace(len);
1141 if (!ptr) return NO_MEMORY;
1142
Jeff Brown13b16042014-11-11 16:44:25 -08001143 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001144 return NO_ERROR;
1145 }
1146
Steve Block6807e592011-10-20 11:56:00 +01001147 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 int fd = ashmem_create_region("Parcel Blob", len);
1149 if (fd < 0) return NO_MEMORY;
1150
1151 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1152 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001153 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001154 } else {
1155 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1156 if (ptr == MAP_FAILED) {
1157 status = -errno;
1158 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001159 if (!mutableCopy) {
1160 result = ashmem_set_prot_region(fd, PROT_READ);
1161 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001162 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001163 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001164 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001165 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001166 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001167 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001168 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001169 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001170 return NO_ERROR;
1171 }
1172 }
1173 }
1174 }
1175 ::munmap(ptr, len);
1176 }
1177 ::close(fd);
1178 return status;
1179}
1180
Jeff Brown13b16042014-11-11 16:44:25 -08001181status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1182{
1183 // Must match up with what's done in writeBlob.
1184 if (!mAllowFds) return FDS_NOT_ALLOWED;
1185 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1186 if (status) return status;
1187 return writeDupFileDescriptor(fd);
1188}
1189
Mathias Agopiane1424282013-07-29 21:24:40 -07001190status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001191{
1192 status_t err;
1193
1194 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001195 const size_t len = val.getFlattenedSize();
1196 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001197
Nick Kralevichb6b14232015-04-02 09:36:02 -07001198 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1199 // don't accept size_t values which may have come from an
1200 // inadvertent conversion from a negative int.
1201 return BAD_VALUE;
1202 }
1203
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001204 err = this->writeInt32(len);
1205 if (err) return err;
1206
1207 err = this->writeInt32(fd_count);
1208 if (err) return err;
1209
1210 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001211 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001212 if (buf == NULL)
1213 return BAD_VALUE;
1214
1215 int* fds = NULL;
1216 if (fd_count) {
1217 fds = new int[fd_count];
1218 }
1219
1220 err = val.flatten(buf, len, fds, fd_count);
1221 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1222 err = this->writeDupFileDescriptor( fds[i] );
1223 }
1224
1225 if (fd_count) {
1226 delete [] fds;
1227 }
1228
1229 return err;
1230}
1231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001232status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1233{
1234 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1235 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1236 if (enoughData && enoughObjects) {
1237restart_write:
1238 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001239
Christopher Tate98e67d32015-06-03 18:44:15 -07001240 // remember if it's a file descriptor
1241 if (val.type == BINDER_TYPE_FD) {
1242 if (!mAllowFds) {
1243 // fail before modifying our object index
1244 return FDS_NOT_ALLOWED;
1245 }
1246 mHasFds = mFdsKnown = true;
1247 }
1248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001250 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001252 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 mObjectsSize++;
1254 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001256 return finishWrite(sizeof(flat_binder_object));
1257 }
1258
1259 if (!enoughData) {
1260 const status_t err = growData(sizeof(val));
1261 if (err != NO_ERROR) return err;
1262 }
1263 if (!enoughObjects) {
1264 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001265 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001266 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001267 if (objects == NULL) return NO_MEMORY;
1268 mObjects = objects;
1269 mObjectsCapacity = newSize;
1270 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001271
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272 goto restart_write;
1273}
1274
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001275status_t Parcel::writeNoException()
1276{
1277 return writeInt32(0);
1278}
1279
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001280void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281{
1282 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1283}
1284
1285status_t Parcel::read(void* outData, size_t len) const
1286{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001287 if (len > INT32_MAX) {
1288 // don't accept size_t values which may have come from an
1289 // inadvertent conversion from a negative int.
1290 return BAD_VALUE;
1291 }
1292
1293 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1294 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001295 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001296 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001297 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001298 return NO_ERROR;
1299 }
1300 return NOT_ENOUGH_DATA;
1301}
1302
1303const void* Parcel::readInplace(size_t len) const
1304{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001305 if (len > INT32_MAX) {
1306 // don't accept size_t values which may have come from an
1307 // inadvertent conversion from a negative int.
1308 return NULL;
1309 }
1310
1311 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1312 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001314 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001315 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316 return data;
1317 }
1318 return NULL;
1319}
1320
Andreas Huber84a6d042009-08-17 13:33:27 -07001321template<class T>
1322status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001323 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001324
1325 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001327 mDataPos += sizeof(T);
1328 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 return NO_ERROR;
1330 } else {
1331 return NOT_ENOUGH_DATA;
1332 }
1333}
1334
Andreas Huber84a6d042009-08-17 13:33:27 -07001335template<class T>
1336T Parcel::readAligned() const {
1337 T result;
1338 if (readAligned(&result) != NO_ERROR) {
1339 result = 0;
1340 }
1341
1342 return result;
1343}
1344
1345template<class T>
1346status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001347 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001348
1349 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1350restart_write:
1351 *reinterpret_cast<T*>(mData+mDataPos) = val;
1352 return finishWrite(sizeof(val));
1353 }
1354
1355 status_t err = growData(sizeof(val));
1356 if (err == NO_ERROR) goto restart_write;
1357 return err;
1358}
1359
Casey Dahlin08bb1452015-10-19 18:12:18 -07001360status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1361 val->clear();
1362
1363 int32_t size;
1364 status_t status = readInt32(&size);
1365
1366 if (status != OK) {
1367 return status;
1368 }
1369
1370 if (size < 0) {
1371 return BAD_VALUE;
1372 }
1373
1374 val->resize(size);
1375
1376 for (auto& v : *val) {
1377 status = readByte(&v);
1378
1379 if (status != OK) {
1380 return status;
1381 }
1382 }
1383
1384 return OK;
1385}
1386
1387status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
1388 val->clear();
1389
1390 int32_t size;
1391 status_t status = readInt32(&size);
1392
1393 if (status != OK) {
1394 return status;
1395 }
1396
1397 if (size < 0) {
1398 return BAD_VALUE;
1399 }
1400
1401 val->resize(size);
1402
1403 for (auto& v: *val) {
1404 status = readInt32(&v);
1405
1406 if (status != OK) {
1407 return status;
1408 }
1409 }
1410
1411 return OK;
1412}
1413
1414status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
1415 val->clear();
1416
1417 int32_t size;
1418 status_t status = readInt32(&size);
1419
1420 if (status != OK) {
1421 return status;
1422 }
1423
1424 if (size < 0) {
1425 return BAD_VALUE;
1426 }
1427
1428 val->resize(size);
1429
1430 for (auto& v : *val) {
1431 status = readInt64(&v);
1432
1433 if (status != OK) {
1434 return status;
1435 }
1436 }
1437
1438 return OK;
1439}
1440
1441status_t Parcel::readFloatVector(std::vector<float>* val) const {
1442 val->clear();
1443
1444 int32_t size;
1445 status_t status = readInt32(&size);
1446
1447 if (status != OK) {
1448 return status;
1449 }
1450
1451 if (size < 0) {
1452 return BAD_VALUE;
1453 }
1454
1455 val->resize(size);
1456
1457 for (auto& v : *val) {
1458 status = readFloat(&v);
1459
1460 if (status != OK) {
1461 return status;
1462 }
1463 }
1464
1465 return OK;
1466}
1467
1468status_t Parcel::readDoubleVector(std::vector<double>* val) const {
1469 val->clear();
1470
1471 int32_t size;
1472 status_t status = readInt32(&size);
1473
1474 if (status != OK) {
1475 return status;
1476 }
1477
1478 if (size < 0) {
1479 return BAD_VALUE;
1480 }
1481
1482 val->resize(size);
1483
1484 for (auto& v : *val) {
1485 status = readDouble(&v);
1486
1487 if (status != OK) {
1488 return status;
1489 }
1490 }
1491
1492 return OK;
1493}
1494
1495status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1496 val->clear();
1497
1498 int32_t size;
1499 status_t status = readInt32(&size);
1500
1501 if (status != OK) {
1502 return status;
1503 }
1504
1505 if (size < 0) {
1506 return BAD_VALUE;
1507 }
1508
1509 val->resize(size);
1510
1511 /* C++ bool handling means a vector of bools isn't necessarily addressable
1512 * (we might use individual bits)
1513 */
1514 for (int32_t i = 0; i < size; size++) {
1515 bool data;
1516 status = readBool(&data);
1517 (*val)[i] = data;
1518
1519 if (status != OK) {
1520 return status;
1521 }
1522 }
1523
1524 return OK;
1525}
1526
1527status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
1528 val->clear();
1529
1530 int32_t size;
1531 status_t status = readInt32(&size);
1532
1533 if (status != OK) {
1534 return status;
1535 }
1536
1537 if (size < 0) {
1538 return BAD_VALUE;
1539 }
1540
1541 val->resize(size);
1542
1543 for (auto& v : *val) {
1544 status = readChar(&v);
1545
1546 if (status != OK) {
1547 return status;
1548 }
1549 }
1550
1551 return OK;
1552}
1553
1554status_t Parcel::readString16Vector(std::vector<String16>* val) const {
1555 val->clear();
1556
1557 int32_t size;
1558 status_t status = readInt32(&size);
1559
1560 if (status != OK) {
1561 return status;
1562 }
1563
1564 if (size < 0) {
1565 return BAD_VALUE;
1566 }
1567
1568 val->reserve(size);
1569
1570 while (size-- > 0) {
1571 const char16_t *data;
1572 size_t size;
1573 data = readString16Inplace(&size);
1574
1575 if (data == nullptr) {
1576 return UNKNOWN_ERROR;
1577 }
1578
1579 val->emplace_back(data, size);
1580 }
1581
1582 return OK;
1583}
1584
1585
Andreas Huber84a6d042009-08-17 13:33:27 -07001586status_t Parcel::readInt32(int32_t *pArg) const
1587{
1588 return readAligned(pArg);
1589}
1590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001591int32_t Parcel::readInt32() const
1592{
Andreas Huber84a6d042009-08-17 13:33:27 -07001593 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001594}
1595
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001596status_t Parcel::readUint32(uint32_t *pArg) const
1597{
1598 return readAligned(pArg);
1599}
1600
1601uint32_t Parcel::readUint32() const
1602{
1603 return readAligned<uint32_t>();
1604}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605
1606status_t Parcel::readInt64(int64_t *pArg) const
1607{
Andreas Huber84a6d042009-08-17 13:33:27 -07001608 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609}
1610
1611
1612int64_t Parcel::readInt64() const
1613{
Andreas Huber84a6d042009-08-17 13:33:27 -07001614 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001615}
1616
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001617status_t Parcel::readUint64(uint64_t *pArg) const
1618{
1619 return readAligned(pArg);
1620}
1621
1622uint64_t Parcel::readUint64() const
1623{
1624 return readAligned<uint64_t>();
1625}
1626
Serban Constantinescuf683e012013-11-05 16:53:55 +00001627status_t Parcel::readPointer(uintptr_t *pArg) const
1628{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001629 status_t ret;
1630 binder_uintptr_t ptr;
1631 ret = readAligned(&ptr);
1632 if (!ret)
1633 *pArg = ptr;
1634 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001635}
1636
1637uintptr_t Parcel::readPointer() const
1638{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001639 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001640}
1641
1642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643status_t Parcel::readFloat(float *pArg) const
1644{
Andreas Huber84a6d042009-08-17 13:33:27 -07001645 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646}
1647
1648
1649float Parcel::readFloat() const
1650{
Andreas Huber84a6d042009-08-17 13:33:27 -07001651 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652}
1653
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001654#if defined(__mips__) && defined(__mips_hard_float)
1655
1656status_t Parcel::readDouble(double *pArg) const
1657{
1658 union {
1659 double d;
1660 unsigned long long ll;
1661 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001662 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001663 status_t status;
1664 status = readAligned(&u.ll);
1665 *pArg = u.d;
1666 return status;
1667}
1668
1669double Parcel::readDouble() const
1670{
1671 union {
1672 double d;
1673 unsigned long long ll;
1674 } u;
1675 u.ll = readAligned<unsigned long long>();
1676 return u.d;
1677}
1678
1679#else
1680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681status_t Parcel::readDouble(double *pArg) const
1682{
Andreas Huber84a6d042009-08-17 13:33:27 -07001683 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001684}
1685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686double Parcel::readDouble() const
1687{
Andreas Huber84a6d042009-08-17 13:33:27 -07001688 return readAligned<double>();
1689}
1690
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001691#endif
1692
Andreas Huber84a6d042009-08-17 13:33:27 -07001693status_t Parcel::readIntPtr(intptr_t *pArg) const
1694{
1695 return readAligned(pArg);
1696}
1697
1698
1699intptr_t Parcel::readIntPtr() const
1700{
1701 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702}
1703
Casey Dahlina3774912015-10-15 15:44:59 -07001704status_t Parcel::readBool(bool *pArg) const
1705{
1706 int32_t tmp;
1707 status_t ret = readInt32(&tmp);
1708 *pArg = (tmp != 0);
1709 return ret;
1710}
1711
1712bool Parcel::readBool() const
1713{
1714 return readInt32() != 0;
1715}
1716
1717status_t Parcel::readChar(char16_t *pArg) const
1718{
1719 int32_t tmp;
1720 status_t ret = readInt32(&tmp);
1721 *pArg = char16_t(tmp);
1722 return ret;
1723}
1724
1725char16_t Parcel::readChar() const
1726{
1727 return char16_t(readInt32());
1728}
1729
1730status_t Parcel::readByte(int8_t *pArg) const
1731{
1732 int32_t tmp;
1733 status_t ret = readInt32(&tmp);
1734 *pArg = int8_t(tmp);
1735 return ret;
1736}
1737
1738int8_t Parcel::readByte() const
1739{
1740 return int8_t(readInt32());
1741}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742
1743const char* Parcel::readCString() const
1744{
1745 const size_t avail = mDataSize-mDataPos;
1746 if (avail > 0) {
1747 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1748 // is the string's trailing NUL within the parcel's valid bounds?
1749 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1750 if (eos) {
1751 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001752 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001753 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754 return str;
1755 }
1756 }
1757 return NULL;
1758}
1759
1760String8 Parcel::readString8() const
1761{
1762 int32_t size = readInt32();
1763 // watch for potential int overflow adding 1 for trailing NUL
1764 if (size > 0 && size < INT32_MAX) {
1765 const char* str = (const char*)readInplace(size+1);
1766 if (str) return String8(str, size);
1767 }
1768 return String8();
1769}
1770
1771String16 Parcel::readString16() const
1772{
1773 size_t len;
1774 const char16_t* str = readString16Inplace(&len);
1775 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001776 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 return String16();
1778}
1779
Casey Dahlin08bb1452015-10-19 18:12:18 -07001780status_t Parcel::readString16(String16* pArg) const
1781{
1782 size_t len;
1783 const char16_t* str = readString16Inplace(&len);
1784 if (str) {
Casey Dahlina5bcc952015-10-20 16:26:23 -07001785 pArg->setTo(str, len);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001786 return 0;
1787 } else {
1788 *pArg = String16();
1789 return UNKNOWN_ERROR;
1790 }
1791}
1792
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001793const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1794{
1795 int32_t size = readInt32();
1796 // watch for potential int overflow from size+1
1797 if (size >= 0 && size < INT32_MAX) {
1798 *outLen = size;
1799 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1800 if (str != NULL) {
1801 return str;
1802 }
1803 }
1804 *outLen = 0;
1805 return NULL;
1806}
1807
1808sp<IBinder> Parcel::readStrongBinder() const
1809{
1810 sp<IBinder> val;
1811 unflatten_binder(ProcessState::self(), *this, &val);
1812 return val;
1813}
1814
1815wp<IBinder> Parcel::readWeakBinder() const
1816{
1817 wp<IBinder> val;
1818 unflatten_binder(ProcessState::self(), *this, &val);
1819 return val;
1820}
1821
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001822int32_t Parcel::readExceptionCode() const
1823{
1824 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001825 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001826 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001827 int32_t header_size = readAligned<int32_t>();
1828 // Skip over fat responses headers. Not used (or propagated) in
1829 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001830 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001831 // And fat response headers are currently only used when there are no
1832 // exceptions, so return no error:
1833 return 0;
1834 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001835 return exception_code;
1836}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001837
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001838native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001839{
1840 int numFds, numInts;
1841 status_t err;
1842 err = readInt32(&numFds);
1843 if (err != NO_ERROR) return 0;
1844 err = readInt32(&numInts);
1845 if (err != NO_ERROR) return 0;
1846
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001847 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001848 if (!h) {
1849 return 0;
1850 }
1851
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001852 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001853 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001854 if (h->data[i] < 0) err = BAD_VALUE;
1855 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001856 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001857 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001858 native_handle_close(h);
1859 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001860 h = 0;
1861 }
1862 return h;
1863}
1864
1865
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001866int Parcel::readFileDescriptor() const
1867{
1868 const flat_binder_object* flat = readObject(true);
1869 if (flat) {
1870 switch (flat->type) {
1871 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001872 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001874 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001875 }
1876 return BAD_TYPE;
1877}
1878
Jeff Brown5707dbf2011-09-23 21:17:56 -07001879status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1880{
Jeff Brown13b16042014-11-11 16:44:25 -08001881 int32_t blobType;
1882 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001883 if (status) return status;
1884
Jeff Brown13b16042014-11-11 16:44:25 -08001885 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001886 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001887 const void* ptr = readInplace(len);
1888 if (!ptr) return BAD_VALUE;
1889
Jeff Brown13b16042014-11-11 16:44:25 -08001890 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001891 return NO_ERROR;
1892 }
1893
Steve Block6807e592011-10-20 11:56:00 +01001894 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001895 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001896 int fd = readFileDescriptor();
1897 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1898
Jeff Brown13b16042014-11-11 16:44:25 -08001899 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1900 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001901 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001902
Jeff Brown13b16042014-11-11 16:44:25 -08001903 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001904 return NO_ERROR;
1905}
1906
Mathias Agopiane1424282013-07-29 21:24:40 -07001907status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001908{
1909 // size
1910 const size_t len = this->readInt32();
1911 const size_t fd_count = this->readInt32();
1912
Nick Kralevichb6b14232015-04-02 09:36:02 -07001913 if (len > INT32_MAX) {
1914 // don't accept size_t values which may have come from an
1915 // inadvertent conversion from a negative int.
1916 return BAD_VALUE;
1917 }
1918
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001919 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001920 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001921 if (buf == NULL)
1922 return BAD_VALUE;
1923
1924 int* fds = NULL;
1925 if (fd_count) {
1926 fds = new int[fd_count];
1927 }
1928
1929 status_t err = NO_ERROR;
1930 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001931 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001932 if (fds[i] < 0) {
1933 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001934 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1935 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001936 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001937 }
1938
1939 if (err == NO_ERROR) {
1940 err = val.unflatten(buf, len, fds, fd_count);
1941 }
1942
1943 if (fd_count) {
1944 delete [] fds;
1945 }
1946
1947 return err;
1948}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1950{
1951 const size_t DPOS = mDataPos;
1952 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1953 const flat_binder_object* obj
1954 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1955 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001956 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001957 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958 // the object list, so we don't want to check for it when
1959 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001960 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001961 return obj;
1962 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001964 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001965 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001966 const size_t N = mObjectsSize;
1967 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001968
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001969 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001970 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001972
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973 // Start at the current hint position, looking for an object at
1974 // the current data position.
1975 if (opos < N) {
1976 while (opos < (N-1) && OBJS[opos] < DPOS) {
1977 opos++;
1978 }
1979 } else {
1980 opos = N-1;
1981 }
1982 if (OBJS[opos] == DPOS) {
1983 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001984 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985 this, DPOS, opos);
1986 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001987 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988 return obj;
1989 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991 // Look backwards for it...
1992 while (opos > 0 && OBJS[opos] > DPOS) {
1993 opos--;
1994 }
1995 if (OBJS[opos] == DPOS) {
1996 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001997 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 this, DPOS, opos);
1999 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002000 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001 return obj;
2002 }
2003 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002004 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 -07002005 this, DPOS);
2006 }
2007 return NULL;
2008}
2009
2010void Parcel::closeFileDescriptors()
2011{
2012 size_t i = mObjectsSize;
2013 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002014 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 }
2016 while (i > 0) {
2017 i--;
2018 const flat_binder_object* flat
2019 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2020 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002021 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022 close(flat->handle);
2023 }
2024 }
2025}
2026
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002027uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002028{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002029 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002030}
2031
2032size_t Parcel::ipcDataSize() const
2033{
2034 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2035}
2036
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002037uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002038{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002039 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040}
2041
2042size_t Parcel::ipcObjectsCount() const
2043{
2044 return mObjectsSize;
2045}
2046
2047void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002048 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002050 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 freeDataNoInit();
2052 mError = NO_ERROR;
2053 mData = const_cast<uint8_t*>(data);
2054 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002055 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002057 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002058 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059 mObjectsSize = mObjectsCapacity = objectsCount;
2060 mNextObjectHint = 0;
2061 mOwner = relFunc;
2062 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002063 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002064 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002065 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002066 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002067 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002068 mObjectsSize = 0;
2069 break;
2070 }
2071 minOffset = offset + sizeof(flat_binder_object);
2072 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002073 scanForFds();
2074}
2075
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002076void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077{
2078 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 if (errorCheck() != NO_ERROR) {
2081 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002082 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083 } else if (dataSize() > 0) {
2084 const uint8_t* DATA = data();
2085 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002086 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087 const size_t N = objectsCount();
2088 for (size_t i=0; i<N; i++) {
2089 const flat_binder_object* flat
2090 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2091 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2092 << TypeCode(flat->type & 0x7f7f7f00)
2093 << " = " << flat->binder;
2094 }
2095 } else {
2096 to << "NULL";
2097 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002098
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 to << ")";
2100}
2101
2102void Parcel::releaseObjects()
2103{
2104 const sp<ProcessState> proc(ProcessState::self());
2105 size_t i = mObjectsSize;
2106 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002107 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 while (i > 0) {
2109 i--;
2110 const flat_binder_object* flat
2111 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002112 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 }
2114}
2115
2116void Parcel::acquireObjects()
2117{
2118 const sp<ProcessState> proc(ProcessState::self());
2119 size_t i = mObjectsSize;
2120 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002121 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 while (i > 0) {
2123 i--;
2124 const flat_binder_object* flat
2125 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002126 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 }
2128}
2129
2130void Parcel::freeData()
2131{
2132 freeDataNoInit();
2133 initState();
2134}
2135
2136void Parcel::freeDataNoInit()
2137{
2138 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002139 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002140 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2142 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002143 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002145 if (mData) {
2146 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002147 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austinf28b2952015-09-10 13:46:02 -07002148 if (mDataCapacity <= gParcelGlobalAllocSize) {
2149 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2150 } else {
2151 gParcelGlobalAllocSize = 0;
2152 }
2153 if (gParcelGlobalAllocCount > 0) {
2154 gParcelGlobalAllocCount--;
2155 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002156 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002157 free(mData);
2158 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 if (mObjects) free(mObjects);
2160 }
2161}
2162
2163status_t Parcel::growData(size_t len)
2164{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002165 if (len > INT32_MAX) {
2166 // don't accept size_t values which may have come from an
2167 // inadvertent conversion from a negative int.
2168 return BAD_VALUE;
2169 }
2170
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 size_t newSize = ((mDataSize+len)*3)/2;
2172 return (newSize <= mDataSize)
2173 ? (status_t) NO_MEMORY
2174 : continueWrite(newSize);
2175}
2176
2177status_t Parcel::restartWrite(size_t desired)
2178{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002179 if (desired > INT32_MAX) {
2180 // don't accept size_t values which may have come from an
2181 // inadvertent conversion from a negative int.
2182 return BAD_VALUE;
2183 }
2184
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 if (mOwner) {
2186 freeData();
2187 return continueWrite(desired);
2188 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002189
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190 uint8_t* data = (uint8_t*)realloc(mData, desired);
2191 if (!data && desired > mDataCapacity) {
2192 mError = NO_MEMORY;
2193 return NO_MEMORY;
2194 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002195
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002199 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002200 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002201 gParcelGlobalAllocSize += desired;
2202 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002203 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 mData = data;
2205 mDataCapacity = desired;
2206 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002209 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2210 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002212 free(mObjects);
2213 mObjects = NULL;
2214 mObjectsSize = mObjectsCapacity = 0;
2215 mNextObjectHint = 0;
2216 mHasFds = false;
2217 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002218 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002219
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 return NO_ERROR;
2221}
2222
2223status_t Parcel::continueWrite(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 shrinking, first adjust for any objects that appear
2232 // after the new data size.
2233 size_t objectsSize = mObjectsSize;
2234 if (desired < mDataSize) {
2235 if (desired == 0) {
2236 objectsSize = 0;
2237 } else {
2238 while (objectsSize > 0) {
2239 if (mObjects[objectsSize-1] < desired)
2240 break;
2241 objectsSize--;
2242 }
2243 }
2244 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 if (mOwner) {
2247 // If the size is going to zero, just release the owner's data.
2248 if (desired == 0) {
2249 freeData();
2250 return NO_ERROR;
2251 }
2252
2253 // If there is a different owner, we need to take
2254 // posession.
2255 uint8_t* data = (uint8_t*)malloc(desired);
2256 if (!data) {
2257 mError = NO_MEMORY;
2258 return NO_MEMORY;
2259 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002260 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002263 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002265 free(data);
2266
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 mError = NO_MEMORY;
2268 return NO_MEMORY;
2269 }
2270
2271 // Little hack to only acquire references on objects
2272 // we will be keeping.
2273 size_t oldObjectsSize = mObjectsSize;
2274 mObjectsSize = objectsSize;
2275 acquireObjects();
2276 mObjectsSize = oldObjectsSize;
2277 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 if (mData) {
2280 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2281 }
2282 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002283 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002285 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2287 mOwner = NULL;
2288
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002289 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002290 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002291 gParcelGlobalAllocSize += desired;
2292 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002293 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 mData = data;
2296 mObjects = objects;
2297 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002298 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 mDataCapacity = desired;
2300 mObjectsSize = mObjectsCapacity = objectsSize;
2301 mNextObjectHint = 0;
2302
2303 } else if (mData) {
2304 if (objectsSize < mObjectsSize) {
2305 // Need to release refs on any objects we are dropping.
2306 const sp<ProcessState> proc(ProcessState::self());
2307 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2308 const flat_binder_object* flat
2309 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2310 if (flat->type == BINDER_TYPE_FD) {
2311 // will need to rescan because we may have lopped off the only FDs
2312 mFdsKnown = false;
2313 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002314 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002316 binder_size_t* objects =
2317 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 if (objects) {
2319 mObjects = objects;
2320 }
2321 mObjectsSize = objectsSize;
2322 mNextObjectHint = 0;
2323 }
2324
2325 // We own the data, so we can just do a realloc().
2326 if (desired > mDataCapacity) {
2327 uint8_t* data = (uint8_t*)realloc(mData, desired);
2328 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002329 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2330 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002331 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002332 gParcelGlobalAllocSize += desired;
2333 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austinf28b2952015-09-10 13:46:02 -07002334 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002335 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 mData = data;
2337 mDataCapacity = desired;
2338 } else if (desired > mDataCapacity) {
2339 mError = NO_MEMORY;
2340 return NO_MEMORY;
2341 }
2342 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002343 if (mDataSize > desired) {
2344 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002345 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002346 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002347 if (mDataPos > desired) {
2348 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002349 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 }
2351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 } else {
2354 // This is the first data. Easy!
2355 uint8_t* data = (uint8_t*)malloc(desired);
2356 if (!data) {
2357 mError = NO_MEMORY;
2358 return NO_MEMORY;
2359 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002361 if(!(mDataCapacity == 0 && mObjects == NULL
2362 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002363 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002366 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002368 gParcelGlobalAllocSize += desired;
2369 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002370 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002372 mData = data;
2373 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002374 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2375 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 mDataCapacity = desired;
2377 }
2378
2379 return NO_ERROR;
2380}
2381
2382void Parcel::initState()
2383{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002384 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 mError = NO_ERROR;
2386 mData = 0;
2387 mDataSize = 0;
2388 mDataCapacity = 0;
2389 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2391 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 mObjects = NULL;
2393 mObjectsSize = 0;
2394 mObjectsCapacity = 0;
2395 mNextObjectHint = 0;
2396 mHasFds = false;
2397 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002398 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002400 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401}
2402
2403void Parcel::scanForFds() const
2404{
2405 bool hasFds = false;
2406 for (size_t i=0; i<mObjectsSize; i++) {
2407 const flat_binder_object* flat
2408 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2409 if (flat->type == BINDER_TYPE_FD) {
2410 hasFds = true;
2411 break;
2412 }
2413 }
2414 mHasFds = hasFds;
2415 mFdsKnown = true;
2416}
2417
Dan Sandleraa5c2342015-04-10 10:08:45 -04002418size_t Parcel::getBlobAshmemSize() const
2419{
Adrian Roos6bb31142015-10-22 16:46:12 -07002420 // This used to return the size of all blobs that were written to ashmem, now we're returning
2421 // the ashmem currently referenced by this Parcel, which should be equivalent.
2422 // TODO: Remove method once ABI can be changed.
2423 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002424}
2425
Adrian Rooscbf37262015-10-22 16:12:53 -07002426size_t Parcel::getOpenAshmemSize() const
2427{
2428 return mOpenAshmemSize;
2429}
2430
Jeff Brown5707dbf2011-09-23 21:17:56 -07002431// --- Parcel::Blob ---
2432
2433Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002434 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002435}
2436
2437Parcel::Blob::~Blob() {
2438 release();
2439}
2440
2441void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002442 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002443 ::munmap(mData, mSize);
2444 }
2445 clear();
2446}
2447
Jeff Brown13b16042014-11-11 16:44:25 -08002448void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2449 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002450 mData = data;
2451 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002452 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002453}
2454
2455void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002456 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002457 mData = NULL;
2458 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002459 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002460}
2461
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462}; // namespace android