blob: 3acd86955b359d013c8af3f9ca69f22c1c8b1344 [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{
387 // TODO: decide what to do about the possibility that this can
388 // report an available-data size that exceeds a Java int's max
389 // positive value, causing havoc. Fortunately this will only
390 // happen if someone constructs a Parcel containing more than two
391 // gigabytes of data, which on typical phone hardware is simply
392 // not possible.
393 return dataSize() - dataPosition();
394}
395
396size_t Parcel::dataPosition() const
397{
398 return mDataPos;
399}
400
401size_t Parcel::dataCapacity() const
402{
403 return mDataCapacity;
404}
405
406status_t Parcel::setDataSize(size_t size)
407{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700408 if (size > INT32_MAX) {
409 // don't accept size_t values which may have come from an
410 // inadvertent conversion from a negative int.
411 return BAD_VALUE;
412 }
413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700414 status_t err;
415 err = continueWrite(size);
416 if (err == NO_ERROR) {
417 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700418 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 }
420 return err;
421}
422
423void Parcel::setDataPosition(size_t pos) const
424{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700425 if (pos > INT32_MAX) {
426 // don't accept size_t values which may have come from an
427 // inadvertent conversion from a negative int.
428 abort();
429 }
430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431 mDataPos = pos;
432 mNextObjectHint = 0;
433}
434
435status_t Parcel::setDataCapacity(size_t size)
436{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700437 if (size > INT32_MAX) {
438 // don't accept size_t values which may have come from an
439 // inadvertent conversion from a negative int.
440 return BAD_VALUE;
441 }
442
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700443 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 return NO_ERROR;
445}
446
447status_t Parcel::setData(const uint8_t* buffer, size_t len)
448{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700449 if (len > INT32_MAX) {
450 // don't accept size_t values which may have come from an
451 // inadvertent conversion from a negative int.
452 return BAD_VALUE;
453 }
454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 status_t err = restartWrite(len);
456 if (err == NO_ERROR) {
457 memcpy(const_cast<uint8_t*>(data()), buffer, len);
458 mDataSize = len;
459 mFdsKnown = false;
460 }
461 return err;
462}
463
Andreas Huber51faf462011-04-13 10:21:56 -0700464status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465{
466 const sp<ProcessState> proc(ProcessState::self());
467 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700468 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800469 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 size_t size = parcel->mObjectsSize;
471 int startPos = mDataPos;
472 int firstIndex = -1, lastIndex = -2;
473
474 if (len == 0) {
475 return NO_ERROR;
476 }
477
Nick Kralevichb6b14232015-04-02 09:36:02 -0700478 if (len > INT32_MAX) {
479 // don't accept size_t values which may have come from an
480 // inadvertent conversion from a negative int.
481 return BAD_VALUE;
482 }
483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 // range checks against the source parcel size
485 if ((offset > parcel->mDataSize)
486 || (len > parcel->mDataSize)
487 || (offset + len > parcel->mDataSize)) {
488 return BAD_VALUE;
489 }
490
491 // Count objects in range
492 for (int i = 0; i < (int) size; i++) {
493 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700494 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700495 if (firstIndex == -1) {
496 firstIndex = i;
497 }
498 lastIndex = i;
499 }
500 }
501 int numObjects = lastIndex - firstIndex + 1;
502
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700503 if ((mDataSize+len) > mDataCapacity) {
504 // grow data
505 err = growData(len);
506 if (err != NO_ERROR) {
507 return err;
508 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 }
510
511 // append data
512 memcpy(mData + mDataPos, data + offset, len);
513 mDataPos += len;
514 mDataSize += len;
515
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400516 err = NO_ERROR;
517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 if (numObjects > 0) {
519 // grow objects
520 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700521 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
522 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800523 binder_size_t *objects =
524 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
525 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700526 return NO_MEMORY;
527 }
528 mObjects = objects;
529 mObjectsCapacity = newSize;
530 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532 // append and acquire objects
533 int idx = mObjectsSize;
534 for (int i = firstIndex; i <= lastIndex; i++) {
535 size_t off = objects[i] - offset + startPos;
536 mObjects[idx++] = off;
537 mObjectsSize++;
538
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700539 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700541 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700544 // If this is a file descriptor, we need to dup it so the
545 // new Parcel now owns its own fd, and can declare that we
546 // officially know we have fds.
547 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800548 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700549 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400550 if (!mAllowFds) {
551 err = FDS_NOT_ALLOWED;
552 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 }
554 }
555 }
556
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400557 return err;
558}
559
Jeff Brown13b16042014-11-11 16:44:25 -0800560bool Parcel::allowFds() const
561{
562 return mAllowFds;
563}
564
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700565bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400566{
567 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700568 if (!allowFds) {
569 mAllowFds = false;
570 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400571 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572}
573
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700574void Parcel::restoreAllowFds(bool lastValue)
575{
576 mAllowFds = lastValue;
577}
578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579bool Parcel::hasFileDescriptors() const
580{
581 if (!mFdsKnown) {
582 scanForFds();
583 }
584 return mHasFds;
585}
586
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700587// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700588status_t Parcel::writeInterfaceToken(const String16& interface)
589{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700590 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
591 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700592 // currently the interface identification token is just its name as a string
593 return writeString16(interface);
594}
595
Mathias Agopian83c04462009-05-22 19:00:22 -0700596bool Parcel::checkInterface(IBinder* binder) const
597{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700598 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700599}
600
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700601bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700602 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700604 int32_t strictPolicy = readInt32();
605 if (threadState == NULL) {
606 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700607 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700608 if ((threadState->getLastTransactionBinderFlags() &
609 IBinder::FLAG_ONEWAY) != 0) {
610 // For one-way calls, the callee is running entirely
611 // disconnected from the caller, so disable StrictMode entirely.
612 // Not only does disk/network usage not impact the caller, but
613 // there's no way to commuicate back any violations anyway.
614 threadState->setStrictModePolicy(0);
615 } else {
616 threadState->setStrictModePolicy(strictPolicy);
617 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700618 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619 if (str == interface) {
620 return true;
621 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700622 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700623 String8(interface).string(), String8(str).string());
624 return false;
625 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700626}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800628const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629{
630 return mObjects;
631}
632
633size_t Parcel::objectsCount() const
634{
635 return mObjectsSize;
636}
637
638status_t Parcel::errorCheck() const
639{
640 return mError;
641}
642
643void Parcel::setError(status_t err)
644{
645 mError = err;
646}
647
648status_t Parcel::finishWrite(size_t len)
649{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700650 if (len > INT32_MAX) {
651 // don't accept size_t values which may have come from an
652 // inadvertent conversion from a negative int.
653 return BAD_VALUE;
654 }
655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656 //printf("Finish write of %d\n", len);
657 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700658 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700659 if (mDataPos > mDataSize) {
660 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700661 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700662 }
663 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
664 return NO_ERROR;
665}
666
667status_t Parcel::writeUnpadded(const void* data, size_t len)
668{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700669 if (len > INT32_MAX) {
670 // don't accept size_t values which may have come from an
671 // inadvertent conversion from a negative int.
672 return BAD_VALUE;
673 }
674
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700675 size_t end = mDataPos + len;
676 if (end < mDataPos) {
677 // integer overflow
678 return BAD_VALUE;
679 }
680
681 if (end <= mDataCapacity) {
682restart_write:
683 memcpy(mData+mDataPos, data, len);
684 return finishWrite(len);
685 }
686
687 status_t err = growData(len);
688 if (err == NO_ERROR) goto restart_write;
689 return err;
690}
691
692status_t Parcel::write(const void* data, size_t len)
693{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700694 if (len > INT32_MAX) {
695 // don't accept size_t values which may have come from an
696 // inadvertent conversion from a negative int.
697 return BAD_VALUE;
698 }
699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700 void* const d = writeInplace(len);
701 if (d) {
702 memcpy(d, data, len);
703 return NO_ERROR;
704 }
705 return mError;
706}
707
708void* Parcel::writeInplace(size_t len)
709{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700710 if (len > INT32_MAX) {
711 // don't accept size_t values which may have come from an
712 // inadvertent conversion from a negative int.
713 return NULL;
714 }
715
716 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700717
718 // sanity check for integer overflow
719 if (mDataPos+padded < mDataPos) {
720 return NULL;
721 }
722
723 if ((mDataPos+padded) <= mDataCapacity) {
724restart_write:
725 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
726 uint8_t* const data = mData+mDataPos;
727
728 // Need to pad at end?
729 if (padded != len) {
730#if BYTE_ORDER == BIG_ENDIAN
731 static const uint32_t mask[4] = {
732 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
733 };
734#endif
735#if BYTE_ORDER == LITTLE_ENDIAN
736 static const uint32_t mask[4] = {
737 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
738 };
739#endif
740 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
741 // *reinterpret_cast<void**>(data+padded-4));
742 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
743 }
744
745 finishWrite(padded);
746 return data;
747 }
748
749 status_t err = growData(padded);
750 if (err == NO_ERROR) goto restart_write;
751 return NULL;
752}
753
Casey Dahlinb9872622015-11-25 15:09:45 -0800754status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
755{
756 if (!val) {
757 return writeInt32(-1);
758 }
759
760 return writeByteVector(*val);
761}
762
Casey Dahlin451ff582015-10-19 18:12:18 -0700763status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
764{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700765 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700766 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700767 status = BAD_VALUE;
768 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700769 }
770
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700771 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700772 if (status != OK) {
773 return status;
774 }
775
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700776 void* data = writeInplace(val.size());
777 if (!data) {
778 status = BAD_VALUE;
779 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700780 }
781
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700782 memcpy(data, val.data(), val.size());
783 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700784}
785
786status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
787{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800788 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700789}
790
Casey Dahlinb9872622015-11-25 15:09:45 -0800791status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
792{
793 return writeNullableTypedVector(val, &Parcel::writeInt32);
794}
795
Casey Dahlin451ff582015-10-19 18:12:18 -0700796status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
797{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800798 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700799}
800
Casey Dahlinb9872622015-11-25 15:09:45 -0800801status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
802{
803 return writeNullableTypedVector(val, &Parcel::writeInt64);
804}
805
Casey Dahlin451ff582015-10-19 18:12:18 -0700806status_t Parcel::writeFloatVector(const std::vector<float>& val)
807{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800808 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700809}
810
Casey Dahlinb9872622015-11-25 15:09:45 -0800811status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
812{
813 return writeNullableTypedVector(val, &Parcel::writeFloat);
814}
815
Casey Dahlin451ff582015-10-19 18:12:18 -0700816status_t Parcel::writeDoubleVector(const std::vector<double>& val)
817{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800818 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700819}
820
Casey Dahlinb9872622015-11-25 15:09:45 -0800821status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
822{
823 return writeNullableTypedVector(val, &Parcel::writeDouble);
824}
825
Casey Dahlin451ff582015-10-19 18:12:18 -0700826status_t Parcel::writeBoolVector(const std::vector<bool>& val)
827{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800828 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700829}
830
Casey Dahlinb9872622015-11-25 15:09:45 -0800831status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
832{
833 return writeNullableTypedVector(val, &Parcel::writeBool);
834}
835
Casey Dahlin451ff582015-10-19 18:12:18 -0700836status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
837{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800838 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700839}
840
Casey Dahlinb9872622015-11-25 15:09:45 -0800841status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
842{
843 return writeNullableTypedVector(val, &Parcel::writeChar);
844}
845
Casey Dahlin451ff582015-10-19 18:12:18 -0700846status_t Parcel::writeString16Vector(const std::vector<String16>& val)
847{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800848 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700849}
850
Casey Dahlinb9872622015-11-25 15:09:45 -0800851status_t Parcel::writeString16Vector(
852 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
853{
854 return writeNullableTypedVector(val, &Parcel::writeString16);
855}
856
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700857status_t Parcel::writeInt32(int32_t val)
858{
Andreas Huber84a6d042009-08-17 13:33:27 -0700859 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700860}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800861
862status_t Parcel::writeUint32(uint32_t val)
863{
864 return writeAligned(val);
865}
866
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700867status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700868 if (len > INT32_MAX) {
869 // don't accept size_t values which may have come from an
870 // inadvertent conversion from a negative int.
871 return BAD_VALUE;
872 }
873
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700874 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700875 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700876 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700877 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700878 if (ret == NO_ERROR) {
879 ret = write(val, len * sizeof(*val));
880 }
881 return ret;
882}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700883status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700884 if (len > INT32_MAX) {
885 // don't accept size_t values which may have come from an
886 // inadvertent conversion from a negative int.
887 return BAD_VALUE;
888 }
889
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700890 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700891 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700892 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700893 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700894 if (ret == NO_ERROR) {
895 ret = write(val, len * sizeof(*val));
896 }
897 return ret;
898}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700899
Casey Dahlind6848f52015-10-15 15:44:59 -0700900status_t Parcel::writeBool(bool val)
901{
902 return writeInt32(int32_t(val));
903}
904
905status_t Parcel::writeChar(char16_t val)
906{
907 return writeInt32(int32_t(val));
908}
909
910status_t Parcel::writeByte(int8_t val)
911{
912 return writeInt32(int32_t(val));
913}
914
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700915status_t Parcel::writeInt64(int64_t val)
916{
Andreas Huber84a6d042009-08-17 13:33:27 -0700917 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700918}
919
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700920status_t Parcel::writeUint64(uint64_t val)
921{
922 return writeAligned(val);
923}
924
Serban Constantinescuf683e012013-11-05 16:53:55 +0000925status_t Parcel::writePointer(uintptr_t val)
926{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800927 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000928}
929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930status_t Parcel::writeFloat(float val)
931{
Andreas Huber84a6d042009-08-17 13:33:27 -0700932 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933}
934
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800935#if defined(__mips__) && defined(__mips_hard_float)
936
937status_t Parcel::writeDouble(double val)
938{
939 union {
940 double d;
941 unsigned long long ll;
942 } u;
943 u.d = val;
944 return writeAligned(u.ll);
945}
946
947#else
948
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700949status_t Parcel::writeDouble(double val)
950{
Andreas Huber84a6d042009-08-17 13:33:27 -0700951 return writeAligned(val);
952}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800954#endif
955
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956status_t Parcel::writeCString(const char* str)
957{
958 return write(str, strlen(str)+1);
959}
960
961status_t Parcel::writeString8(const String8& str)
962{
963 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100964 // only write string if its length is more than zero characters,
965 // as readString8 will only read if the length field is non-zero.
966 // this is slightly different from how writeString16 works.
967 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968 err = write(str.string(), str.bytes()+1);
969 }
970 return err;
971}
972
Casey Dahlinb9872622015-11-25 15:09:45 -0800973status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
974{
975 if (!str) {
976 return writeInt32(-1);
977 }
978
979 return writeString16(*str);
980}
981
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982status_t Parcel::writeString16(const String16& str)
983{
984 return writeString16(str.string(), str.size());
985}
986
987status_t Parcel::writeString16(const char16_t* str, size_t len)
988{
989 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991 status_t err = writeInt32(len);
992 if (err == NO_ERROR) {
993 len *= sizeof(char16_t);
994 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
995 if (data) {
996 memcpy(data, str, len);
997 *reinterpret_cast<char16_t*>(data+len) = 0;
998 return NO_ERROR;
999 }
1000 err = mError;
1001 }
1002 return err;
1003}
1004
1005status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1006{
1007 return flatten_binder(ProcessState::self(), val, this);
1008}
1009
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001010status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1011{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001012 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001013}
1014
Casey Dahlinb9872622015-11-25 15:09:45 -08001015status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1016{
1017 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1018}
1019
1020status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1021 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1022}
1023
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001024status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001025 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001026}
1027
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1029{
1030 return flatten_binder(ProcessState::self(), val, this);
1031}
1032
Casey Dahlinb9872622015-11-25 15:09:45 -08001033status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1034 if (!parcelable) {
1035 return writeInt32(0);
1036 }
1037
1038 return writeParcelable(*parcelable);
1039}
1040
Christopher Wiley97f048d2015-11-19 06:49:05 -08001041status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1042 status_t status = writeInt32(1); // parcelable is not null.
1043 if (status != OK) {
1044 return status;
1045 }
1046 return parcelable.writeToParcel(this);
1047}
1048
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001049status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001050{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001051 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001052 return BAD_TYPE;
1053
1054 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001055 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001056 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001058 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001059 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001061 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1062 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
1064 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001065 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 return err;
1067 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001068 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001069 return err;
1070}
1071
Jeff Brown93ff1f92011-11-04 19:01:44 -07001072status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073{
1074 flat_binder_object obj;
1075 obj.type = BINDER_TYPE_FD;
1076 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001077 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001078 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001079 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080 return writeObject(obj, true);
1081}
1082
1083status_t Parcel::writeDupFileDescriptor(int fd)
1084{
Jeff Brownd341c712011-11-04 20:19:33 -07001085 int dupFd = dup(fd);
1086 if (dupFd < 0) {
1087 return -errno;
1088 }
1089 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001090 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001091 close(dupFd);
1092 }
1093 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094}
1095
Casey Dahlin06673e32015-11-23 13:24:23 -08001096status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1097 return writeDupFileDescriptor(fd.get());
1098}
1099
1100status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1101 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1102}
1103
Casey Dahlinb9872622015-11-25 15:09:45 -08001104status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1105 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1106}
1107
Jeff Brown13b16042014-11-11 16:44:25 -08001108status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001109{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001110 if (len > INT32_MAX) {
1111 // don't accept size_t values which may have come from an
1112 // inadvertent conversion from a negative int.
1113 return BAD_VALUE;
1114 }
1115
Jeff Brown13b16042014-11-11 16:44:25 -08001116 status_t status;
1117 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001118 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001119 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001120 if (status) return status;
1121
1122 void* ptr = writeInplace(len);
1123 if (!ptr) return NO_MEMORY;
1124
Jeff Brown13b16042014-11-11 16:44:25 -08001125 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001126 return NO_ERROR;
1127 }
1128
Steve Block6807e592011-10-20 11:56:00 +01001129 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001130 int fd = ashmem_create_region("Parcel Blob", len);
1131 if (fd < 0) return NO_MEMORY;
1132
1133 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1134 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001135 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001136 } else {
1137 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1138 if (ptr == MAP_FAILED) {
1139 status = -errno;
1140 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001141 if (!mutableCopy) {
1142 result = ashmem_set_prot_region(fd, PROT_READ);
1143 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001144 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001145 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001146 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001147 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001148 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001149 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001150 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001151 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001152 return NO_ERROR;
1153 }
1154 }
1155 }
1156 }
1157 ::munmap(ptr, len);
1158 }
1159 ::close(fd);
1160 return status;
1161}
1162
Jeff Brown13b16042014-11-11 16:44:25 -08001163status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1164{
1165 // Must match up with what's done in writeBlob.
1166 if (!mAllowFds) return FDS_NOT_ALLOWED;
1167 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1168 if (status) return status;
1169 return writeDupFileDescriptor(fd);
1170}
1171
Mathias Agopiane1424282013-07-29 21:24:40 -07001172status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001173{
1174 status_t err;
1175
1176 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001177 const size_t len = val.getFlattenedSize();
1178 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001179
Nick Kralevichb6b14232015-04-02 09:36:02 -07001180 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1181 // don't accept size_t values which may have come from an
1182 // inadvertent conversion from a negative int.
1183 return BAD_VALUE;
1184 }
1185
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001186 err = this->writeInt32(len);
1187 if (err) return err;
1188
1189 err = this->writeInt32(fd_count);
1190 if (err) return err;
1191
1192 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001193 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001194 if (buf == NULL)
1195 return BAD_VALUE;
1196
1197 int* fds = NULL;
1198 if (fd_count) {
1199 fds = new int[fd_count];
1200 }
1201
1202 err = val.flatten(buf, len, fds, fd_count);
1203 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1204 err = this->writeDupFileDescriptor( fds[i] );
1205 }
1206
1207 if (fd_count) {
1208 delete [] fds;
1209 }
1210
1211 return err;
1212}
1213
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001214status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1215{
1216 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1217 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1218 if (enoughData && enoughObjects) {
1219restart_write:
1220 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001221
Christopher Tate98e67d32015-06-03 18:44:15 -07001222 // remember if it's a file descriptor
1223 if (val.type == BINDER_TYPE_FD) {
1224 if (!mAllowFds) {
1225 // fail before modifying our object index
1226 return FDS_NOT_ALLOWED;
1227 }
1228 mHasFds = mFdsKnown = true;
1229 }
1230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001232 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001233 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001234 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001235 mObjectsSize++;
1236 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238 return finishWrite(sizeof(flat_binder_object));
1239 }
1240
1241 if (!enoughData) {
1242 const status_t err = growData(sizeof(val));
1243 if (err != NO_ERROR) return err;
1244 }
1245 if (!enoughObjects) {
1246 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001247 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001248 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249 if (objects == NULL) return NO_MEMORY;
1250 mObjects = objects;
1251 mObjectsCapacity = newSize;
1252 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001254 goto restart_write;
1255}
1256
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001257status_t Parcel::writeNoException()
1258{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001259 binder::Status status;
1260 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001261}
1262
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001263void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001264{
1265 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1266}
1267
1268status_t Parcel::read(void* outData, size_t len) const
1269{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001270 if (len > INT32_MAX) {
1271 // don't accept size_t values which may have come from an
1272 // inadvertent conversion from a negative int.
1273 return BAD_VALUE;
1274 }
1275
1276 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1277 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001279 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001280 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001281 return NO_ERROR;
1282 }
1283 return NOT_ENOUGH_DATA;
1284}
1285
1286const void* Parcel::readInplace(size_t len) const
1287{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001288 if (len > INT32_MAX) {
1289 // don't accept size_t values which may have come from an
1290 // inadvertent conversion from a negative int.
1291 return NULL;
1292 }
1293
1294 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1295 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001297 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001298 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299 return data;
1300 }
1301 return NULL;
1302}
1303
Andreas Huber84a6d042009-08-17 13:33:27 -07001304template<class T>
1305status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001306 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001307
1308 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001309 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001310 mDataPos += sizeof(T);
1311 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001312 return NO_ERROR;
1313 } else {
1314 return NOT_ENOUGH_DATA;
1315 }
1316}
1317
Andreas Huber84a6d042009-08-17 13:33:27 -07001318template<class T>
1319T Parcel::readAligned() const {
1320 T result;
1321 if (readAligned(&result) != NO_ERROR) {
1322 result = 0;
1323 }
1324
1325 return result;
1326}
1327
1328template<class T>
1329status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001330 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001331
1332 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1333restart_write:
1334 *reinterpret_cast<T*>(mData+mDataPos) = val;
1335 return finishWrite(sizeof(val));
1336 }
1337
1338 status_t err = growData(sizeof(val));
1339 if (err == NO_ERROR) goto restart_write;
1340 return err;
1341}
1342
Casey Dahlin451ff582015-10-19 18:12:18 -07001343status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1344 val->clear();
1345
1346 int32_t size;
1347 status_t status = readInt32(&size);
1348
1349 if (status != OK) {
1350 return status;
1351 }
1352
Christopher Wiley4db672d2015-11-10 09:44:30 -08001353 if (size < 0) {
1354 status = UNEXPECTED_NULL;
1355 return status;
1356 }
1357 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001358 status = BAD_VALUE;
1359 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001360 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001361
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001362 const void* data = readInplace(size);
1363 if (!data) {
1364 status = BAD_VALUE;
1365 return status;
1366 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001367 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001368 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001369
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001370 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001371}
1372
Casey Dahlinb9872622015-11-25 15:09:45 -08001373status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1374 const int32_t start = dataPosition();
1375 int32_t size;
1376 status_t status = readInt32(&size);
1377 val->reset();
1378
1379 if (status != OK || size < 0) {
1380 return status;
1381 }
1382
1383 setDataPosition(start);
1384 val->reset(new std::vector<int8_t>());
1385
1386 status = readByteVector(val->get());
1387
1388 if (status != OK) {
1389 val->reset();
1390 }
1391
1392 return status;
1393}
1394
1395status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1396 return readNullableTypedVector(val, &Parcel::readInt32);
1397}
1398
Casey Dahlin451ff582015-10-19 18:12:18 -07001399status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001400 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001401}
1402
Casey Dahlinb9872622015-11-25 15:09:45 -08001403status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1404 return readNullableTypedVector(val, &Parcel::readInt64);
1405}
1406
Casey Dahlin451ff582015-10-19 18:12:18 -07001407status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001408 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001409}
1410
Casey Dahlinb9872622015-11-25 15:09:45 -08001411status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1412 return readNullableTypedVector(val, &Parcel::readFloat);
1413}
1414
Casey Dahlin451ff582015-10-19 18:12:18 -07001415status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001416 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001417}
1418
Casey Dahlinb9872622015-11-25 15:09:45 -08001419status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1420 return readNullableTypedVector(val, &Parcel::readDouble);
1421}
1422
Casey Dahlin451ff582015-10-19 18:12:18 -07001423status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001424 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001425}
1426
Casey Dahlinb9872622015-11-25 15:09:45 -08001427status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1428 const int32_t start = dataPosition();
1429 int32_t size;
1430 status_t status = readInt32(&size);
1431 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001432
Casey Dahlinb9872622015-11-25 15:09:45 -08001433 if (status != OK || size < 0) {
1434 return status;
1435 }
1436
1437 setDataPosition(start);
1438 val->reset(new std::vector<bool>());
1439
1440 status = readBoolVector(val->get());
1441
1442 if (status != OK) {
1443 val->reset();
1444 }
1445
1446 return status;
1447}
1448
1449status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001450 int32_t size;
1451 status_t status = readInt32(&size);
1452
1453 if (status != OK) {
1454 return status;
1455 }
1456
1457 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001458 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001459 }
1460
1461 val->resize(size);
1462
1463 /* C++ bool handling means a vector of bools isn't necessarily addressable
1464 * (we might use individual bits)
1465 */
Christopher Wiley97887982015-10-27 16:33:47 -07001466 bool data;
1467 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001468 status = readBool(&data);
1469 (*val)[i] = data;
1470
1471 if (status != OK) {
1472 return status;
1473 }
1474 }
1475
1476 return OK;
1477}
1478
Casey Dahlinb9872622015-11-25 15:09:45 -08001479status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1480 return readNullableTypedVector(val, &Parcel::readChar);
1481}
1482
Casey Dahlin451ff582015-10-19 18:12:18 -07001483status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001484 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001485}
1486
Casey Dahlinb9872622015-11-25 15:09:45 -08001487status_t Parcel::readString16Vector(
1488 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1489 return readNullableTypedVector(val, &Parcel::readString16);
1490}
1491
Casey Dahlin451ff582015-10-19 18:12:18 -07001492status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001493 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001494}
1495
1496
Andreas Huber84a6d042009-08-17 13:33:27 -07001497status_t Parcel::readInt32(int32_t *pArg) const
1498{
1499 return readAligned(pArg);
1500}
1501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502int32_t Parcel::readInt32() const
1503{
Andreas Huber84a6d042009-08-17 13:33:27 -07001504 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001505}
1506
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001507status_t Parcel::readUint32(uint32_t *pArg) const
1508{
1509 return readAligned(pArg);
1510}
1511
1512uint32_t Parcel::readUint32() const
1513{
1514 return readAligned<uint32_t>();
1515}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516
1517status_t Parcel::readInt64(int64_t *pArg) const
1518{
Andreas Huber84a6d042009-08-17 13:33:27 -07001519 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001520}
1521
1522
1523int64_t Parcel::readInt64() const
1524{
Andreas Huber84a6d042009-08-17 13:33:27 -07001525 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526}
1527
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001528status_t Parcel::readUint64(uint64_t *pArg) const
1529{
1530 return readAligned(pArg);
1531}
1532
1533uint64_t Parcel::readUint64() const
1534{
1535 return readAligned<uint64_t>();
1536}
1537
Serban Constantinescuf683e012013-11-05 16:53:55 +00001538status_t Parcel::readPointer(uintptr_t *pArg) const
1539{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001540 status_t ret;
1541 binder_uintptr_t ptr;
1542 ret = readAligned(&ptr);
1543 if (!ret)
1544 *pArg = ptr;
1545 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001546}
1547
1548uintptr_t Parcel::readPointer() const
1549{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001550 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001551}
1552
1553
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001554status_t Parcel::readFloat(float *pArg) const
1555{
Andreas Huber84a6d042009-08-17 13:33:27 -07001556 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001557}
1558
1559
1560float Parcel::readFloat() const
1561{
Andreas Huber84a6d042009-08-17 13:33:27 -07001562 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001563}
1564
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001565#if defined(__mips__) && defined(__mips_hard_float)
1566
1567status_t Parcel::readDouble(double *pArg) const
1568{
1569 union {
1570 double d;
1571 unsigned long long ll;
1572 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001573 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001574 status_t status;
1575 status = readAligned(&u.ll);
1576 *pArg = u.d;
1577 return status;
1578}
1579
1580double Parcel::readDouble() const
1581{
1582 union {
1583 double d;
1584 unsigned long long ll;
1585 } u;
1586 u.ll = readAligned<unsigned long long>();
1587 return u.d;
1588}
1589
1590#else
1591
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592status_t Parcel::readDouble(double *pArg) const
1593{
Andreas Huber84a6d042009-08-17 13:33:27 -07001594 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595}
1596
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001597double Parcel::readDouble() const
1598{
Andreas Huber84a6d042009-08-17 13:33:27 -07001599 return readAligned<double>();
1600}
1601
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001602#endif
1603
Andreas Huber84a6d042009-08-17 13:33:27 -07001604status_t Parcel::readIntPtr(intptr_t *pArg) const
1605{
1606 return readAligned(pArg);
1607}
1608
1609
1610intptr_t Parcel::readIntPtr() const
1611{
1612 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613}
1614
Casey Dahlind6848f52015-10-15 15:44:59 -07001615status_t Parcel::readBool(bool *pArg) const
1616{
1617 int32_t tmp;
1618 status_t ret = readInt32(&tmp);
1619 *pArg = (tmp != 0);
1620 return ret;
1621}
1622
1623bool Parcel::readBool() const
1624{
1625 return readInt32() != 0;
1626}
1627
1628status_t Parcel::readChar(char16_t *pArg) const
1629{
1630 int32_t tmp;
1631 status_t ret = readInt32(&tmp);
1632 *pArg = char16_t(tmp);
1633 return ret;
1634}
1635
1636char16_t Parcel::readChar() const
1637{
1638 return char16_t(readInt32());
1639}
1640
1641status_t Parcel::readByte(int8_t *pArg) const
1642{
1643 int32_t tmp;
1644 status_t ret = readInt32(&tmp);
1645 *pArg = int8_t(tmp);
1646 return ret;
1647}
1648
1649int8_t Parcel::readByte() const
1650{
1651 return int8_t(readInt32());
1652}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001653
1654const char* Parcel::readCString() const
1655{
1656 const size_t avail = mDataSize-mDataPos;
1657 if (avail > 0) {
1658 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1659 // is the string's trailing NUL within the parcel's valid bounds?
1660 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1661 if (eos) {
1662 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001663 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001664 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665 return str;
1666 }
1667 }
1668 return NULL;
1669}
1670
1671String8 Parcel::readString8() const
1672{
1673 int32_t size = readInt32();
1674 // watch for potential int overflow adding 1 for trailing NUL
1675 if (size > 0 && size < INT32_MAX) {
1676 const char* str = (const char*)readInplace(size+1);
1677 if (str) return String8(str, size);
1678 }
1679 return String8();
1680}
1681
1682String16 Parcel::readString16() const
1683{
1684 size_t len;
1685 const char16_t* str = readString16Inplace(&len);
1686 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001687 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688 return String16();
1689}
1690
Casey Dahlinb9872622015-11-25 15:09:45 -08001691status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1692{
1693 const int32_t start = dataPosition();
1694 int32_t size;
1695 status_t status = readInt32(&size);
1696 pArg->reset();
1697
1698 if (status != OK || size < 0) {
1699 return status;
1700 }
1701
1702 setDataPosition(start);
1703 pArg->reset(new String16());
1704
1705 status = readString16(pArg->get());
1706
1707 if (status != OK) {
1708 pArg->reset();
1709 }
1710
1711 return status;
1712}
1713
Casey Dahlin451ff582015-10-19 18:12:18 -07001714status_t Parcel::readString16(String16* pArg) const
1715{
1716 size_t len;
1717 const char16_t* str = readString16Inplace(&len);
1718 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001719 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001720 return 0;
1721 } else {
1722 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001723 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001724 }
1725}
1726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1728{
1729 int32_t size = readInt32();
1730 // watch for potential int overflow from size+1
1731 if (size >= 0 && size < INT32_MAX) {
1732 *outLen = size;
1733 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1734 if (str != NULL) {
1735 return str;
1736 }
1737 }
1738 *outLen = 0;
1739 return NULL;
1740}
1741
Casey Dahlinf0c13772015-10-27 18:33:56 -07001742status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1743{
1744 return unflatten_binder(ProcessState::self(), *this, val);
1745}
1746
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001747sp<IBinder> Parcel::readStrongBinder() const
1748{
1749 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001750 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751 return val;
1752}
1753
1754wp<IBinder> Parcel::readWeakBinder() const
1755{
1756 wp<IBinder> val;
1757 unflatten_binder(ProcessState::self(), *this, &val);
1758 return val;
1759}
1760
Christopher Wiley97f048d2015-11-19 06:49:05 -08001761status_t Parcel::readParcelable(Parcelable* parcelable) const {
1762 int32_t have_parcelable = 0;
1763 status_t status = readInt32(&have_parcelable);
1764 if (status != OK) {
1765 return status;
1766 }
1767 if (!have_parcelable) {
1768 return UNEXPECTED_NULL;
1769 }
1770 return parcelable->readFromParcel(this);
1771}
1772
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001773int32_t Parcel::readExceptionCode() const
1774{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001775 binder::Status status;
1776 status.readFromParcel(*this);
1777 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001778}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001779
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001780native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001781{
1782 int numFds, numInts;
1783 status_t err;
1784 err = readInt32(&numFds);
1785 if (err != NO_ERROR) return 0;
1786 err = readInt32(&numInts);
1787 if (err != NO_ERROR) return 0;
1788
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001789 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001790 if (!h) {
1791 return 0;
1792 }
1793
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001794 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001795 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001796 if (h->data[i] < 0) err = BAD_VALUE;
1797 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001798 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001799 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001800 native_handle_close(h);
1801 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001802 h = 0;
1803 }
1804 return h;
1805}
1806
1807
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808int Parcel::readFileDescriptor() const
1809{
1810 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001811
1812 if (flat && flat->type == BINDER_TYPE_FD) {
1813 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001815
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001816 return BAD_TYPE;
1817}
1818
Casey Dahlin06673e32015-11-23 13:24:23 -08001819status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1820{
1821 int got = readFileDescriptor();
1822
1823 if (got == BAD_TYPE) {
1824 return BAD_TYPE;
1825 }
1826
1827 val->reset(dup(got));
1828
1829 if (val->get() < 0) {
1830 return BAD_VALUE;
1831 }
1832
1833 return OK;
1834}
1835
1836
Casey Dahlinb9872622015-11-25 15:09:45 -08001837status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1838 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1839}
1840
Casey Dahlin06673e32015-11-23 13:24:23 -08001841status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1842 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1843}
1844
Jeff Brown5707dbf2011-09-23 21:17:56 -07001845status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1846{
Jeff Brown13b16042014-11-11 16:44:25 -08001847 int32_t blobType;
1848 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001849 if (status) return status;
1850
Jeff Brown13b16042014-11-11 16:44:25 -08001851 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001852 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001853 const void* ptr = readInplace(len);
1854 if (!ptr) return BAD_VALUE;
1855
Jeff Brown13b16042014-11-11 16:44:25 -08001856 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001857 return NO_ERROR;
1858 }
1859
Steve Block6807e592011-10-20 11:56:00 +01001860 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001861 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001862 int fd = readFileDescriptor();
1863 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1864
Jeff Brown13b16042014-11-11 16:44:25 -08001865 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1866 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001867 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001868
Jeff Brown13b16042014-11-11 16:44:25 -08001869 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001870 return NO_ERROR;
1871}
1872
Mathias Agopiane1424282013-07-29 21:24:40 -07001873status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001874{
1875 // size
1876 const size_t len = this->readInt32();
1877 const size_t fd_count = this->readInt32();
1878
Nick Kralevichb6b14232015-04-02 09:36:02 -07001879 if (len > INT32_MAX) {
1880 // don't accept size_t values which may have come from an
1881 // inadvertent conversion from a negative int.
1882 return BAD_VALUE;
1883 }
1884
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001885 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001886 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001887 if (buf == NULL)
1888 return BAD_VALUE;
1889
1890 int* fds = NULL;
1891 if (fd_count) {
1892 fds = new int[fd_count];
1893 }
1894
1895 status_t err = NO_ERROR;
1896 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001897 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001898 if (fds[i] < 0) {
1899 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001900 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1901 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001902 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001903 }
1904
1905 if (err == NO_ERROR) {
1906 err = val.unflatten(buf, len, fds, fd_count);
1907 }
1908
1909 if (fd_count) {
1910 delete [] fds;
1911 }
1912
1913 return err;
1914}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001915const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1916{
1917 const size_t DPOS = mDataPos;
1918 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1919 const flat_binder_object* obj
1920 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1921 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001922 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001923 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924 // the object list, so we don't want to check for it when
1925 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001926 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 return obj;
1928 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001931 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932 const size_t N = mObjectsSize;
1933 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001936 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001937 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001938
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 // Start at the current hint position, looking for an object at
1940 // the current data position.
1941 if (opos < N) {
1942 while (opos < (N-1) && OBJS[opos] < DPOS) {
1943 opos++;
1944 }
1945 } else {
1946 opos = N-1;
1947 }
1948 if (OBJS[opos] == DPOS) {
1949 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001950 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951 this, DPOS, opos);
1952 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001953 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001954 return obj;
1955 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001956
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 // Look backwards for it...
1958 while (opos > 0 && OBJS[opos] > DPOS) {
1959 opos--;
1960 }
1961 if (OBJS[opos] == DPOS) {
1962 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001963 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001964 this, DPOS, opos);
1965 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001966 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967 return obj;
1968 }
1969 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001970 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 -07001971 this, DPOS);
1972 }
1973 return NULL;
1974}
1975
1976void Parcel::closeFileDescriptors()
1977{
1978 size_t i = mObjectsSize;
1979 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001980 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001981 }
1982 while (i > 0) {
1983 i--;
1984 const flat_binder_object* flat
1985 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1986 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001987 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988 close(flat->handle);
1989 }
1990 }
1991}
1992
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001993uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001995 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001996}
1997
1998size_t Parcel::ipcDataSize() const
1999{
2000 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2001}
2002
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002003uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002004{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002005 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002006}
2007
2008size_t Parcel::ipcObjectsCount() const
2009{
2010 return mObjectsSize;
2011}
2012
2013void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002014 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002015{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002016 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017 freeDataNoInit();
2018 mError = NO_ERROR;
2019 mData = const_cast<uint8_t*>(data);
2020 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002021 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002023 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002024 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025 mObjectsSize = mObjectsCapacity = objectsCount;
2026 mNextObjectHint = 0;
2027 mOwner = relFunc;
2028 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002029 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002030 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002031 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002032 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002033 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002034 mObjectsSize = 0;
2035 break;
2036 }
2037 minOffset = offset + sizeof(flat_binder_object);
2038 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002039 scanForFds();
2040}
2041
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002042void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043{
2044 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 if (errorCheck() != NO_ERROR) {
2047 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002048 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049 } else if (dataSize() > 0) {
2050 const uint8_t* DATA = data();
2051 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002052 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 const size_t N = objectsCount();
2054 for (size_t i=0; i<N; i++) {
2055 const flat_binder_object* flat
2056 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2057 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2058 << TypeCode(flat->type & 0x7f7f7f00)
2059 << " = " << flat->binder;
2060 }
2061 } else {
2062 to << "NULL";
2063 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002064
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 to << ")";
2066}
2067
2068void Parcel::releaseObjects()
2069{
2070 const sp<ProcessState> proc(ProcessState::self());
2071 size_t i = mObjectsSize;
2072 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002073 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 while (i > 0) {
2075 i--;
2076 const flat_binder_object* flat
2077 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002078 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 }
2080}
2081
2082void Parcel::acquireObjects()
2083{
2084 const sp<ProcessState> proc(ProcessState::self());
2085 size_t i = mObjectsSize;
2086 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002087 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002088 while (i > 0) {
2089 i--;
2090 const flat_binder_object* flat
2091 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002092 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093 }
2094}
2095
2096void Parcel::freeData()
2097{
2098 freeDataNoInit();
2099 initState();
2100}
2101
2102void Parcel::freeDataNoInit()
2103{
2104 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002105 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002106 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2108 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002109 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002110 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002111 if (mData) {
2112 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002113 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002114 if (mDataCapacity <= gParcelGlobalAllocSize) {
2115 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2116 } else {
2117 gParcelGlobalAllocSize = 0;
2118 }
2119 if (gParcelGlobalAllocCount > 0) {
2120 gParcelGlobalAllocCount--;
2121 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002122 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002123 free(mData);
2124 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125 if (mObjects) free(mObjects);
2126 }
2127}
2128
2129status_t Parcel::growData(size_t len)
2130{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002131 if (len > INT32_MAX) {
2132 // don't accept size_t values which may have come from an
2133 // inadvertent conversion from a negative int.
2134 return BAD_VALUE;
2135 }
2136
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137 size_t newSize = ((mDataSize+len)*3)/2;
2138 return (newSize <= mDataSize)
2139 ? (status_t) NO_MEMORY
2140 : continueWrite(newSize);
2141}
2142
2143status_t Parcel::restartWrite(size_t desired)
2144{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002145 if (desired > INT32_MAX) {
2146 // don't accept size_t values which may have come from an
2147 // inadvertent conversion from a negative int.
2148 return BAD_VALUE;
2149 }
2150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 if (mOwner) {
2152 freeData();
2153 return continueWrite(desired);
2154 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156 uint8_t* data = (uint8_t*)realloc(mData, desired);
2157 if (!data && desired > mDataCapacity) {
2158 mError = NO_MEMORY;
2159 return NO_MEMORY;
2160 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002165 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002166 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002167 gParcelGlobalAllocSize += desired;
2168 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002169 if (!mData) {
2170 gParcelGlobalAllocCount++;
2171 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002172 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 mData = data;
2174 mDataCapacity = desired;
2175 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002176
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002177 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002178 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2179 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 free(mObjects);
2182 mObjects = NULL;
2183 mObjectsSize = mObjectsCapacity = 0;
2184 mNextObjectHint = 0;
2185 mHasFds = false;
2186 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002187 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002188
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002189 return NO_ERROR;
2190}
2191
2192status_t Parcel::continueWrite(size_t desired)
2193{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002194 if (desired > INT32_MAX) {
2195 // don't accept size_t values which may have come from an
2196 // inadvertent conversion from a negative int.
2197 return BAD_VALUE;
2198 }
2199
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002200 // If shrinking, first adjust for any objects that appear
2201 // after the new data size.
2202 size_t objectsSize = mObjectsSize;
2203 if (desired < mDataSize) {
2204 if (desired == 0) {
2205 objectsSize = 0;
2206 } else {
2207 while (objectsSize > 0) {
2208 if (mObjects[objectsSize-1] < desired)
2209 break;
2210 objectsSize--;
2211 }
2212 }
2213 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002214
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 if (mOwner) {
2216 // If the size is going to zero, just release the owner's data.
2217 if (desired == 0) {
2218 freeData();
2219 return NO_ERROR;
2220 }
2221
2222 // If there is a different owner, we need to take
2223 // posession.
2224 uint8_t* data = (uint8_t*)malloc(desired);
2225 if (!data) {
2226 mError = NO_MEMORY;
2227 return NO_MEMORY;
2228 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002229 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002230
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002232 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002234 free(data);
2235
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002236 mError = NO_MEMORY;
2237 return NO_MEMORY;
2238 }
2239
2240 // Little hack to only acquire references on objects
2241 // we will be keeping.
2242 size_t oldObjectsSize = mObjectsSize;
2243 mObjectsSize = objectsSize;
2244 acquireObjects();
2245 mObjectsSize = oldObjectsSize;
2246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248 if (mData) {
2249 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2250 }
2251 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002252 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002253 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002254 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2256 mOwner = NULL;
2257
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002258 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002259 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002260 gParcelGlobalAllocSize += desired;
2261 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002262 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002264 mData = data;
2265 mObjects = objects;
2266 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002267 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002268 mDataCapacity = desired;
2269 mObjectsSize = mObjectsCapacity = objectsSize;
2270 mNextObjectHint = 0;
2271
2272 } else if (mData) {
2273 if (objectsSize < mObjectsSize) {
2274 // Need to release refs on any objects we are dropping.
2275 const sp<ProcessState> proc(ProcessState::self());
2276 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2277 const flat_binder_object* flat
2278 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2279 if (flat->type == BINDER_TYPE_FD) {
2280 // will need to rescan because we may have lopped off the only FDs
2281 mFdsKnown = false;
2282 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002283 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002285 binder_size_t* objects =
2286 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 if (objects) {
2288 mObjects = objects;
2289 }
2290 mObjectsSize = objectsSize;
2291 mNextObjectHint = 0;
2292 }
2293
2294 // We own the data, so we can just do a realloc().
2295 if (desired > mDataCapacity) {
2296 uint8_t* data = (uint8_t*)realloc(mData, desired);
2297 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002298 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2299 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002300 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002301 gParcelGlobalAllocSize += desired;
2302 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002303 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002304 mData = data;
2305 mDataCapacity = desired;
2306 } else if (desired > mDataCapacity) {
2307 mError = NO_MEMORY;
2308 return NO_MEMORY;
2309 }
2310 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002311 if (mDataSize > desired) {
2312 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002313 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002314 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 if (mDataPos > desired) {
2316 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002317 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 }
2319 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 } else {
2322 // This is the first data. Easy!
2323 uint8_t* data = (uint8_t*)malloc(desired);
2324 if (!data) {
2325 mError = NO_MEMORY;
2326 return NO_MEMORY;
2327 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 if(!(mDataCapacity == 0 && mObjects == NULL
2330 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002331 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002332 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002334 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002335 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002336 gParcelGlobalAllocSize += desired;
2337 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002338 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 mData = data;
2341 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002342 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2343 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 mDataCapacity = desired;
2345 }
2346
2347 return NO_ERROR;
2348}
2349
2350void Parcel::initState()
2351{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002352 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 mError = NO_ERROR;
2354 mData = 0;
2355 mDataSize = 0;
2356 mDataCapacity = 0;
2357 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002358 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2359 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 mObjects = NULL;
2361 mObjectsSize = 0;
2362 mObjectsCapacity = 0;
2363 mNextObjectHint = 0;
2364 mHasFds = false;
2365 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002366 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002368 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002369}
2370
2371void Parcel::scanForFds() const
2372{
2373 bool hasFds = false;
2374 for (size_t i=0; i<mObjectsSize; i++) {
2375 const flat_binder_object* flat
2376 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2377 if (flat->type == BINDER_TYPE_FD) {
2378 hasFds = true;
2379 break;
2380 }
2381 }
2382 mHasFds = hasFds;
2383 mFdsKnown = true;
2384}
2385
Dan Sandleraa5c2342015-04-10 10:08:45 -04002386size_t Parcel::getBlobAshmemSize() const
2387{
Adrian Roos6bb31142015-10-22 16:46:12 -07002388 // This used to return the size of all blobs that were written to ashmem, now we're returning
2389 // the ashmem currently referenced by this Parcel, which should be equivalent.
2390 // TODO: Remove method once ABI can be changed.
2391 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002392}
2393
Adrian Rooscbf37262015-10-22 16:12:53 -07002394size_t Parcel::getOpenAshmemSize() const
2395{
2396 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002397}
2398
2399// --- Parcel::Blob ---
2400
2401Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002402 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002403}
2404
2405Parcel::Blob::~Blob() {
2406 release();
2407}
2408
2409void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002410 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002411 ::munmap(mData, mSize);
2412 }
2413 clear();
2414}
2415
Jeff Brown13b16042014-11-11 16:44:25 -08002416void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2417 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002418 mData = data;
2419 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002420 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002421}
2422
2423void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002424 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002425 mData = NULL;
2426 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002427 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002428}
2429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430}; // namespace android