blob: 3ca49b1eb1cedfd28c2fade0fc68d5dd7f0b2acf [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/Parcel.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070021
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -070022#include <binder/IPCThreadState.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Binder.h>
24#include <binder/BpBinder.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080026#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070027#include <binder/TextOutput.h>
28
Jun Jiangabf8a2c2014-04-29 14:22:10 +080029#include <errno.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070030#include <utils/Debug.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/Log.h>
32#include <utils/String8.h>
33#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070034#include <utils/misc.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080035#include <utils/Flattenable.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070036#include <cutils/ashmem.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037
Mathias Agopian208059f2009-05-18 15:08:03 -070038#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080039#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -080041#include <inttypes.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <stdint.h>
Jeff Brown5707dbf2011-09-23 21:17:56 -070045#include <sys/mman.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046
47#ifndef INT32_MAX
48#define INT32_MAX ((int32_t)(2147483647))
49#endif
50
51#define LOG_REFS(...)
Steve Block9f760152011-10-12 17:27:03 +010052//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080053#define LOG_ALLOC(...)
54//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
56// ---------------------------------------------------------------------------
57
Nick Kralevichb6b14232015-04-02 09:36:02 -070058// This macro should never be used at runtime, as a too large value
59// of s could cause an integer overflow. Instead, you should always
60// use the wrapper function pad_size()
61#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
62
63static size_t pad_size(size_t s) {
64 if (s > (SIZE_T_MAX - 3)) {
65 abort();
66 }
67 return PAD_SIZE_UNSAFE(s);
68}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070069
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080071#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070073// XXX This can be made public if we want to provide
74// support for typed data.
75struct small_flat_data
76{
77 uint32_t type;
78 uint32_t data;
79};
80
81namespace android {
82
Dianne Hackborna4cff882014-11-13 17:07:40 -080083static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
84static size_t gParcelGlobalAllocSize = 0;
85static size_t gParcelGlobalAllocCount = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
99 switch (obj.type) {
100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
112 if (b != NULL) {
113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
120 if (b != NULL) b.get_refs()->incWeak(who);
121 return;
122 }
123 case BINDER_TYPE_FD: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700124 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700125 if (outAshmemSize != NULL) {
126 // If we own an ashmem fd, keep track of how much memory it refers to.
127 int size = ashmem_get_size_region(obj.handle);
128 if (size > 0) {
129 *outAshmemSize += size;
130 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700131 }
132 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 return;
134 }
135 }
136
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800137 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138}
139
Adrian Roos6bb31142015-10-22 16:46:12 -0700140void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141 const flat_binder_object& obj, const void* who)
142{
Adrian Roos6bb31142015-10-22 16:46:12 -0700143 acquire_object(proc, obj, who, NULL);
144}
145
146static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700147 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148{
149 switch (obj.type) {
150 case BINDER_TYPE_BINDER:
151 if (obj.binder) {
152 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800153 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 }
155 return;
156 case BINDER_TYPE_WEAK_BINDER:
157 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800158 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 return;
160 case BINDER_TYPE_HANDLE: {
161 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
162 if (b != NULL) {
163 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
164 b->decStrong(who);
165 }
166 return;
167 }
168 case BINDER_TYPE_WEAK_HANDLE: {
169 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
170 if (b != NULL) b.get_refs()->decWeak(who);
171 return;
172 }
173 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700174 if (outAshmemSize != NULL) {
175 if (obj.cookie != 0) {
176 int size = ashmem_get_size_region(obj.handle);
177 if (size > 0) {
178 *outAshmemSize -= size;
179 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700180
Adrian Roos6bb31142015-10-22 16:46:12 -0700181 close(obj.handle);
182 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700183 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700184 return;
185 }
186 }
187
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800188 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189}
190
Adrian Roos6bb31142015-10-22 16:46:12 -0700191void release_object(const sp<ProcessState>& proc,
192 const flat_binder_object& obj, const void* who)
193{
194 release_object(proc, obj, who, NULL);
195}
196
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800198 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199{
200 return out->writeObject(flat, false);
201}
202
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800203status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700204 const sp<IBinder>& binder, Parcel* out)
205{
206 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
209 if (binder != NULL) {
210 IBinder *local = binder->localBinder();
211 if (!local) {
212 BpBinder *proxy = binder->remoteBinder();
213 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000214 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215 }
216 const int32_t handle = proxy ? proxy->handle() : 0;
217 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800218 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800220 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 } else {
222 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800223 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
224 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 }
226 } else {
227 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.binder = 0;
229 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 return finish_flatten_binder(binder, obj, out);
233}
234
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800235status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 const wp<IBinder>& binder, Parcel* out)
237{
238 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
241 if (binder != NULL) {
242 sp<IBinder> real = binder.promote();
243 if (real != NULL) {
244 IBinder *local = real->localBinder();
245 if (!local) {
246 BpBinder *proxy = real->remoteBinder();
247 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000248 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
250 const int32_t handle = proxy ? proxy->handle() : 0;
251 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800252 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800254 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 } else {
256 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800257 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
258 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 return finish_flatten_binder(real, obj, out);
261 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 // XXX How to deal? In order to flatten the given binder,
264 // we need to probe it for information, which requires a primary
265 // reference... but we don't have one.
266 //
267 // The OpenBinder implementation uses a dynamic_cast<> here,
268 // but we can't do that with the different reference counting
269 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000270 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 obj.binder = 0;
273 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 } else {
277 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800278 obj.binder = 0;
279 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 return finish_flatten_binder(NULL, obj, out);
281 }
282}
283
284inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800285 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
286 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287{
288 return NO_ERROR;
289}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700290
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700291status_t unflatten_binder(const sp<ProcessState>& proc,
292 const Parcel& in, sp<IBinder>* out)
293{
294 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700295
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296 if (flat) {
297 switch (flat->type) {
298 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800299 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 return finish_unflatten_binder(NULL, *flat, in);
301 case BINDER_TYPE_HANDLE:
302 *out = proc->getStrongProxyForHandle(flat->handle);
303 return finish_unflatten_binder(
304 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700305 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 }
307 return BAD_TYPE;
308}
309
310status_t unflatten_binder(const sp<ProcessState>& proc,
311 const Parcel& in, wp<IBinder>* out)
312{
313 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700314
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700315 if (flat) {
316 switch (flat->type) {
317 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800318 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 return finish_unflatten_binder(NULL, *flat, in);
320 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800321 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800323 reinterpret_cast<IBinder*>(flat->cookie),
324 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 } else {
326 *out = NULL;
327 }
328 return finish_unflatten_binder(NULL, *flat, in);
329 case BINDER_TYPE_HANDLE:
330 case BINDER_TYPE_WEAK_HANDLE:
331 *out = proc->getWeakProxyForHandle(flat->handle);
332 return finish_unflatten_binder(
333 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
334 }
335 }
336 return BAD_TYPE;
337}
338
339// ---------------------------------------------------------------------------
340
341Parcel::Parcel()
342{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800343 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700344 initState();
345}
346
347Parcel::~Parcel()
348{
349 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800350 LOG_ALLOC("Parcel %p: destroyed", this);
351}
352
353size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800354 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
355 size_t size = gParcelGlobalAllocSize;
356 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
357 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800358}
359
360size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800361 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
362 size_t count = gParcelGlobalAllocCount;
363 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
364 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365}
366
367const uint8_t* Parcel::data() const
368{
369 return mData;
370}
371
372size_t Parcel::dataSize() const
373{
374 return (mDataSize > mDataPos ? mDataSize : mDataPos);
375}
376
377size_t Parcel::dataAvail() const
378{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700379 size_t result = dataSize() - dataPosition();
380 if (result > INT32_MAX) {
381 abort();
382 }
383 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384}
385
386size_t Parcel::dataPosition() const
387{
388 return mDataPos;
389}
390
391size_t Parcel::dataCapacity() const
392{
393 return mDataCapacity;
394}
395
396status_t Parcel::setDataSize(size_t size)
397{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700398 if (size > INT32_MAX) {
399 // don't accept size_t values which may have come from an
400 // inadvertent conversion from a negative int.
401 return BAD_VALUE;
402 }
403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 status_t err;
405 err = continueWrite(size);
406 if (err == NO_ERROR) {
407 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700408 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 }
410 return err;
411}
412
413void Parcel::setDataPosition(size_t pos) const
414{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700415 if (pos > INT32_MAX) {
416 // don't accept size_t values which may have come from an
417 // inadvertent conversion from a negative int.
418 abort();
419 }
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 mDataPos = pos;
422 mNextObjectHint = 0;
423}
424
425status_t Parcel::setDataCapacity(size_t size)
426{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 if (size > INT32_MAX) {
428 // don't accept size_t values which may have come from an
429 // inadvertent conversion from a negative int.
430 return BAD_VALUE;
431 }
432
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700433 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 return NO_ERROR;
435}
436
437status_t Parcel::setData(const uint8_t* buffer, size_t len)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (len > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 status_t err = restartWrite(len);
446 if (err == NO_ERROR) {
447 memcpy(const_cast<uint8_t*>(data()), buffer, len);
448 mDataSize = len;
449 mFdsKnown = false;
450 }
451 return err;
452}
453
Andreas Huber51faf462011-04-13 10:21:56 -0700454status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455{
456 const sp<ProcessState> proc(ProcessState::self());
457 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700458 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800459 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 size_t size = parcel->mObjectsSize;
461 int startPos = mDataPos;
462 int firstIndex = -1, lastIndex = -2;
463
464 if (len == 0) {
465 return NO_ERROR;
466 }
467
Nick Kralevichb6b14232015-04-02 09:36:02 -0700468 if (len > INT32_MAX) {
469 // don't accept size_t values which may have come from an
470 // inadvertent conversion from a negative int.
471 return BAD_VALUE;
472 }
473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 // range checks against the source parcel size
475 if ((offset > parcel->mDataSize)
476 || (len > parcel->mDataSize)
477 || (offset + len > parcel->mDataSize)) {
478 return BAD_VALUE;
479 }
480
481 // Count objects in range
482 for (int i = 0; i < (int) size; i++) {
483 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700484 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 if (firstIndex == -1) {
486 firstIndex = i;
487 }
488 lastIndex = i;
489 }
490 }
491 int numObjects = lastIndex - firstIndex + 1;
492
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700493 if ((mDataSize+len) > mDataCapacity) {
494 // grow data
495 err = growData(len);
496 if (err != NO_ERROR) {
497 return err;
498 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 }
500
501 // append data
502 memcpy(mData + mDataPos, data + offset, len);
503 mDataPos += len;
504 mDataSize += len;
505
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400506 err = NO_ERROR;
507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 if (numObjects > 0) {
509 // grow objects
510 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700511 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
512 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800513 binder_size_t *objects =
514 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
515 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 return NO_MEMORY;
517 }
518 mObjects = objects;
519 mObjectsCapacity = newSize;
520 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 // append and acquire objects
523 int idx = mObjectsSize;
524 for (int i = firstIndex; i <= lastIndex; i++) {
525 size_t off = objects[i] - offset + startPos;
526 mObjects[idx++] = off;
527 mObjectsSize++;
528
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700529 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700531 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700534 // If this is a file descriptor, we need to dup it so the
535 // new Parcel now owns its own fd, and can declare that we
536 // officially know we have fds.
537 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800538 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400540 if (!mAllowFds) {
541 err = FDS_NOT_ALLOWED;
542 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 }
544 }
545 }
546
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400547 return err;
548}
549
Jeff Brown13b16042014-11-11 16:44:25 -0800550bool Parcel::allowFds() const
551{
552 return mAllowFds;
553}
554
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700555bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400556{
557 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700558 if (!allowFds) {
559 mAllowFds = false;
560 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562}
563
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700564void Parcel::restoreAllowFds(bool lastValue)
565{
566 mAllowFds = lastValue;
567}
568
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569bool Parcel::hasFileDescriptors() const
570{
571 if (!mFdsKnown) {
572 scanForFds();
573 }
574 return mHasFds;
575}
576
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700577// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700578status_t Parcel::writeInterfaceToken(const String16& interface)
579{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700580 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
581 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700582 // currently the interface identification token is just its name as a string
583 return writeString16(interface);
584}
585
Mathias Agopian83c04462009-05-22 19:00:22 -0700586bool Parcel::checkInterface(IBinder* binder) const
587{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700588 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700589}
590
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700591bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700592 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700593{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700594 int32_t strictPolicy = readInt32();
595 if (threadState == NULL) {
596 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700597 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700598 if ((threadState->getLastTransactionBinderFlags() &
599 IBinder::FLAG_ONEWAY) != 0) {
600 // For one-way calls, the callee is running entirely
601 // disconnected from the caller, so disable StrictMode entirely.
602 // Not only does disk/network usage not impact the caller, but
603 // there's no way to commuicate back any violations anyway.
604 threadState->setStrictModePolicy(0);
605 } else {
606 threadState->setStrictModePolicy(strictPolicy);
607 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700608 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700609 if (str == interface) {
610 return true;
611 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700612 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700613 String8(interface).string(), String8(str).string());
614 return false;
615 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700616}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800618const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619{
620 return mObjects;
621}
622
623size_t Parcel::objectsCount() const
624{
625 return mObjectsSize;
626}
627
628status_t Parcel::errorCheck() const
629{
630 return mError;
631}
632
633void Parcel::setError(status_t err)
634{
635 mError = err;
636}
637
638status_t Parcel::finishWrite(size_t len)
639{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700640 if (len > INT32_MAX) {
641 // don't accept size_t values which may have come from an
642 // inadvertent conversion from a negative int.
643 return BAD_VALUE;
644 }
645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646 //printf("Finish write of %d\n", len);
647 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700648 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 if (mDataPos > mDataSize) {
650 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700651 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 }
653 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
654 return NO_ERROR;
655}
656
657status_t Parcel::writeUnpadded(const void* data, size_t len)
658{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700659 if (len > INT32_MAX) {
660 // don't accept size_t values which may have come from an
661 // inadvertent conversion from a negative int.
662 return BAD_VALUE;
663 }
664
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700665 size_t end = mDataPos + len;
666 if (end < mDataPos) {
667 // integer overflow
668 return BAD_VALUE;
669 }
670
671 if (end <= mDataCapacity) {
672restart_write:
673 memcpy(mData+mDataPos, data, len);
674 return finishWrite(len);
675 }
676
677 status_t err = growData(len);
678 if (err == NO_ERROR) goto restart_write;
679 return err;
680}
681
682status_t Parcel::write(const void* data, size_t len)
683{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700684 if (len > INT32_MAX) {
685 // don't accept size_t values which may have come from an
686 // inadvertent conversion from a negative int.
687 return BAD_VALUE;
688 }
689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690 void* const d = writeInplace(len);
691 if (d) {
692 memcpy(d, data, len);
693 return NO_ERROR;
694 }
695 return mError;
696}
697
698void* Parcel::writeInplace(size_t len)
699{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700700 if (len > INT32_MAX) {
701 // don't accept size_t values which may have come from an
702 // inadvertent conversion from a negative int.
703 return NULL;
704 }
705
706 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700707
708 // sanity check for integer overflow
709 if (mDataPos+padded < mDataPos) {
710 return NULL;
711 }
712
713 if ((mDataPos+padded) <= mDataCapacity) {
714restart_write:
715 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
716 uint8_t* const data = mData+mDataPos;
717
718 // Need to pad at end?
719 if (padded != len) {
720#if BYTE_ORDER == BIG_ENDIAN
721 static const uint32_t mask[4] = {
722 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
723 };
724#endif
725#if BYTE_ORDER == LITTLE_ENDIAN
726 static const uint32_t mask[4] = {
727 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
728 };
729#endif
730 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
731 // *reinterpret_cast<void**>(data+padded-4));
732 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
733 }
734
735 finishWrite(padded);
736 return data;
737 }
738
739 status_t err = growData(padded);
740 if (err == NO_ERROR) goto restart_write;
741 return NULL;
742}
743
Casey Dahlin451ff582015-10-19 18:12:18 -0700744status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
745{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700746 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700747 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700748 status = BAD_VALUE;
749 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700750 }
751
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700752 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700753 if (status != OK) {
754 return status;
755 }
756
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700757 void* data = writeInplace(val.size());
758 if (!data) {
759 status = BAD_VALUE;
760 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700761 }
762
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700763 memcpy(data, val.data(), val.size());
764 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700765}
766
767status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
768{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800769 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700770}
771
772status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
773{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800774 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700775}
776
777status_t Parcel::writeFloatVector(const std::vector<float>& val)
778{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800779 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700780}
781
782status_t Parcel::writeDoubleVector(const std::vector<double>& val)
783{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800784 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700785}
786
787status_t Parcel::writeBoolVector(const std::vector<bool>& val)
788{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800789 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700790}
791
792status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
793{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800794 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700795}
796
797status_t Parcel::writeString16Vector(const std::vector<String16>& val)
798{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800799 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700800}
801
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700802status_t Parcel::writeInt32(int32_t val)
803{
Andreas Huber84a6d042009-08-17 13:33:27 -0700804 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700805}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800806
807status_t Parcel::writeUint32(uint32_t val)
808{
809 return writeAligned(val);
810}
811
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700812status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700813 if (len > INT32_MAX) {
814 // don't accept size_t values which may have come from an
815 // inadvertent conversion from a negative int.
816 return BAD_VALUE;
817 }
818
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700819 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700820 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700821 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700822 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700823 if (ret == NO_ERROR) {
824 ret = write(val, len * sizeof(*val));
825 }
826 return ret;
827}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700828status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700829 if (len > INT32_MAX) {
830 // don't accept size_t values which may have come from an
831 // inadvertent conversion from a negative int.
832 return BAD_VALUE;
833 }
834
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700835 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700836 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700837 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700838 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700839 if (ret == NO_ERROR) {
840 ret = write(val, len * sizeof(*val));
841 }
842 return ret;
843}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700844
Casey Dahlind6848f52015-10-15 15:44:59 -0700845status_t Parcel::writeBool(bool val)
846{
847 return writeInt32(int32_t(val));
848}
849
850status_t Parcel::writeChar(char16_t val)
851{
852 return writeInt32(int32_t(val));
853}
854
855status_t Parcel::writeByte(int8_t val)
856{
857 return writeInt32(int32_t(val));
858}
859
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700860status_t Parcel::writeInt64(int64_t val)
861{
Andreas Huber84a6d042009-08-17 13:33:27 -0700862 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700863}
864
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700865status_t Parcel::writeUint64(uint64_t val)
866{
867 return writeAligned(val);
868}
869
Serban Constantinescuf683e012013-11-05 16:53:55 +0000870status_t Parcel::writePointer(uintptr_t val)
871{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800872 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000873}
874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875status_t Parcel::writeFloat(float val)
876{
Andreas Huber84a6d042009-08-17 13:33:27 -0700877 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878}
879
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800880#if defined(__mips__) && defined(__mips_hard_float)
881
882status_t Parcel::writeDouble(double val)
883{
884 union {
885 double d;
886 unsigned long long ll;
887 } u;
888 u.d = val;
889 return writeAligned(u.ll);
890}
891
892#else
893
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894status_t Parcel::writeDouble(double val)
895{
Andreas Huber84a6d042009-08-17 13:33:27 -0700896 return writeAligned(val);
897}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700898
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800899#endif
900
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901status_t Parcel::writeCString(const char* str)
902{
903 return write(str, strlen(str)+1);
904}
905
906status_t Parcel::writeString8(const String8& str)
907{
908 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100909 // only write string if its length is more than zero characters,
910 // as readString8 will only read if the length field is non-zero.
911 // this is slightly different from how writeString16 works.
912 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700913 err = write(str.string(), str.bytes()+1);
914 }
915 return err;
916}
917
918status_t Parcel::writeString16(const String16& str)
919{
920 return writeString16(str.string(), str.size());
921}
922
923status_t Parcel::writeString16(const char16_t* str, size_t len)
924{
925 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927 status_t err = writeInt32(len);
928 if (err == NO_ERROR) {
929 len *= sizeof(char16_t);
930 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
931 if (data) {
932 memcpy(data, str, len);
933 *reinterpret_cast<char16_t*>(data+len) = 0;
934 return NO_ERROR;
935 }
936 err = mError;
937 }
938 return err;
939}
940
941status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
942{
943 return flatten_binder(ProcessState::self(), val, this);
944}
945
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800946status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
947{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800948 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800949}
950
951status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800952 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -0800953}
954
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700955status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
956{
957 return flatten_binder(ProcessState::self(), val, this);
958}
959
Christopher Wiley97f048d2015-11-19 06:49:05 -0800960status_t Parcel::writeParcelable(const Parcelable& parcelable) {
961 status_t status = writeInt32(1); // parcelable is not null.
962 if (status != OK) {
963 return status;
964 }
965 return parcelable.writeToParcel(this);
966}
967
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700968status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800969{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700970 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800971 return BAD_TYPE;
972
973 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700974 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800975 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700977 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800978 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700980 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
981 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982
983 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000984 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 return err;
986 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700987 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800988 return err;
989}
990
Jeff Brown93ff1f92011-11-04 19:01:44 -0700991status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992{
993 flat_binder_object obj;
994 obj.type = BINDER_TYPE_FD;
995 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800996 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700997 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800998 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999 return writeObject(obj, true);
1000}
1001
1002status_t Parcel::writeDupFileDescriptor(int fd)
1003{
Jeff Brownd341c712011-11-04 20:19:33 -07001004 int dupFd = dup(fd);
1005 if (dupFd < 0) {
1006 return -errno;
1007 }
1008 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001009 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001010 close(dupFd);
1011 }
1012 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013}
1014
Casey Dahlin06673e32015-11-23 13:24:23 -08001015status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1016 return writeDupFileDescriptor(fd.get());
1017}
1018
1019status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1020 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1021}
1022
Jeff Brown13b16042014-11-11 16:44:25 -08001023status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001024{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001025 if (len > INT32_MAX) {
1026 // don't accept size_t values which may have come from an
1027 // inadvertent conversion from a negative int.
1028 return BAD_VALUE;
1029 }
1030
Jeff Brown13b16042014-11-11 16:44:25 -08001031 status_t status;
1032 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001033 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001034 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001035 if (status) return status;
1036
1037 void* ptr = writeInplace(len);
1038 if (!ptr) return NO_MEMORY;
1039
Jeff Brown13b16042014-11-11 16:44:25 -08001040 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001041 return NO_ERROR;
1042 }
1043
Steve Block6807e592011-10-20 11:56:00 +01001044 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001045 int fd = ashmem_create_region("Parcel Blob", len);
1046 if (fd < 0) return NO_MEMORY;
1047
1048 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1049 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001050 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001051 } else {
1052 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1053 if (ptr == MAP_FAILED) {
1054 status = -errno;
1055 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001056 if (!mutableCopy) {
1057 result = ashmem_set_prot_region(fd, PROT_READ);
1058 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001059 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001060 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001061 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001062 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001063 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001064 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001065 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001066 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001067 return NO_ERROR;
1068 }
1069 }
1070 }
1071 }
1072 ::munmap(ptr, len);
1073 }
1074 ::close(fd);
1075 return status;
1076}
1077
Jeff Brown13b16042014-11-11 16:44:25 -08001078status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1079{
1080 // Must match up with what's done in writeBlob.
1081 if (!mAllowFds) return FDS_NOT_ALLOWED;
1082 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1083 if (status) return status;
1084 return writeDupFileDescriptor(fd);
1085}
1086
Mathias Agopiane1424282013-07-29 21:24:40 -07001087status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001088{
1089 status_t err;
1090
1091 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001092 const size_t len = val.getFlattenedSize();
1093 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001094
Nick Kralevichb6b14232015-04-02 09:36:02 -07001095 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1096 // don't accept size_t values which may have come from an
1097 // inadvertent conversion from a negative int.
1098 return BAD_VALUE;
1099 }
1100
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001101 err = this->writeInt32(len);
1102 if (err) return err;
1103
1104 err = this->writeInt32(fd_count);
1105 if (err) return err;
1106
1107 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001108 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001109 if (buf == NULL)
1110 return BAD_VALUE;
1111
1112 int* fds = NULL;
1113 if (fd_count) {
1114 fds = new int[fd_count];
1115 }
1116
1117 err = val.flatten(buf, len, fds, fd_count);
1118 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1119 err = this->writeDupFileDescriptor( fds[i] );
1120 }
1121
1122 if (fd_count) {
1123 delete [] fds;
1124 }
1125
1126 return err;
1127}
1128
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001129status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1130{
1131 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1132 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1133 if (enoughData && enoughObjects) {
1134restart_write:
1135 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001136
Christopher Tate98e67d32015-06-03 18:44:15 -07001137 // remember if it's a file descriptor
1138 if (val.type == BINDER_TYPE_FD) {
1139 if (!mAllowFds) {
1140 // fail before modifying our object index
1141 return FDS_NOT_ALLOWED;
1142 }
1143 mHasFds = mFdsKnown = true;
1144 }
1145
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001146 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001147 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001148 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001149 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001150 mObjectsSize++;
1151 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001152
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153 return finishWrite(sizeof(flat_binder_object));
1154 }
1155
1156 if (!enoughData) {
1157 const status_t err = growData(sizeof(val));
1158 if (err != NO_ERROR) return err;
1159 }
1160 if (!enoughObjects) {
1161 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001162 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001163 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 if (objects == NULL) return NO_MEMORY;
1165 mObjects = objects;
1166 mObjectsCapacity = newSize;
1167 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001168
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169 goto restart_write;
1170}
1171
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001172status_t Parcel::writeNoException()
1173{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001174 binder::Status status;
1175 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001176}
1177
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001178void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179{
1180 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1181}
1182
1183status_t Parcel::read(void* outData, size_t len) const
1184{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001185 if (len > INT32_MAX) {
1186 // don't accept size_t values which may have come from an
1187 // inadvertent conversion from a negative int.
1188 return BAD_VALUE;
1189 }
1190
1191 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1192 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001193 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001194 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001195 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001196 return NO_ERROR;
1197 }
1198 return NOT_ENOUGH_DATA;
1199}
1200
1201const void* Parcel::readInplace(size_t len) const
1202{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001203 if (len > INT32_MAX) {
1204 // don't accept size_t values which may have come from an
1205 // inadvertent conversion from a negative int.
1206 return NULL;
1207 }
1208
1209 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1210 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001212 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001213 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214 return data;
1215 }
1216 return NULL;
1217}
1218
Andreas Huber84a6d042009-08-17 13:33:27 -07001219template<class T>
1220status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001221 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001222
1223 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001224 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001225 mDataPos += sizeof(T);
1226 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001227 return NO_ERROR;
1228 } else {
1229 return NOT_ENOUGH_DATA;
1230 }
1231}
1232
Andreas Huber84a6d042009-08-17 13:33:27 -07001233template<class T>
1234T Parcel::readAligned() const {
1235 T result;
1236 if (readAligned(&result) != NO_ERROR) {
1237 result = 0;
1238 }
1239
1240 return result;
1241}
1242
1243template<class T>
1244status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001245 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001246
1247 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1248restart_write:
1249 *reinterpret_cast<T*>(mData+mDataPos) = val;
1250 return finishWrite(sizeof(val));
1251 }
1252
1253 status_t err = growData(sizeof(val));
1254 if (err == NO_ERROR) goto restart_write;
1255 return err;
1256}
1257
Casey Dahlin451ff582015-10-19 18:12:18 -07001258status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1259 val->clear();
1260
1261 int32_t size;
1262 status_t status = readInt32(&size);
1263
1264 if (status != OK) {
1265 return status;
1266 }
1267
Christopher Wiley4db672d2015-11-10 09:44:30 -08001268 if (size < 0) {
1269 status = UNEXPECTED_NULL;
1270 return status;
1271 }
1272 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001273 status = BAD_VALUE;
1274 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001275 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001276
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001277 const void* data = readInplace(size);
1278 if (!data) {
1279 status = BAD_VALUE;
1280 return status;
1281 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001282 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001283 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001284
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001285 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001286}
1287
1288status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001289 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001290}
1291
1292status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001293 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001294}
1295
1296status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001297 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001298}
1299
1300status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001301 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001302}
1303
1304status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1305 val->clear();
1306
1307 int32_t size;
1308 status_t status = readInt32(&size);
1309
1310 if (status != OK) {
1311 return status;
1312 }
1313
1314 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001315 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001316 }
1317
1318 val->resize(size);
1319
1320 /* C++ bool handling means a vector of bools isn't necessarily addressable
1321 * (we might use individual bits)
1322 */
Christopher Wiley97887982015-10-27 16:33:47 -07001323 bool data;
1324 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001325 status = readBool(&data);
1326 (*val)[i] = data;
1327
1328 if (status != OK) {
1329 return status;
1330 }
1331 }
1332
1333 return OK;
1334}
1335
1336status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001337 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001338}
1339
1340status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001341 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001342}
1343
1344
Andreas Huber84a6d042009-08-17 13:33:27 -07001345status_t Parcel::readInt32(int32_t *pArg) const
1346{
1347 return readAligned(pArg);
1348}
1349
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350int32_t Parcel::readInt32() const
1351{
Andreas Huber84a6d042009-08-17 13:33:27 -07001352 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353}
1354
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001355status_t Parcel::readUint32(uint32_t *pArg) const
1356{
1357 return readAligned(pArg);
1358}
1359
1360uint32_t Parcel::readUint32() const
1361{
1362 return readAligned<uint32_t>();
1363}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364
1365status_t Parcel::readInt64(int64_t *pArg) const
1366{
Andreas Huber84a6d042009-08-17 13:33:27 -07001367 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368}
1369
1370
1371int64_t Parcel::readInt64() const
1372{
Andreas Huber84a6d042009-08-17 13:33:27 -07001373 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001374}
1375
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001376status_t Parcel::readUint64(uint64_t *pArg) const
1377{
1378 return readAligned(pArg);
1379}
1380
1381uint64_t Parcel::readUint64() const
1382{
1383 return readAligned<uint64_t>();
1384}
1385
Serban Constantinescuf683e012013-11-05 16:53:55 +00001386status_t Parcel::readPointer(uintptr_t *pArg) const
1387{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001388 status_t ret;
1389 binder_uintptr_t ptr;
1390 ret = readAligned(&ptr);
1391 if (!ret)
1392 *pArg = ptr;
1393 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001394}
1395
1396uintptr_t Parcel::readPointer() const
1397{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001398 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001399}
1400
1401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402status_t Parcel::readFloat(float *pArg) const
1403{
Andreas Huber84a6d042009-08-17 13:33:27 -07001404 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001405}
1406
1407
1408float Parcel::readFloat() const
1409{
Andreas Huber84a6d042009-08-17 13:33:27 -07001410 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001411}
1412
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001413#if defined(__mips__) && defined(__mips_hard_float)
1414
1415status_t Parcel::readDouble(double *pArg) const
1416{
1417 union {
1418 double d;
1419 unsigned long long ll;
1420 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001421 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001422 status_t status;
1423 status = readAligned(&u.ll);
1424 *pArg = u.d;
1425 return status;
1426}
1427
1428double Parcel::readDouble() const
1429{
1430 union {
1431 double d;
1432 unsigned long long ll;
1433 } u;
1434 u.ll = readAligned<unsigned long long>();
1435 return u.d;
1436}
1437
1438#else
1439
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440status_t Parcel::readDouble(double *pArg) const
1441{
Andreas Huber84a6d042009-08-17 13:33:27 -07001442 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001443}
1444
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001445double Parcel::readDouble() const
1446{
Andreas Huber84a6d042009-08-17 13:33:27 -07001447 return readAligned<double>();
1448}
1449
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001450#endif
1451
Andreas Huber84a6d042009-08-17 13:33:27 -07001452status_t Parcel::readIntPtr(intptr_t *pArg) const
1453{
1454 return readAligned(pArg);
1455}
1456
1457
1458intptr_t Parcel::readIntPtr() const
1459{
1460 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461}
1462
Casey Dahlind6848f52015-10-15 15:44:59 -07001463status_t Parcel::readBool(bool *pArg) const
1464{
1465 int32_t tmp;
1466 status_t ret = readInt32(&tmp);
1467 *pArg = (tmp != 0);
1468 return ret;
1469}
1470
1471bool Parcel::readBool() const
1472{
1473 return readInt32() != 0;
1474}
1475
1476status_t Parcel::readChar(char16_t *pArg) const
1477{
1478 int32_t tmp;
1479 status_t ret = readInt32(&tmp);
1480 *pArg = char16_t(tmp);
1481 return ret;
1482}
1483
1484char16_t Parcel::readChar() const
1485{
1486 return char16_t(readInt32());
1487}
1488
1489status_t Parcel::readByte(int8_t *pArg) const
1490{
1491 int32_t tmp;
1492 status_t ret = readInt32(&tmp);
1493 *pArg = int8_t(tmp);
1494 return ret;
1495}
1496
1497int8_t Parcel::readByte() const
1498{
1499 return int8_t(readInt32());
1500}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001501
1502const char* Parcel::readCString() const
1503{
1504 const size_t avail = mDataSize-mDataPos;
1505 if (avail > 0) {
1506 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1507 // is the string's trailing NUL within the parcel's valid bounds?
1508 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1509 if (eos) {
1510 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001511 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001512 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 return str;
1514 }
1515 }
1516 return NULL;
1517}
1518
1519String8 Parcel::readString8() const
1520{
1521 int32_t size = readInt32();
1522 // watch for potential int overflow adding 1 for trailing NUL
1523 if (size > 0 && size < INT32_MAX) {
1524 const char* str = (const char*)readInplace(size+1);
1525 if (str) return String8(str, size);
1526 }
1527 return String8();
1528}
1529
1530String16 Parcel::readString16() const
1531{
1532 size_t len;
1533 const char16_t* str = readString16Inplace(&len);
1534 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001535 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001536 return String16();
1537}
1538
Casey Dahlin451ff582015-10-19 18:12:18 -07001539status_t Parcel::readString16(String16* pArg) const
1540{
1541 size_t len;
1542 const char16_t* str = readString16Inplace(&len);
1543 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001544 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001545 return 0;
1546 } else {
1547 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001548 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001549 }
1550}
1551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1553{
1554 int32_t size = readInt32();
1555 // watch for potential int overflow from size+1
1556 if (size >= 0 && size < INT32_MAX) {
1557 *outLen = size;
1558 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1559 if (str != NULL) {
1560 return str;
1561 }
1562 }
1563 *outLen = 0;
1564 return NULL;
1565}
1566
Casey Dahlinf0c13772015-10-27 18:33:56 -07001567status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1568{
1569 return unflatten_binder(ProcessState::self(), *this, val);
1570}
1571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572sp<IBinder> Parcel::readStrongBinder() const
1573{
1574 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001575 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576 return val;
1577}
1578
1579wp<IBinder> Parcel::readWeakBinder() const
1580{
1581 wp<IBinder> val;
1582 unflatten_binder(ProcessState::self(), *this, &val);
1583 return val;
1584}
1585
Christopher Wiley97f048d2015-11-19 06:49:05 -08001586status_t Parcel::readParcelable(Parcelable* parcelable) const {
1587 int32_t have_parcelable = 0;
1588 status_t status = readInt32(&have_parcelable);
1589 if (status != OK) {
1590 return status;
1591 }
1592 if (!have_parcelable) {
1593 return UNEXPECTED_NULL;
1594 }
1595 return parcelable->readFromParcel(this);
1596}
1597
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001598int32_t Parcel::readExceptionCode() const
1599{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001600 binder::Status status;
1601 status.readFromParcel(*this);
1602 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001603}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001604
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001605native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001606{
1607 int numFds, numInts;
1608 status_t err;
1609 err = readInt32(&numFds);
1610 if (err != NO_ERROR) return 0;
1611 err = readInt32(&numInts);
1612 if (err != NO_ERROR) return 0;
1613
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001614 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001615 if (!h) {
1616 return 0;
1617 }
1618
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001619 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001620 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001621 if (h->data[i] < 0) err = BAD_VALUE;
1622 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001623 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001624 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001625 native_handle_close(h);
1626 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001627 h = 0;
1628 }
1629 return h;
1630}
1631
1632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633int Parcel::readFileDescriptor() const
1634{
1635 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001636
1637 if (flat && flat->type == BINDER_TYPE_FD) {
1638 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001639 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001641 return BAD_TYPE;
1642}
1643
Casey Dahlin06673e32015-11-23 13:24:23 -08001644status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1645{
1646 int got = readFileDescriptor();
1647
1648 if (got == BAD_TYPE) {
1649 return BAD_TYPE;
1650 }
1651
1652 val->reset(dup(got));
1653
1654 if (val->get() < 0) {
1655 return BAD_VALUE;
1656 }
1657
1658 return OK;
1659}
1660
1661
1662status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1663 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1664}
1665
Jeff Brown5707dbf2011-09-23 21:17:56 -07001666status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1667{
Jeff Brown13b16042014-11-11 16:44:25 -08001668 int32_t blobType;
1669 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001670 if (status) return status;
1671
Jeff Brown13b16042014-11-11 16:44:25 -08001672 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001673 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001674 const void* ptr = readInplace(len);
1675 if (!ptr) return BAD_VALUE;
1676
Jeff Brown13b16042014-11-11 16:44:25 -08001677 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001678 return NO_ERROR;
1679 }
1680
Steve Block6807e592011-10-20 11:56:00 +01001681 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001682 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001683 int fd = readFileDescriptor();
1684 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1685
Jeff Brown13b16042014-11-11 16:44:25 -08001686 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1687 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001688 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001689
Jeff Brown13b16042014-11-11 16:44:25 -08001690 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001691 return NO_ERROR;
1692}
1693
Mathias Agopiane1424282013-07-29 21:24:40 -07001694status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001695{
1696 // size
1697 const size_t len = this->readInt32();
1698 const size_t fd_count = this->readInt32();
1699
Nick Kralevichb6b14232015-04-02 09:36:02 -07001700 if (len > INT32_MAX) {
1701 // don't accept size_t values which may have come from an
1702 // inadvertent conversion from a negative int.
1703 return BAD_VALUE;
1704 }
1705
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001706 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001707 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001708 if (buf == NULL)
1709 return BAD_VALUE;
1710
1711 int* fds = NULL;
1712 if (fd_count) {
1713 fds = new int[fd_count];
1714 }
1715
1716 status_t err = NO_ERROR;
1717 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001718 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001719 if (fds[i] < 0) {
1720 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001721 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1722 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001723 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001724 }
1725
1726 if (err == NO_ERROR) {
1727 err = val.unflatten(buf, len, fds, fd_count);
1728 }
1729
1730 if (fd_count) {
1731 delete [] fds;
1732 }
1733
1734 return err;
1735}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1737{
1738 const size_t DPOS = mDataPos;
1739 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1740 const flat_binder_object* obj
1741 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1742 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001743 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001744 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745 // the object list, so we don't want to check for it when
1746 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001747 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 return obj;
1749 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001750
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001752 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753 const size_t N = mObjectsSize;
1754 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001755
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001756 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001757 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001758 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001759
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001760 // Start at the current hint position, looking for an object at
1761 // the current data position.
1762 if (opos < N) {
1763 while (opos < (N-1) && OBJS[opos] < DPOS) {
1764 opos++;
1765 }
1766 } else {
1767 opos = N-1;
1768 }
1769 if (OBJS[opos] == DPOS) {
1770 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001771 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001772 this, DPOS, opos);
1773 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001774 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775 return obj;
1776 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001777
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001778 // Look backwards for it...
1779 while (opos > 0 && OBJS[opos] > DPOS) {
1780 opos--;
1781 }
1782 if (OBJS[opos] == DPOS) {
1783 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001784 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785 this, DPOS, opos);
1786 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001787 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001788 return obj;
1789 }
1790 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001791 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 -07001792 this, DPOS);
1793 }
1794 return NULL;
1795}
1796
1797void Parcel::closeFileDescriptors()
1798{
1799 size_t i = mObjectsSize;
1800 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001801 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001802 }
1803 while (i > 0) {
1804 i--;
1805 const flat_binder_object* flat
1806 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1807 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001808 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809 close(flat->handle);
1810 }
1811 }
1812}
1813
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001814uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001816 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001817}
1818
1819size_t Parcel::ipcDataSize() const
1820{
1821 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1822}
1823
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001824uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001825{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001826 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827}
1828
1829size_t Parcel::ipcObjectsCount() const
1830{
1831 return mObjectsSize;
1832}
1833
1834void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001835 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001837 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838 freeDataNoInit();
1839 mError = NO_ERROR;
1840 mData = const_cast<uint8_t*>(data);
1841 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001842 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001844 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001845 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846 mObjectsSize = mObjectsCapacity = objectsCount;
1847 mNextObjectHint = 0;
1848 mOwner = relFunc;
1849 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001850 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001851 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001852 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001853 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001854 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001855 mObjectsSize = 0;
1856 break;
1857 }
1858 minOffset = offset + sizeof(flat_binder_object);
1859 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860 scanForFds();
1861}
1862
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001863void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864{
1865 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 if (errorCheck() != NO_ERROR) {
1868 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001869 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870 } else if (dataSize() > 0) {
1871 const uint8_t* DATA = data();
1872 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001873 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874 const size_t N = objectsCount();
1875 for (size_t i=0; i<N; i++) {
1876 const flat_binder_object* flat
1877 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1878 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1879 << TypeCode(flat->type & 0x7f7f7f00)
1880 << " = " << flat->binder;
1881 }
1882 } else {
1883 to << "NULL";
1884 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001885
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001886 to << ")";
1887}
1888
1889void Parcel::releaseObjects()
1890{
1891 const sp<ProcessState> proc(ProcessState::self());
1892 size_t i = mObjectsSize;
1893 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001894 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895 while (i > 0) {
1896 i--;
1897 const flat_binder_object* flat
1898 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001899 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900 }
1901}
1902
1903void Parcel::acquireObjects()
1904{
1905 const sp<ProcessState> proc(ProcessState::self());
1906 size_t i = mObjectsSize;
1907 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001908 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001909 while (i > 0) {
1910 i--;
1911 const flat_binder_object* flat
1912 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001913 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914 }
1915}
1916
1917void Parcel::freeData()
1918{
1919 freeDataNoInit();
1920 initState();
1921}
1922
1923void Parcel::freeDataNoInit()
1924{
1925 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001926 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001927 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1929 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001930 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001931 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001932 if (mData) {
1933 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001934 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07001935 if (mDataCapacity <= gParcelGlobalAllocSize) {
1936 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1937 } else {
1938 gParcelGlobalAllocSize = 0;
1939 }
1940 if (gParcelGlobalAllocCount > 0) {
1941 gParcelGlobalAllocCount--;
1942 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001943 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001944 free(mData);
1945 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001946 if (mObjects) free(mObjects);
1947 }
1948}
1949
1950status_t Parcel::growData(size_t len)
1951{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001952 if (len > INT32_MAX) {
1953 // don't accept size_t values which may have come from an
1954 // inadvertent conversion from a negative int.
1955 return BAD_VALUE;
1956 }
1957
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001958 size_t newSize = ((mDataSize+len)*3)/2;
1959 return (newSize <= mDataSize)
1960 ? (status_t) NO_MEMORY
1961 : continueWrite(newSize);
1962}
1963
1964status_t Parcel::restartWrite(size_t desired)
1965{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001966 if (desired > INT32_MAX) {
1967 // don't accept size_t values which may have come from an
1968 // inadvertent conversion from a negative int.
1969 return BAD_VALUE;
1970 }
1971
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972 if (mOwner) {
1973 freeData();
1974 return continueWrite(desired);
1975 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977 uint8_t* data = (uint8_t*)realloc(mData, desired);
1978 if (!data && desired > mDataCapacity) {
1979 mError = NO_MEMORY;
1980 return NO_MEMORY;
1981 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001982
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001984
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001985 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001986 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001987 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001988 gParcelGlobalAllocSize += desired;
1989 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08001990 if (!mData) {
1991 gParcelGlobalAllocCount++;
1992 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001993 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994 mData = data;
1995 mDataCapacity = desired;
1996 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001998 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001999 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2000 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 free(mObjects);
2003 mObjects = NULL;
2004 mObjectsSize = mObjectsCapacity = 0;
2005 mNextObjectHint = 0;
2006 mHasFds = false;
2007 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002008 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 return NO_ERROR;
2011}
2012
2013status_t Parcel::continueWrite(size_t desired)
2014{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002015 if (desired > INT32_MAX) {
2016 // don't accept size_t values which may have come from an
2017 // inadvertent conversion from a negative int.
2018 return BAD_VALUE;
2019 }
2020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 // If shrinking, first adjust for any objects that appear
2022 // after the new data size.
2023 size_t objectsSize = mObjectsSize;
2024 if (desired < mDataSize) {
2025 if (desired == 0) {
2026 objectsSize = 0;
2027 } else {
2028 while (objectsSize > 0) {
2029 if (mObjects[objectsSize-1] < desired)
2030 break;
2031 objectsSize--;
2032 }
2033 }
2034 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002035
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002036 if (mOwner) {
2037 // If the size is going to zero, just release the owner's data.
2038 if (desired == 0) {
2039 freeData();
2040 return NO_ERROR;
2041 }
2042
2043 // If there is a different owner, we need to take
2044 // posession.
2045 uint8_t* data = (uint8_t*)malloc(desired);
2046 if (!data) {
2047 mError = NO_MEMORY;
2048 return NO_MEMORY;
2049 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002050 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002052 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002053 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002055 free(data);
2056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002057 mError = NO_MEMORY;
2058 return NO_MEMORY;
2059 }
2060
2061 // Little hack to only acquire references on objects
2062 // we will be keeping.
2063 size_t oldObjectsSize = mObjectsSize;
2064 mObjectsSize = objectsSize;
2065 acquireObjects();
2066 mObjectsSize = oldObjectsSize;
2067 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069 if (mData) {
2070 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2071 }
2072 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002073 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002075 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2077 mOwner = NULL;
2078
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002079 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002080 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002081 gParcelGlobalAllocSize += desired;
2082 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002083 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002084
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002085 mData = data;
2086 mObjects = objects;
2087 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002088 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002089 mDataCapacity = desired;
2090 mObjectsSize = mObjectsCapacity = objectsSize;
2091 mNextObjectHint = 0;
2092
2093 } else if (mData) {
2094 if (objectsSize < mObjectsSize) {
2095 // Need to release refs on any objects we are dropping.
2096 const sp<ProcessState> proc(ProcessState::self());
2097 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2098 const flat_binder_object* flat
2099 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2100 if (flat->type == BINDER_TYPE_FD) {
2101 // will need to rescan because we may have lopped off the only FDs
2102 mFdsKnown = false;
2103 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002104 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002106 binder_size_t* objects =
2107 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 if (objects) {
2109 mObjects = objects;
2110 }
2111 mObjectsSize = objectsSize;
2112 mNextObjectHint = 0;
2113 }
2114
2115 // We own the data, so we can just do a realloc().
2116 if (desired > mDataCapacity) {
2117 uint8_t* data = (uint8_t*)realloc(mData, desired);
2118 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002119 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2120 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002121 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002122 gParcelGlobalAllocSize += desired;
2123 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002124 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125 mData = data;
2126 mDataCapacity = desired;
2127 } else if (desired > mDataCapacity) {
2128 mError = NO_MEMORY;
2129 return NO_MEMORY;
2130 }
2131 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002132 if (mDataSize > desired) {
2133 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002134 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002135 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136 if (mDataPos > desired) {
2137 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002138 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 }
2140 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002141
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142 } else {
2143 // This is the first data. Easy!
2144 uint8_t* data = (uint8_t*)malloc(desired);
2145 if (!data) {
2146 mError = NO_MEMORY;
2147 return NO_MEMORY;
2148 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002149
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 if(!(mDataCapacity == 0 && mObjects == NULL
2151 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002152 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002154
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002155 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002156 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002157 gParcelGlobalAllocSize += desired;
2158 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002159 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161 mData = data;
2162 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002163 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2164 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 mDataCapacity = desired;
2166 }
2167
2168 return NO_ERROR;
2169}
2170
2171void Parcel::initState()
2172{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002173 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 mError = NO_ERROR;
2175 mData = 0;
2176 mDataSize = 0;
2177 mDataCapacity = 0;
2178 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002179 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2180 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 mObjects = NULL;
2182 mObjectsSize = 0;
2183 mObjectsCapacity = 0;
2184 mNextObjectHint = 0;
2185 mHasFds = false;
2186 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002187 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002189 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190}
2191
2192void Parcel::scanForFds() const
2193{
2194 bool hasFds = false;
2195 for (size_t i=0; i<mObjectsSize; i++) {
2196 const flat_binder_object* flat
2197 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2198 if (flat->type == BINDER_TYPE_FD) {
2199 hasFds = true;
2200 break;
2201 }
2202 }
2203 mHasFds = hasFds;
2204 mFdsKnown = true;
2205}
2206
Dan Sandleraa5c2342015-04-10 10:08:45 -04002207size_t Parcel::getBlobAshmemSize() const
2208{
Adrian Roos6bb31142015-10-22 16:46:12 -07002209 // This used to return the size of all blobs that were written to ashmem, now we're returning
2210 // the ashmem currently referenced by this Parcel, which should be equivalent.
2211 // TODO: Remove method once ABI can be changed.
2212 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002213}
2214
Adrian Rooscbf37262015-10-22 16:12:53 -07002215size_t Parcel::getOpenAshmemSize() const
2216{
2217 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002218}
2219
2220// --- Parcel::Blob ---
2221
2222Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002223 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002224}
2225
2226Parcel::Blob::~Blob() {
2227 release();
2228}
2229
2230void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002231 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002232 ::munmap(mData, mSize);
2233 }
2234 clear();
2235}
2236
Jeff Brown13b16042014-11-11 16:44:25 -08002237void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2238 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002239 mData = data;
2240 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002241 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002242}
2243
2244void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002245 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002246 mData = NULL;
2247 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002248 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002249}
2250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251}; // namespace android