blob: c8371e535b6b15500a996a1d9039b3a31aab1a45 [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 Salyzyn8bb1a532016-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>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070026
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/Binder.h>
28#include <binder/BpBinder.h>
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080029#include <binder/IPCThreadState.h>
30#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/ProcessState.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070032#include <binder/TextOutput.h>
33
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080034#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070035#include <utils/Debug.h>
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080036#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070037#include <utils/Log.h>
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080038#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039#include <utils/String8.h>
40#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041
Mathias Agopian208059f2009-05-18 15:08:03 -070042#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080043#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#ifndef INT32_MAX
46#define INT32_MAX ((int32_t)(2147483647))
47#endif
48
49#define LOG_REFS(...)
Mark Salyzyn4d64cf92016-01-27 08:02:48 -080050//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#define LOG_ALLOC(...)
Mark Salyzyn4d64cf92016-01-27 08:02:48 -080052//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053
54// ---------------------------------------------------------------------------
55
Nick Kralevichb6b14232015-04-02 09:36:02 -070056// This macro should never be used at runtime, as a too large value
57// of s could cause an integer overflow. Instead, you should always
58// use the wrapper function pad_size()
59#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
60
61static size_t pad_size(size_t s) {
62 if (s > (SIZE_T_MAX - 3)) {
63 abort();
64 }
65 return PAD_SIZE_UNSAFE(s);
66}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070067
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070068// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080069#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070070
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070071// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
72#define EX_HAS_REPLY_HEADER -128
73
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074// XXX This can be made public if we want to provide
75// support for typed data.
76struct small_flat_data
77{
78 uint32_t type;
79 uint32_t data;
80};
81
82namespace android {
83
Dianne Hackborna4cff882014-11-13 17:07:40 -080084static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
85static size_t gParcelGlobalAllocSize = 0;
86static size_t gParcelGlobalAllocCount = 0;
87
Jeff Brown13b16042014-11-11 16:44:25 -080088// Maximum size of a blob to transfer in-place.
89static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
90
91enum {
92 BLOB_INPLACE = 0,
93 BLOB_ASHMEM_IMMUTABLE = 1,
94 BLOB_ASHMEM_MUTABLE = 2,
95};
96
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070098 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070099{
100 switch (obj.type) {
101 case BINDER_TYPE_BINDER:
102 if (obj.binder) {
103 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800104 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700105 }
106 return;
107 case BINDER_TYPE_WEAK_BINDER:
108 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800109 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 return;
111 case BINDER_TYPE_HANDLE: {
112 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
113 if (b != NULL) {
114 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
115 b->incStrong(who);
116 }
117 return;
118 }
119 case BINDER_TYPE_WEAK_HANDLE: {
120 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
121 if (b != NULL) b.get_refs()->incWeak(who);
122 return;
123 }
124 case BINDER_TYPE_FD: {
Adrian Rooscbf37262015-10-22 16:12:53 -0700125 if (obj.cookie != 0) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700126 if (outAshmemSize != NULL) {
127 // If we own an ashmem fd, keep track of how much memory it refers to.
128 int size = ashmem_get_size_region(obj.handle);
129 if (size > 0) {
130 *outAshmemSize += size;
131 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700132 }
133 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 return;
135 }
136 }
137
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800138 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139}
140
Adrian Roos6bb31142015-10-22 16:46:12 -0700141void acquire_object(const sp<ProcessState>& proc,
142 const flat_binder_object& obj, const void* who)
143{
144 acquire_object(proc, obj, who, NULL);
145}
146
147static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700148 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149{
150 switch (obj.type) {
151 case BINDER_TYPE_BINDER:
152 if (obj.binder) {
153 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800154 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 }
156 return;
157 case BINDER_TYPE_WEAK_BINDER:
158 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 return;
161 case BINDER_TYPE_HANDLE: {
162 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
163 if (b != NULL) {
164 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
165 b->decStrong(who);
166 }
167 return;
168 }
169 case BINDER_TYPE_WEAK_HANDLE: {
170 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
171 if (b != NULL) b.get_refs()->decWeak(who);
172 return;
173 }
174 case BINDER_TYPE_FD: {
Adrian Roos6bb31142015-10-22 16:46:12 -0700175 if (outAshmemSize != NULL) {
176 if (obj.cookie != 0) {
177 int size = ashmem_get_size_region(obj.handle);
178 if (size > 0) {
179 *outAshmemSize -= size;
180 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700181
Adrian Roos6bb31142015-10-22 16:46:12 -0700182 close(obj.handle);
183 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700184 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 return;
186 }
187 }
188
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800189 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190}
191
Adrian Roos6bb31142015-10-22 16:46:12 -0700192void release_object(const sp<ProcessState>& proc,
193 const flat_binder_object& obj, const void* who)
194{
195 release_object(proc, obj, who, NULL);
196}
197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800199 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200{
201 return out->writeObject(flat, false);
202}
203
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 const sp<IBinder>& binder, Parcel* out)
206{
207 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700208
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
210 if (binder != NULL) {
211 IBinder *local = binder->localBinder();
212 if (!local) {
213 BpBinder *proxy = binder->remoteBinder();
214 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000215 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 }
217 const int32_t handle = proxy ? proxy->handle() : 0;
218 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800219 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800221 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 } else {
223 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800224 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
225 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 }
227 } else {
228 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800229 obj.binder = 0;
230 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700232
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 return finish_flatten_binder(binder, obj, out);
234}
235
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800236status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 const wp<IBinder>& binder, Parcel* out)
238{
239 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700240
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
242 if (binder != NULL) {
243 sp<IBinder> real = binder.promote();
244 if (real != NULL) {
245 IBinder *local = real->localBinder();
246 if (!local) {
247 BpBinder *proxy = real->remoteBinder();
248 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000249 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 }
251 const int32_t handle = proxy ? proxy->handle() : 0;
252 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800253 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800255 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 } else {
257 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800258 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
259 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 }
261 return finish_flatten_binder(real, obj, out);
262 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700263
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700264 // XXX How to deal? In order to flatten the given binder,
265 // we need to probe it for information, which requires a primary
266 // reference... but we don't have one.
267 //
268 // The OpenBinder implementation uses a dynamic_cast<> here,
269 // but we can't do that with the different reference counting
270 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000271 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 obj.binder = 0;
274 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700276
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700277 } else {
278 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800279 obj.binder = 0;
280 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700281 return finish_flatten_binder(NULL, obj, out);
282 }
283}
284
285inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800286 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
287 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700288{
289 return NO_ERROR;
290}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292status_t unflatten_binder(const sp<ProcessState>& proc,
293 const Parcel& in, sp<IBinder>* out)
294{
295 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700296
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297 if (flat) {
298 switch (flat->type) {
299 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800300 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301 return finish_unflatten_binder(NULL, *flat, in);
302 case BINDER_TYPE_HANDLE:
303 *out = proc->getStrongProxyForHandle(flat->handle);
304 return finish_unflatten_binder(
305 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700306 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 }
308 return BAD_TYPE;
309}
310
311status_t unflatten_binder(const sp<ProcessState>& proc,
312 const Parcel& in, wp<IBinder>* out)
313{
314 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 if (flat) {
317 switch (flat->type) {
318 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800319 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700320 return finish_unflatten_binder(NULL, *flat, in);
321 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800324 reinterpret_cast<IBinder*>(flat->cookie),
325 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326 } else {
327 *out = NULL;
328 }
329 return finish_unflatten_binder(NULL, *flat, in);
330 case BINDER_TYPE_HANDLE:
331 case BINDER_TYPE_WEAK_HANDLE:
332 *out = proc->getWeakProxyForHandle(flat->handle);
333 return finish_unflatten_binder(
334 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
335 }
336 }
337 return BAD_TYPE;
338}
339
340// ---------------------------------------------------------------------------
341
342Parcel::Parcel()
343{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800344 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345 initState();
346}
347
348Parcel::~Parcel()
349{
350 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800351 LOG_ALLOC("Parcel %p: destroyed", this);
352}
353
354size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800355 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
356 size_t size = gParcelGlobalAllocSize;
357 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
358 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800359}
360
361size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800362 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
363 size_t count = gParcelGlobalAllocCount;
364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
365 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700366}
367
368const uint8_t* Parcel::data() const
369{
370 return mData;
371}
372
373size_t Parcel::dataSize() const
374{
375 return (mDataSize > mDataPos ? mDataSize : mDataPos);
376}
377
378size_t Parcel::dataAvail() const
379{
380 // TODO: decide what to do about the possibility that this can
381 // report an available-data size that exceeds a Java int's max
382 // positive value, causing havoc. Fortunately this will only
383 // happen if someone constructs a Parcel containing more than two
384 // gigabytes of data, which on typical phone hardware is simply
385 // not possible.
386 return dataSize() - dataPosition();
387}
388
389size_t Parcel::dataPosition() const
390{
391 return mDataPos;
392}
393
394size_t Parcel::dataCapacity() const
395{
396 return mDataCapacity;
397}
398
399status_t Parcel::setDataSize(size_t size)
400{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700401 if (size > INT32_MAX) {
402 // don't accept size_t values which may have come from an
403 // inadvertent conversion from a negative int.
404 return BAD_VALUE;
405 }
406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700407 status_t err;
408 err = continueWrite(size);
409 if (err == NO_ERROR) {
410 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700411 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 }
413 return err;
414}
415
416void Parcel::setDataPosition(size_t pos) const
417{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700418 if (pos > INT32_MAX) {
419 // don't accept size_t values which may have come from an
420 // inadvertent conversion from a negative int.
421 abort();
422 }
423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700424 mDataPos = pos;
425 mNextObjectHint = 0;
426}
427
428status_t Parcel::setDataCapacity(size_t size)
429{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700430 if (size > INT32_MAX) {
431 // don't accept size_t values which may have come from an
432 // inadvertent conversion from a negative int.
433 return BAD_VALUE;
434 }
435
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700436 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 return NO_ERROR;
438}
439
440status_t Parcel::setData(const uint8_t* buffer, size_t len)
441{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 status_t err = restartWrite(len);
449 if (err == NO_ERROR) {
450 memcpy(const_cast<uint8_t*>(data()), buffer, len);
451 mDataSize = len;
452 mFdsKnown = false;
453 }
454 return err;
455}
456
Andreas Huber51faf462011-04-13 10:21:56 -0700457status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458{
459 const sp<ProcessState> proc(ProcessState::self());
460 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700461 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800462 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 size_t size = parcel->mObjectsSize;
464 int startPos = mDataPos;
465 int firstIndex = -1, lastIndex = -2;
466
467 if (len == 0) {
468 return NO_ERROR;
469 }
470
Nick Kralevichb6b14232015-04-02 09:36:02 -0700471 if (len > INT32_MAX) {
472 // don't accept size_t values which may have come from an
473 // inadvertent conversion from a negative int.
474 return BAD_VALUE;
475 }
476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477 // range checks against the source parcel size
478 if ((offset > parcel->mDataSize)
479 || (len > parcel->mDataSize)
480 || (offset + len > parcel->mDataSize)) {
481 return BAD_VALUE;
482 }
483
484 // Count objects in range
485 for (int i = 0; i < (int) size; i++) {
486 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700487 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 if (firstIndex == -1) {
489 firstIndex = i;
490 }
491 lastIndex = i;
492 }
493 }
494 int numObjects = lastIndex - firstIndex + 1;
495
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700496 if ((mDataSize+len) > mDataCapacity) {
497 // grow data
498 err = growData(len);
499 if (err != NO_ERROR) {
500 return err;
501 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502 }
503
504 // append data
505 memcpy(mData + mDataPos, data + offset, len);
506 mDataPos += len;
507 mDataSize += len;
508
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400509 err = NO_ERROR;
510
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 if (numObjects > 0) {
512 // grow objects
513 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700514 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
515 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800516 binder_size_t *objects =
517 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
518 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700519 return NO_MEMORY;
520 }
521 mObjects = objects;
522 mObjectsCapacity = newSize;
523 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 // append and acquire objects
526 int idx = mObjectsSize;
527 for (int i = firstIndex; i <= lastIndex; i++) {
528 size_t off = objects[i] - offset + startPos;
529 mObjects[idx++] = off;
530 mObjectsSize++;
531
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700532 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700533 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700534 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700537 // If this is a file descriptor, we need to dup it so the
538 // new Parcel now owns its own fd, and can declare that we
539 // officially know we have fds.
540 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800541 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400543 if (!mAllowFds) {
544 err = FDS_NOT_ALLOWED;
545 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546 }
547 }
548 }
549
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400550 return err;
551}
552
Jeff Brown13b16042014-11-11 16:44:25 -0800553bool Parcel::allowFds() const
554{
555 return mAllowFds;
556}
557
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700558bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400559{
560 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700561 if (!allowFds) {
562 mAllowFds = false;
563 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700565}
566
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700567void Parcel::restoreAllowFds(bool lastValue)
568{
569 mAllowFds = lastValue;
570}
571
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700572bool Parcel::hasFileDescriptors() const
573{
574 if (!mFdsKnown) {
575 scanForFds();
576 }
577 return mHasFds;
578}
579
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700580// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581status_t Parcel::writeInterfaceToken(const String16& interface)
582{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700583 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
584 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700585 // currently the interface identification token is just its name as a string
586 return writeString16(interface);
587}
588
Mathias Agopian83c04462009-05-22 19:00:22 -0700589bool Parcel::checkInterface(IBinder* binder) const
590{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700591 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700592}
593
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700594bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700595 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700596{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700597 int32_t strictPolicy = readInt32();
598 if (threadState == NULL) {
599 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700600 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700601 if ((threadState->getLastTransactionBinderFlags() &
602 IBinder::FLAG_ONEWAY) != 0) {
603 // For one-way calls, the callee is running entirely
604 // disconnected from the caller, so disable StrictMode entirely.
605 // Not only does disk/network usage not impact the caller, but
606 // there's no way to commuicate back any violations anyway.
607 threadState->setStrictModePolicy(0);
608 } else {
609 threadState->setStrictModePolicy(strictPolicy);
610 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700611 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700612 if (str == interface) {
613 return true;
614 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700615 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616 String8(interface).string(), String8(str).string());
617 return false;
618 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700619}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700620
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800621const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700622{
623 return mObjects;
624}
625
626size_t Parcel::objectsCount() const
627{
628 return mObjectsSize;
629}
630
631status_t Parcel::errorCheck() const
632{
633 return mError;
634}
635
636void Parcel::setError(status_t err)
637{
638 mError = err;
639}
640
641status_t Parcel::finishWrite(size_t len)
642{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700643 if (len > INT32_MAX) {
644 // don't accept size_t values which may have come from an
645 // inadvertent conversion from a negative int.
646 return BAD_VALUE;
647 }
648
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700649 //printf("Finish write of %d\n", len);
650 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700651 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700652 if (mDataPos > mDataSize) {
653 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700654 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655 }
656 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
657 return NO_ERROR;
658}
659
660status_t Parcel::writeUnpadded(const void* data, size_t len)
661{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700662 if (len > INT32_MAX) {
663 // don't accept size_t values which may have come from an
664 // inadvertent conversion from a negative int.
665 return BAD_VALUE;
666 }
667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668 size_t end = mDataPos + len;
669 if (end < mDataPos) {
670 // integer overflow
671 return BAD_VALUE;
672 }
673
674 if (end <= mDataCapacity) {
675restart_write:
676 memcpy(mData+mDataPos, data, len);
677 return finishWrite(len);
678 }
679
680 status_t err = growData(len);
681 if (err == NO_ERROR) goto restart_write;
682 return err;
683}
684
685status_t Parcel::write(const void* data, size_t len)
686{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700687 if (len > INT32_MAX) {
688 // don't accept size_t values which may have come from an
689 // inadvertent conversion from a negative int.
690 return BAD_VALUE;
691 }
692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693 void* const d = writeInplace(len);
694 if (d) {
695 memcpy(d, data, len);
696 return NO_ERROR;
697 }
698 return mError;
699}
700
701void* Parcel::writeInplace(size_t len)
702{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700703 if (len > INT32_MAX) {
704 // don't accept size_t values which may have come from an
705 // inadvertent conversion from a negative int.
706 return NULL;
707 }
708
709 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700710
711 // sanity check for integer overflow
712 if (mDataPos+padded < mDataPos) {
713 return NULL;
714 }
715
716 if ((mDataPos+padded) <= mDataCapacity) {
717restart_write:
718 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
719 uint8_t* const data = mData+mDataPos;
720
721 // Need to pad at end?
722 if (padded != len) {
723#if BYTE_ORDER == BIG_ENDIAN
724 static const uint32_t mask[4] = {
725 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
726 };
727#endif
728#if BYTE_ORDER == LITTLE_ENDIAN
729 static const uint32_t mask[4] = {
730 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
731 };
732#endif
733 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
734 // *reinterpret_cast<void**>(data+padded-4));
735 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
736 }
737
738 finishWrite(padded);
739 return data;
740 }
741
742 status_t err = growData(padded);
743 if (err == NO_ERROR) goto restart_write;
744 return NULL;
745}
746
747status_t Parcel::writeInt32(int32_t val)
748{
Andreas Huber84a6d042009-08-17 13:33:27 -0700749 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800751
752status_t Parcel::writeUint32(uint32_t val)
753{
754 return writeAligned(val);
755}
756
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700757status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700758 if (len > INT32_MAX) {
759 // don't accept size_t values which may have come from an
760 // inadvertent conversion from a negative int.
761 return BAD_VALUE;
762 }
763
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700764 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700765 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700766 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700767 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700768 if (ret == NO_ERROR) {
769 ret = write(val, len * sizeof(*val));
770 }
771 return ret;
772}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700773status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700774 if (len > INT32_MAX) {
775 // don't accept size_t values which may have come from an
776 // inadvertent conversion from a negative int.
777 return BAD_VALUE;
778 }
779
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700780 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700781 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700782 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700783 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700784 if (ret == NO_ERROR) {
785 ret = write(val, len * sizeof(*val));
786 }
787 return ret;
788}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700789
790status_t Parcel::writeInt64(int64_t val)
791{
Andreas Huber84a6d042009-08-17 13:33:27 -0700792 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700793}
794
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700795status_t Parcel::writeUint64(uint64_t val)
796{
797 return writeAligned(val);
798}
799
Serban Constantinescuf683e012013-11-05 16:53:55 +0000800status_t Parcel::writePointer(uintptr_t val)
801{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800802 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000803}
804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700805status_t Parcel::writeFloat(float val)
806{
Andreas Huber84a6d042009-08-17 13:33:27 -0700807 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700808}
809
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800810#if defined(__mips__) && defined(__mips_hard_float)
811
812status_t Parcel::writeDouble(double val)
813{
814 union {
815 double d;
816 unsigned long long ll;
817 } u;
818 u.d = val;
819 return writeAligned(u.ll);
820}
821
822#else
823
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700824status_t Parcel::writeDouble(double val)
825{
Andreas Huber84a6d042009-08-17 13:33:27 -0700826 return writeAligned(val);
827}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700828
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800829#endif
830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700831status_t Parcel::writeCString(const char* str)
832{
833 return write(str, strlen(str)+1);
834}
835
836status_t Parcel::writeString8(const String8& str)
837{
838 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100839 // only write string if its length is more than zero characters,
840 // as readString8 will only read if the length field is non-zero.
841 // this is slightly different from how writeString16 works.
842 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700843 err = write(str.string(), str.bytes()+1);
844 }
845 return err;
846}
847
848status_t Parcel::writeString16(const String16& str)
849{
850 return writeString16(str.string(), str.size());
851}
852
853status_t Parcel::writeString16(const char16_t* str, size_t len)
854{
855 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700856
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700857 status_t err = writeInt32(len);
858 if (err == NO_ERROR) {
859 len *= sizeof(char16_t);
860 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
861 if (data) {
862 memcpy(data, str, len);
863 *reinterpret_cast<char16_t*>(data+len) = 0;
864 return NO_ERROR;
865 }
866 err = mError;
867 }
868 return err;
869}
870
871status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
872{
873 return flatten_binder(ProcessState::self(), val, this);
874}
875
876status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
877{
878 return flatten_binder(ProcessState::self(), val, this);
879}
880
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700881status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800882{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700883 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800884 return BAD_TYPE;
885
886 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700887 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800888 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700890 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800891 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700893 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
894 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895
896 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000897 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 return err;
899 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700900 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800901 return err;
902}
903
Jeff Brown93ff1f92011-11-04 19:01:44 -0700904status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700905{
906 flat_binder_object obj;
907 obj.type = BINDER_TYPE_FD;
908 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800909 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800911 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912 return writeObject(obj, true);
913}
914
915status_t Parcel::writeDupFileDescriptor(int fd)
916{
Jeff Brownd341c712011-11-04 20:19:33 -0700917 int dupFd = dup(fd);
918 if (dupFd < 0) {
919 return -errno;
920 }
921 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
922 if (err) {
923 close(dupFd);
924 }
925 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700926}
927
Jeff Brown13b16042014-11-11 16:44:25 -0800928status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700929{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700930 if (len > INT32_MAX) {
931 // don't accept size_t values which may have come from an
932 // inadvertent conversion from a negative int.
933 return BAD_VALUE;
934 }
935
Jeff Brown13b16042014-11-11 16:44:25 -0800936 status_t status;
937 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100938 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800939 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700940 if (status) return status;
941
942 void* ptr = writeInplace(len);
943 if (!ptr) return NO_MEMORY;
944
Jeff Brown13b16042014-11-11 16:44:25 -0800945 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700946 return NO_ERROR;
947 }
948
Steve Block6807e592011-10-20 11:56:00 +0100949 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700950 int fd = ashmem_create_region("Parcel Blob", len);
951 if (fd < 0) return NO_MEMORY;
952
953 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
954 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700955 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700956 } else {
957 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
958 if (ptr == MAP_FAILED) {
959 status = -errno;
960 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800961 if (!mutableCopy) {
962 result = ashmem_set_prot_region(fd, PROT_READ);
963 }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700964 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700965 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700966 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800967 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700968 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700969 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700970 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -0800971 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700972 return NO_ERROR;
973 }
974 }
975 }
976 }
977 ::munmap(ptr, len);
978 }
979 ::close(fd);
980 return status;
981}
982
Jeff Brown13b16042014-11-11 16:44:25 -0800983status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
984{
985 // Must match up with what's done in writeBlob.
986 if (!mAllowFds) return FDS_NOT_ALLOWED;
987 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
988 if (status) return status;
989 return writeDupFileDescriptor(fd);
990}
991
Mathias Agopiane1424282013-07-29 21:24:40 -0700992status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800993{
994 status_t err;
995
996 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -0700997 const size_t len = val.getFlattenedSize();
998 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800999
Nick Kralevichb6b14232015-04-02 09:36:02 -07001000 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1001 // don't accept size_t values which may have come from an
1002 // inadvertent conversion from a negative int.
1003 return BAD_VALUE;
1004 }
1005
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001006 err = this->writeInt32(len);
1007 if (err) return err;
1008
1009 err = this->writeInt32(fd_count);
1010 if (err) return err;
1011
1012 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001013 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001014 if (buf == NULL)
1015 return BAD_VALUE;
1016
1017 int* fds = NULL;
1018 if (fd_count) {
1019 fds = new int[fd_count];
1020 }
1021
1022 err = val.flatten(buf, len, fds, fd_count);
1023 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1024 err = this->writeDupFileDescriptor( fds[i] );
1025 }
1026
1027 if (fd_count) {
1028 delete [] fds;
1029 }
1030
1031 return err;
1032}
1033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1035{
1036 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1037 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1038 if (enoughData && enoughObjects) {
1039restart_write:
1040 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001041
Christopher Tate98e67d32015-06-03 18:44:15 -07001042 // remember if it's a file descriptor
1043 if (val.type == BINDER_TYPE_FD) {
1044 if (!mAllowFds) {
1045 // fail before modifying our object index
1046 return FDS_NOT_ALLOWED;
1047 }
1048 mHasFds = mFdsKnown = true;
1049 }
1050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001052 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001054 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055 mObjectsSize++;
1056 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058 return finishWrite(sizeof(flat_binder_object));
1059 }
1060
1061 if (!enoughData) {
1062 const status_t err = growData(sizeof(val));
1063 if (err != NO_ERROR) return err;
1064 }
1065 if (!enoughObjects) {
1066 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001067 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001068 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069 if (objects == NULL) return NO_MEMORY;
1070 mObjects = objects;
1071 mObjectsCapacity = newSize;
1072 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001073
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001074 goto restart_write;
1075}
1076
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001077status_t Parcel::writeNoException()
1078{
1079 return writeInt32(0);
1080}
1081
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001082void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083{
1084 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1085}
1086
1087status_t Parcel::read(void* outData, size_t len) const
1088{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001089 if (len > INT32_MAX) {
1090 // don't accept size_t values which may have come from an
1091 // inadvertent conversion from a negative int.
1092 return BAD_VALUE;
1093 }
1094
1095 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1096 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001097 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001098 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001099 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100 return NO_ERROR;
1101 }
1102 return NOT_ENOUGH_DATA;
1103}
1104
1105const void* Parcel::readInplace(size_t len) const
1106{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001107 if (len > INT32_MAX) {
1108 // don't accept size_t values which may have come from an
1109 // inadvertent conversion from a negative int.
1110 return NULL;
1111 }
1112
1113 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1114 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001116 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001117 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001118 return data;
1119 }
1120 return NULL;
1121}
1122
Andreas Huber84a6d042009-08-17 13:33:27 -07001123template<class T>
1124status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001125 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001126
1127 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001129 mDataPos += sizeof(T);
1130 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001131 return NO_ERROR;
1132 } else {
1133 return NOT_ENOUGH_DATA;
1134 }
1135}
1136
Andreas Huber84a6d042009-08-17 13:33:27 -07001137template<class T>
1138T Parcel::readAligned() const {
1139 T result;
1140 if (readAligned(&result) != NO_ERROR) {
1141 result = 0;
1142 }
1143
1144 return result;
1145}
1146
1147template<class T>
1148status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001149 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001150
1151 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1152restart_write:
1153 *reinterpret_cast<T*>(mData+mDataPos) = val;
1154 return finishWrite(sizeof(val));
1155 }
1156
1157 status_t err = growData(sizeof(val));
1158 if (err == NO_ERROR) goto restart_write;
1159 return err;
1160}
1161
1162status_t Parcel::readInt32(int32_t *pArg) const
1163{
1164 return readAligned(pArg);
1165}
1166
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167int32_t Parcel::readInt32() const
1168{
Andreas Huber84a6d042009-08-17 13:33:27 -07001169 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001170}
1171
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001172status_t Parcel::readUint32(uint32_t *pArg) const
1173{
1174 return readAligned(pArg);
1175}
1176
1177uint32_t Parcel::readUint32() const
1178{
1179 return readAligned<uint32_t>();
1180}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181
1182status_t Parcel::readInt64(int64_t *pArg) const
1183{
Andreas Huber84a6d042009-08-17 13:33:27 -07001184 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185}
1186
1187
1188int64_t Parcel::readInt64() const
1189{
Andreas Huber84a6d042009-08-17 13:33:27 -07001190 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191}
1192
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001193status_t Parcel::readUint64(uint64_t *pArg) const
1194{
1195 return readAligned(pArg);
1196}
1197
1198uint64_t Parcel::readUint64() const
1199{
1200 return readAligned<uint64_t>();
1201}
1202
Serban Constantinescuf683e012013-11-05 16:53:55 +00001203status_t Parcel::readPointer(uintptr_t *pArg) const
1204{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001205 status_t ret;
1206 binder_uintptr_t ptr;
1207 ret = readAligned(&ptr);
1208 if (!ret)
1209 *pArg = ptr;
1210 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001211}
1212
1213uintptr_t Parcel::readPointer() const
1214{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001215 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001216}
1217
1218
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001219status_t Parcel::readFloat(float *pArg) const
1220{
Andreas Huber84a6d042009-08-17 13:33:27 -07001221 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001222}
1223
1224
1225float Parcel::readFloat() const
1226{
Andreas Huber84a6d042009-08-17 13:33:27 -07001227 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228}
1229
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001230#if defined(__mips__) && defined(__mips_hard_float)
1231
1232status_t Parcel::readDouble(double *pArg) const
1233{
1234 union {
1235 double d;
1236 unsigned long long ll;
1237 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001238 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001239 status_t status;
1240 status = readAligned(&u.ll);
1241 *pArg = u.d;
1242 return status;
1243}
1244
1245double Parcel::readDouble() const
1246{
1247 union {
1248 double d;
1249 unsigned long long ll;
1250 } u;
1251 u.ll = readAligned<unsigned long long>();
1252 return u.d;
1253}
1254
1255#else
1256
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001257status_t Parcel::readDouble(double *pArg) const
1258{
Andreas Huber84a6d042009-08-17 13:33:27 -07001259 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001260}
1261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001262double Parcel::readDouble() const
1263{
Andreas Huber84a6d042009-08-17 13:33:27 -07001264 return readAligned<double>();
1265}
1266
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001267#endif
1268
Andreas Huber84a6d042009-08-17 13:33:27 -07001269status_t Parcel::readIntPtr(intptr_t *pArg) const
1270{
1271 return readAligned(pArg);
1272}
1273
1274
1275intptr_t Parcel::readIntPtr() const
1276{
1277 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001278}
1279
1280
1281const char* Parcel::readCString() const
1282{
1283 const size_t avail = mDataSize-mDataPos;
1284 if (avail > 0) {
1285 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1286 // is the string's trailing NUL within the parcel's valid bounds?
1287 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1288 if (eos) {
1289 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001290 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001291 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001292 return str;
1293 }
1294 }
1295 return NULL;
1296}
1297
1298String8 Parcel::readString8() const
1299{
1300 int32_t size = readInt32();
1301 // watch for potential int overflow adding 1 for trailing NUL
1302 if (size > 0 && size < INT32_MAX) {
1303 const char* str = (const char*)readInplace(size+1);
1304 if (str) return String8(str, size);
1305 }
1306 return String8();
1307}
1308
1309String16 Parcel::readString16() const
1310{
1311 size_t len;
1312 const char16_t* str = readString16Inplace(&len);
1313 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001314 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315 return String16();
1316}
1317
1318const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1319{
1320 int32_t size = readInt32();
1321 // watch for potential int overflow from size+1
1322 if (size >= 0 && size < INT32_MAX) {
1323 *outLen = size;
1324 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1325 if (str != NULL) {
1326 return str;
1327 }
1328 }
1329 *outLen = 0;
1330 return NULL;
1331}
1332
1333sp<IBinder> Parcel::readStrongBinder() const
1334{
1335 sp<IBinder> val;
1336 unflatten_binder(ProcessState::self(), *this, &val);
1337 return val;
1338}
1339
1340wp<IBinder> Parcel::readWeakBinder() const
1341{
1342 wp<IBinder> val;
1343 unflatten_binder(ProcessState::self(), *this, &val);
1344 return val;
1345}
1346
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001347int32_t Parcel::readExceptionCode() const
1348{
1349 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001350 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001351 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001352 int32_t header_size = readAligned<int32_t>();
1353 // Skip over fat responses headers. Not used (or propagated) in
1354 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001355 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001356 // And fat response headers are currently only used when there are no
1357 // exceptions, so return no error:
1358 return 0;
1359 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001360 return exception_code;
1361}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001362
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001363native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001364{
1365 int numFds, numInts;
1366 status_t err;
1367 err = readInt32(&numFds);
1368 if (err != NO_ERROR) return 0;
1369 err = readInt32(&numInts);
1370 if (err != NO_ERROR) return 0;
1371
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001372 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001373 if (!h) {
1374 return 0;
1375 }
1376
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001377 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001378 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001379 if (h->data[i] < 0) err = BAD_VALUE;
1380 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001381 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001382 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001383 native_handle_close(h);
1384 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001385 h = 0;
1386 }
1387 return h;
1388}
1389
1390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391int Parcel::readFileDescriptor() const
1392{
1393 const flat_binder_object* flat = readObject(true);
1394 if (flat) {
1395 switch (flat->type) {
1396 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001397 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001398 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001399 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400 }
1401 return BAD_TYPE;
1402}
1403
Jeff Brown5707dbf2011-09-23 21:17:56 -07001404status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1405{
Jeff Brown13b16042014-11-11 16:44:25 -08001406 int32_t blobType;
1407 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001408 if (status) return status;
1409
Jeff Brown13b16042014-11-11 16:44:25 -08001410 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001411 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001412 const void* ptr = readInplace(len);
1413 if (!ptr) return BAD_VALUE;
1414
Jeff Brown13b16042014-11-11 16:44:25 -08001415 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001416 return NO_ERROR;
1417 }
1418
Steve Block6807e592011-10-20 11:56:00 +01001419 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001420 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001421 int fd = readFileDescriptor();
1422 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1423
Jeff Brown13b16042014-11-11 16:44:25 -08001424 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1425 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001426 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001427
Jeff Brown13b16042014-11-11 16:44:25 -08001428 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001429 return NO_ERROR;
1430}
1431
Mathias Agopiane1424282013-07-29 21:24:40 -07001432status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001433{
1434 // size
1435 const size_t len = this->readInt32();
1436 const size_t fd_count = this->readInt32();
1437
Nick Kralevichb6b14232015-04-02 09:36:02 -07001438 if (len > INT32_MAX) {
1439 // don't accept size_t values which may have come from an
1440 // inadvertent conversion from a negative int.
1441 return BAD_VALUE;
1442 }
1443
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001444 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001445 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001446 if (buf == NULL)
1447 return BAD_VALUE;
1448
1449 int* fds = NULL;
1450 if (fd_count) {
1451 fds = new int[fd_count];
1452 }
1453
1454 status_t err = NO_ERROR;
1455 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001456 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001457 if (fds[i] < 0) {
1458 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001459 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1460 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001461 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001462 }
1463
1464 if (err == NO_ERROR) {
1465 err = val.unflatten(buf, len, fds, fd_count);
1466 }
1467
1468 if (fd_count) {
1469 delete [] fds;
1470 }
1471
1472 return err;
1473}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001474const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1475{
1476 const size_t DPOS = mDataPos;
1477 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1478 const flat_binder_object* obj
1479 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1480 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001481 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001482 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483 // the object list, so we don't want to check for it when
1484 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001485 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001486 return obj;
1487 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001490 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001491 const size_t N = mObjectsSize;
1492 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001493
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001494 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001495 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 // Start at the current hint position, looking for an object at
1499 // the current data position.
1500 if (opos < N) {
1501 while (opos < (N-1) && OBJS[opos] < DPOS) {
1502 opos++;
1503 }
1504 } else {
1505 opos = N-1;
1506 }
1507 if (OBJS[opos] == DPOS) {
1508 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001509 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001510 this, DPOS, opos);
1511 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001512 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 return obj;
1514 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 // Look backwards for it...
1517 while (opos > 0 && OBJS[opos] > DPOS) {
1518 opos--;
1519 }
1520 if (OBJS[opos] == DPOS) {
1521 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001522 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001523 this, DPOS, opos);
1524 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001525 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 return obj;
1527 }
1528 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001529 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 -07001530 this, DPOS);
1531 }
1532 return NULL;
1533}
1534
1535void Parcel::closeFileDescriptors()
1536{
1537 size_t i = mObjectsSize;
1538 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001539 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001540 }
1541 while (i > 0) {
1542 i--;
1543 const flat_binder_object* flat
1544 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1545 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001546 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001547 close(flat->handle);
1548 }
1549 }
1550}
1551
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001552uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001554 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555}
1556
1557size_t Parcel::ipcDataSize() const
1558{
1559 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1560}
1561
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001562uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001563{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001564 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565}
1566
1567size_t Parcel::ipcObjectsCount() const
1568{
1569 return mObjectsSize;
1570}
1571
1572void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001573 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001574{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001575 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576 freeDataNoInit();
1577 mError = NO_ERROR;
1578 mData = const_cast<uint8_t*>(data);
1579 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001580 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001582 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001583 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 mObjectsSize = mObjectsCapacity = objectsCount;
1585 mNextObjectHint = 0;
1586 mOwner = relFunc;
1587 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001588 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001589 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001590 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001591 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001592 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001593 mObjectsSize = 0;
1594 break;
1595 }
1596 minOffset = offset + sizeof(flat_binder_object);
1597 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 scanForFds();
1599}
1600
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001601void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001602{
1603 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 if (errorCheck() != NO_ERROR) {
1606 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001607 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001608 } else if (dataSize() > 0) {
1609 const uint8_t* DATA = data();
1610 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001611 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001612 const size_t N = objectsCount();
1613 for (size_t i=0; i<N; i++) {
1614 const flat_binder_object* flat
1615 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1616 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1617 << TypeCode(flat->type & 0x7f7f7f00)
1618 << " = " << flat->binder;
1619 }
1620 } else {
1621 to << "NULL";
1622 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001624 to << ")";
1625}
1626
1627void Parcel::releaseObjects()
1628{
1629 const sp<ProcessState> proc(ProcessState::self());
1630 size_t i = mObjectsSize;
1631 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001632 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633 while (i > 0) {
1634 i--;
1635 const flat_binder_object* flat
1636 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001637 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638 }
1639}
1640
1641void Parcel::acquireObjects()
1642{
1643 const sp<ProcessState> proc(ProcessState::self());
1644 size_t i = mObjectsSize;
1645 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001646 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647 while (i > 0) {
1648 i--;
1649 const flat_binder_object* flat
1650 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001651 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652 }
1653}
1654
1655void Parcel::freeData()
1656{
1657 freeDataNoInit();
1658 initState();
1659}
1660
1661void Parcel::freeDataNoInit()
1662{
1663 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001664 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001665 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1667 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001668 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001670 if (mData) {
1671 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001672 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001673 gParcelGlobalAllocSize -= mDataCapacity;
1674 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001675 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001676 free(mData);
1677 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678 if (mObjects) free(mObjects);
1679 }
1680}
1681
1682status_t Parcel::growData(size_t len)
1683{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001684 if (len > INT32_MAX) {
1685 // don't accept size_t values which may have come from an
1686 // inadvertent conversion from a negative int.
1687 return BAD_VALUE;
1688 }
1689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001690 size_t newSize = ((mDataSize+len)*3)/2;
1691 return (newSize <= mDataSize)
1692 ? (status_t) NO_MEMORY
1693 : continueWrite(newSize);
1694}
1695
1696status_t Parcel::restartWrite(size_t desired)
1697{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001698 if (desired > INT32_MAX) {
1699 // don't accept size_t values which may have come from an
1700 // inadvertent conversion from a negative int.
1701 return BAD_VALUE;
1702 }
1703
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704 if (mOwner) {
1705 freeData();
1706 return continueWrite(desired);
1707 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001709 uint8_t* data = (uint8_t*)realloc(mData, desired);
1710 if (!data && desired > mDataCapacity) {
1711 mError = NO_MEMORY;
1712 return NO_MEMORY;
1713 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001714
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001715 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001716
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001717 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001718 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001719 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001720 gParcelGlobalAllocSize += desired;
1721 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001722 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723 mData = data;
1724 mDataCapacity = desired;
1725 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001728 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1729 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001731 free(mObjects);
1732 mObjects = NULL;
1733 mObjectsSize = mObjectsCapacity = 0;
1734 mNextObjectHint = 0;
1735 mHasFds = false;
1736 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001737 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001738
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001739 return NO_ERROR;
1740}
1741
1742status_t Parcel::continueWrite(size_t desired)
1743{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001744 if (desired > INT32_MAX) {
1745 // don't accept size_t values which may have come from an
1746 // inadvertent conversion from a negative int.
1747 return BAD_VALUE;
1748 }
1749
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001750 // If shrinking, first adjust for any objects that appear
1751 // after the new data size.
1752 size_t objectsSize = mObjectsSize;
1753 if (desired < mDataSize) {
1754 if (desired == 0) {
1755 objectsSize = 0;
1756 } else {
1757 while (objectsSize > 0) {
1758 if (mObjects[objectsSize-1] < desired)
1759 break;
1760 objectsSize--;
1761 }
1762 }
1763 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001764
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001765 if (mOwner) {
1766 // If the size is going to zero, just release the owner's data.
1767 if (desired == 0) {
1768 freeData();
1769 return NO_ERROR;
1770 }
1771
1772 // If there is a different owner, we need to take
1773 // posession.
1774 uint8_t* data = (uint8_t*)malloc(desired);
1775 if (!data) {
1776 mError = NO_MEMORY;
1777 return NO_MEMORY;
1778 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001779 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001780
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001782 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001784 free(data);
1785
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001786 mError = NO_MEMORY;
1787 return NO_MEMORY;
1788 }
1789
1790 // Little hack to only acquire references on objects
1791 // we will be keeping.
1792 size_t oldObjectsSize = mObjectsSize;
1793 mObjectsSize = objectsSize;
1794 acquireObjects();
1795 mObjectsSize = oldObjectsSize;
1796 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001797
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001798 if (mData) {
1799 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1800 }
1801 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001802 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001804 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1806 mOwner = NULL;
1807
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001808 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001809 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001810 gParcelGlobalAllocSize += desired;
1811 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001812 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 mData = data;
1815 mObjects = objects;
1816 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001817 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818 mDataCapacity = desired;
1819 mObjectsSize = mObjectsCapacity = objectsSize;
1820 mNextObjectHint = 0;
1821
1822 } else if (mData) {
1823 if (objectsSize < mObjectsSize) {
1824 // Need to release refs on any objects we are dropping.
1825 const sp<ProcessState> proc(ProcessState::self());
1826 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1827 const flat_binder_object* flat
1828 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1829 if (flat->type == BINDER_TYPE_FD) {
1830 // will need to rescan because we may have lopped off the only FDs
1831 mFdsKnown = false;
1832 }
Adrian Rooscbf37262015-10-22 16:12:53 -07001833 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001835 binder_size_t* objects =
1836 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 if (objects) {
1838 mObjects = objects;
1839 }
1840 mObjectsSize = objectsSize;
1841 mNextObjectHint = 0;
1842 }
1843
1844 // We own the data, so we can just do a realloc().
1845 if (desired > mDataCapacity) {
1846 uint8_t* data = (uint8_t*)realloc(mData, desired);
1847 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001848 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1849 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001850 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001851 gParcelGlobalAllocSize += desired;
1852 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001853 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854 mData = data;
1855 mDataCapacity = desired;
1856 } else if (desired > mDataCapacity) {
1857 mError = NO_MEMORY;
1858 return NO_MEMORY;
1859 }
1860 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001861 if (mDataSize > desired) {
1862 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001863 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001864 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001865 if (mDataPos > desired) {
1866 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001867 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001868 }
1869 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001870
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001871 } else {
1872 // This is the first data. Easy!
1873 uint8_t* data = (uint8_t*)malloc(desired);
1874 if (!data) {
1875 mError = NO_MEMORY;
1876 return NO_MEMORY;
1877 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001878
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879 if(!(mDataCapacity == 0 && mObjects == NULL
1880 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001881 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001883
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001884 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001885 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001886 gParcelGlobalAllocSize += desired;
1887 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001888 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 mData = data;
1891 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001892 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1893 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001894 mDataCapacity = desired;
1895 }
1896
1897 return NO_ERROR;
1898}
1899
1900void Parcel::initState()
1901{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001902 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 mError = NO_ERROR;
1904 mData = 0;
1905 mDataSize = 0;
1906 mDataCapacity = 0;
1907 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001908 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1909 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910 mObjects = NULL;
1911 mObjectsSize = 0;
1912 mObjectsCapacity = 0;
1913 mNextObjectHint = 0;
1914 mHasFds = false;
1915 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001916 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07001918 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919}
1920
1921void Parcel::scanForFds() const
1922{
1923 bool hasFds = false;
1924 for (size_t i=0; i<mObjectsSize; i++) {
1925 const flat_binder_object* flat
1926 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1927 if (flat->type == BINDER_TYPE_FD) {
1928 hasFds = true;
1929 break;
1930 }
1931 }
1932 mHasFds = hasFds;
1933 mFdsKnown = true;
1934}
1935
Dan Sandleraa5c2342015-04-10 10:08:45 -04001936size_t Parcel::getBlobAshmemSize() const
1937{
Adrian Roos6bb31142015-10-22 16:46:12 -07001938 // This used to return the size of all blobs that were written to ashmem, now we're returning
1939 // the ashmem currently referenced by this Parcel, which should be equivalent.
1940 // TODO: Remove method once ABI can be changed.
1941 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001942}
1943
Adrian Rooscbf37262015-10-22 16:12:53 -07001944size_t Parcel::getOpenAshmemSize() const
1945{
1946 return mOpenAshmemSize;
1947}
1948
Jeff Brown5707dbf2011-09-23 21:17:56 -07001949// --- Parcel::Blob ---
1950
1951Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08001952 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001953}
1954
1955Parcel::Blob::~Blob() {
1956 release();
1957}
1958
1959void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08001960 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001961 ::munmap(mData, mSize);
1962 }
1963 clear();
1964}
1965
Jeff Brown13b16042014-11-11 16:44:25 -08001966void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
1967 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001968 mData = data;
1969 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08001970 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001971}
1972
1973void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08001974 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001975 mData = NULL;
1976 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08001977 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001978}
1979
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001980}; // namespace android