blob: 1d6ec9e9fe01d51ba1aa383d1d3b4da68623c885 [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 Wiley6e361342015-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,
141 const flat_binder_object& obj, const void* who)
142{
143 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
Christopher Wiley6264c4c2015-11-10 09:44:30 -0800339namespace {
340
341template<typename T>
342status_t readTypedVector(std::vector<T>* val, const Parcel* p,
343 status_t(Parcel::*read_func)(T*) const) {
344 val->clear();
345
346 int32_t size;
347 status_t status = p->readInt32(&size);
348
349 if (status != OK) {
350 return status;
351 }
352
353 if (size < 0) {
354 return UNEXPECTED_NULL;
355 }
356
357 val->resize(size);
358
359 for (auto& v: *val) {
360 status = (p->*read_func)(&v);
361
362 if (status != OK) {
363 return status;
364 }
365 }
366
367 return OK;
368}
369
370} // namespace
371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700372// ---------------------------------------------------------------------------
373
374Parcel::Parcel()
375{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800376 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377 initState();
378}
379
380Parcel::~Parcel()
381{
382 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800383 LOG_ALLOC("Parcel %p: destroyed", this);
384}
385
386size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800387 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
388 size_t size = gParcelGlobalAllocSize;
389 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
390 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800391}
392
393size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800394 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
395 size_t count = gParcelGlobalAllocCount;
396 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
397 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398}
399
400const uint8_t* Parcel::data() const
401{
402 return mData;
403}
404
405size_t Parcel::dataSize() const
406{
407 return (mDataSize > mDataPos ? mDataSize : mDataPos);
408}
409
410size_t Parcel::dataAvail() const
411{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700412 size_t result = dataSize() - dataPosition();
413 if (result > INT32_MAX) {
414 abort();
415 }
416 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417}
418
419size_t Parcel::dataPosition() const
420{
421 return mDataPos;
422}
423
424size_t Parcel::dataCapacity() const
425{
426 return mDataCapacity;
427}
428
429status_t Parcel::setDataSize(size_t size)
430{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700431 if (size > INT32_MAX) {
432 // don't accept size_t values which may have come from an
433 // inadvertent conversion from a negative int.
434 return BAD_VALUE;
435 }
436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 status_t err;
438 err = continueWrite(size);
439 if (err == NO_ERROR) {
440 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700441 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 }
443 return err;
444}
445
446void Parcel::setDataPosition(size_t pos) const
447{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700448 if (pos > INT32_MAX) {
449 // don't accept size_t values which may have come from an
450 // inadvertent conversion from a negative int.
451 abort();
452 }
453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 mDataPos = pos;
455 mNextObjectHint = 0;
456}
457
458status_t Parcel::setDataCapacity(size_t size)
459{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700460 if (size > INT32_MAX) {
461 // don't accept size_t values which may have come from an
462 // inadvertent conversion from a negative int.
463 return BAD_VALUE;
464 }
465
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700466 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467 return NO_ERROR;
468}
469
470status_t Parcel::setData(const uint8_t* buffer, size_t len)
471{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700472 if (len > INT32_MAX) {
473 // don't accept size_t values which may have come from an
474 // inadvertent conversion from a negative int.
475 return BAD_VALUE;
476 }
477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478 status_t err = restartWrite(len);
479 if (err == NO_ERROR) {
480 memcpy(const_cast<uint8_t*>(data()), buffer, len);
481 mDataSize = len;
482 mFdsKnown = false;
483 }
484 return err;
485}
486
Andreas Huber51faf462011-04-13 10:21:56 -0700487status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488{
489 const sp<ProcessState> proc(ProcessState::self());
490 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700491 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800492 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 size_t size = parcel->mObjectsSize;
494 int startPos = mDataPos;
495 int firstIndex = -1, lastIndex = -2;
496
497 if (len == 0) {
498 return NO_ERROR;
499 }
500
Nick Kralevichb6b14232015-04-02 09:36:02 -0700501 if (len > INT32_MAX) {
502 // don't accept size_t values which may have come from an
503 // inadvertent conversion from a negative int.
504 return BAD_VALUE;
505 }
506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 // range checks against the source parcel size
508 if ((offset > parcel->mDataSize)
509 || (len > parcel->mDataSize)
510 || (offset + len > parcel->mDataSize)) {
511 return BAD_VALUE;
512 }
513
514 // Count objects in range
515 for (int i = 0; i < (int) size; i++) {
516 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700517 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 if (firstIndex == -1) {
519 firstIndex = i;
520 }
521 lastIndex = i;
522 }
523 }
524 int numObjects = lastIndex - firstIndex + 1;
525
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700526 if ((mDataSize+len) > mDataCapacity) {
527 // grow data
528 err = growData(len);
529 if (err != NO_ERROR) {
530 return err;
531 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532 }
533
534 // append data
535 memcpy(mData + mDataPos, data + offset, len);
536 mDataPos += len;
537 mDataSize += len;
538
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400539 err = NO_ERROR;
540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 if (numObjects > 0) {
542 // grow objects
543 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700544 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
545 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800546 binder_size_t *objects =
547 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
548 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549 return NO_MEMORY;
550 }
551 mObjects = objects;
552 mObjectsCapacity = newSize;
553 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700554
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 // append and acquire objects
556 int idx = mObjectsSize;
557 for (int i = firstIndex; i <= lastIndex; i++) {
558 size_t off = objects[i] - offset + startPos;
559 mObjects[idx++] = off;
560 mObjectsSize++;
561
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700562 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700563 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700564 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700566 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700567 // If this is a file descriptor, we need to dup it so the
568 // new Parcel now owns its own fd, and can declare that we
569 // officially know we have fds.
570 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800571 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400573 if (!mAllowFds) {
574 err = FDS_NOT_ALLOWED;
575 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700576 }
577 }
578 }
579
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400580 return err;
581}
582
Jeff Brown13b16042014-11-11 16:44:25 -0800583bool Parcel::allowFds() const
584{
585 return mAllowFds;
586}
587
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700588bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400589{
590 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700591 if (!allowFds) {
592 mAllowFds = false;
593 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400594 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595}
596
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700597void Parcel::restoreAllowFds(bool lastValue)
598{
599 mAllowFds = lastValue;
600}
601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700602bool Parcel::hasFileDescriptors() const
603{
604 if (!mFdsKnown) {
605 scanForFds();
606 }
607 return mHasFds;
608}
609
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700610// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700611status_t Parcel::writeInterfaceToken(const String16& interface)
612{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700613 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
614 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615 // currently the interface identification token is just its name as a string
616 return writeString16(interface);
617}
618
Mathias Agopian83c04462009-05-22 19:00:22 -0700619bool Parcel::checkInterface(IBinder* binder) const
620{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700621 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700622}
623
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700624bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700625 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700627 int32_t strictPolicy = readInt32();
628 if (threadState == NULL) {
629 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700630 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700631 if ((threadState->getLastTransactionBinderFlags() &
632 IBinder::FLAG_ONEWAY) != 0) {
633 // For one-way calls, the callee is running entirely
634 // disconnected from the caller, so disable StrictMode entirely.
635 // Not only does disk/network usage not impact the caller, but
636 // there's no way to commuicate back any violations anyway.
637 threadState->setStrictModePolicy(0);
638 } else {
639 threadState->setStrictModePolicy(strictPolicy);
640 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700641 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642 if (str == interface) {
643 return true;
644 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700645 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646 String8(interface).string(), String8(str).string());
647 return false;
648 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700649}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700650
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800651const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652{
653 return mObjects;
654}
655
656size_t Parcel::objectsCount() const
657{
658 return mObjectsSize;
659}
660
661status_t Parcel::errorCheck() const
662{
663 return mError;
664}
665
666void Parcel::setError(status_t err)
667{
668 mError = err;
669}
670
671status_t Parcel::finishWrite(size_t len)
672{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700673 if (len > INT32_MAX) {
674 // don't accept size_t values which may have come from an
675 // inadvertent conversion from a negative int.
676 return BAD_VALUE;
677 }
678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679 //printf("Finish write of %d\n", len);
680 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700681 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 if (mDataPos > mDataSize) {
683 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700684 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700685 }
686 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
687 return NO_ERROR;
688}
689
690status_t Parcel::writeUnpadded(const void* data, size_t len)
691{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700692 if (len > INT32_MAX) {
693 // don't accept size_t values which may have come from an
694 // inadvertent conversion from a negative int.
695 return BAD_VALUE;
696 }
697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 size_t end = mDataPos + len;
699 if (end < mDataPos) {
700 // integer overflow
701 return BAD_VALUE;
702 }
703
704 if (end <= mDataCapacity) {
705restart_write:
706 memcpy(mData+mDataPos, data, len);
707 return finishWrite(len);
708 }
709
710 status_t err = growData(len);
711 if (err == NO_ERROR) goto restart_write;
712 return err;
713}
714
715status_t Parcel::write(const void* data, size_t len)
716{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700717 if (len > INT32_MAX) {
718 // don't accept size_t values which may have come from an
719 // inadvertent conversion from a negative int.
720 return BAD_VALUE;
721 }
722
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700723 void* const d = writeInplace(len);
724 if (d) {
725 memcpy(d, data, len);
726 return NO_ERROR;
727 }
728 return mError;
729}
730
731void* Parcel::writeInplace(size_t len)
732{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700733 if (len > INT32_MAX) {
734 // don't accept size_t values which may have come from an
735 // inadvertent conversion from a negative int.
736 return NULL;
737 }
738
739 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740
741 // sanity check for integer overflow
742 if (mDataPos+padded < mDataPos) {
743 return NULL;
744 }
745
746 if ((mDataPos+padded) <= mDataCapacity) {
747restart_write:
748 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
749 uint8_t* const data = mData+mDataPos;
750
751 // Need to pad at end?
752 if (padded != len) {
753#if BYTE_ORDER == BIG_ENDIAN
754 static const uint32_t mask[4] = {
755 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
756 };
757#endif
758#if BYTE_ORDER == LITTLE_ENDIAN
759 static const uint32_t mask[4] = {
760 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
761 };
762#endif
763 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
764 // *reinterpret_cast<void**>(data+padded-4));
765 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
766 }
767
768 finishWrite(padded);
769 return data;
770 }
771
772 status_t err = growData(padded);
773 if (err == NO_ERROR) goto restart_write;
774 return NULL;
775}
776
Casey Dahlin32d29a52015-11-13 13:46:29 -0800777namespace {
778
779template<typename T, typename U>
780status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p,
781 status_t(Parcel::*write_func)(U)) {
782 if (val.size() > std::numeric_limits<int32_t>::max()) {
783 return BAD_VALUE;
784 }
785
786 status_t status = p->writeInt32(val.size());
787
788 if (status != OK) {
789 return status;
790 }
791
792 for (const auto& item : val) {
793 status = (p->*write_func)(item);
794
795 if (status != OK) {
796 return status;
797 }
798 }
799
800 return OK;
801}
802
803template<typename T>
804status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
805 status_t(Parcel::*write_func)(const T&)) {
806 return unsafeWriteTypedVector(val, p, write_func);
807}
808
809template<typename T>
810status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
811 status_t(Parcel::*write_func)(T)) {
812 return unsafeWriteTypedVector(val, p, write_func);
813}
814
815} // namespace
816
Casey Dahlin08bb1452015-10-19 18:12:18 -0700817status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
818{
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700819 status_t status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700820 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700821 status = BAD_VALUE;
822 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700823 }
824
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700825 status = writeInt32(val.size());
Casey Dahlin08bb1452015-10-19 18:12:18 -0700826 if (status != OK) {
827 return status;
828 }
829
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700830 void* data = writeInplace(val.size());
831 if (!data) {
832 status = BAD_VALUE;
833 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700834 }
835
Christopher Wiley714a3ab2015-10-31 13:22:15 -0700836 memcpy(data, val.data(), val.size());
837 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -0700838}
839
840status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
841{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800842 return writeTypedVector(val, this, &Parcel::writeInt32);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700843}
844
845status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
846{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800847 return writeTypedVector(val, this, &Parcel::writeInt64);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700848}
849
850status_t Parcel::writeFloatVector(const std::vector<float>& val)
851{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800852 return writeTypedVector(val, this, &Parcel::writeFloat);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700853}
854
855status_t Parcel::writeDoubleVector(const std::vector<double>& val)
856{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800857 return writeTypedVector(val, this, &Parcel::writeDouble);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700858}
859
860status_t Parcel::writeBoolVector(const std::vector<bool>& val)
861{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800862 return writeTypedVector(val, this, &Parcel::writeBool);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700863}
864
865status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
866{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800867 return writeTypedVector(val, this, &Parcel::writeChar);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700868}
869
870status_t Parcel::writeString16Vector(const std::vector<String16>& val)
871{
Casey Dahlin32d29a52015-11-13 13:46:29 -0800872 return writeTypedVector(val, this, &Parcel::writeString16);
Casey Dahlin08bb1452015-10-19 18:12:18 -0700873}
874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875status_t Parcel::writeInt32(int32_t val)
876{
Andreas Huber84a6d042009-08-17 13:33:27 -0700877 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800879
880status_t Parcel::writeUint32(uint32_t val)
881{
882 return writeAligned(val);
883}
884
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700885status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700886 if (len > INT32_MAX) {
887 // don't accept size_t values which may have come from an
888 // inadvertent conversion from a negative int.
889 return BAD_VALUE;
890 }
891
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700892 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700893 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700894 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700895 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700896 if (ret == NO_ERROR) {
897 ret = write(val, len * sizeof(*val));
898 }
899 return ret;
900}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700901status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700902 if (len > INT32_MAX) {
903 // don't accept size_t values which may have come from an
904 // inadvertent conversion from a negative int.
905 return BAD_VALUE;
906 }
907
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700908 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700909 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700910 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700911 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700912 if (ret == NO_ERROR) {
913 ret = write(val, len * sizeof(*val));
914 }
915 return ret;
916}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700917
Casey Dahlina3774912015-10-15 15:44:59 -0700918status_t Parcel::writeBool(bool val)
919{
920 return writeInt32(int32_t(val));
921}
922
923status_t Parcel::writeChar(char16_t val)
924{
925 return writeInt32(int32_t(val));
926}
927
928status_t Parcel::writeByte(int8_t val)
929{
930 return writeInt32(int32_t(val));
931}
932
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933status_t Parcel::writeInt64(int64_t val)
934{
Andreas Huber84a6d042009-08-17 13:33:27 -0700935 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700936}
937
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700938status_t Parcel::writeUint64(uint64_t val)
939{
940 return writeAligned(val);
941}
942
Serban Constantinescuf683e012013-11-05 16:53:55 +0000943status_t Parcel::writePointer(uintptr_t val)
944{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800945 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000946}
947
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700948status_t Parcel::writeFloat(float val)
949{
Andreas Huber84a6d042009-08-17 13:33:27 -0700950 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951}
952
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800953#if defined(__mips__) && defined(__mips_hard_float)
954
955status_t Parcel::writeDouble(double val)
956{
957 union {
958 double d;
959 unsigned long long ll;
960 } u;
961 u.d = val;
962 return writeAligned(u.ll);
963}
964
965#else
966
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967status_t Parcel::writeDouble(double val)
968{
Andreas Huber84a6d042009-08-17 13:33:27 -0700969 return writeAligned(val);
970}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800972#endif
973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700974status_t Parcel::writeCString(const char* str)
975{
976 return write(str, strlen(str)+1);
977}
978
979status_t Parcel::writeString8(const String8& str)
980{
981 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100982 // only write string if its length is more than zero characters,
983 // as readString8 will only read if the length field is non-zero.
984 // this is slightly different from how writeString16 works.
985 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986 err = write(str.string(), str.bytes()+1);
987 }
988 return err;
989}
990
991status_t Parcel::writeString16(const String16& str)
992{
993 return writeString16(str.string(), str.size());
994}
995
996status_t Parcel::writeString16(const char16_t* str, size_t len)
997{
998 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700999
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000 status_t err = writeInt32(len);
1001 if (err == NO_ERROR) {
1002 len *= sizeof(char16_t);
1003 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1004 if (data) {
1005 memcpy(data, str, len);
1006 *reinterpret_cast<char16_t*>(data+len) = 0;
1007 return NO_ERROR;
1008 }
1009 err = mError;
1010 }
1011 return err;
1012}
1013
1014status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1015{
1016 return flatten_binder(ProcessState::self(), val, this);
1017}
1018
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001019status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1020{
Casey Dahlin32d29a52015-11-13 13:46:29 -08001021 return writeTypedVector(val, this, &Parcel::writeStrongBinder);
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001022}
1023
1024status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001025 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlin3e4b6d22015-11-03 13:50:37 -08001026}
1027
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1029{
1030 return flatten_binder(ProcessState::self(), val, this);
1031}
1032
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001033status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001034{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001035 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001036 return BAD_TYPE;
1037
1038 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001039 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001040 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001042 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001043 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001045 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1046 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047
1048 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001049 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 return err;
1051 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001052 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001053 return err;
1054}
1055
Jeff Brown93ff1f92011-11-04 19:01:44 -07001056status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057{
1058 flat_binder_object obj;
1059 obj.type = BINDER_TYPE_FD;
1060 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001061 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001063 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064 return writeObject(obj, true);
1065}
1066
1067status_t Parcel::writeDupFileDescriptor(int fd)
1068{
Jeff Brownd341c712011-11-04 20:19:33 -07001069 int dupFd = dup(fd);
1070 if (dupFd < 0) {
1071 return -errno;
1072 }
1073 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1074 if (err) {
1075 close(dupFd);
1076 }
1077 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001078}
1079
Jeff Brown13b16042014-11-11 16:44:25 -08001080status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001081{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001082 if (len > INT32_MAX) {
1083 // don't accept size_t values which may have come from an
1084 // inadvertent conversion from a negative int.
1085 return BAD_VALUE;
1086 }
1087
Jeff Brown13b16042014-11-11 16:44:25 -08001088 status_t status;
1089 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001090 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001091 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001092 if (status) return status;
1093
1094 void* ptr = writeInplace(len);
1095 if (!ptr) return NO_MEMORY;
1096
Jeff Brown13b16042014-11-11 16:44:25 -08001097 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001098 return NO_ERROR;
1099 }
1100
Steve Block6807e592011-10-20 11:56:00 +01001101 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001102 int fd = ashmem_create_region("Parcel Blob", len);
1103 if (fd < 0) return NO_MEMORY;
1104
1105 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1106 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001107 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001108 } else {
1109 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1110 if (ptr == MAP_FAILED) {
1111 status = -errno;
1112 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001113 if (!mutableCopy) {
1114 result = ashmem_set_prot_region(fd, PROT_READ);
1115 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001116 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001117 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001118 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001119 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001120 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001121 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001122 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001123 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001124 return NO_ERROR;
1125 }
1126 }
1127 }
1128 }
1129 ::munmap(ptr, len);
1130 }
1131 ::close(fd);
1132 return status;
1133}
1134
Jeff Brown13b16042014-11-11 16:44:25 -08001135status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1136{
1137 // Must match up with what's done in writeBlob.
1138 if (!mAllowFds) return FDS_NOT_ALLOWED;
1139 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1140 if (status) return status;
1141 return writeDupFileDescriptor(fd);
1142}
1143
Mathias Agopiane1424282013-07-29 21:24:40 -07001144status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001145{
1146 status_t err;
1147
1148 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001149 const size_t len = val.getFlattenedSize();
1150 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001151
Nick Kralevichb6b14232015-04-02 09:36:02 -07001152 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1153 // don't accept size_t values which may have come from an
1154 // inadvertent conversion from a negative int.
1155 return BAD_VALUE;
1156 }
1157
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001158 err = this->writeInt32(len);
1159 if (err) return err;
1160
1161 err = this->writeInt32(fd_count);
1162 if (err) return err;
1163
1164 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001165 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001166 if (buf == NULL)
1167 return BAD_VALUE;
1168
1169 int* fds = NULL;
1170 if (fd_count) {
1171 fds = new int[fd_count];
1172 }
1173
1174 err = val.flatten(buf, len, fds, fd_count);
1175 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1176 err = this->writeDupFileDescriptor( fds[i] );
1177 }
1178
1179 if (fd_count) {
1180 delete [] fds;
1181 }
1182
1183 return err;
1184}
1185
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001186status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1187{
1188 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1189 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1190 if (enoughData && enoughObjects) {
1191restart_write:
1192 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001193
Christopher Tate98e67d32015-06-03 18:44:15 -07001194 // remember if it's a file descriptor
1195 if (val.type == BINDER_TYPE_FD) {
1196 if (!mAllowFds) {
1197 // fail before modifying our object index
1198 return FDS_NOT_ALLOWED;
1199 }
1200 mHasFds = mFdsKnown = true;
1201 }
1202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001203 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001204 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001205 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001206 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001207 mObjectsSize++;
1208 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001209
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001210 return finishWrite(sizeof(flat_binder_object));
1211 }
1212
1213 if (!enoughData) {
1214 const status_t err = growData(sizeof(val));
1215 if (err != NO_ERROR) return err;
1216 }
1217 if (!enoughObjects) {
1218 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001219 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001220 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001221 if (objects == NULL) return NO_MEMORY;
1222 mObjects = objects;
1223 mObjectsCapacity = newSize;
1224 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001225
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001226 goto restart_write;
1227}
1228
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001229status_t Parcel::writeNoException()
1230{
Christopher Wiley6e361342015-11-09 15:06:15 -08001231 binder::Status status;
1232 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001233}
1234
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001235void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001236{
1237 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1238}
1239
1240status_t Parcel::read(void* outData, size_t len) const
1241{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001242 if (len > INT32_MAX) {
1243 // don't accept size_t values which may have come from an
1244 // inadvertent conversion from a negative int.
1245 return BAD_VALUE;
1246 }
1247
1248 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1249 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001250 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001251 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001252 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 return NO_ERROR;
1254 }
1255 return NOT_ENOUGH_DATA;
1256}
1257
1258const void* Parcel::readInplace(size_t len) const
1259{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001260 if (len > INT32_MAX) {
1261 // don't accept size_t values which may have come from an
1262 // inadvertent conversion from a negative int.
1263 return NULL;
1264 }
1265
1266 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1267 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001268 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001269 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001270 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 return data;
1272 }
1273 return NULL;
1274}
1275
Andreas Huber84a6d042009-08-17 13:33:27 -07001276template<class T>
1277status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001278 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001279
1280 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001282 mDataPos += sizeof(T);
1283 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001284 return NO_ERROR;
1285 } else {
1286 return NOT_ENOUGH_DATA;
1287 }
1288}
1289
Andreas Huber84a6d042009-08-17 13:33:27 -07001290template<class T>
1291T Parcel::readAligned() const {
1292 T result;
1293 if (readAligned(&result) != NO_ERROR) {
1294 result = 0;
1295 }
1296
1297 return result;
1298}
1299
1300template<class T>
1301status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001302 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001303
1304 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1305restart_write:
1306 *reinterpret_cast<T*>(mData+mDataPos) = val;
1307 return finishWrite(sizeof(val));
1308 }
1309
1310 status_t err = growData(sizeof(val));
1311 if (err == NO_ERROR) goto restart_write;
1312 return err;
1313}
1314
Casey Dahlin08bb1452015-10-19 18:12:18 -07001315status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1316 val->clear();
1317
1318 int32_t size;
1319 status_t status = readInt32(&size);
1320
1321 if (status != OK) {
1322 return status;
1323 }
1324
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001325 if (size < 0) {
1326 status = UNEXPECTED_NULL;
1327 return status;
1328 }
1329 if (size_t(size) > dataAvail()) {
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001330 status = BAD_VALUE;
1331 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001332 }
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001333
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001334 const void* data = readInplace(size);
1335 if (!data) {
1336 status = BAD_VALUE;
1337 return status;
1338 }
Casey Dahlin08bb1452015-10-19 18:12:18 -07001339 val->resize(size);
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001340 memcpy(val->data(), data, size);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001341
Christopher Wiley714a3ab2015-10-31 13:22:15 -07001342 return status;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001343}
1344
1345status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001346 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001347}
1348
1349status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001350 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001351}
1352
1353status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001354 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001355}
1356
1357status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001358 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001359}
1360
1361status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1362 val->clear();
1363
1364 int32_t size;
1365 status_t status = readInt32(&size);
1366
1367 if (status != OK) {
1368 return status;
1369 }
1370
1371 if (size < 0) {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001372 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001373 }
1374
1375 val->resize(size);
1376
1377 /* C++ bool handling means a vector of bools isn't necessarily addressable
1378 * (we might use individual bits)
1379 */
Christopher Wiley489931b2015-10-27 16:33:47 -07001380 bool data;
1381 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin08bb1452015-10-19 18:12:18 -07001382 status = readBool(&data);
1383 (*val)[i] = data;
1384
1385 if (status != OK) {
1386 return status;
1387 }
1388 }
1389
1390 return OK;
1391}
1392
1393status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001394 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001395}
1396
1397status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001398 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001399}
1400
1401
Andreas Huber84a6d042009-08-17 13:33:27 -07001402status_t Parcel::readInt32(int32_t *pArg) const
1403{
1404 return readAligned(pArg);
1405}
1406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407int32_t Parcel::readInt32() const
1408{
Andreas Huber84a6d042009-08-17 13:33:27 -07001409 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410}
1411
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001412status_t Parcel::readUint32(uint32_t *pArg) const
1413{
1414 return readAligned(pArg);
1415}
1416
1417uint32_t Parcel::readUint32() const
1418{
1419 return readAligned<uint32_t>();
1420}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421
1422status_t Parcel::readInt64(int64_t *pArg) const
1423{
Andreas Huber84a6d042009-08-17 13:33:27 -07001424 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001425}
1426
1427
1428int64_t Parcel::readInt64() const
1429{
Andreas Huber84a6d042009-08-17 13:33:27 -07001430 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431}
1432
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001433status_t Parcel::readUint64(uint64_t *pArg) const
1434{
1435 return readAligned(pArg);
1436}
1437
1438uint64_t Parcel::readUint64() const
1439{
1440 return readAligned<uint64_t>();
1441}
1442
Serban Constantinescuf683e012013-11-05 16:53:55 +00001443status_t Parcel::readPointer(uintptr_t *pArg) const
1444{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001445 status_t ret;
1446 binder_uintptr_t ptr;
1447 ret = readAligned(&ptr);
1448 if (!ret)
1449 *pArg = ptr;
1450 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001451}
1452
1453uintptr_t Parcel::readPointer() const
1454{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001455 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001456}
1457
1458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001459status_t Parcel::readFloat(float *pArg) const
1460{
Andreas Huber84a6d042009-08-17 13:33:27 -07001461 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001462}
1463
1464
1465float Parcel::readFloat() const
1466{
Andreas Huber84a6d042009-08-17 13:33:27 -07001467 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468}
1469
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001470#if defined(__mips__) && defined(__mips_hard_float)
1471
1472status_t Parcel::readDouble(double *pArg) const
1473{
1474 union {
1475 double d;
1476 unsigned long long ll;
1477 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001478 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001479 status_t status;
1480 status = readAligned(&u.ll);
1481 *pArg = u.d;
1482 return status;
1483}
1484
1485double Parcel::readDouble() const
1486{
1487 union {
1488 double d;
1489 unsigned long long ll;
1490 } u;
1491 u.ll = readAligned<unsigned long long>();
1492 return u.d;
1493}
1494
1495#else
1496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497status_t Parcel::readDouble(double *pArg) const
1498{
Andreas Huber84a6d042009-08-17 13:33:27 -07001499 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500}
1501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502double Parcel::readDouble() const
1503{
Andreas Huber84a6d042009-08-17 13:33:27 -07001504 return readAligned<double>();
1505}
1506
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001507#endif
1508
Andreas Huber84a6d042009-08-17 13:33:27 -07001509status_t Parcel::readIntPtr(intptr_t *pArg) const
1510{
1511 return readAligned(pArg);
1512}
1513
1514
1515intptr_t Parcel::readIntPtr() const
1516{
1517 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518}
1519
Casey Dahlina3774912015-10-15 15:44:59 -07001520status_t Parcel::readBool(bool *pArg) const
1521{
1522 int32_t tmp;
1523 status_t ret = readInt32(&tmp);
1524 *pArg = (tmp != 0);
1525 return ret;
1526}
1527
1528bool Parcel::readBool() const
1529{
1530 return readInt32() != 0;
1531}
1532
1533status_t Parcel::readChar(char16_t *pArg) const
1534{
1535 int32_t tmp;
1536 status_t ret = readInt32(&tmp);
1537 *pArg = char16_t(tmp);
1538 return ret;
1539}
1540
1541char16_t Parcel::readChar() const
1542{
1543 return char16_t(readInt32());
1544}
1545
1546status_t Parcel::readByte(int8_t *pArg) const
1547{
1548 int32_t tmp;
1549 status_t ret = readInt32(&tmp);
1550 *pArg = int8_t(tmp);
1551 return ret;
1552}
1553
1554int8_t Parcel::readByte() const
1555{
1556 return int8_t(readInt32());
1557}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001558
1559const char* Parcel::readCString() const
1560{
1561 const size_t avail = mDataSize-mDataPos;
1562 if (avail > 0) {
1563 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1564 // is the string's trailing NUL within the parcel's valid bounds?
1565 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1566 if (eos) {
1567 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001568 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001569 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 return str;
1571 }
1572 }
1573 return NULL;
1574}
1575
1576String8 Parcel::readString8() const
1577{
1578 int32_t size = readInt32();
1579 // watch for potential int overflow adding 1 for trailing NUL
1580 if (size > 0 && size < INT32_MAX) {
1581 const char* str = (const char*)readInplace(size+1);
1582 if (str) return String8(str, size);
1583 }
1584 return String8();
1585}
1586
1587String16 Parcel::readString16() const
1588{
1589 size_t len;
1590 const char16_t* str = readString16Inplace(&len);
1591 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001592 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593 return String16();
1594}
1595
Casey Dahlin08bb1452015-10-19 18:12:18 -07001596status_t Parcel::readString16(String16* pArg) const
1597{
1598 size_t len;
1599 const char16_t* str = readString16Inplace(&len);
1600 if (str) {
Casey Dahlina5bcc952015-10-20 16:26:23 -07001601 pArg->setTo(str, len);
Casey Dahlin08bb1452015-10-19 18:12:18 -07001602 return 0;
1603 } else {
1604 *pArg = String16();
Christopher Wiley6264c4c2015-11-10 09:44:30 -08001605 return UNEXPECTED_NULL;
Casey Dahlin08bb1452015-10-19 18:12:18 -07001606 }
1607}
1608
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1610{
1611 int32_t size = readInt32();
1612 // watch for potential int overflow from size+1
1613 if (size >= 0 && size < INT32_MAX) {
1614 *outLen = size;
1615 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1616 if (str != NULL) {
1617 return str;
1618 }
1619 }
1620 *outLen = 0;
1621 return NULL;
1622}
1623
Casey Dahlin930a5722015-10-27 18:33:56 -07001624status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1625{
1626 return unflatten_binder(ProcessState::self(), *this, val);
1627}
1628
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629sp<IBinder> Parcel::readStrongBinder() const
1630{
1631 sp<IBinder> val;
Casey Dahlin930a5722015-10-27 18:33:56 -07001632 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633 return val;
1634}
1635
1636wp<IBinder> Parcel::readWeakBinder() const
1637{
1638 wp<IBinder> val;
1639 unflatten_binder(ProcessState::self(), *this, &val);
1640 return val;
1641}
1642
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001643int32_t Parcel::readExceptionCode() const
1644{
Christopher Wiley6e361342015-11-09 15:06:15 -08001645 binder::Status status;
1646 status.readFromParcel(*this);
1647 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001648}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001649
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001650native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001651{
1652 int numFds, numInts;
1653 status_t err;
1654 err = readInt32(&numFds);
1655 if (err != NO_ERROR) return 0;
1656 err = readInt32(&numInts);
1657 if (err != NO_ERROR) return 0;
1658
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001659 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001660 if (!h) {
1661 return 0;
1662 }
1663
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001664 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001665 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001666 if (h->data[i] < 0) err = BAD_VALUE;
1667 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001668 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001669 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001670 native_handle_close(h);
1671 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001672 h = 0;
1673 }
1674 return h;
1675}
1676
1677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678int Parcel::readFileDescriptor() const
1679{
1680 const flat_binder_object* flat = readObject(true);
1681 if (flat) {
1682 switch (flat->type) {
1683 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001684 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001685 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001686 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687 }
1688 return BAD_TYPE;
1689}
1690
Jeff Brown5707dbf2011-09-23 21:17:56 -07001691status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1692{
Jeff Brown13b16042014-11-11 16:44:25 -08001693 int32_t blobType;
1694 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001695 if (status) return status;
1696
Jeff Brown13b16042014-11-11 16:44:25 -08001697 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001698 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001699 const void* ptr = readInplace(len);
1700 if (!ptr) return BAD_VALUE;
1701
Jeff Brown13b16042014-11-11 16:44:25 -08001702 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001703 return NO_ERROR;
1704 }
1705
Steve Block6807e592011-10-20 11:56:00 +01001706 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001707 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001708 int fd = readFileDescriptor();
1709 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1710
Jeff Brown13b16042014-11-11 16:44:25 -08001711 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1712 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001713 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001714
Jeff Brown13b16042014-11-11 16:44:25 -08001715 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001716 return NO_ERROR;
1717}
1718
Mathias Agopiane1424282013-07-29 21:24:40 -07001719status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001720{
1721 // size
1722 const size_t len = this->readInt32();
1723 const size_t fd_count = this->readInt32();
1724
Nick Kralevichb6b14232015-04-02 09:36:02 -07001725 if (len > INT32_MAX) {
1726 // don't accept size_t values which may have come from an
1727 // inadvertent conversion from a negative int.
1728 return BAD_VALUE;
1729 }
1730
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001731 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001732 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001733 if (buf == NULL)
1734 return BAD_VALUE;
1735
1736 int* fds = NULL;
1737 if (fd_count) {
1738 fds = new int[fd_count];
1739 }
1740
1741 status_t err = NO_ERROR;
1742 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001743 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001744 if (fds[i] < 0) {
1745 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001746 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1747 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001748 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001749 }
1750
1751 if (err == NO_ERROR) {
1752 err = val.unflatten(buf, len, fds, fd_count);
1753 }
1754
1755 if (fd_count) {
1756 delete [] fds;
1757 }
1758
1759 return err;
1760}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001761const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1762{
1763 const size_t DPOS = mDataPos;
1764 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1765 const flat_binder_object* obj
1766 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1767 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001768 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001769 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770 // the object list, so we don't want to check for it when
1771 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001772 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001773 return obj;
1774 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001775
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001776 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001777 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001778 const size_t N = mObjectsSize;
1779 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001780
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001782 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001784
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785 // Start at the current hint position, looking for an object at
1786 // the current data position.
1787 if (opos < N) {
1788 while (opos < (N-1) && OBJS[opos] < DPOS) {
1789 opos++;
1790 }
1791 } else {
1792 opos = N-1;
1793 }
1794 if (OBJS[opos] == DPOS) {
1795 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001796 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001797 this, DPOS, opos);
1798 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001799 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800 return obj;
1801 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 // Look backwards for it...
1804 while (opos > 0 && OBJS[opos] > DPOS) {
1805 opos--;
1806 }
1807 if (OBJS[opos] == DPOS) {
1808 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001809 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 this, DPOS, opos);
1811 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001812 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813 return obj;
1814 }
1815 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001816 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 -07001817 this, DPOS);
1818 }
1819 return NULL;
1820}
1821
1822void Parcel::closeFileDescriptors()
1823{
1824 size_t i = mObjectsSize;
1825 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001826 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827 }
1828 while (i > 0) {
1829 i--;
1830 const flat_binder_object* flat
1831 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1832 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001833 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834 close(flat->handle);
1835 }
1836 }
1837}
1838
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001839uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001841 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001842}
1843
1844size_t Parcel::ipcDataSize() const
1845{
1846 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1847}
1848
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001849uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001851 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852}
1853
1854size_t Parcel::ipcObjectsCount() const
1855{
1856 return mObjectsSize;
1857}
1858
1859void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001860 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001861{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001862 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863 freeDataNoInit();
1864 mError = NO_ERROR;
1865 mData = const_cast<uint8_t*>(data);
1866 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001867 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001869 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001870 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 mObjectsSize = mObjectsCapacity = objectsCount;
1872 mNextObjectHint = 0;
1873 mOwner = relFunc;
1874 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001875 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001876 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001877 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001878 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001879 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001880 mObjectsSize = 0;
1881 break;
1882 }
1883 minOffset = offset + sizeof(flat_binder_object);
1884 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001885 scanForFds();
1886}
1887
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001888void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889{
1890 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001891
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892 if (errorCheck() != NO_ERROR) {
1893 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001894 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895 } else if (dataSize() > 0) {
1896 const uint8_t* DATA = data();
1897 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001898 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899 const size_t N = objectsCount();
1900 for (size_t i=0; i<N; i++) {
1901 const flat_binder_object* flat
1902 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1903 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1904 << TypeCode(flat->type & 0x7f7f7f00)
1905 << " = " << flat->binder;
1906 }
1907 } else {
1908 to << "NULL";
1909 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001910
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 to << ")";
1912}
1913
1914void Parcel::releaseObjects()
1915{
1916 const sp<ProcessState> proc(ProcessState::self());
1917 size_t i = mObjectsSize;
1918 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001919 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001920 while (i > 0) {
1921 i--;
1922 const flat_binder_object* flat
1923 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001924 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925 }
1926}
1927
1928void Parcel::acquireObjects()
1929{
1930 const sp<ProcessState> proc(ProcessState::self());
1931 size_t i = mObjectsSize;
1932 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001933 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001934 while (i > 0) {
1935 i--;
1936 const flat_binder_object* flat
1937 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001938 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 }
1940}
1941
1942void Parcel::freeData()
1943{
1944 freeDataNoInit();
1945 initState();
1946}
1947
1948void Parcel::freeDataNoInit()
1949{
1950 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001951 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001952 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1954 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001955 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001957 if (mData) {
1958 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001959 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austinf28b2952015-09-10 13:46:02 -07001960 if (mDataCapacity <= gParcelGlobalAllocSize) {
1961 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1962 } else {
1963 gParcelGlobalAllocSize = 0;
1964 }
1965 if (gParcelGlobalAllocCount > 0) {
1966 gParcelGlobalAllocCount--;
1967 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08001968 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001969 free(mData);
1970 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971 if (mObjects) free(mObjects);
1972 }
1973}
1974
1975status_t Parcel::growData(size_t len)
1976{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001977 if (len > INT32_MAX) {
1978 // don't accept size_t values which may have come from an
1979 // inadvertent conversion from a negative int.
1980 return BAD_VALUE;
1981 }
1982
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 size_t newSize = ((mDataSize+len)*3)/2;
1984 return (newSize <= mDataSize)
1985 ? (status_t) NO_MEMORY
1986 : continueWrite(newSize);
1987}
1988
1989status_t Parcel::restartWrite(size_t desired)
1990{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001991 if (desired > INT32_MAX) {
1992 // don't accept size_t values which may have come from an
1993 // inadvertent conversion from a negative int.
1994 return BAD_VALUE;
1995 }
1996
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997 if (mOwner) {
1998 freeData();
1999 return continueWrite(desired);
2000 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002 uint8_t* data = (uint8_t*)realloc(mData, desired);
2003 if (!data && desired > mDataCapacity) {
2004 mError = NO_MEMORY;
2005 return NO_MEMORY;
2006 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002007
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002011 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002012 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002013 gParcelGlobalAllocSize += desired;
2014 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002015 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016 mData = data;
2017 mDataCapacity = desired;
2018 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002021 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2022 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002024 free(mObjects);
2025 mObjects = NULL;
2026 mObjectsSize = mObjectsCapacity = 0;
2027 mNextObjectHint = 0;
2028 mHasFds = false;
2029 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002030 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002032 return NO_ERROR;
2033}
2034
2035status_t Parcel::continueWrite(size_t desired)
2036{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002037 if (desired > INT32_MAX) {
2038 // don't accept size_t values which may have come from an
2039 // inadvertent conversion from a negative int.
2040 return BAD_VALUE;
2041 }
2042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043 // If shrinking, first adjust for any objects that appear
2044 // after the new data size.
2045 size_t objectsSize = mObjectsSize;
2046 if (desired < mDataSize) {
2047 if (desired == 0) {
2048 objectsSize = 0;
2049 } else {
2050 while (objectsSize > 0) {
2051 if (mObjects[objectsSize-1] < desired)
2052 break;
2053 objectsSize--;
2054 }
2055 }
2056 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058 if (mOwner) {
2059 // If the size is going to zero, just release the owner's data.
2060 if (desired == 0) {
2061 freeData();
2062 return NO_ERROR;
2063 }
2064
2065 // If there is a different owner, we need to take
2066 // posession.
2067 uint8_t* data = (uint8_t*)malloc(desired);
2068 if (!data) {
2069 mError = NO_MEMORY;
2070 return NO_MEMORY;
2071 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002072 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002073
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002075 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002077 free(data);
2078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 mError = NO_MEMORY;
2080 return NO_MEMORY;
2081 }
2082
2083 // Little hack to only acquire references on objects
2084 // we will be keeping.
2085 size_t oldObjectsSize = mObjectsSize;
2086 mObjectsSize = objectsSize;
2087 acquireObjects();
2088 mObjectsSize = oldObjectsSize;
2089 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002090
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091 if (mData) {
2092 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2093 }
2094 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002095 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002096 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002097 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2099 mOwner = NULL;
2100
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002101 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002102 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002103 gParcelGlobalAllocSize += desired;
2104 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002105 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002106
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 mData = data;
2108 mObjects = objects;
2109 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002110 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 mDataCapacity = desired;
2112 mObjectsSize = mObjectsCapacity = objectsSize;
2113 mNextObjectHint = 0;
2114
2115 } else if (mData) {
2116 if (objectsSize < mObjectsSize) {
2117 // Need to release refs on any objects we are dropping.
2118 const sp<ProcessState> proc(ProcessState::self());
2119 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2120 const flat_binder_object* flat
2121 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2122 if (flat->type == BINDER_TYPE_FD) {
2123 // will need to rescan because we may have lopped off the only FDs
2124 mFdsKnown = false;
2125 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002126 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002128 binder_size_t* objects =
2129 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130 if (objects) {
2131 mObjects = objects;
2132 }
2133 mObjectsSize = objectsSize;
2134 mNextObjectHint = 0;
2135 }
2136
2137 // We own the data, so we can just do a realloc().
2138 if (desired > mDataCapacity) {
2139 uint8_t* data = (uint8_t*)realloc(mData, desired);
2140 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002141 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2142 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002143 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002144 gParcelGlobalAllocSize += desired;
2145 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austinf28b2952015-09-10 13:46:02 -07002146 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002147 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 mData = data;
2149 mDataCapacity = desired;
2150 } else if (desired > mDataCapacity) {
2151 mError = NO_MEMORY;
2152 return NO_MEMORY;
2153 }
2154 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002155 if (mDataSize > desired) {
2156 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002157 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002158 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159 if (mDataPos > desired) {
2160 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002161 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 }
2163 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 } else {
2166 // This is the first data. Easy!
2167 uint8_t* data = (uint8_t*)malloc(desired);
2168 if (!data) {
2169 mError = NO_MEMORY;
2170 return NO_MEMORY;
2171 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002172
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 if(!(mDataCapacity == 0 && mObjects == NULL
2174 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002175 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002177
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002178 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002179 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002180 gParcelGlobalAllocSize += desired;
2181 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002182 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002183
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 mData = data;
2185 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2187 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002188 mDataCapacity = desired;
2189 }
2190
2191 return NO_ERROR;
2192}
2193
2194void Parcel::initState()
2195{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002196 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197 mError = NO_ERROR;
2198 mData = 0;
2199 mDataSize = 0;
2200 mDataCapacity = 0;
2201 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002202 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2203 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 mObjects = NULL;
2205 mObjectsSize = 0;
2206 mObjectsCapacity = 0;
2207 mNextObjectHint = 0;
2208 mHasFds = false;
2209 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002210 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002212 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213}
2214
2215void Parcel::scanForFds() const
2216{
2217 bool hasFds = false;
2218 for (size_t i=0; i<mObjectsSize; i++) {
2219 const flat_binder_object* flat
2220 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2221 if (flat->type == BINDER_TYPE_FD) {
2222 hasFds = true;
2223 break;
2224 }
2225 }
2226 mHasFds = hasFds;
2227 mFdsKnown = true;
2228}
2229
Dan Sandleraa5c2342015-04-10 10:08:45 -04002230size_t Parcel::getBlobAshmemSize() const
2231{
Adrian Roos6bb31142015-10-22 16:46:12 -07002232 // This used to return the size of all blobs that were written to ashmem, now we're returning
2233 // the ashmem currently referenced by this Parcel, which should be equivalent.
2234 // TODO: Remove method once ABI can be changed.
2235 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002236}
2237
Adrian Rooscbf37262015-10-22 16:12:53 -07002238size_t Parcel::getOpenAshmemSize() const
2239{
2240 return mOpenAshmemSize;
2241}
2242
Jeff Brown5707dbf2011-09-23 21:17:56 -07002243// --- Parcel::Blob ---
2244
2245Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002246 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002247}
2248
2249Parcel::Blob::~Blob() {
2250 release();
2251}
2252
2253void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002254 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002255 ::munmap(mData, mSize);
2256 }
2257 clear();
2258}
2259
Jeff Brown13b16042014-11-11 16:44:25 -08002260void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2261 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002262 mData = data;
2263 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002264 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002265}
2266
2267void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002268 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002269 mData = NULL;
2270 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002271 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002272}
2273
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274}; // namespace android