blob: 6893e3915f8cde7bb5bde6c22f41ff600d46569c [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
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
21#include <inttypes.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080026#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070029
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/Binder.h>
31#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080032#include <binder/IPCThreadState.h>
33#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080035#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070036#include <binder/TextOutput.h>
37
Mark Salyzynabed7f72016-01-27 08:02:48 -080038#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080040#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043#include <utils/String8.h>
44#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
Mathias Agopian208059f2009-05-18 15:08:03 -070046#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080047#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049#ifndef INT32_MAX
50#define INT32_MAX ((int32_t)(2147483647))
51#endif
52
53#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
66 if (s > (SIZE_T_MAX - 3)) {
67 abort();
68 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080073#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborna4cff882014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100{
101 switch (obj.type) {
102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return;
112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
114 if (b != NULL) {
115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
120 case BINDER_TYPE_WEAK_HANDLE: {
121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
122 if (b != NULL) b.get_refs()->incWeak(who);
123 return;
124 }
125 case BINDER_TYPE_FD: {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800126 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
127 struct stat st;
128 int ret = fstat(obj.handle, &st);
129 if (!ret && S_ISCHR(st.st_mode)) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700130 // If we own an ashmem fd, keep track of how much memory it refers to.
131 int size = ashmem_get_size_region(obj.handle);
132 if (size > 0) {
133 *outAshmemSize += size;
134 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 }
136 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 return;
138 }
139 }
140
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800141 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142}
143
Adrian Roos6bb31142015-10-22 16:46:12 -0700144void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 const flat_binder_object& obj, const void* who)
146{
Adrian Roos6bb31142015-10-22 16:46:12 -0700147 acquire_object(proc, obj, who, NULL);
148}
149
150static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700151 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152{
153 switch (obj.type) {
154 case BINDER_TYPE_BINDER:
155 if (obj.binder) {
156 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800157 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700158 }
159 return;
160 case BINDER_TYPE_WEAK_BINDER:
161 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800162 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 return;
164 case BINDER_TYPE_HANDLE: {
165 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
166 if (b != NULL) {
167 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
168 b->decStrong(who);
169 }
170 return;
171 }
172 case BINDER_TYPE_WEAK_HANDLE: {
173 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
174 if (b != NULL) b.get_refs()->decWeak(who);
175 return;
176 }
177 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800178 if (obj.cookie != 0) { // owned
179 if (outAshmemSize != NULL) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800180 struct stat st;
181 int ret = fstat(obj.handle, &st);
182 if (!ret && S_ISCHR(st.st_mode)) {
183 int size = ashmem_get_size_region(obj.handle);
184 if (size > 0) {
185 *outAshmemSize -= size;
186 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700187 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700188 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800189
190 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700191 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 return;
193 }
194 }
195
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800196 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197}
198
Adrian Roos6bb31142015-10-22 16:46:12 -0700199void release_object(const sp<ProcessState>& proc,
200 const flat_binder_object& obj, const void* who)
201{
202 release_object(proc, obj, who, NULL);
203}
204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800206 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207{
208 return out->writeObject(flat, false);
209}
210
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800211status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 const sp<IBinder>& binder, Parcel* out)
213{
214 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 if (binder != NULL) {
218 IBinder *local = binder->localBinder();
219 if (!local) {
220 BpBinder *proxy = binder->remoteBinder();
221 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
225 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
230 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
232 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
234 } else {
235 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = 0;
237 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 return finish_flatten_binder(binder, obj, out);
241}
242
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800243status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 const wp<IBinder>& binder, Parcel* out)
245{
246 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
249 if (binder != NULL) {
250 sp<IBinder> real = binder.promote();
251 if (real != NULL) {
252 IBinder *local = real->localBinder();
253 if (!local) {
254 BpBinder *proxy = real->remoteBinder();
255 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000256 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 const int32_t handle = proxy ? proxy->handle() : 0;
259 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800260 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 } else {
264 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
266 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 }
268 return finish_flatten_binder(real, obj, out);
269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 // XXX How to deal? In order to flatten the given binder,
272 // we need to probe it for information, which requires a primary
273 // reference... but we don't have one.
274 //
275 // The OpenBinder implementation uses a dynamic_cast<> here,
276 // but we can't do that with the different reference counting
277 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000278 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 } else {
285 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800286 obj.binder = 0;
287 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700288 return finish_flatten_binder(NULL, obj, out);
289 }
290}
291
292inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800293 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
294 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295{
296 return NO_ERROR;
297}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299status_t unflatten_binder(const sp<ProcessState>& proc,
300 const Parcel& in, sp<IBinder>* out)
301{
302 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 if (flat) {
305 switch (flat->type) {
306 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800307 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308 return finish_unflatten_binder(NULL, *flat, in);
309 case BINDER_TYPE_HANDLE:
310 *out = proc->getStrongProxyForHandle(flat->handle);
311 return finish_unflatten_binder(
312 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 }
315 return BAD_TYPE;
316}
317
318status_t unflatten_binder(const sp<ProcessState>& proc,
319 const Parcel& in, wp<IBinder>* out)
320{
321 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 if (flat) {
324 switch (flat->type) {
325 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800326 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 return finish_unflatten_binder(NULL, *flat, in);
328 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 reinterpret_cast<IBinder*>(flat->cookie),
332 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 } else {
334 *out = NULL;
335 }
336 return finish_unflatten_binder(NULL, *flat, in);
337 case BINDER_TYPE_HANDLE:
338 case BINDER_TYPE_WEAK_HANDLE:
339 *out = proc->getWeakProxyForHandle(flat->handle);
340 return finish_unflatten_binder(
341 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
342 }
343 }
344 return BAD_TYPE;
345}
346
347// ---------------------------------------------------------------------------
348
349Parcel::Parcel()
350{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800351 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 initState();
353}
354
355Parcel::~Parcel()
356{
357 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800358 LOG_ALLOC("Parcel %p: destroyed", this);
359}
360
361size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800362 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
363 size_t size = gParcelGlobalAllocSize;
364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
365 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800366}
367
368size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800369 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
370 size_t count = gParcelGlobalAllocCount;
371 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
372 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373}
374
375const uint8_t* Parcel::data() const
376{
377 return mData;
378}
379
380size_t Parcel::dataSize() const
381{
382 return (mDataSize > mDataPos ? mDataSize : mDataPos);
383}
384
385size_t Parcel::dataAvail() const
386{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700387 size_t result = dataSize() - dataPosition();
388 if (result > INT32_MAX) {
389 abort();
390 }
391 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392}
393
394size_t Parcel::dataPosition() const
395{
396 return mDataPos;
397}
398
399size_t Parcel::dataCapacity() const
400{
401 return mDataCapacity;
402}
403
404status_t Parcel::setDataSize(size_t size)
405{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700406 if (size > INT32_MAX) {
407 // don't accept size_t values which may have come from an
408 // inadvertent conversion from a negative int.
409 return BAD_VALUE;
410 }
411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 status_t err;
413 err = continueWrite(size);
414 if (err == NO_ERROR) {
415 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700416 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 }
418 return err;
419}
420
421void Parcel::setDataPosition(size_t pos) const
422{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700423 if (pos > INT32_MAX) {
424 // don't accept size_t values which may have come from an
425 // inadvertent conversion from a negative int.
426 abort();
427 }
428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 mDataPos = pos;
430 mNextObjectHint = 0;
431}
432
433status_t Parcel::setDataCapacity(size_t size)
434{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700435 if (size > INT32_MAX) {
436 // don't accept size_t values which may have come from an
437 // inadvertent conversion from a negative int.
438 return BAD_VALUE;
439 }
440
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700441 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 return NO_ERROR;
443}
444
445status_t Parcel::setData(const uint8_t* buffer, size_t len)
446{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700447 if (len > INT32_MAX) {
448 // don't accept size_t values which may have come from an
449 // inadvertent conversion from a negative int.
450 return BAD_VALUE;
451 }
452
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 status_t err = restartWrite(len);
454 if (err == NO_ERROR) {
455 memcpy(const_cast<uint8_t*>(data()), buffer, len);
456 mDataSize = len;
457 mFdsKnown = false;
458 }
459 return err;
460}
461
Andreas Huber51faf462011-04-13 10:21:56 -0700462status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463{
464 const sp<ProcessState> proc(ProcessState::self());
465 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700466 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800467 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 size_t size = parcel->mObjectsSize;
469 int startPos = mDataPos;
470 int firstIndex = -1, lastIndex = -2;
471
472 if (len == 0) {
473 return NO_ERROR;
474 }
475
Nick Kralevichb6b14232015-04-02 09:36:02 -0700476 if (len > INT32_MAX) {
477 // don't accept size_t values which may have come from an
478 // inadvertent conversion from a negative int.
479 return BAD_VALUE;
480 }
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 // range checks against the source parcel size
483 if ((offset > parcel->mDataSize)
484 || (len > parcel->mDataSize)
485 || (offset + len > parcel->mDataSize)) {
486 return BAD_VALUE;
487 }
488
489 // Count objects in range
490 for (int i = 0; i < (int) size; i++) {
491 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700492 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 if (firstIndex == -1) {
494 firstIndex = i;
495 }
496 lastIndex = i;
497 }
498 }
499 int numObjects = lastIndex - firstIndex + 1;
500
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700501 if ((mDataSize+len) > mDataCapacity) {
502 // grow data
503 err = growData(len);
504 if (err != NO_ERROR) {
505 return err;
506 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 }
508
509 // append data
510 memcpy(mData + mDataPos, data + offset, len);
511 mDataPos += len;
512 mDataSize += len;
513
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 err = NO_ERROR;
515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 if (numObjects > 0) {
517 // grow objects
518 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700519 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
520 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800521 binder_size_t *objects =
522 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
523 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 return NO_MEMORY;
525 }
526 mObjects = objects;
527 mObjectsCapacity = newSize;
528 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 // append and acquire objects
531 int idx = mObjectsSize;
532 for (int i = firstIndex; i <= lastIndex; i++) {
533 size_t off = objects[i] - offset + startPos;
534 mObjects[idx++] = off;
535 mObjectsSize++;
536
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700537 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700539 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700542 // If this is a file descriptor, we need to dup it so the
543 // new Parcel now owns its own fd, and can declare that we
544 // officially know we have fds.
545 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800546 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400548 if (!mAllowFds) {
549 err = FDS_NOT_ALLOWED;
550 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 }
552 }
553 }
554
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400555 return err;
556}
557
Jeff Brown13b16042014-11-11 16:44:25 -0800558bool Parcel::allowFds() const
559{
560 return mAllowFds;
561}
562
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700563bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564{
565 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700566 if (!allowFds) {
567 mAllowFds = false;
568 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400569 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570}
571
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572void Parcel::restoreAllowFds(bool lastValue)
573{
574 mAllowFds = lastValue;
575}
576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577bool Parcel::hasFileDescriptors() const
578{
579 if (!mFdsKnown) {
580 scanForFds();
581 }
582 return mHasFds;
583}
584
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700585// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586status_t Parcel::writeInterfaceToken(const String16& interface)
587{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700588 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
589 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 // currently the interface identification token is just its name as a string
591 return writeString16(interface);
592}
593
Mathias Agopian83c04462009-05-22 19:00:22 -0700594bool Parcel::checkInterface(IBinder* binder) const
595{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700596 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700597}
598
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700599bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700600 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700602 int32_t strictPolicy = readInt32();
603 if (threadState == NULL) {
604 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700605 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700606 if ((threadState->getLastTransactionBinderFlags() &
607 IBinder::FLAG_ONEWAY) != 0) {
608 // For one-way calls, the callee is running entirely
609 // disconnected from the caller, so disable StrictMode entirely.
610 // Not only does disk/network usage not impact the caller, but
611 // there's no way to commuicate back any violations anyway.
612 threadState->setStrictModePolicy(0);
613 } else {
614 threadState->setStrictModePolicy(strictPolicy);
615 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700616 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 if (str == interface) {
618 return true;
619 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700620 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 String8(interface).string(), String8(str).string());
622 return false;
623 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700624}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800626const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627{
628 return mObjects;
629}
630
631size_t Parcel::objectsCount() const
632{
633 return mObjectsSize;
634}
635
636status_t Parcel::errorCheck() const
637{
638 return mError;
639}
640
641void Parcel::setError(status_t err)
642{
643 mError = err;
644}
645
646status_t Parcel::finishWrite(size_t len)
647{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700648 if (len > INT32_MAX) {
649 // don't accept size_t values which may have come from an
650 // inadvertent conversion from a negative int.
651 return BAD_VALUE;
652 }
653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 //printf("Finish write of %d\n", len);
655 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700656 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657 if (mDataPos > mDataSize) {
658 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700659 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 }
661 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
662 return NO_ERROR;
663}
664
665status_t Parcel::writeUnpadded(const void* data, size_t len)
666{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700667 if (len > INT32_MAX) {
668 // don't accept size_t values which may have come from an
669 // inadvertent conversion from a negative int.
670 return BAD_VALUE;
671 }
672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 size_t end = mDataPos + len;
674 if (end < mDataPos) {
675 // integer overflow
676 return BAD_VALUE;
677 }
678
679 if (end <= mDataCapacity) {
680restart_write:
681 memcpy(mData+mDataPos, data, len);
682 return finishWrite(len);
683 }
684
685 status_t err = growData(len);
686 if (err == NO_ERROR) goto restart_write;
687 return err;
688}
689
690status_t Parcel::write(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 void* const d = writeInplace(len);
699 if (d) {
700 memcpy(d, data, len);
701 return NO_ERROR;
702 }
703 return mError;
704}
705
706void* Parcel::writeInplace(size_t len)
707{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700708 if (len > INT32_MAX) {
709 // don't accept size_t values which may have come from an
710 // inadvertent conversion from a negative int.
711 return NULL;
712 }
713
714 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715
716 // sanity check for integer overflow
717 if (mDataPos+padded < mDataPos) {
718 return NULL;
719 }
720
721 if ((mDataPos+padded) <= mDataCapacity) {
722restart_write:
723 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
724 uint8_t* const data = mData+mDataPos;
725
726 // Need to pad at end?
727 if (padded != len) {
728#if BYTE_ORDER == BIG_ENDIAN
729 static const uint32_t mask[4] = {
730 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
731 };
732#endif
733#if BYTE_ORDER == LITTLE_ENDIAN
734 static const uint32_t mask[4] = {
735 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
736 };
737#endif
738 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
739 // *reinterpret_cast<void**>(data+padded-4));
740 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
741 }
742
743 finishWrite(padded);
744 return data;
745 }
746
747 status_t err = growData(padded);
748 if (err == NO_ERROR) goto restart_write;
749 return NULL;
750}
751
Casey Dahlinb9872622015-11-25 15:09:45 -0800752status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
753{
754 if (!val) {
755 return writeInt32(-1);
756 }
757
758 return writeByteVector(*val);
759}
760
Casey Dahlin451ff582015-10-19 18:12:18 -0700761status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
762{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700763 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700764 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700765 status = BAD_VALUE;
766 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700767 }
768
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700769 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700770 if (status != OK) {
771 return status;
772 }
773
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700774 void* data = writeInplace(val.size());
775 if (!data) {
776 status = BAD_VALUE;
777 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700778 }
779
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700780 memcpy(data, val.data(), val.size());
781 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700782}
783
784status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
785{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800786 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700787}
788
Casey Dahlinb9872622015-11-25 15:09:45 -0800789status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
790{
791 return writeNullableTypedVector(val, &Parcel::writeInt32);
792}
793
Casey Dahlin451ff582015-10-19 18:12:18 -0700794status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
795{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800796 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700797}
798
Casey Dahlinb9872622015-11-25 15:09:45 -0800799status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
800{
801 return writeNullableTypedVector(val, &Parcel::writeInt64);
802}
803
Casey Dahlin451ff582015-10-19 18:12:18 -0700804status_t Parcel::writeFloatVector(const std::vector<float>& val)
805{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800806 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700807}
808
Casey Dahlinb9872622015-11-25 15:09:45 -0800809status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
810{
811 return writeNullableTypedVector(val, &Parcel::writeFloat);
812}
813
Casey Dahlin451ff582015-10-19 18:12:18 -0700814status_t Parcel::writeDoubleVector(const std::vector<double>& val)
815{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800816 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700817}
818
Casey Dahlinb9872622015-11-25 15:09:45 -0800819status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
820{
821 return writeNullableTypedVector(val, &Parcel::writeDouble);
822}
823
Casey Dahlin451ff582015-10-19 18:12:18 -0700824status_t Parcel::writeBoolVector(const std::vector<bool>& val)
825{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800826 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700827}
828
Casey Dahlinb9872622015-11-25 15:09:45 -0800829status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
830{
831 return writeNullableTypedVector(val, &Parcel::writeBool);
832}
833
Casey Dahlin451ff582015-10-19 18:12:18 -0700834status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
835{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800836 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700837}
838
Casey Dahlinb9872622015-11-25 15:09:45 -0800839status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
840{
841 return writeNullableTypedVector(val, &Parcel::writeChar);
842}
843
Casey Dahlin451ff582015-10-19 18:12:18 -0700844status_t Parcel::writeString16Vector(const std::vector<String16>& val)
845{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800846 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700847}
848
Casey Dahlinb9872622015-11-25 15:09:45 -0800849status_t Parcel::writeString16Vector(
850 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeString16);
853}
854
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700855status_t Parcel::writeInt32(int32_t val)
856{
Andreas Huber84a6d042009-08-17 13:33:27 -0700857 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700858}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800859
860status_t Parcel::writeUint32(uint32_t val)
861{
862 return writeAligned(val);
863}
864
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700865status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700866 if (len > INT32_MAX) {
867 // don't accept size_t values which may have come from an
868 // inadvertent conversion from a negative int.
869 return BAD_VALUE;
870 }
871
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700872 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700873 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700874 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700875 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700876 if (ret == NO_ERROR) {
877 ret = write(val, len * sizeof(*val));
878 }
879 return ret;
880}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700881status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700882 if (len > INT32_MAX) {
883 // don't accept size_t values which may have come from an
884 // inadvertent conversion from a negative int.
885 return BAD_VALUE;
886 }
887
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700888 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700889 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700890 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700891 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700892 if (ret == NO_ERROR) {
893 ret = write(val, len * sizeof(*val));
894 }
895 return ret;
896}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700897
Casey Dahlind6848f52015-10-15 15:44:59 -0700898status_t Parcel::writeBool(bool val)
899{
900 return writeInt32(int32_t(val));
901}
902
903status_t Parcel::writeChar(char16_t val)
904{
905 return writeInt32(int32_t(val));
906}
907
908status_t Parcel::writeByte(int8_t val)
909{
910 return writeInt32(int32_t(val));
911}
912
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700913status_t Parcel::writeInt64(int64_t val)
914{
Andreas Huber84a6d042009-08-17 13:33:27 -0700915 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700916}
917
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700918status_t Parcel::writeUint64(uint64_t val)
919{
920 return writeAligned(val);
921}
922
Serban Constantinescuf683e012013-11-05 16:53:55 +0000923status_t Parcel::writePointer(uintptr_t val)
924{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800925 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000926}
927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700928status_t Parcel::writeFloat(float val)
929{
Andreas Huber84a6d042009-08-17 13:33:27 -0700930 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700931}
932
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800933#if defined(__mips__) && defined(__mips_hard_float)
934
935status_t Parcel::writeDouble(double val)
936{
937 union {
938 double d;
939 unsigned long long ll;
940 } u;
941 u.d = val;
942 return writeAligned(u.ll);
943}
944
945#else
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947status_t Parcel::writeDouble(double val)
948{
Andreas Huber84a6d042009-08-17 13:33:27 -0700949 return writeAligned(val);
950}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700951
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800952#endif
953
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700954status_t Parcel::writeCString(const char* str)
955{
956 return write(str, strlen(str)+1);
957}
958
959status_t Parcel::writeString8(const String8& str)
960{
961 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100962 // only write string if its length is more than zero characters,
963 // as readString8 will only read if the length field is non-zero.
964 // this is slightly different from how writeString16 works.
965 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966 err = write(str.string(), str.bytes()+1);
967 }
968 return err;
969}
970
Casey Dahlinb9872622015-11-25 15:09:45 -0800971status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
972{
973 if (!str) {
974 return writeInt32(-1);
975 }
976
977 return writeString16(*str);
978}
979
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980status_t Parcel::writeString16(const String16& str)
981{
982 return writeString16(str.string(), str.size());
983}
984
985status_t Parcel::writeString16(const char16_t* str, size_t len)
986{
987 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700988
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700989 status_t err = writeInt32(len);
990 if (err == NO_ERROR) {
991 len *= sizeof(char16_t);
992 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
993 if (data) {
994 memcpy(data, str, len);
995 *reinterpret_cast<char16_t*>(data+len) = 0;
996 return NO_ERROR;
997 }
998 err = mError;
999 }
1000 return err;
1001}
1002
1003status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1004{
1005 return flatten_binder(ProcessState::self(), val, this);
1006}
1007
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001008status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1009{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001010 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001011}
1012
Casey Dahlinb9872622015-11-25 15:09:45 -08001013status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1014{
1015 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1016}
1017
1018status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1019 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1020}
1021
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001022status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001023 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001024}
1025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1027{
1028 return flatten_binder(ProcessState::self(), val, this);
1029}
1030
Casey Dahlinb9872622015-11-25 15:09:45 -08001031status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1032 if (!parcelable) {
1033 return writeInt32(0);
1034 }
1035
1036 return writeParcelable(*parcelable);
1037}
1038
Christopher Wiley97f048d2015-11-19 06:49:05 -08001039status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1040 status_t status = writeInt32(1); // parcelable is not null.
1041 if (status != OK) {
1042 return status;
1043 }
1044 return parcelable.writeToParcel(this);
1045}
1046
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001047status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001048{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001049 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001050 return BAD_TYPE;
1051
1052 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001053 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001054 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001056 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001057 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001059 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1060 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
1062 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001063 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 return err;
1065 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001066 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001067 return err;
1068}
1069
Jeff Brown93ff1f92011-11-04 19:01:44 -07001070status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071{
1072 flat_binder_object obj;
1073 obj.type = BINDER_TYPE_FD;
1074 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001075 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001077 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001078 return writeObject(obj, true);
1079}
1080
1081status_t Parcel::writeDupFileDescriptor(int fd)
1082{
Jeff Brownd341c712011-11-04 20:19:33 -07001083 int dupFd = dup(fd);
1084 if (dupFd < 0) {
1085 return -errno;
1086 }
1087 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001088 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001089 close(dupFd);
1090 }
1091 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092}
1093
Casey Dahlin06673e32015-11-23 13:24:23 -08001094status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1095 return writeDupFileDescriptor(fd.get());
1096}
1097
1098status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1099 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1100}
1101
Casey Dahlinb9872622015-11-25 15:09:45 -08001102status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1103 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1104}
1105
Jeff Brown13b16042014-11-11 16:44:25 -08001106status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001107{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001108 if (len > INT32_MAX) {
1109 // don't accept size_t values which may have come from an
1110 // inadvertent conversion from a negative int.
1111 return BAD_VALUE;
1112 }
1113
Jeff Brown13b16042014-11-11 16:44:25 -08001114 status_t status;
1115 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001116 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001117 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001118 if (status) return status;
1119
1120 void* ptr = writeInplace(len);
1121 if (!ptr) return NO_MEMORY;
1122
Jeff Brown13b16042014-11-11 16:44:25 -08001123 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001124 return NO_ERROR;
1125 }
1126
Steve Block6807e592011-10-20 11:56:00 +01001127 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001128 int fd = ashmem_create_region("Parcel Blob", len);
1129 if (fd < 0) return NO_MEMORY;
1130
1131 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1132 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001133 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001134 } else {
1135 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1136 if (ptr == MAP_FAILED) {
1137 status = -errno;
1138 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001139 if (!mutableCopy) {
1140 result = ashmem_set_prot_region(fd, PROT_READ);
1141 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001142 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001143 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001144 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001145 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001146 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001147 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001149 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001150 return NO_ERROR;
1151 }
1152 }
1153 }
1154 }
1155 ::munmap(ptr, len);
1156 }
1157 ::close(fd);
1158 return status;
1159}
1160
Jeff Brown13b16042014-11-11 16:44:25 -08001161status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1162{
1163 // Must match up with what's done in writeBlob.
1164 if (!mAllowFds) return FDS_NOT_ALLOWED;
1165 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1166 if (status) return status;
1167 return writeDupFileDescriptor(fd);
1168}
1169
Mathias Agopiane1424282013-07-29 21:24:40 -07001170status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001171{
1172 status_t err;
1173
1174 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001175 const size_t len = val.getFlattenedSize();
1176 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001177
Nick Kralevichb6b14232015-04-02 09:36:02 -07001178 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1179 // don't accept size_t values which may have come from an
1180 // inadvertent conversion from a negative int.
1181 return BAD_VALUE;
1182 }
1183
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001184 err = this->writeInt32(len);
1185 if (err) return err;
1186
1187 err = this->writeInt32(fd_count);
1188 if (err) return err;
1189
1190 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001191 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001192 if (buf == NULL)
1193 return BAD_VALUE;
1194
1195 int* fds = NULL;
1196 if (fd_count) {
1197 fds = new int[fd_count];
1198 }
1199
1200 err = val.flatten(buf, len, fds, fd_count);
1201 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1202 err = this->writeDupFileDescriptor( fds[i] );
1203 }
1204
1205 if (fd_count) {
1206 delete [] fds;
1207 }
1208
1209 return err;
1210}
1211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001212status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1213{
1214 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1215 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1216 if (enoughData && enoughObjects) {
1217restart_write:
1218 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001219
Christopher Tate98e67d32015-06-03 18:44:15 -07001220 // remember if it's a file descriptor
1221 if (val.type == BINDER_TYPE_FD) {
1222 if (!mAllowFds) {
1223 // fail before modifying our object index
1224 return FDS_NOT_ALLOWED;
1225 }
1226 mHasFds = mFdsKnown = true;
1227 }
1228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001229 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001230 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001232 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 mObjectsSize++;
1234 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001236 return finishWrite(sizeof(flat_binder_object));
1237 }
1238
1239 if (!enoughData) {
1240 const status_t err = growData(sizeof(val));
1241 if (err != NO_ERROR) return err;
1242 }
1243 if (!enoughObjects) {
1244 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001245 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001246 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247 if (objects == NULL) return NO_MEMORY;
1248 mObjects = objects;
1249 mObjectsCapacity = newSize;
1250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001252 goto restart_write;
1253}
1254
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001255status_t Parcel::writeNoException()
1256{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001257 binder::Status status;
1258 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001259}
1260
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001261void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001262{
1263 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1264}
1265
1266status_t Parcel::read(void* outData, size_t len) const
1267{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001268 if (len > INT32_MAX) {
1269 // don't accept size_t values which may have come from an
1270 // inadvertent conversion from a negative int.
1271 return BAD_VALUE;
1272 }
1273
1274 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1275 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001277 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001278 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001279 return NO_ERROR;
1280 }
1281 return NOT_ENOUGH_DATA;
1282}
1283
1284const void* Parcel::readInplace(size_t len) const
1285{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001286 if (len > INT32_MAX) {
1287 // don't accept size_t values which may have come from an
1288 // inadvertent conversion from a negative int.
1289 return NULL;
1290 }
1291
1292 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1293 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001294 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001295 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001296 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001297 return data;
1298 }
1299 return NULL;
1300}
1301
Andreas Huber84a6d042009-08-17 13:33:27 -07001302template<class T>
1303status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001304 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001305
1306 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001307 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001308 mDataPos += sizeof(T);
1309 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001310 return NO_ERROR;
1311 } else {
1312 return NOT_ENOUGH_DATA;
1313 }
1314}
1315
Andreas Huber84a6d042009-08-17 13:33:27 -07001316template<class T>
1317T Parcel::readAligned() const {
1318 T result;
1319 if (readAligned(&result) != NO_ERROR) {
1320 result = 0;
1321 }
1322
1323 return result;
1324}
1325
1326template<class T>
1327status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001328 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001329
1330 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1331restart_write:
1332 *reinterpret_cast<T*>(mData+mDataPos) = val;
1333 return finishWrite(sizeof(val));
1334 }
1335
1336 status_t err = growData(sizeof(val));
1337 if (err == NO_ERROR) goto restart_write;
1338 return err;
1339}
1340
Casey Dahlin451ff582015-10-19 18:12:18 -07001341status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1342 val->clear();
1343
1344 int32_t size;
1345 status_t status = readInt32(&size);
1346
1347 if (status != OK) {
1348 return status;
1349 }
1350
Christopher Wiley4db672d2015-11-10 09:44:30 -08001351 if (size < 0) {
1352 status = UNEXPECTED_NULL;
1353 return status;
1354 }
1355 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001356 status = BAD_VALUE;
1357 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001358 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001359
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001360 const void* data = readInplace(size);
1361 if (!data) {
1362 status = BAD_VALUE;
1363 return status;
1364 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001365 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001366 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001367
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001368 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001369}
1370
Casey Dahlinb9872622015-11-25 15:09:45 -08001371status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1372 const int32_t start = dataPosition();
1373 int32_t size;
1374 status_t status = readInt32(&size);
1375 val->reset();
1376
1377 if (status != OK || size < 0) {
1378 return status;
1379 }
1380
1381 setDataPosition(start);
1382 val->reset(new std::vector<int8_t>());
1383
1384 status = readByteVector(val->get());
1385
1386 if (status != OK) {
1387 val->reset();
1388 }
1389
1390 return status;
1391}
1392
1393status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1394 return readNullableTypedVector(val, &Parcel::readInt32);
1395}
1396
Casey Dahlin451ff582015-10-19 18:12:18 -07001397status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001398 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001399}
1400
Casey Dahlinb9872622015-11-25 15:09:45 -08001401status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1402 return readNullableTypedVector(val, &Parcel::readInt64);
1403}
1404
Casey Dahlin451ff582015-10-19 18:12:18 -07001405status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001406 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001407}
1408
Casey Dahlinb9872622015-11-25 15:09:45 -08001409status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1410 return readNullableTypedVector(val, &Parcel::readFloat);
1411}
1412
Casey Dahlin451ff582015-10-19 18:12:18 -07001413status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001414 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001415}
1416
Casey Dahlinb9872622015-11-25 15:09:45 -08001417status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1418 return readNullableTypedVector(val, &Parcel::readDouble);
1419}
1420
Casey Dahlin451ff582015-10-19 18:12:18 -07001421status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001422 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001423}
1424
Casey Dahlinb9872622015-11-25 15:09:45 -08001425status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1426 const int32_t start = dataPosition();
1427 int32_t size;
1428 status_t status = readInt32(&size);
1429 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001430
Casey Dahlinb9872622015-11-25 15:09:45 -08001431 if (status != OK || size < 0) {
1432 return status;
1433 }
1434
1435 setDataPosition(start);
1436 val->reset(new std::vector<bool>());
1437
1438 status = readBoolVector(val->get());
1439
1440 if (status != OK) {
1441 val->reset();
1442 }
1443
1444 return status;
1445}
1446
1447status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001448 int32_t size;
1449 status_t status = readInt32(&size);
1450
1451 if (status != OK) {
1452 return status;
1453 }
1454
1455 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001456 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001457 }
1458
1459 val->resize(size);
1460
1461 /* C++ bool handling means a vector of bools isn't necessarily addressable
1462 * (we might use individual bits)
1463 */
Christopher Wiley97887982015-10-27 16:33:47 -07001464 bool data;
1465 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001466 status = readBool(&data);
1467 (*val)[i] = data;
1468
1469 if (status != OK) {
1470 return status;
1471 }
1472 }
1473
1474 return OK;
1475}
1476
Casey Dahlinb9872622015-11-25 15:09:45 -08001477status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1478 return readNullableTypedVector(val, &Parcel::readChar);
1479}
1480
Casey Dahlin451ff582015-10-19 18:12:18 -07001481status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001482 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001483}
1484
Casey Dahlinb9872622015-11-25 15:09:45 -08001485status_t Parcel::readString16Vector(
1486 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1487 return readNullableTypedVector(val, &Parcel::readString16);
1488}
1489
Casey Dahlin451ff582015-10-19 18:12:18 -07001490status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001491 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001492}
1493
1494
Andreas Huber84a6d042009-08-17 13:33:27 -07001495status_t Parcel::readInt32(int32_t *pArg) const
1496{
1497 return readAligned(pArg);
1498}
1499
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500int32_t Parcel::readInt32() const
1501{
Andreas Huber84a6d042009-08-17 13:33:27 -07001502 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503}
1504
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001505status_t Parcel::readUint32(uint32_t *pArg) const
1506{
1507 return readAligned(pArg);
1508}
1509
1510uint32_t Parcel::readUint32() const
1511{
1512 return readAligned<uint32_t>();
1513}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001514
1515status_t Parcel::readInt64(int64_t *pArg) const
1516{
Andreas Huber84a6d042009-08-17 13:33:27 -07001517 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518}
1519
1520
1521int64_t Parcel::readInt64() const
1522{
Andreas Huber84a6d042009-08-17 13:33:27 -07001523 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001524}
1525
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001526status_t Parcel::readUint64(uint64_t *pArg) const
1527{
1528 return readAligned(pArg);
1529}
1530
1531uint64_t Parcel::readUint64() const
1532{
1533 return readAligned<uint64_t>();
1534}
1535
Serban Constantinescuf683e012013-11-05 16:53:55 +00001536status_t Parcel::readPointer(uintptr_t *pArg) const
1537{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001538 status_t ret;
1539 binder_uintptr_t ptr;
1540 ret = readAligned(&ptr);
1541 if (!ret)
1542 *pArg = ptr;
1543 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001544}
1545
1546uintptr_t Parcel::readPointer() const
1547{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001548 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001549}
1550
1551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001552status_t Parcel::readFloat(float *pArg) const
1553{
Andreas Huber84a6d042009-08-17 13:33:27 -07001554 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555}
1556
1557
1558float Parcel::readFloat() const
1559{
Andreas Huber84a6d042009-08-17 13:33:27 -07001560 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561}
1562
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001563#if defined(__mips__) && defined(__mips_hard_float)
1564
1565status_t Parcel::readDouble(double *pArg) const
1566{
1567 union {
1568 double d;
1569 unsigned long long ll;
1570 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001571 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001572 status_t status;
1573 status = readAligned(&u.ll);
1574 *pArg = u.d;
1575 return status;
1576}
1577
1578double Parcel::readDouble() const
1579{
1580 union {
1581 double d;
1582 unsigned long long ll;
1583 } u;
1584 u.ll = readAligned<unsigned long long>();
1585 return u.d;
1586}
1587
1588#else
1589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590status_t Parcel::readDouble(double *pArg) const
1591{
Andreas Huber84a6d042009-08-17 13:33:27 -07001592 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593}
1594
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595double Parcel::readDouble() const
1596{
Andreas Huber84a6d042009-08-17 13:33:27 -07001597 return readAligned<double>();
1598}
1599
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001600#endif
1601
Andreas Huber84a6d042009-08-17 13:33:27 -07001602status_t Parcel::readIntPtr(intptr_t *pArg) const
1603{
1604 return readAligned(pArg);
1605}
1606
1607
1608intptr_t Parcel::readIntPtr() const
1609{
1610 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611}
1612
Casey Dahlind6848f52015-10-15 15:44:59 -07001613status_t Parcel::readBool(bool *pArg) const
1614{
1615 int32_t tmp;
1616 status_t ret = readInt32(&tmp);
1617 *pArg = (tmp != 0);
1618 return ret;
1619}
1620
1621bool Parcel::readBool() const
1622{
1623 return readInt32() != 0;
1624}
1625
1626status_t Parcel::readChar(char16_t *pArg) const
1627{
1628 int32_t tmp;
1629 status_t ret = readInt32(&tmp);
1630 *pArg = char16_t(tmp);
1631 return ret;
1632}
1633
1634char16_t Parcel::readChar() const
1635{
1636 return char16_t(readInt32());
1637}
1638
1639status_t Parcel::readByte(int8_t *pArg) const
1640{
1641 int32_t tmp;
1642 status_t ret = readInt32(&tmp);
1643 *pArg = int8_t(tmp);
1644 return ret;
1645}
1646
1647int8_t Parcel::readByte() const
1648{
1649 return int8_t(readInt32());
1650}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651
1652const char* Parcel::readCString() const
1653{
1654 const size_t avail = mDataSize-mDataPos;
1655 if (avail > 0) {
1656 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1657 // is the string's trailing NUL within the parcel's valid bounds?
1658 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1659 if (eos) {
1660 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001661 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001662 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001663 return str;
1664 }
1665 }
1666 return NULL;
1667}
1668
1669String8 Parcel::readString8() const
1670{
1671 int32_t size = readInt32();
1672 // watch for potential int overflow adding 1 for trailing NUL
1673 if (size > 0 && size < INT32_MAX) {
1674 const char* str = (const char*)readInplace(size+1);
1675 if (str) return String8(str, size);
1676 }
1677 return String8();
1678}
1679
1680String16 Parcel::readString16() const
1681{
1682 size_t len;
1683 const char16_t* str = readString16Inplace(&len);
1684 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001685 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686 return String16();
1687}
1688
Casey Dahlinb9872622015-11-25 15:09:45 -08001689status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1690{
1691 const int32_t start = dataPosition();
1692 int32_t size;
1693 status_t status = readInt32(&size);
1694 pArg->reset();
1695
1696 if (status != OK || size < 0) {
1697 return status;
1698 }
1699
1700 setDataPosition(start);
1701 pArg->reset(new String16());
1702
1703 status = readString16(pArg->get());
1704
1705 if (status != OK) {
1706 pArg->reset();
1707 }
1708
1709 return status;
1710}
1711
Casey Dahlin451ff582015-10-19 18:12:18 -07001712status_t Parcel::readString16(String16* pArg) const
1713{
1714 size_t len;
1715 const char16_t* str = readString16Inplace(&len);
1716 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001717 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001718 return 0;
1719 } else {
1720 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001721 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001722 }
1723}
1724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001725const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1726{
1727 int32_t size = readInt32();
1728 // watch for potential int overflow from size+1
1729 if (size >= 0 && size < INT32_MAX) {
1730 *outLen = size;
1731 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1732 if (str != NULL) {
1733 return str;
1734 }
1735 }
1736 *outLen = 0;
1737 return NULL;
1738}
1739
Casey Dahlinf0c13772015-10-27 18:33:56 -07001740status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1741{
1742 return unflatten_binder(ProcessState::self(), *this, val);
1743}
1744
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001745sp<IBinder> Parcel::readStrongBinder() const
1746{
1747 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001748 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001749 return val;
1750}
1751
1752wp<IBinder> Parcel::readWeakBinder() const
1753{
1754 wp<IBinder> val;
1755 unflatten_binder(ProcessState::self(), *this, &val);
1756 return val;
1757}
1758
Christopher Wiley97f048d2015-11-19 06:49:05 -08001759status_t Parcel::readParcelable(Parcelable* parcelable) const {
1760 int32_t have_parcelable = 0;
1761 status_t status = readInt32(&have_parcelable);
1762 if (status != OK) {
1763 return status;
1764 }
1765 if (!have_parcelable) {
1766 return UNEXPECTED_NULL;
1767 }
1768 return parcelable->readFromParcel(this);
1769}
1770
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001771int32_t Parcel::readExceptionCode() const
1772{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001773 binder::Status status;
1774 status.readFromParcel(*this);
1775 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001776}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001777
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001778native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001779{
1780 int numFds, numInts;
1781 status_t err;
1782 err = readInt32(&numFds);
1783 if (err != NO_ERROR) return 0;
1784 err = readInt32(&numInts);
1785 if (err != NO_ERROR) return 0;
1786
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001787 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001788 if (!h) {
1789 return 0;
1790 }
1791
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001792 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001793 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001794 if (h->data[i] < 0) err = BAD_VALUE;
1795 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001796 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001797 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001798 native_handle_close(h);
1799 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001800 h = 0;
1801 }
1802 return h;
1803}
1804
1805
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806int Parcel::readFileDescriptor() const
1807{
1808 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001809
1810 if (flat && flat->type == BINDER_TYPE_FD) {
1811 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 return BAD_TYPE;
1815}
1816
Casey Dahlin06673e32015-11-23 13:24:23 -08001817status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1818{
1819 int got = readFileDescriptor();
1820
1821 if (got == BAD_TYPE) {
1822 return BAD_TYPE;
1823 }
1824
1825 val->reset(dup(got));
1826
1827 if (val->get() < 0) {
1828 return BAD_VALUE;
1829 }
1830
1831 return OK;
1832}
1833
1834
Casey Dahlinb9872622015-11-25 15:09:45 -08001835status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1836 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1837}
1838
Casey Dahlin06673e32015-11-23 13:24:23 -08001839status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1840 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1841}
1842
Jeff Brown5707dbf2011-09-23 21:17:56 -07001843status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1844{
Jeff Brown13b16042014-11-11 16:44:25 -08001845 int32_t blobType;
1846 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001847 if (status) return status;
1848
Jeff Brown13b16042014-11-11 16:44:25 -08001849 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001850 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001851 const void* ptr = readInplace(len);
1852 if (!ptr) return BAD_VALUE;
1853
Jeff Brown13b16042014-11-11 16:44:25 -08001854 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001855 return NO_ERROR;
1856 }
1857
Steve Block6807e592011-10-20 11:56:00 +01001858 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001859 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001860 int fd = readFileDescriptor();
1861 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1862
Jeff Brown13b16042014-11-11 16:44:25 -08001863 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1864 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001865 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001866
Jeff Brown13b16042014-11-11 16:44:25 -08001867 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001868 return NO_ERROR;
1869}
1870
Mathias Agopiane1424282013-07-29 21:24:40 -07001871status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001872{
1873 // size
1874 const size_t len = this->readInt32();
1875 const size_t fd_count = this->readInt32();
1876
Nick Kralevichb6b14232015-04-02 09:36:02 -07001877 if (len > INT32_MAX) {
1878 // don't accept size_t values which may have come from an
1879 // inadvertent conversion from a negative int.
1880 return BAD_VALUE;
1881 }
1882
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001883 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001884 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001885 if (buf == NULL)
1886 return BAD_VALUE;
1887
1888 int* fds = NULL;
1889 if (fd_count) {
1890 fds = new int[fd_count];
1891 }
1892
1893 status_t err = NO_ERROR;
1894 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001895 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001896 if (fds[i] < 0) {
1897 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001898 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1899 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001900 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001901 }
1902
1903 if (err == NO_ERROR) {
1904 err = val.unflatten(buf, len, fds, fd_count);
1905 }
1906
1907 if (fd_count) {
1908 delete [] fds;
1909 }
1910
1911 return err;
1912}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1914{
1915 const size_t DPOS = mDataPos;
1916 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1917 const flat_binder_object* obj
1918 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1919 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001920 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001921 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922 // the object list, so we don't want to check for it when
1923 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001924 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925 return obj;
1926 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001929 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 const size_t N = mObjectsSize;
1931 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001932
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001933 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001934 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001936
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937 // Start at the current hint position, looking for an object at
1938 // the current data position.
1939 if (opos < N) {
1940 while (opos < (N-1) && OBJS[opos] < DPOS) {
1941 opos++;
1942 }
1943 } else {
1944 opos = N-1;
1945 }
1946 if (OBJS[opos] == DPOS) {
1947 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001948 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001949 this, DPOS, opos);
1950 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001951 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952 return obj;
1953 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001954
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001955 // Look backwards for it...
1956 while (opos > 0 && OBJS[opos] > DPOS) {
1957 opos--;
1958 }
1959 if (OBJS[opos] == DPOS) {
1960 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001961 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001962 this, DPOS, opos);
1963 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001964 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001965 return obj;
1966 }
1967 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001968 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 -07001969 this, DPOS);
1970 }
1971 return NULL;
1972}
1973
1974void Parcel::closeFileDescriptors()
1975{
1976 size_t i = mObjectsSize;
1977 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001978 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979 }
1980 while (i > 0) {
1981 i--;
1982 const flat_binder_object* flat
1983 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1984 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001985 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001986 close(flat->handle);
1987 }
1988 }
1989}
1990
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001991uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001992{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001993 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994}
1995
1996size_t Parcel::ipcDataSize() const
1997{
1998 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1999}
2000
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002001uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002002{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002003 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002004}
2005
2006size_t Parcel::ipcObjectsCount() const
2007{
2008 return mObjectsSize;
2009}
2010
2011void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002012 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002014 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015 freeDataNoInit();
2016 mError = NO_ERROR;
2017 mData = const_cast<uint8_t*>(data);
2018 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002019 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002021 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002022 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023 mObjectsSize = mObjectsCapacity = objectsCount;
2024 mNextObjectHint = 0;
2025 mOwner = relFunc;
2026 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002027 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002028 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002029 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002030 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002031 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002032 mObjectsSize = 0;
2033 break;
2034 }
2035 minOffset = offset + sizeof(flat_binder_object);
2036 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 scanForFds();
2038}
2039
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002040void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002041{
2042 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 if (errorCheck() != NO_ERROR) {
2045 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002046 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047 } else if (dataSize() > 0) {
2048 const uint8_t* DATA = data();
2049 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002050 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051 const size_t N = objectsCount();
2052 for (size_t i=0; i<N; i++) {
2053 const flat_binder_object* flat
2054 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2055 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2056 << TypeCode(flat->type & 0x7f7f7f00)
2057 << " = " << flat->binder;
2058 }
2059 } else {
2060 to << "NULL";
2061 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002063 to << ")";
2064}
2065
2066void Parcel::releaseObjects()
2067{
2068 const sp<ProcessState> proc(ProcessState::self());
2069 size_t i = mObjectsSize;
2070 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002071 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072 while (i > 0) {
2073 i--;
2074 const flat_binder_object* flat
2075 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002076 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 }
2078}
2079
2080void Parcel::acquireObjects()
2081{
2082 const sp<ProcessState> proc(ProcessState::self());
2083 size_t i = mObjectsSize;
2084 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002085 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 while (i > 0) {
2087 i--;
2088 const flat_binder_object* flat
2089 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002090 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091 }
2092}
2093
2094void Parcel::freeData()
2095{
2096 freeDataNoInit();
2097 initState();
2098}
2099
2100void Parcel::freeDataNoInit()
2101{
2102 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002103 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002104 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2106 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002107 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002109 if (mData) {
2110 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002111 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002112 if (mDataCapacity <= gParcelGlobalAllocSize) {
2113 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2114 } else {
2115 gParcelGlobalAllocSize = 0;
2116 }
2117 if (gParcelGlobalAllocCount > 0) {
2118 gParcelGlobalAllocCount--;
2119 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002120 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002121 free(mData);
2122 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002123 if (mObjects) free(mObjects);
2124 }
2125}
2126
2127status_t Parcel::growData(size_t len)
2128{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002129 if (len > INT32_MAX) {
2130 // don't accept size_t values which may have come from an
2131 // inadvertent conversion from a negative int.
2132 return BAD_VALUE;
2133 }
2134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 size_t newSize = ((mDataSize+len)*3)/2;
2136 return (newSize <= mDataSize)
2137 ? (status_t) NO_MEMORY
2138 : continueWrite(newSize);
2139}
2140
2141status_t Parcel::restartWrite(size_t desired)
2142{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002143 if (desired > INT32_MAX) {
2144 // don't accept size_t values which may have come from an
2145 // inadvertent conversion from a negative int.
2146 return BAD_VALUE;
2147 }
2148
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 if (mOwner) {
2150 freeData();
2151 return continueWrite(desired);
2152 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 uint8_t* data = (uint8_t*)realloc(mData, desired);
2155 if (!data && desired > mDataCapacity) {
2156 mError = NO_MEMORY;
2157 return NO_MEMORY;
2158 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002163 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002164 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002165 gParcelGlobalAllocSize += desired;
2166 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002167 if (!mData) {
2168 gParcelGlobalAllocCount++;
2169 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002170 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 mData = data;
2172 mDataCapacity = desired;
2173 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002174
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002176 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2177 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2178
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 free(mObjects);
2180 mObjects = NULL;
2181 mObjectsSize = mObjectsCapacity = 0;
2182 mNextObjectHint = 0;
2183 mHasFds = false;
2184 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002185 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002186
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 return NO_ERROR;
2188}
2189
2190status_t Parcel::continueWrite(size_t desired)
2191{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002192 if (desired > INT32_MAX) {
2193 // don't accept size_t values which may have come from an
2194 // inadvertent conversion from a negative int.
2195 return BAD_VALUE;
2196 }
2197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 // If shrinking, first adjust for any objects that appear
2199 // after the new data size.
2200 size_t objectsSize = mObjectsSize;
2201 if (desired < mDataSize) {
2202 if (desired == 0) {
2203 objectsSize = 0;
2204 } else {
2205 while (objectsSize > 0) {
2206 if (mObjects[objectsSize-1] < desired)
2207 break;
2208 objectsSize--;
2209 }
2210 }
2211 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213 if (mOwner) {
2214 // If the size is going to zero, just release the owner's data.
2215 if (desired == 0) {
2216 freeData();
2217 return NO_ERROR;
2218 }
2219
2220 // If there is a different owner, we need to take
2221 // posession.
2222 uint8_t* data = (uint8_t*)malloc(desired);
2223 if (!data) {
2224 mError = NO_MEMORY;
2225 return NO_MEMORY;
2226 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002227 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002230 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002232 free(data);
2233
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 mError = NO_MEMORY;
2235 return NO_MEMORY;
2236 }
2237
2238 // Little hack to only acquire references on objects
2239 // we will be keeping.
2240 size_t oldObjectsSize = mObjectsSize;
2241 mObjectsSize = objectsSize;
2242 acquireObjects();
2243 mObjectsSize = oldObjectsSize;
2244 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002245
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002246 if (mData) {
2247 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2248 }
2249 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002250 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002252 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2254 mOwner = NULL;
2255
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002256 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002257 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 gParcelGlobalAllocSize += desired;
2259 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002260 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 mData = data;
2263 mObjects = objects;
2264 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002265 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 mDataCapacity = desired;
2267 mObjectsSize = mObjectsCapacity = objectsSize;
2268 mNextObjectHint = 0;
2269
2270 } else if (mData) {
2271 if (objectsSize < mObjectsSize) {
2272 // Need to release refs on any objects we are dropping.
2273 const sp<ProcessState> proc(ProcessState::self());
2274 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2275 const flat_binder_object* flat
2276 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2277 if (flat->type == BINDER_TYPE_FD) {
2278 // will need to rescan because we may have lopped off the only FDs
2279 mFdsKnown = false;
2280 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002281 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002283 binder_size_t* objects =
2284 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002285 if (objects) {
2286 mObjects = objects;
2287 }
2288 mObjectsSize = objectsSize;
2289 mNextObjectHint = 0;
2290 }
2291
2292 // We own the data, so we can just do a realloc().
2293 if (desired > mDataCapacity) {
2294 uint8_t* data = (uint8_t*)realloc(mData, desired);
2295 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002296 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2297 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002298 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002299 gParcelGlobalAllocSize += desired;
2300 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002301 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 mData = data;
2303 mDataCapacity = desired;
2304 } else if (desired > mDataCapacity) {
2305 mError = NO_MEMORY;
2306 return NO_MEMORY;
2307 }
2308 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002309 if (mDataSize > desired) {
2310 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002311 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002312 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002313 if (mDataPos > desired) {
2314 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002315 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 }
2317 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002318
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 } else {
2320 // This is the first data. Easy!
2321 uint8_t* data = (uint8_t*)malloc(desired);
2322 if (!data) {
2323 mError = NO_MEMORY;
2324 return NO_MEMORY;
2325 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 if(!(mDataCapacity == 0 && mObjects == NULL
2328 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002329 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002331
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002332 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002333 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002334 gParcelGlobalAllocSize += desired;
2335 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002336 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002337
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 mData = data;
2339 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002340 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2341 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002342 mDataCapacity = desired;
2343 }
2344
2345 return NO_ERROR;
2346}
2347
2348void Parcel::initState()
2349{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002350 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002351 mError = NO_ERROR;
2352 mData = 0;
2353 mDataSize = 0;
2354 mDataCapacity = 0;
2355 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2357 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 mObjects = NULL;
2359 mObjectsSize = 0;
2360 mObjectsCapacity = 0;
2361 mNextObjectHint = 0;
2362 mHasFds = false;
2363 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002364 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002366 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367}
2368
2369void Parcel::scanForFds() const
2370{
2371 bool hasFds = false;
2372 for (size_t i=0; i<mObjectsSize; i++) {
2373 const flat_binder_object* flat
2374 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2375 if (flat->type == BINDER_TYPE_FD) {
2376 hasFds = true;
2377 break;
2378 }
2379 }
2380 mHasFds = hasFds;
2381 mFdsKnown = true;
2382}
2383
Dan Sandleraa5c2342015-04-10 10:08:45 -04002384size_t Parcel::getBlobAshmemSize() const
2385{
Adrian Roos6bb31142015-10-22 16:46:12 -07002386 // This used to return the size of all blobs that were written to ashmem, now we're returning
2387 // the ashmem currently referenced by this Parcel, which should be equivalent.
2388 // TODO: Remove method once ABI can be changed.
2389 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002390}
2391
Adrian Rooscbf37262015-10-22 16:12:53 -07002392size_t Parcel::getOpenAshmemSize() const
2393{
2394 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002395}
2396
2397// --- Parcel::Blob ---
2398
2399Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002400 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002401}
2402
2403Parcel::Blob::~Blob() {
2404 release();
2405}
2406
2407void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002408 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002409 ::munmap(mData, mSize);
2410 }
2411 clear();
2412}
2413
Jeff Brown13b16042014-11-11 16:44:25 -08002414void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2415 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002416 mData = data;
2417 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002418 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002419}
2420
2421void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002422 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002423 mData = NULL;
2424 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002425 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002426}
2427
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002428}; // namespace android