blob: 7e2f0d034e6c14a383f6e5d1276561bdb430aa76 [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
746status_t Parcel::writeInt32(int32_t val)
747{
Andreas Huber84a6d042009-08-17 13:33:27 -0700748 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700749}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800750
751status_t Parcel::writeUint32(uint32_t val)
752{
753 return writeAligned(val);
754}
755
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700756status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700757 if (len > INT32_MAX) {
758 // don't accept size_t values which may have come from an
759 // inadvertent conversion from a negative int.
760 return BAD_VALUE;
761 }
762
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700763 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700764 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700765 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700766 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700767 if (ret == NO_ERROR) {
768 ret = write(val, len * sizeof(*val));
769 }
770 return ret;
771}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700772status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700773 if (len > INT32_MAX) {
774 // don't accept size_t values which may have come from an
775 // inadvertent conversion from a negative int.
776 return BAD_VALUE;
777 }
778
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700779 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700780 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700781 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700782 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700783 if (ret == NO_ERROR) {
784 ret = write(val, len * sizeof(*val));
785 }
786 return ret;
787}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700788
789status_t Parcel::writeInt64(int64_t val)
790{
Andreas Huber84a6d042009-08-17 13:33:27 -0700791 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700792}
793
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700794status_t Parcel::writeUint64(uint64_t val)
795{
796 return writeAligned(val);
797}
798
Serban Constantinescuf683e012013-11-05 16:53:55 +0000799status_t Parcel::writePointer(uintptr_t val)
800{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800801 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000802}
803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700804status_t Parcel::writeFloat(float val)
805{
Andreas Huber84a6d042009-08-17 13:33:27 -0700806 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700807}
808
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800809#if defined(__mips__) && defined(__mips_hard_float)
810
811status_t Parcel::writeDouble(double val)
812{
813 union {
814 double d;
815 unsigned long long ll;
816 } u;
817 u.d = val;
818 return writeAligned(u.ll);
819}
820
821#else
822
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700823status_t Parcel::writeDouble(double val)
824{
Andreas Huber84a6d042009-08-17 13:33:27 -0700825 return writeAligned(val);
826}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700827
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800828#endif
829
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700830status_t Parcel::writeCString(const char* str)
831{
832 return write(str, strlen(str)+1);
833}
834
835status_t Parcel::writeString8(const String8& str)
836{
837 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100838 // only write string if its length is more than zero characters,
839 // as readString8 will only read if the length field is non-zero.
840 // this is slightly different from how writeString16 works.
841 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700842 err = write(str.string(), str.bytes()+1);
843 }
844 return err;
845}
846
847status_t Parcel::writeString16(const String16& str)
848{
849 return writeString16(str.string(), str.size());
850}
851
852status_t Parcel::writeString16(const char16_t* str, size_t len)
853{
854 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700855
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700856 status_t err = writeInt32(len);
857 if (err == NO_ERROR) {
858 len *= sizeof(char16_t);
859 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
860 if (data) {
861 memcpy(data, str, len);
862 *reinterpret_cast<char16_t*>(data+len) = 0;
863 return NO_ERROR;
864 }
865 err = mError;
866 }
867 return err;
868}
869
870status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
871{
872 return flatten_binder(ProcessState::self(), val, this);
873}
874
875status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
876{
877 return flatten_binder(ProcessState::self(), val, this);
878}
879
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700880status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800881{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700882 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800883 return BAD_TYPE;
884
885 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700886 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800887 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700889 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800890 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700892 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
893 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894
895 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000896 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 return err;
898 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700899 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800900 return err;
901}
902
Jeff Brown93ff1f92011-11-04 19:01:44 -0700903status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700904{
905 flat_binder_object obj;
906 obj.type = BINDER_TYPE_FD;
907 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800908 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800910 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700911 return writeObject(obj, true);
912}
913
914status_t Parcel::writeDupFileDescriptor(int fd)
915{
Jeff Brownd341c712011-11-04 20:19:33 -0700916 int dupFd = dup(fd);
917 if (dupFd < 0) {
918 return -errno;
919 }
920 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
921 if (err) {
922 close(dupFd);
923 }
924 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700925}
926
Jeff Brown13b16042014-11-11 16:44:25 -0800927status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700928{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700929 if (len > INT32_MAX) {
930 // don't accept size_t values which may have come from an
931 // inadvertent conversion from a negative int.
932 return BAD_VALUE;
933 }
934
Jeff Brown13b16042014-11-11 16:44:25 -0800935 status_t status;
936 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100937 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800938 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700939 if (status) return status;
940
941 void* ptr = writeInplace(len);
942 if (!ptr) return NO_MEMORY;
943
Jeff Brown13b16042014-11-11 16:44:25 -0800944 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700945 return NO_ERROR;
946 }
947
Steve Block6807e592011-10-20 11:56:00 +0100948 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700949 int fd = ashmem_create_region("Parcel Blob", len);
950 if (fd < 0) return NO_MEMORY;
951
952 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
953 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700954 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700955 } else {
956 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
957 if (ptr == MAP_FAILED) {
958 status = -errno;
959 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800960 if (!mutableCopy) {
961 result = ashmem_set_prot_region(fd, PROT_READ);
962 }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700963 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700964 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700965 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800966 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700967 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700968 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700969 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -0800970 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700971 return NO_ERROR;
972 }
973 }
974 }
975 }
976 ::munmap(ptr, len);
977 }
978 ::close(fd);
979 return status;
980}
981
Jeff Brown13b16042014-11-11 16:44:25 -0800982status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
983{
984 // Must match up with what's done in writeBlob.
985 if (!mAllowFds) return FDS_NOT_ALLOWED;
986 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
987 if (status) return status;
988 return writeDupFileDescriptor(fd);
989}
990
Mathias Agopiane1424282013-07-29 21:24:40 -0700991status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800992{
993 status_t err;
994
995 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700996 const size_t len = val.getFlattenedSize();
997 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800998
Nick Kralevichb6b14232015-04-02 09:36:02 -0700999 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1000 // don't accept size_t values which may have come from an
1001 // inadvertent conversion from a negative int.
1002 return BAD_VALUE;
1003 }
1004
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001005 err = this->writeInt32(len);
1006 if (err) return err;
1007
1008 err = this->writeInt32(fd_count);
1009 if (err) return err;
1010
1011 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001012 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001013 if (buf == NULL)
1014 return BAD_VALUE;
1015
1016 int* fds = NULL;
1017 if (fd_count) {
1018 fds = new int[fd_count];
1019 }
1020
1021 err = val.flatten(buf, len, fds, fd_count);
1022 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1023 err = this->writeDupFileDescriptor( fds[i] );
1024 }
1025
1026 if (fd_count) {
1027 delete [] fds;
1028 }
1029
1030 return err;
1031}
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1034{
1035 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1036 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1037 if (enoughData && enoughObjects) {
1038restart_write:
1039 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001040
Christopher Tate98e67d32015-06-03 18:44:15 -07001041 // remember if it's a file descriptor
1042 if (val.type == BINDER_TYPE_FD) {
1043 if (!mAllowFds) {
1044 // fail before modifying our object index
1045 return FDS_NOT_ALLOWED;
1046 }
1047 mHasFds = mFdsKnown = true;
1048 }
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001051 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001053 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054 mObjectsSize++;
1055 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057 return finishWrite(sizeof(flat_binder_object));
1058 }
1059
1060 if (!enoughData) {
1061 const status_t err = growData(sizeof(val));
1062 if (err != NO_ERROR) return err;
1063 }
1064 if (!enoughObjects) {
1065 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001066 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001067 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068 if (objects == NULL) return NO_MEMORY;
1069 mObjects = objects;
1070 mObjectsCapacity = newSize;
1071 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073 goto restart_write;
1074}
1075
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001076status_t Parcel::writeNoException()
1077{
1078 return writeInt32(0);
1079}
1080
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001081void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001082{
1083 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1084}
1085
1086status_t Parcel::read(void* outData, size_t len) const
1087{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001088 if (len > INT32_MAX) {
1089 // don't accept size_t values which may have come from an
1090 // inadvertent conversion from a negative int.
1091 return BAD_VALUE;
1092 }
1093
1094 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1095 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001096 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001097 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001098 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 return NO_ERROR;
1100 }
1101 return NOT_ENOUGH_DATA;
1102}
1103
1104const void* Parcel::readInplace(size_t len) const
1105{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001106 if (len > INT32_MAX) {
1107 // don't accept size_t values which may have come from an
1108 // inadvertent conversion from a negative int.
1109 return NULL;
1110 }
1111
1112 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1113 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001115 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001116 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001117 return data;
1118 }
1119 return NULL;
1120}
1121
Andreas Huber84a6d042009-08-17 13:33:27 -07001122template<class T>
1123status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001124 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001125
1126 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001127 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001128 mDataPos += sizeof(T);
1129 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001130 return NO_ERROR;
1131 } else {
1132 return NOT_ENOUGH_DATA;
1133 }
1134}
1135
Andreas Huber84a6d042009-08-17 13:33:27 -07001136template<class T>
1137T Parcel::readAligned() const {
1138 T result;
1139 if (readAligned(&result) != NO_ERROR) {
1140 result = 0;
1141 }
1142
1143 return result;
1144}
1145
1146template<class T>
1147status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001148 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001149
1150 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1151restart_write:
1152 *reinterpret_cast<T*>(mData+mDataPos) = val;
1153 return finishWrite(sizeof(val));
1154 }
1155
1156 status_t err = growData(sizeof(val));
1157 if (err == NO_ERROR) goto restart_write;
1158 return err;
1159}
1160
1161status_t Parcel::readInt32(int32_t *pArg) const
1162{
1163 return readAligned(pArg);
1164}
1165
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166int32_t Parcel::readInt32() const
1167{
Andreas Huber84a6d042009-08-17 13:33:27 -07001168 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169}
1170
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001171status_t Parcel::readUint32(uint32_t *pArg) const
1172{
1173 return readAligned(pArg);
1174}
1175
1176uint32_t Parcel::readUint32() const
1177{
1178 return readAligned<uint32_t>();
1179}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001180
1181status_t Parcel::readInt64(int64_t *pArg) const
1182{
Andreas Huber84a6d042009-08-17 13:33:27 -07001183 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001184}
1185
1186
1187int64_t Parcel::readInt64() const
1188{
Andreas Huber84a6d042009-08-17 13:33:27 -07001189 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001190}
1191
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001192status_t Parcel::readUint64(uint64_t *pArg) const
1193{
1194 return readAligned(pArg);
1195}
1196
1197uint64_t Parcel::readUint64() const
1198{
1199 return readAligned<uint64_t>();
1200}
1201
Serban Constantinescuf683e012013-11-05 16:53:55 +00001202status_t Parcel::readPointer(uintptr_t *pArg) const
1203{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001204 status_t ret;
1205 binder_uintptr_t ptr;
1206 ret = readAligned(&ptr);
1207 if (!ret)
1208 *pArg = ptr;
1209 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001210}
1211
1212uintptr_t Parcel::readPointer() const
1213{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001214 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001215}
1216
1217
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001218status_t Parcel::readFloat(float *pArg) const
1219{
Andreas Huber84a6d042009-08-17 13:33:27 -07001220 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001221}
1222
1223
1224float Parcel::readFloat() const
1225{
Andreas Huber84a6d042009-08-17 13:33:27 -07001226 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227}
1228
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001229#if defined(__mips__) && defined(__mips_hard_float)
1230
1231status_t Parcel::readDouble(double *pArg) const
1232{
1233 union {
1234 double d;
1235 unsigned long long ll;
1236 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001237 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001238 status_t status;
1239 status = readAligned(&u.ll);
1240 *pArg = u.d;
1241 return status;
1242}
1243
1244double Parcel::readDouble() const
1245{
1246 union {
1247 double d;
1248 unsigned long long ll;
1249 } u;
1250 u.ll = readAligned<unsigned long long>();
1251 return u.d;
1252}
1253
1254#else
1255
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001256status_t Parcel::readDouble(double *pArg) const
1257{
Andreas Huber84a6d042009-08-17 13:33:27 -07001258 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001259}
1260
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001261double Parcel::readDouble() const
1262{
Andreas Huber84a6d042009-08-17 13:33:27 -07001263 return readAligned<double>();
1264}
1265
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001266#endif
1267
Andreas Huber84a6d042009-08-17 13:33:27 -07001268status_t Parcel::readIntPtr(intptr_t *pArg) const
1269{
1270 return readAligned(pArg);
1271}
1272
1273
1274intptr_t Parcel::readIntPtr() const
1275{
1276 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001277}
1278
1279
1280const char* Parcel::readCString() const
1281{
1282 const size_t avail = mDataSize-mDataPos;
1283 if (avail > 0) {
1284 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1285 // is the string's trailing NUL within the parcel's valid bounds?
1286 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1287 if (eos) {
1288 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001289 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001290 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001291 return str;
1292 }
1293 }
1294 return NULL;
1295}
1296
1297String8 Parcel::readString8() const
1298{
1299 int32_t size = readInt32();
1300 // watch for potential int overflow adding 1 for trailing NUL
1301 if (size > 0 && size < INT32_MAX) {
1302 const char* str = (const char*)readInplace(size+1);
1303 if (str) return String8(str, size);
1304 }
1305 return String8();
1306}
1307
1308String16 Parcel::readString16() const
1309{
1310 size_t len;
1311 const char16_t* str = readString16Inplace(&len);
1312 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001313 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314 return String16();
1315}
1316
1317const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1318{
1319 int32_t size = readInt32();
1320 // watch for potential int overflow from size+1
1321 if (size >= 0 && size < INT32_MAX) {
1322 *outLen = size;
1323 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1324 if (str != NULL) {
1325 return str;
1326 }
1327 }
1328 *outLen = 0;
1329 return NULL;
1330}
1331
1332sp<IBinder> Parcel::readStrongBinder() const
1333{
1334 sp<IBinder> val;
1335 unflatten_binder(ProcessState::self(), *this, &val);
1336 return val;
1337}
1338
1339wp<IBinder> Parcel::readWeakBinder() const
1340{
1341 wp<IBinder> val;
1342 unflatten_binder(ProcessState::self(), *this, &val);
1343 return val;
1344}
1345
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001346int32_t Parcel::readExceptionCode() const
1347{
1348 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001349 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001350 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001351 int32_t header_size = readAligned<int32_t>();
1352 // Skip over fat responses headers. Not used (or propagated) in
1353 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001354 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001355 // And fat response headers are currently only used when there are no
1356 // exceptions, so return no error:
1357 return 0;
1358 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001359 return exception_code;
1360}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001361
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001362native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001363{
1364 int numFds, numInts;
1365 status_t err;
1366 err = readInt32(&numFds);
1367 if (err != NO_ERROR) return 0;
1368 err = readInt32(&numInts);
1369 if (err != NO_ERROR) return 0;
1370
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001371 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001372 if (!h) {
1373 return 0;
1374 }
1375
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001376 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001377 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001378 if (h->data[i] < 0) err = BAD_VALUE;
1379 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001380 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001381 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001382 native_handle_close(h);
1383 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001384 h = 0;
1385 }
1386 return h;
1387}
1388
1389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001390int Parcel::readFileDescriptor() const
1391{
1392 const flat_binder_object* flat = readObject(true);
1393 if (flat) {
1394 switch (flat->type) {
1395 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001396 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001398 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001399 }
1400 return BAD_TYPE;
1401}
1402
Jeff Brown5707dbf2011-09-23 21:17:56 -07001403status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1404{
Jeff Brown13b16042014-11-11 16:44:25 -08001405 int32_t blobType;
1406 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001407 if (status) return status;
1408
Jeff Brown13b16042014-11-11 16:44:25 -08001409 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001410 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001411 const void* ptr = readInplace(len);
1412 if (!ptr) return BAD_VALUE;
1413
Jeff Brown13b16042014-11-11 16:44:25 -08001414 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001415 return NO_ERROR;
1416 }
1417
Steve Block6807e592011-10-20 11:56:00 +01001418 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001419 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001420 int fd = readFileDescriptor();
1421 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1422
Jeff Brown13b16042014-11-11 16:44:25 -08001423 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1424 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001425 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001426
Jeff Brown13b16042014-11-11 16:44:25 -08001427 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001428 return NO_ERROR;
1429}
1430
Mathias Agopiane1424282013-07-29 21:24:40 -07001431status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001432{
1433 // size
1434 const size_t len = this->readInt32();
1435 const size_t fd_count = this->readInt32();
1436
Nick Kralevichb6b14232015-04-02 09:36:02 -07001437 if (len > INT32_MAX) {
1438 // don't accept size_t values which may have come from an
1439 // inadvertent conversion from a negative int.
1440 return BAD_VALUE;
1441 }
1442
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001443 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001444 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001445 if (buf == NULL)
1446 return BAD_VALUE;
1447
1448 int* fds = NULL;
1449 if (fd_count) {
1450 fds = new int[fd_count];
1451 }
1452
1453 status_t err = NO_ERROR;
1454 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001455 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001456 if (fds[i] < 0) {
1457 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001458 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1459 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001460 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001461 }
1462
1463 if (err == NO_ERROR) {
1464 err = val.unflatten(buf, len, fds, fd_count);
1465 }
1466
1467 if (fd_count) {
1468 delete [] fds;
1469 }
1470
1471 return err;
1472}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001473const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1474{
1475 const size_t DPOS = mDataPos;
1476 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1477 const flat_binder_object* obj
1478 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1479 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001480 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001481 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 // the object list, so we don't want to check for it when
1483 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001484 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 return obj;
1486 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001489 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001490 const size_t N = mObjectsSize;
1491 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001493 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001494 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497 // Start at the current hint position, looking for an object at
1498 // the current data position.
1499 if (opos < N) {
1500 while (opos < (N-1) && OBJS[opos] < DPOS) {
1501 opos++;
1502 }
1503 } else {
1504 opos = N-1;
1505 }
1506 if (OBJS[opos] == DPOS) {
1507 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001508 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001509 this, DPOS, opos);
1510 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001511 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512 return obj;
1513 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001515 // Look backwards for it...
1516 while (opos > 0 && OBJS[opos] > DPOS) {
1517 opos--;
1518 }
1519 if (OBJS[opos] == DPOS) {
1520 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001521 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 this, DPOS, opos);
1523 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001524 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525 return obj;
1526 }
1527 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001528 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 -07001529 this, DPOS);
1530 }
1531 return NULL;
1532}
1533
1534void Parcel::closeFileDescriptors()
1535{
1536 size_t i = mObjectsSize;
1537 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001538 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001539 }
1540 while (i > 0) {
1541 i--;
1542 const flat_binder_object* flat
1543 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1544 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001545 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001546 close(flat->handle);
1547 }
1548 }
1549}
1550
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001551uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001553 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554}
1555
1556size_t Parcel::ipcDataSize() const
1557{
1558 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1559}
1560
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001561uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001563 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564}
1565
1566size_t Parcel::ipcObjectsCount() const
1567{
1568 return mObjectsSize;
1569}
1570
1571void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001572 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001574 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575 freeDataNoInit();
1576 mError = NO_ERROR;
1577 mData = const_cast<uint8_t*>(data);
1578 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001579 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001580 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001581 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001582 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583 mObjectsSize = mObjectsCapacity = objectsCount;
1584 mNextObjectHint = 0;
1585 mOwner = relFunc;
1586 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001587 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001588 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001589 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001590 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001591 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001592 mObjectsSize = 0;
1593 break;
1594 }
1595 minOffset = offset + sizeof(flat_binder_object);
1596 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597 scanForFds();
1598}
1599
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001600void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601{
1602 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001603
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001604 if (errorCheck() != NO_ERROR) {
1605 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001606 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001607 } else if (dataSize() > 0) {
1608 const uint8_t* DATA = data();
1609 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001610 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611 const size_t N = objectsCount();
1612 for (size_t i=0; i<N; i++) {
1613 const flat_binder_object* flat
1614 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1615 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1616 << TypeCode(flat->type & 0x7f7f7f00)
1617 << " = " << flat->binder;
1618 }
1619 } else {
1620 to << "NULL";
1621 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001622
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623 to << ")";
1624}
1625
1626void Parcel::releaseObjects()
1627{
1628 const sp<ProcessState> proc(ProcessState::self());
1629 size_t i = mObjectsSize;
1630 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001631 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001632 while (i > 0) {
1633 i--;
1634 const flat_binder_object* flat
1635 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001636 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637 }
1638}
1639
1640void Parcel::acquireObjects()
1641{
1642 const sp<ProcessState> proc(ProcessState::self());
1643 size_t i = mObjectsSize;
1644 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001645 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001646 while (i > 0) {
1647 i--;
1648 const flat_binder_object* flat
1649 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001650 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651 }
1652}
1653
1654void Parcel::freeData()
1655{
1656 freeDataNoInit();
1657 initState();
1658}
1659
1660void Parcel::freeDataNoInit()
1661{
1662 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001663 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001664 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1666 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001667 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001669 if (mData) {
1670 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001671 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austinf28b2952015-09-10 13:46:02 -07001672 if (mDataCapacity <= gParcelGlobalAllocSize) {
1673 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1674 } else {
1675 gParcelGlobalAllocSize = 0;
1676 }
1677 if (gParcelGlobalAllocCount > 0) {
1678 gParcelGlobalAllocCount--;
1679 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001680 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001681 free(mData);
1682 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001683 if (mObjects) free(mObjects);
1684 }
1685}
1686
1687status_t Parcel::growData(size_t len)
1688{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001689 if (len > INT32_MAX) {
1690 // don't accept size_t values which may have come from an
1691 // inadvertent conversion from a negative int.
1692 return BAD_VALUE;
1693 }
1694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001695 size_t newSize = ((mDataSize+len)*3)/2;
1696 return (newSize <= mDataSize)
1697 ? (status_t) NO_MEMORY
1698 : continueWrite(newSize);
1699}
1700
1701status_t Parcel::restartWrite(size_t desired)
1702{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001703 if (desired > INT32_MAX) {
1704 // don't accept size_t values which may have come from an
1705 // inadvertent conversion from a negative int.
1706 return BAD_VALUE;
1707 }
1708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709 if (mOwner) {
1710 freeData();
1711 return continueWrite(desired);
1712 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 uint8_t* data = (uint8_t*)realloc(mData, desired);
1715 if (!data && desired > mDataCapacity) {
1716 mError = NO_MEMORY;
1717 return NO_MEMORY;
1718 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001720 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001721
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001722 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001723 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001724 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001725 gParcelGlobalAllocSize += desired;
1726 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001727 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001728 mData = data;
1729 mDataCapacity = desired;
1730 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001733 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1734 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736 free(mObjects);
1737 mObjects = NULL;
1738 mObjectsSize = mObjectsCapacity = 0;
1739 mNextObjectHint = 0;
1740 mHasFds = false;
1741 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001742 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001743
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744 return NO_ERROR;
1745}
1746
1747status_t Parcel::continueWrite(size_t desired)
1748{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001749 if (desired > INT32_MAX) {
1750 // don't accept size_t values which may have come from an
1751 // inadvertent conversion from a negative int.
1752 return BAD_VALUE;
1753 }
1754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755 // If shrinking, first adjust for any objects that appear
1756 // after the new data size.
1757 size_t objectsSize = mObjectsSize;
1758 if (desired < mDataSize) {
1759 if (desired == 0) {
1760 objectsSize = 0;
1761 } else {
1762 while (objectsSize > 0) {
1763 if (mObjects[objectsSize-1] < desired)
1764 break;
1765 objectsSize--;
1766 }
1767 }
1768 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001769
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770 if (mOwner) {
1771 // If the size is going to zero, just release the owner's data.
1772 if (desired == 0) {
1773 freeData();
1774 return NO_ERROR;
1775 }
1776
1777 // If there is a different owner, we need to take
1778 // posession.
1779 uint8_t* data = (uint8_t*)malloc(desired);
1780 if (!data) {
1781 mError = NO_MEMORY;
1782 return NO_MEMORY;
1783 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001784 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001785
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001787 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001789 free(data);
1790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 mError = NO_MEMORY;
1792 return NO_MEMORY;
1793 }
1794
1795 // Little hack to only acquire references on objects
1796 // we will be keeping.
1797 size_t oldObjectsSize = mObjectsSize;
1798 mObjectsSize = objectsSize;
1799 acquireObjects();
1800 mObjectsSize = oldObjectsSize;
1801 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 if (mData) {
1804 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1805 }
1806 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001807 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001809 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1811 mOwner = NULL;
1812
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001813 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001814 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001815 gParcelGlobalAllocSize += desired;
1816 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001817 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001818
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001819 mData = data;
1820 mObjects = objects;
1821 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001822 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 mDataCapacity = desired;
1824 mObjectsSize = mObjectsCapacity = objectsSize;
1825 mNextObjectHint = 0;
1826
1827 } else if (mData) {
1828 if (objectsSize < mObjectsSize) {
1829 // Need to release refs on any objects we are dropping.
1830 const sp<ProcessState> proc(ProcessState::self());
1831 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1832 const flat_binder_object* flat
1833 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1834 if (flat->type == BINDER_TYPE_FD) {
1835 // will need to rescan because we may have lopped off the only FDs
1836 mFdsKnown = false;
1837 }
Adrian Rooscbf37262015-10-22 16:12:53 -07001838 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001840 binder_size_t* objects =
1841 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001842 if (objects) {
1843 mObjects = objects;
1844 }
1845 mObjectsSize = objectsSize;
1846 mNextObjectHint = 0;
1847 }
1848
1849 // We own the data, so we can just do a realloc().
1850 if (desired > mDataCapacity) {
1851 uint8_t* data = (uint8_t*)realloc(mData, desired);
1852 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001853 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1854 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001855 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001856 gParcelGlobalAllocSize += desired;
1857 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austinf28b2952015-09-10 13:46:02 -07001858 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001859 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860 mData = data;
1861 mDataCapacity = desired;
1862 } else if (desired > mDataCapacity) {
1863 mError = NO_MEMORY;
1864 return NO_MEMORY;
1865 }
1866 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001867 if (mDataSize > desired) {
1868 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001869 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001870 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 if (mDataPos > desired) {
1872 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001873 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874 }
1875 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001876
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877 } else {
1878 // This is the first data. Easy!
1879 uint8_t* data = (uint8_t*)malloc(desired);
1880 if (!data) {
1881 mError = NO_MEMORY;
1882 return NO_MEMORY;
1883 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001884
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 if(!(mDataCapacity == 0 && mObjects == NULL
1886 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001887 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001889
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001890 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001891 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001892 gParcelGlobalAllocSize += desired;
1893 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001894 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001895
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 mData = data;
1897 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001898 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1899 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900 mDataCapacity = desired;
1901 }
1902
1903 return NO_ERROR;
1904}
1905
1906void Parcel::initState()
1907{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001908 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909 mError = NO_ERROR;
1910 mData = 0;
1911 mDataSize = 0;
1912 mDataCapacity = 0;
1913 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001914 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1915 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916 mObjects = NULL;
1917 mObjectsSize = 0;
1918 mObjectsCapacity = 0;
1919 mNextObjectHint = 0;
1920 mHasFds = false;
1921 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001922 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001923 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07001924 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925}
1926
1927void Parcel::scanForFds() const
1928{
1929 bool hasFds = false;
1930 for (size_t i=0; i<mObjectsSize; i++) {
1931 const flat_binder_object* flat
1932 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1933 if (flat->type == BINDER_TYPE_FD) {
1934 hasFds = true;
1935 break;
1936 }
1937 }
1938 mHasFds = hasFds;
1939 mFdsKnown = true;
1940}
1941
Dan Sandleraa5c2342015-04-10 10:08:45 -04001942size_t Parcel::getBlobAshmemSize() const
1943{
Adrian Roos6bb31142015-10-22 16:46:12 -07001944 // This used to return the size of all blobs that were written to ashmem, now we're returning
1945 // the ashmem currently referenced by this Parcel, which should be equivalent.
1946 // TODO: Remove method once ABI can be changed.
1947 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001948}
1949
Adrian Rooscbf37262015-10-22 16:12:53 -07001950size_t Parcel::getOpenAshmemSize() const
1951{
1952 return mOpenAshmemSize;
1953}
1954
Jeff Brown5707dbf2011-09-23 21:17:56 -07001955// --- Parcel::Blob ---
1956
1957Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08001958 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001959}
1960
1961Parcel::Blob::~Blob() {
1962 release();
1963}
1964
1965void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08001966 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001967 ::munmap(mData, mSize);
1968 }
1969 clear();
1970}
1971
Jeff Brown13b16042014-11-11 16:44:25 -08001972void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
1973 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001974 mData = data;
1975 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08001976 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001977}
1978
1979void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08001980 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001981 mData = NULL;
1982 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08001983 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001984}
1985
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986}; // namespace android