blob: 1429a5c7b114e3bee1d9e46ee2ec0916b4361e2e [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>
Mark Salyzyn393aa432016-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 Salyzyn8bb1a532016-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>
Mathias Agopian002e1e52013-05-06 20:20:50 -070035#include <binder/TextOutput.h>
36
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080037#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070038#include <utils/Debug.h>
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080039#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070040#include <utils/Log.h>
Mark Salyzyn8bb1a532016-01-27 08:02:48 -080041#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042#include <utils/String8.h>
43#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070044
Mathias Agopian208059f2009-05-18 15:08:03 -070045#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080046#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#ifndef INT32_MAX
49#define INT32_MAX ((int32_t)(2147483647))
50#endif
51
52#define LOG_REFS(...)
Mark Salyzyn4d64cf92016-01-27 08:02:48 -080053//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080054#define LOG_ALLOC(...)
Mark Salyzyn4d64cf92016-01-27 08:02:48 -080055//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056
57// ---------------------------------------------------------------------------
58
Nick Kralevichb6b14232015-04-02 09:36:02 -070059// This macro should never be used at runtime, as a too large value
60// of s could cause an integer overflow. Instead, you should always
61// use the wrapper function pad_size()
62#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
63
64static size_t pad_size(size_t s) {
65 if (s > (SIZE_T_MAX - 3)) {
66 abort();
67 }
68 return PAD_SIZE_UNSAFE(s);
69}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070070
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070071// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080072#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070073
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -070074// Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER
75#define EX_HAS_REPLY_HEADER -128
76
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070077// XXX This can be made public if we want to provide
78// support for typed data.
79struct small_flat_data
80{
81 uint32_t type;
82 uint32_t data;
83};
84
85namespace android {
86
Dianne Hackborna4cff882014-11-13 17:07:40 -080087static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
88static size_t gParcelGlobalAllocSize = 0;
89static size_t gParcelGlobalAllocCount = 0;
90
Jeff Brown13b16042014-11-11 16:44:25 -080091// Maximum size of a blob to transfer in-place.
92static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
93
94enum {
95 BLOB_INPLACE = 0,
96 BLOB_ASHMEM_IMMUTABLE = 1,
97 BLOB_ASHMEM_MUTABLE = 2,
98};
99
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700101 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700102{
103 switch (obj.type) {
104 case BINDER_TYPE_BINDER:
105 if (obj.binder) {
106 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800107 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 }
109 return;
110 case BINDER_TYPE_WEAK_BINDER:
111 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800112 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 return;
114 case BINDER_TYPE_HANDLE: {
115 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
116 if (b != NULL) {
117 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
118 b->incStrong(who);
119 }
120 return;
121 }
122 case BINDER_TYPE_WEAK_HANDLE: {
123 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
124 if (b != NULL) b.get_refs()->incWeak(who);
125 return;
126 }
127 case BINDER_TYPE_FD: {
Mark Salyzyn393aa432016-01-27 08:02:48 -0800128 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
129 struct stat st;
130 int ret = fstat(obj.handle, &st);
131 if (!ret && S_ISCHR(st.st_mode)) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700132 // If we own an ashmem fd, keep track of how much memory it refers to.
133 int size = ashmem_get_size_region(obj.handle);
134 if (size > 0) {
135 *outAshmemSize += size;
136 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800143 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
147 const flat_binder_object& obj, const void* who)
148{
149 acquire_object(proc, obj, who, NULL);
150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
155 switch (obj.type) {
156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
168 if (b != NULL) {
169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
176 if (b != NULL) b.get_refs()->decWeak(who);
177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzyn9cc62e82016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
181 if (outAshmemSize != NULL) {
Mark Salyzyn393aa432016-01-27 08:02:48 -0800182 struct stat st;
183 int ret = fstat(obj.handle, &st);
184 if (!ret && S_ISCHR(st.st_mode)) {
185 int size = ashmem_get_size_region(obj.handle);
186 if (size > 0) {
187 *outAshmemSize -= size;
188 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700189 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700190 }
Mark Salyzyn9cc62e82016-01-27 08:02:48 -0800191
192 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700193 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 return;
195 }
196 }
197
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800198 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700199}
200
Adrian Roos6bb31142015-10-22 16:46:12 -0700201void release_object(const sp<ProcessState>& proc,
202 const flat_binder_object& obj, const void* who)
203{
204 release_object(proc, obj, who, NULL);
205}
206
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800208 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700209{
210 return out->writeObject(flat, false);
211}
212
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800213status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700214 const sp<IBinder>& binder, Parcel* out)
215{
216 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700217
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
219 if (binder != NULL) {
220 IBinder *local = binder->localBinder();
221 if (!local) {
222 BpBinder *proxy = binder->remoteBinder();
223 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000224 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225 }
226 const int32_t handle = proxy ? proxy->handle() : 0;
227 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800228 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800230 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700231 } else {
232 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
234 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 }
236 } else {
237 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800238 obj.binder = 0;
239 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700242 return finish_flatten_binder(binder, obj, out);
243}
244
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800245status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700246 const wp<IBinder>& binder, Parcel* out)
247{
248 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
251 if (binder != NULL) {
252 sp<IBinder> real = binder.promote();
253 if (real != NULL) {
254 IBinder *local = real->localBinder();
255 if (!local) {
256 BpBinder *proxy = real->remoteBinder();
257 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000258 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259 }
260 const int32_t handle = proxy ? proxy->handle() : 0;
261 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800262 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800264 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265 } else {
266 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
268 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269 }
270 return finish_flatten_binder(real, obj, out);
271 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700273 // XXX How to deal? In order to flatten the given binder,
274 // we need to probe it for information, which requires a primary
275 // reference... but we don't have one.
276 //
277 // The OpenBinder implementation uses a dynamic_cast<> here,
278 // but we can't do that with the different reference counting
279 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000280 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700281 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800282 obj.binder = 0;
283 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700286 } else {
287 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800288 obj.binder = 0;
289 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700290 return finish_flatten_binder(NULL, obj, out);
291 }
292}
293
294inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800295 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
296 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700297{
298 return NO_ERROR;
299}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700301status_t unflatten_binder(const sp<ProcessState>& proc,
302 const Parcel& in, sp<IBinder>* out)
303{
304 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 if (flat) {
307 switch (flat->type) {
308 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800309 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700310 return finish_unflatten_binder(NULL, *flat, in);
311 case BINDER_TYPE_HANDLE:
312 *out = proc->getStrongProxyForHandle(flat->handle);
313 return finish_unflatten_binder(
314 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700315 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700316 }
317 return BAD_TYPE;
318}
319
320status_t unflatten_binder(const sp<ProcessState>& proc,
321 const Parcel& in, wp<IBinder>* out)
322{
323 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 if (flat) {
326 switch (flat->type) {
327 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800328 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700329 return finish_unflatten_binder(NULL, *flat, in);
330 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800333 reinterpret_cast<IBinder*>(flat->cookie),
334 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 } else {
336 *out = NULL;
337 }
338 return finish_unflatten_binder(NULL, *flat, in);
339 case BINDER_TYPE_HANDLE:
340 case BINDER_TYPE_WEAK_HANDLE:
341 *out = proc->getWeakProxyForHandle(flat->handle);
342 return finish_unflatten_binder(
343 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
344 }
345 }
346 return BAD_TYPE;
347}
348
349// ---------------------------------------------------------------------------
350
351Parcel::Parcel()
352{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800353 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354 initState();
355}
356
357Parcel::~Parcel()
358{
359 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800360 LOG_ALLOC("Parcel %p: destroyed", this);
361}
362
363size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800364 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
365 size_t size = gParcelGlobalAllocSize;
366 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
367 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800368}
369
370size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800371 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
372 size_t count = gParcelGlobalAllocCount;
373 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
374 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700375}
376
377const uint8_t* Parcel::data() const
378{
379 return mData;
380}
381
382size_t Parcel::dataSize() const
383{
384 return (mDataSize > mDataPos ? mDataSize : mDataPos);
385}
386
387size_t Parcel::dataAvail() const
388{
389 // TODO: decide what to do about the possibility that this can
390 // report an available-data size that exceeds a Java int's max
391 // positive value, causing havoc. Fortunately this will only
392 // happen if someone constructs a Parcel containing more than two
393 // gigabytes of data, which on typical phone hardware is simply
394 // not possible.
395 return dataSize() - dataPosition();
396}
397
398size_t Parcel::dataPosition() const
399{
400 return mDataPos;
401}
402
403size_t Parcel::dataCapacity() const
404{
405 return mDataCapacity;
406}
407
408status_t Parcel::setDataSize(size_t size)
409{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700410 if (size > INT32_MAX) {
411 // don't accept size_t values which may have come from an
412 // inadvertent conversion from a negative int.
413 return BAD_VALUE;
414 }
415
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700416 status_t err;
417 err = continueWrite(size);
418 if (err == NO_ERROR) {
419 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700420 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 }
422 return err;
423}
424
425void Parcel::setDataPosition(size_t pos) const
426{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 if (pos > INT32_MAX) {
428 // don't accept size_t values which may have come from an
429 // inadvertent conversion from a negative int.
430 abort();
431 }
432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700433 mDataPos = pos;
434 mNextObjectHint = 0;
435}
436
437status_t Parcel::setDataCapacity(size_t size)
438{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700439 if (size > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700445 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700446 return NO_ERROR;
447}
448
449status_t Parcel::setData(const uint8_t* buffer, size_t len)
450{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700451 if (len > INT32_MAX) {
452 // don't accept size_t values which may have come from an
453 // inadvertent conversion from a negative int.
454 return BAD_VALUE;
455 }
456
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 status_t err = restartWrite(len);
458 if (err == NO_ERROR) {
459 memcpy(const_cast<uint8_t*>(data()), buffer, len);
460 mDataSize = len;
461 mFdsKnown = false;
462 }
463 return err;
464}
465
Andreas Huber51faf462011-04-13 10:21:56 -0700466status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700467{
468 const sp<ProcessState> proc(ProcessState::self());
469 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700470 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800471 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 size_t size = parcel->mObjectsSize;
473 int startPos = mDataPos;
474 int firstIndex = -1, lastIndex = -2;
475
476 if (len == 0) {
477 return NO_ERROR;
478 }
479
Nick Kralevichb6b14232015-04-02 09:36:02 -0700480 if (len > INT32_MAX) {
481 // don't accept size_t values which may have come from an
482 // inadvertent conversion from a negative int.
483 return BAD_VALUE;
484 }
485
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700486 // range checks against the source parcel size
487 if ((offset > parcel->mDataSize)
488 || (len > parcel->mDataSize)
489 || (offset + len > parcel->mDataSize)) {
490 return BAD_VALUE;
491 }
492
493 // Count objects in range
494 for (int i = 0; i < (int) size; i++) {
495 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700496 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 if (firstIndex == -1) {
498 firstIndex = i;
499 }
500 lastIndex = i;
501 }
502 }
503 int numObjects = lastIndex - firstIndex + 1;
504
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700505 if ((mDataSize+len) > mDataCapacity) {
506 // grow data
507 err = growData(len);
508 if (err != NO_ERROR) {
509 return err;
510 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 }
512
513 // append data
514 memcpy(mData + mDataPos, data + offset, len);
515 mDataPos += len;
516 mDataSize += len;
517
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400518 err = NO_ERROR;
519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 if (numObjects > 0) {
521 // grow objects
522 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700523 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
524 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800525 binder_size_t *objects =
526 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
527 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528 return NO_MEMORY;
529 }
530 mObjects = objects;
531 mObjectsCapacity = newSize;
532 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700534 // append and acquire objects
535 int idx = mObjectsSize;
536 for (int i = firstIndex; i <= lastIndex; i++) {
537 size_t off = objects[i] - offset + startPos;
538 mObjects[idx++] = off;
539 mObjectsSize++;
540
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700541 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700543 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700546 // If this is a file descriptor, we need to dup it so the
547 // new Parcel now owns its own fd, and can declare that we
548 // officially know we have fds.
549 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800550 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 if (!mAllowFds) {
553 err = FDS_NOT_ALLOWED;
554 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555 }
556 }
557 }
558
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400559 return err;
560}
561
Jeff Brown13b16042014-11-11 16:44:25 -0800562bool Parcel::allowFds() const
563{
564 return mAllowFds;
565}
566
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700567bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400568{
569 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700570 if (!allowFds) {
571 mAllowFds = false;
572 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400573 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700574}
575
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700576void Parcel::restoreAllowFds(bool lastValue)
577{
578 mAllowFds = lastValue;
579}
580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700581bool Parcel::hasFileDescriptors() const
582{
583 if (!mFdsKnown) {
584 scanForFds();
585 }
586 return mHasFds;
587}
588
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700589// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590status_t Parcel::writeInterfaceToken(const String16& interface)
591{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700592 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
593 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700594 // currently the interface identification token is just its name as a string
595 return writeString16(interface);
596}
597
Mathias Agopian83c04462009-05-22 19:00:22 -0700598bool Parcel::checkInterface(IBinder* binder) const
599{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700600 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700601}
602
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700603bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700604 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700606 int32_t strictPolicy = readInt32();
607 if (threadState == NULL) {
608 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700609 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700610 if ((threadState->getLastTransactionBinderFlags() &
611 IBinder::FLAG_ONEWAY) != 0) {
612 // For one-way calls, the callee is running entirely
613 // disconnected from the caller, so disable StrictMode entirely.
614 // Not only does disk/network usage not impact the caller, but
615 // there's no way to commuicate back any violations anyway.
616 threadState->setStrictModePolicy(0);
617 } else {
618 threadState->setStrictModePolicy(strictPolicy);
619 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700620 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 if (str == interface) {
622 return true;
623 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700624 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625 String8(interface).string(), String8(str).string());
626 return false;
627 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700628}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700629
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800630const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631{
632 return mObjects;
633}
634
635size_t Parcel::objectsCount() const
636{
637 return mObjectsSize;
638}
639
640status_t Parcel::errorCheck() const
641{
642 return mError;
643}
644
645void Parcel::setError(status_t err)
646{
647 mError = err;
648}
649
650status_t Parcel::finishWrite(size_t len)
651{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700652 if (len > INT32_MAX) {
653 // don't accept size_t values which may have come from an
654 // inadvertent conversion from a negative int.
655 return BAD_VALUE;
656 }
657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700658 //printf("Finish write of %d\n", len);
659 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700660 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700661 if (mDataPos > mDataSize) {
662 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700663 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700664 }
665 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
666 return NO_ERROR;
667}
668
669status_t Parcel::writeUnpadded(const void* data, size_t len)
670{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700671 if (len > INT32_MAX) {
672 // don't accept size_t values which may have come from an
673 // inadvertent conversion from a negative int.
674 return BAD_VALUE;
675 }
676
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 size_t end = mDataPos + len;
678 if (end < mDataPos) {
679 // integer overflow
680 return BAD_VALUE;
681 }
682
683 if (end <= mDataCapacity) {
684restart_write:
685 memcpy(mData+mDataPos, data, len);
686 return finishWrite(len);
687 }
688
689 status_t err = growData(len);
690 if (err == NO_ERROR) goto restart_write;
691 return err;
692}
693
694status_t Parcel::write(const void* data, size_t len)
695{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700696 if (len > INT32_MAX) {
697 // don't accept size_t values which may have come from an
698 // inadvertent conversion from a negative int.
699 return BAD_VALUE;
700 }
701
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700702 void* const d = writeInplace(len);
703 if (d) {
704 memcpy(d, data, len);
705 return NO_ERROR;
706 }
707 return mError;
708}
709
710void* Parcel::writeInplace(size_t len)
711{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700712 if (len > INT32_MAX) {
713 // don't accept size_t values which may have come from an
714 // inadvertent conversion from a negative int.
715 return NULL;
716 }
717
718 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700719
720 // sanity check for integer overflow
721 if (mDataPos+padded < mDataPos) {
722 return NULL;
723 }
724
725 if ((mDataPos+padded) <= mDataCapacity) {
726restart_write:
727 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
728 uint8_t* const data = mData+mDataPos;
729
730 // Need to pad at end?
731 if (padded != len) {
732#if BYTE_ORDER == BIG_ENDIAN
733 static const uint32_t mask[4] = {
734 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
735 };
736#endif
737#if BYTE_ORDER == LITTLE_ENDIAN
738 static const uint32_t mask[4] = {
739 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
740 };
741#endif
742 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
743 // *reinterpret_cast<void**>(data+padded-4));
744 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
745 }
746
747 finishWrite(padded);
748 return data;
749 }
750
751 status_t err = growData(padded);
752 if (err == NO_ERROR) goto restart_write;
753 return NULL;
754}
755
756status_t Parcel::writeInt32(int32_t val)
757{
Andreas Huber84a6d042009-08-17 13:33:27 -0700758 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800760
761status_t Parcel::writeUint32(uint32_t val)
762{
763 return writeAligned(val);
764}
765
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700766status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700767 if (len > INT32_MAX) {
768 // don't accept size_t values which may have come from an
769 // inadvertent conversion from a negative int.
770 return BAD_VALUE;
771 }
772
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700773 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700774 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700775 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700776 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700777 if (ret == NO_ERROR) {
778 ret = write(val, len * sizeof(*val));
779 }
780 return ret;
781}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700782status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700783 if (len > INT32_MAX) {
784 // don't accept size_t values which may have come from an
785 // inadvertent conversion from a negative int.
786 return BAD_VALUE;
787 }
788
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700789 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700790 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700791 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700792 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700793 if (ret == NO_ERROR) {
794 ret = write(val, len * sizeof(*val));
795 }
796 return ret;
797}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700798
799status_t Parcel::writeInt64(int64_t val)
800{
Andreas Huber84a6d042009-08-17 13:33:27 -0700801 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700802}
803
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700804status_t Parcel::writeUint64(uint64_t val)
805{
806 return writeAligned(val);
807}
808
Serban Constantinescuf683e012013-11-05 16:53:55 +0000809status_t Parcel::writePointer(uintptr_t val)
810{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800811 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000812}
813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700814status_t Parcel::writeFloat(float val)
815{
Andreas Huber84a6d042009-08-17 13:33:27 -0700816 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700817}
818
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800819#if defined(__mips__) && defined(__mips_hard_float)
820
821status_t Parcel::writeDouble(double val)
822{
823 union {
824 double d;
825 unsigned long long ll;
826 } u;
827 u.d = val;
828 return writeAligned(u.ll);
829}
830
831#else
832
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700833status_t Parcel::writeDouble(double val)
834{
Andreas Huber84a6d042009-08-17 13:33:27 -0700835 return writeAligned(val);
836}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700837
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800838#endif
839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700840status_t Parcel::writeCString(const char* str)
841{
842 return write(str, strlen(str)+1);
843}
844
845status_t Parcel::writeString8(const String8& str)
846{
847 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +0100848 // only write string if its length is more than zero characters,
849 // as readString8 will only read if the length field is non-zero.
850 // this is slightly different from how writeString16 works.
851 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700852 err = write(str.string(), str.bytes()+1);
853 }
854 return err;
855}
856
857status_t Parcel::writeString16(const String16& str)
858{
859 return writeString16(str.string(), str.size());
860}
861
862status_t Parcel::writeString16(const char16_t* str, size_t len)
863{
864 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700865
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700866 status_t err = writeInt32(len);
867 if (err == NO_ERROR) {
868 len *= sizeof(char16_t);
869 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
870 if (data) {
871 memcpy(data, str, len);
872 *reinterpret_cast<char16_t*>(data+len) = 0;
873 return NO_ERROR;
874 }
875 err = mError;
876 }
877 return err;
878}
879
880status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
881{
882 return flatten_binder(ProcessState::self(), val, this);
883}
884
885status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
886{
887 return flatten_binder(ProcessState::self(), val, this);
888}
889
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700890status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800891{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -0700892 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800893 return BAD_TYPE;
894
895 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700896 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800897 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700899 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800900 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700902 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
903 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904
905 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +0000906 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 return err;
908 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -0700909 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800910 return err;
911}
912
Jeff Brown93ff1f92011-11-04 19:01:44 -0700913status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700914{
915 flat_binder_object obj;
916 obj.type = BINDER_TYPE_FD;
917 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800918 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800920 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921 return writeObject(obj, true);
922}
923
924status_t Parcel::writeDupFileDescriptor(int fd)
925{
Jeff Brownd341c712011-11-04 20:19:33 -0700926 int dupFd = dup(fd);
927 if (dupFd < 0) {
928 return -errno;
929 }
930 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
931 if (err) {
932 close(dupFd);
933 }
934 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700935}
936
Jeff Brown13b16042014-11-11 16:44:25 -0800937status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -0700938{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700939 if (len > INT32_MAX) {
940 // don't accept size_t values which may have come from an
941 // inadvertent conversion from a negative int.
942 return BAD_VALUE;
943 }
944
Jeff Brown13b16042014-11-11 16:44:25 -0800945 status_t status;
946 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +0100947 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -0800948 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700949 if (status) return status;
950
951 void* ptr = writeInplace(len);
952 if (!ptr) return NO_MEMORY;
953
Jeff Brown13b16042014-11-11 16:44:25 -0800954 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700955 return NO_ERROR;
956 }
957
Steve Block6807e592011-10-20 11:56:00 +0100958 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -0700959 int fd = ashmem_create_region("Parcel Blob", len);
960 if (fd < 0) return NO_MEMORY;
961
962 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
963 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700964 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700965 } else {
966 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
967 if (ptr == MAP_FAILED) {
968 status = -errno;
969 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800970 if (!mutableCopy) {
971 result = ashmem_set_prot_region(fd, PROT_READ);
972 }
Jeff Brown5707dbf2011-09-23 21:17:56 -0700973 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -0700974 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -0700975 } else {
Jeff Brown13b16042014-11-11 16:44:25 -0800976 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700977 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -0700978 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700979 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -0800980 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -0700981 return NO_ERROR;
982 }
983 }
984 }
985 }
986 ::munmap(ptr, len);
987 }
988 ::close(fd);
989 return status;
990}
991
Jeff Brown13b16042014-11-11 16:44:25 -0800992status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
993{
994 // Must match up with what's done in writeBlob.
995 if (!mAllowFds) return FDS_NOT_ALLOWED;
996 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
997 if (status) return status;
998 return writeDupFileDescriptor(fd);
999}
1000
Mathias Agopiane1424282013-07-29 21:24:40 -07001001status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001002{
1003 status_t err;
1004
1005 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001006 const size_t len = val.getFlattenedSize();
1007 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001008
Nick Kralevichb6b14232015-04-02 09:36:02 -07001009 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1010 // don't accept size_t values which may have come from an
1011 // inadvertent conversion from a negative int.
1012 return BAD_VALUE;
1013 }
1014
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001015 err = this->writeInt32(len);
1016 if (err) return err;
1017
1018 err = this->writeInt32(fd_count);
1019 if (err) return err;
1020
1021 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001022 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001023 if (buf == NULL)
1024 return BAD_VALUE;
1025
1026 int* fds = NULL;
1027 if (fd_count) {
1028 fds = new int[fd_count];
1029 }
1030
1031 err = val.flatten(buf, len, fds, fd_count);
1032 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1033 err = this->writeDupFileDescriptor( fds[i] );
1034 }
1035
1036 if (fd_count) {
1037 delete [] fds;
1038 }
1039
1040 return err;
1041}
1042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1044{
1045 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1046 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1047 if (enoughData && enoughObjects) {
1048restart_write:
1049 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001050
Christopher Tate98e67d32015-06-03 18:44:15 -07001051 // remember if it's a file descriptor
1052 if (val.type == BINDER_TYPE_FD) {
1053 if (!mAllowFds) {
1054 // fail before modifying our object index
1055 return FDS_NOT_ALLOWED;
1056 }
1057 mHasFds = mFdsKnown = true;
1058 }
1059
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001061 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001063 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064 mObjectsSize++;
1065 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067 return finishWrite(sizeof(flat_binder_object));
1068 }
1069
1070 if (!enoughData) {
1071 const status_t err = growData(sizeof(val));
1072 if (err != NO_ERROR) return err;
1073 }
1074 if (!enoughObjects) {
1075 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001076 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001077 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001078 if (objects == NULL) return NO_MEMORY;
1079 mObjects = objects;
1080 mObjectsCapacity = newSize;
1081 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083 goto restart_write;
1084}
1085
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001086status_t Parcel::writeNoException()
1087{
1088 return writeInt32(0);
1089}
1090
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001091void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092{
1093 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1094}
1095
1096status_t Parcel::read(void* outData, size_t len) const
1097{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001098 if (len > INT32_MAX) {
1099 // don't accept size_t values which may have come from an
1100 // inadvertent conversion from a negative int.
1101 return BAD_VALUE;
1102 }
1103
1104 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1105 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001107 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001108 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001109 return NO_ERROR;
1110 }
1111 return NOT_ENOUGH_DATA;
1112}
1113
1114const void* Parcel::readInplace(size_t len) const
1115{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001116 if (len > INT32_MAX) {
1117 // don't accept size_t values which may have come from an
1118 // inadvertent conversion from a negative int.
1119 return NULL;
1120 }
1121
1122 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1123 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001124 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001125 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001126 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001127 return data;
1128 }
1129 return NULL;
1130}
1131
Andreas Huber84a6d042009-08-17 13:33:27 -07001132template<class T>
1133status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001134 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001135
1136 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001137 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001138 mDataPos += sizeof(T);
1139 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001140 return NO_ERROR;
1141 } else {
1142 return NOT_ENOUGH_DATA;
1143 }
1144}
1145
Andreas Huber84a6d042009-08-17 13:33:27 -07001146template<class T>
1147T Parcel::readAligned() const {
1148 T result;
1149 if (readAligned(&result) != NO_ERROR) {
1150 result = 0;
1151 }
1152
1153 return result;
1154}
1155
1156template<class T>
1157status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001158 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001159
1160 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1161restart_write:
1162 *reinterpret_cast<T*>(mData+mDataPos) = val;
1163 return finishWrite(sizeof(val));
1164 }
1165
1166 status_t err = growData(sizeof(val));
1167 if (err == NO_ERROR) goto restart_write;
1168 return err;
1169}
1170
1171status_t Parcel::readInt32(int32_t *pArg) const
1172{
1173 return readAligned(pArg);
1174}
1175
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176int32_t Parcel::readInt32() const
1177{
Andreas Huber84a6d042009-08-17 13:33:27 -07001178 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001179}
1180
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001181status_t Parcel::readUint32(uint32_t *pArg) const
1182{
1183 return readAligned(pArg);
1184}
1185
1186uint32_t Parcel::readUint32() const
1187{
1188 return readAligned<uint32_t>();
1189}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001190
1191status_t Parcel::readInt64(int64_t *pArg) const
1192{
Andreas Huber84a6d042009-08-17 13:33:27 -07001193 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001194}
1195
1196
1197int64_t Parcel::readInt64() const
1198{
Andreas Huber84a6d042009-08-17 13:33:27 -07001199 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001200}
1201
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001202status_t Parcel::readUint64(uint64_t *pArg) const
1203{
1204 return readAligned(pArg);
1205}
1206
1207uint64_t Parcel::readUint64() const
1208{
1209 return readAligned<uint64_t>();
1210}
1211
Serban Constantinescuf683e012013-11-05 16:53:55 +00001212status_t Parcel::readPointer(uintptr_t *pArg) const
1213{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001214 status_t ret;
1215 binder_uintptr_t ptr;
1216 ret = readAligned(&ptr);
1217 if (!ret)
1218 *pArg = ptr;
1219 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001220}
1221
1222uintptr_t Parcel::readPointer() const
1223{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001224 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001225}
1226
1227
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001228status_t Parcel::readFloat(float *pArg) const
1229{
Andreas Huber84a6d042009-08-17 13:33:27 -07001230 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001231}
1232
1233
1234float Parcel::readFloat() const
1235{
Andreas Huber84a6d042009-08-17 13:33:27 -07001236 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001237}
1238
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001239#if defined(__mips__) && defined(__mips_hard_float)
1240
1241status_t Parcel::readDouble(double *pArg) const
1242{
1243 union {
1244 double d;
1245 unsigned long long ll;
1246 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001247 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001248 status_t status;
1249 status = readAligned(&u.ll);
1250 *pArg = u.d;
1251 return status;
1252}
1253
1254double Parcel::readDouble() const
1255{
1256 union {
1257 double d;
1258 unsigned long long ll;
1259 } u;
1260 u.ll = readAligned<unsigned long long>();
1261 return u.d;
1262}
1263
1264#else
1265
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001266status_t Parcel::readDouble(double *pArg) const
1267{
Andreas Huber84a6d042009-08-17 13:33:27 -07001268 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001269}
1270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271double Parcel::readDouble() const
1272{
Andreas Huber84a6d042009-08-17 13:33:27 -07001273 return readAligned<double>();
1274}
1275
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001276#endif
1277
Andreas Huber84a6d042009-08-17 13:33:27 -07001278status_t Parcel::readIntPtr(intptr_t *pArg) const
1279{
1280 return readAligned(pArg);
1281}
1282
1283
1284intptr_t Parcel::readIntPtr() const
1285{
1286 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287}
1288
1289
1290const char* Parcel::readCString() const
1291{
1292 const size_t avail = mDataSize-mDataPos;
1293 if (avail > 0) {
1294 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1295 // is the string's trailing NUL within the parcel's valid bounds?
1296 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1297 if (eos) {
1298 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001299 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001300 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301 return str;
1302 }
1303 }
1304 return NULL;
1305}
1306
1307String8 Parcel::readString8() const
1308{
1309 int32_t size = readInt32();
1310 // watch for potential int overflow adding 1 for trailing NUL
1311 if (size > 0 && size < INT32_MAX) {
1312 const char* str = (const char*)readInplace(size+1);
1313 if (str) return String8(str, size);
1314 }
1315 return String8();
1316}
1317
1318String16 Parcel::readString16() const
1319{
1320 size_t len;
1321 const char16_t* str = readString16Inplace(&len);
1322 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001323 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 return String16();
1325}
1326
1327const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1328{
1329 int32_t size = readInt32();
1330 // watch for potential int overflow from size+1
1331 if (size >= 0 && size < INT32_MAX) {
1332 *outLen = size;
1333 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1334 if (str != NULL) {
1335 return str;
1336 }
1337 }
1338 *outLen = 0;
1339 return NULL;
1340}
1341
1342sp<IBinder> Parcel::readStrongBinder() const
1343{
1344 sp<IBinder> val;
1345 unflatten_binder(ProcessState::self(), *this, &val);
1346 return val;
1347}
1348
1349wp<IBinder> Parcel::readWeakBinder() const
1350{
1351 wp<IBinder> val;
1352 unflatten_binder(ProcessState::self(), *this, &val);
1353 return val;
1354}
1355
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001356int32_t Parcel::readExceptionCode() const
1357{
1358 int32_t exception_code = readAligned<int32_t>();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001359 if (exception_code == EX_HAS_REPLY_HEADER) {
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001360 int32_t header_start = dataPosition();
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001361 int32_t header_size = readAligned<int32_t>();
1362 // Skip over fat responses headers. Not used (or propagated) in
1363 // native code
Magnus Strandberg1ba24572011-05-03 15:44:00 +02001364 setDataPosition(header_start + header_size);
Brad Fitzpatrickd36f4a52010-07-12 11:05:38 -07001365 // And fat response headers are currently only used when there are no
1366 // exceptions, so return no error:
1367 return 0;
1368 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001369 return exception_code;
1370}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001371
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001372native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001373{
1374 int numFds, numInts;
1375 status_t err;
1376 err = readInt32(&numFds);
1377 if (err != NO_ERROR) return 0;
1378 err = readInt32(&numInts);
1379 if (err != NO_ERROR) return 0;
1380
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001381 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001382 if (!h) {
1383 return 0;
1384 }
1385
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001386 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001387 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001388 if (h->data[i] < 0) err = BAD_VALUE;
1389 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001390 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001391 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001392 native_handle_close(h);
1393 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001394 h = 0;
1395 }
1396 return h;
1397}
1398
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400int Parcel::readFileDescriptor() const
1401{
1402 const flat_binder_object* flat = readObject(true);
1403 if (flat) {
1404 switch (flat->type) {
1405 case BINDER_TYPE_FD:
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001406 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407 return flat->handle;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001408 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 }
1410 return BAD_TYPE;
1411}
1412
Jeff Brown5707dbf2011-09-23 21:17:56 -07001413status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1414{
Jeff Brown13b16042014-11-11 16:44:25 -08001415 int32_t blobType;
1416 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001417 if (status) return status;
1418
Jeff Brown13b16042014-11-11 16:44:25 -08001419 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001420 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001421 const void* ptr = readInplace(len);
1422 if (!ptr) return BAD_VALUE;
1423
Jeff Brown13b16042014-11-11 16:44:25 -08001424 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001425 return NO_ERROR;
1426 }
1427
Steve Block6807e592011-10-20 11:56:00 +01001428 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001429 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001430 int fd = readFileDescriptor();
1431 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1432
Jeff Brown13b16042014-11-11 16:44:25 -08001433 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1434 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001435 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001436
Jeff Brown13b16042014-11-11 16:44:25 -08001437 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001438 return NO_ERROR;
1439}
1440
Mathias Agopiane1424282013-07-29 21:24:40 -07001441status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001442{
1443 // size
1444 const size_t len = this->readInt32();
1445 const size_t fd_count = this->readInt32();
1446
Nick Kralevichb6b14232015-04-02 09:36:02 -07001447 if (len > INT32_MAX) {
1448 // don't accept size_t values which may have come from an
1449 // inadvertent conversion from a negative int.
1450 return BAD_VALUE;
1451 }
1452
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001453 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001454 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001455 if (buf == NULL)
1456 return BAD_VALUE;
1457
1458 int* fds = NULL;
1459 if (fd_count) {
1460 fds = new int[fd_count];
1461 }
1462
1463 status_t err = NO_ERROR;
1464 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001465 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001466 if (fds[i] < 0) {
1467 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001468 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1469 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001470 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001471 }
1472
1473 if (err == NO_ERROR) {
1474 err = val.unflatten(buf, len, fds, fd_count);
1475 }
1476
1477 if (fd_count) {
1478 delete [] fds;
1479 }
1480
1481 return err;
1482}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1484{
1485 const size_t DPOS = mDataPos;
1486 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1487 const flat_binder_object* obj
1488 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1489 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001490 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001491 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001492 // the object list, so we don't want to check for it when
1493 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001494 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495 return obj;
1496 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001499 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 const size_t N = mObjectsSize;
1501 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001504 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001505 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001506
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001507 // Start at the current hint position, looking for an object at
1508 // the current data position.
1509 if (opos < N) {
1510 while (opos < (N-1) && OBJS[opos] < DPOS) {
1511 opos++;
1512 }
1513 } else {
1514 opos = N-1;
1515 }
1516 if (OBJS[opos] == DPOS) {
1517 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001518 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001519 this, DPOS, opos);
1520 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001521 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 return obj;
1523 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525 // Look backwards for it...
1526 while (opos > 0 && OBJS[opos] > DPOS) {
1527 opos--;
1528 }
1529 if (OBJS[opos] == DPOS) {
1530 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001531 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001532 this, DPOS, opos);
1533 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001534 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 return obj;
1536 }
1537 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001538 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 -07001539 this, DPOS);
1540 }
1541 return NULL;
1542}
1543
1544void Parcel::closeFileDescriptors()
1545{
1546 size_t i = mObjectsSize;
1547 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001548 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001549 }
1550 while (i > 0) {
1551 i--;
1552 const flat_binder_object* flat
1553 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1554 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001555 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556 close(flat->handle);
1557 }
1558 }
1559}
1560
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001561uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001563 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564}
1565
1566size_t Parcel::ipcDataSize() const
1567{
1568 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1569}
1570
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001571uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001573 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001574}
1575
1576size_t Parcel::ipcObjectsCount() const
1577{
1578 return mObjectsSize;
1579}
1580
1581void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001582 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001584 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585 freeDataNoInit();
1586 mError = NO_ERROR;
1587 mData = const_cast<uint8_t*>(data);
1588 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001589 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001590 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001591 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001592 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001593 mObjectsSize = mObjectsCapacity = objectsCount;
1594 mNextObjectHint = 0;
1595 mOwner = relFunc;
1596 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001597 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001598 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001599 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08001600 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08001601 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08001602 mObjectsSize = 0;
1603 break;
1604 }
1605 minOffset = offset + sizeof(flat_binder_object);
1606 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001607 scanForFds();
1608}
1609
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001610void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001611{
1612 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614 if (errorCheck() != NO_ERROR) {
1615 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001616 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617 } else if (dataSize() > 0) {
1618 const uint8_t* DATA = data();
1619 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001620 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001621 const size_t N = objectsCount();
1622 for (size_t i=0; i<N; i++) {
1623 const flat_binder_object* flat
1624 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1625 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1626 << TypeCode(flat->type & 0x7f7f7f00)
1627 << " = " << flat->binder;
1628 }
1629 } else {
1630 to << "NULL";
1631 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633 to << ")";
1634}
1635
1636void Parcel::releaseObjects()
1637{
1638 const sp<ProcessState> proc(ProcessState::self());
1639 size_t i = mObjectsSize;
1640 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001641 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642 while (i > 0) {
1643 i--;
1644 const flat_binder_object* flat
1645 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001646 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001647 }
1648}
1649
1650void Parcel::acquireObjects()
1651{
1652 const sp<ProcessState> proc(ProcessState::self());
1653 size_t i = mObjectsSize;
1654 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001655 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001656 while (i > 0) {
1657 i--;
1658 const flat_binder_object* flat
1659 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07001660 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001661 }
1662}
1663
1664void Parcel::freeData()
1665{
1666 freeDataNoInit();
1667 initState();
1668}
1669
1670void Parcel::freeDataNoInit()
1671{
1672 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001673 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001674 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001675 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1676 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001677 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001679 if (mData) {
1680 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001681 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001682 gParcelGlobalAllocSize -= mDataCapacity;
1683 gParcelGlobalAllocCount--;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001684 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001685 free(mData);
1686 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687 if (mObjects) free(mObjects);
1688 }
1689}
1690
1691status_t Parcel::growData(size_t len)
1692{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001693 if (len > INT32_MAX) {
1694 // don't accept size_t values which may have come from an
1695 // inadvertent conversion from a negative int.
1696 return BAD_VALUE;
1697 }
1698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001699 size_t newSize = ((mDataSize+len)*3)/2;
1700 return (newSize <= mDataSize)
1701 ? (status_t) NO_MEMORY
1702 : continueWrite(newSize);
1703}
1704
1705status_t Parcel::restartWrite(size_t desired)
1706{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001707 if (desired > INT32_MAX) {
1708 // don't accept size_t values which may have come from an
1709 // inadvertent conversion from a negative int.
1710 return BAD_VALUE;
1711 }
1712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713 if (mOwner) {
1714 freeData();
1715 return continueWrite(desired);
1716 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718 uint8_t* data = (uint8_t*)realloc(mData, desired);
1719 if (!data && desired > mDataCapacity) {
1720 mError = NO_MEMORY;
1721 return NO_MEMORY;
1722 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001723
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001724 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001727 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001728 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001729 gParcelGlobalAllocSize += desired;
1730 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001731 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732 mData = data;
1733 mDataCapacity = desired;
1734 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001735
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001736 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001737 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
1738 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
1739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740 free(mObjects);
1741 mObjects = NULL;
1742 mObjectsSize = mObjectsCapacity = 0;
1743 mNextObjectHint = 0;
1744 mHasFds = false;
1745 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001746 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 return NO_ERROR;
1749}
1750
1751status_t Parcel::continueWrite(size_t desired)
1752{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001753 if (desired > INT32_MAX) {
1754 // don't accept size_t values which may have come from an
1755 // inadvertent conversion from a negative int.
1756 return BAD_VALUE;
1757 }
1758
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001759 // If shrinking, first adjust for any objects that appear
1760 // after the new data size.
1761 size_t objectsSize = mObjectsSize;
1762 if (desired < mDataSize) {
1763 if (desired == 0) {
1764 objectsSize = 0;
1765 } else {
1766 while (objectsSize > 0) {
1767 if (mObjects[objectsSize-1] < desired)
1768 break;
1769 objectsSize--;
1770 }
1771 }
1772 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001773
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001774 if (mOwner) {
1775 // If the size is going to zero, just release the owner's data.
1776 if (desired == 0) {
1777 freeData();
1778 return NO_ERROR;
1779 }
1780
1781 // If there is a different owner, we need to take
1782 // posession.
1783 uint8_t* data = (uint8_t*)malloc(desired);
1784 if (!data) {
1785 mError = NO_MEMORY;
1786 return NO_MEMORY;
1787 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001788 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001789
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001790 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07001791 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09001793 free(data);
1794
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001795 mError = NO_MEMORY;
1796 return NO_MEMORY;
1797 }
1798
1799 // Little hack to only acquire references on objects
1800 // we will be keeping.
1801 size_t oldObjectsSize = mObjectsSize;
1802 mObjectsSize = objectsSize;
1803 acquireObjects();
1804 mObjectsSize = oldObjectsSize;
1805 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807 if (mData) {
1808 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
1809 }
1810 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001811 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001813 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1815 mOwner = NULL;
1816
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001817 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001818 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001819 gParcelGlobalAllocSize += desired;
1820 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001821 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001822
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001823 mData = data;
1824 mObjects = objects;
1825 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001826 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001827 mDataCapacity = desired;
1828 mObjectsSize = mObjectsCapacity = objectsSize;
1829 mNextObjectHint = 0;
1830
1831 } else if (mData) {
1832 if (objectsSize < mObjectsSize) {
1833 // Need to release refs on any objects we are dropping.
1834 const sp<ProcessState> proc(ProcessState::self());
1835 for (size_t i=objectsSize; i<mObjectsSize; i++) {
1836 const flat_binder_object* flat
1837 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1838 if (flat->type == BINDER_TYPE_FD) {
1839 // will need to rescan because we may have lopped off the only FDs
1840 mFdsKnown = false;
1841 }
Adrian Rooscbf37262015-10-22 16:12:53 -07001842 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001844 binder_size_t* objects =
1845 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846 if (objects) {
1847 mObjects = objects;
1848 }
1849 mObjectsSize = objectsSize;
1850 mNextObjectHint = 0;
1851 }
1852
1853 // We own the data, so we can just do a realloc().
1854 if (desired > mDataCapacity) {
1855 uint8_t* data = (uint8_t*)realloc(mData, desired);
1856 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001857 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
1858 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001859 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001860 gParcelGlobalAllocSize += desired;
1861 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001862 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863 mData = data;
1864 mDataCapacity = desired;
1865 } else if (desired > mDataCapacity) {
1866 mError = NO_MEMORY;
1867 return NO_MEMORY;
1868 }
1869 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001870 if (mDataSize > desired) {
1871 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001872 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07001873 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001874 if (mDataPos > desired) {
1875 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001876 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001877 }
1878 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001879
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001880 } else {
1881 // This is the first data. Easy!
1882 uint8_t* data = (uint8_t*)malloc(desired);
1883 if (!data) {
1884 mError = NO_MEMORY;
1885 return NO_MEMORY;
1886 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09001887
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001888 if(!(mDataCapacity == 0 && mObjects == NULL
1889 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001890 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001891 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001892
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001893 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08001894 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001895 gParcelGlobalAllocSize += desired;
1896 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08001897 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899 mData = data;
1900 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001901 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
1902 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903 mDataCapacity = desired;
1904 }
1905
1906 return NO_ERROR;
1907}
1908
1909void Parcel::initState()
1910{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08001911 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001912 mError = NO_ERROR;
1913 mData = 0;
1914 mDataSize = 0;
1915 mDataCapacity = 0;
1916 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001917 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
1918 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919 mObjects = NULL;
1920 mObjectsSize = 0;
1921 mObjectsCapacity = 0;
1922 mNextObjectHint = 0;
1923 mHasFds = false;
1924 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04001925 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07001927 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001928}
1929
1930void Parcel::scanForFds() const
1931{
1932 bool hasFds = false;
1933 for (size_t i=0; i<mObjectsSize; i++) {
1934 const flat_binder_object* flat
1935 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
1936 if (flat->type == BINDER_TYPE_FD) {
1937 hasFds = true;
1938 break;
1939 }
1940 }
1941 mHasFds = hasFds;
1942 mFdsKnown = true;
1943}
1944
Dan Sandleraa5c2342015-04-10 10:08:45 -04001945size_t Parcel::getBlobAshmemSize() const
1946{
Adrian Roos6bb31142015-10-22 16:46:12 -07001947 // This used to return the size of all blobs that were written to ashmem, now we're returning
1948 // the ashmem currently referenced by this Parcel, which should be equivalent.
1949 // TODO: Remove method once ABI can be changed.
1950 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04001951}
1952
Adrian Rooscbf37262015-10-22 16:12:53 -07001953size_t Parcel::getOpenAshmemSize() const
1954{
1955 return mOpenAshmemSize;
1956}
1957
Jeff Brown5707dbf2011-09-23 21:17:56 -07001958// --- Parcel::Blob ---
1959
1960Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08001961 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001962}
1963
1964Parcel::Blob::~Blob() {
1965 release();
1966}
1967
1968void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08001969 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07001970 ::munmap(mData, mSize);
1971 }
1972 clear();
1973}
1974
Jeff Brown13b16042014-11-11 16:44:25 -08001975void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
1976 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001977 mData = data;
1978 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08001979 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001980}
1981
1982void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08001983 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001984 mData = NULL;
1985 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08001986 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001987}
1988
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001989}; // namespace android