blob: d3fe1589ae11123bceacdcb279e5b27d1a364be9 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
21#include <inttypes.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080026#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070029
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/Binder.h>
31#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080032#include <binder/IPCThreadState.h>
33#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080035#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070036#include <binder/TextOutput.h>
37
Mark Salyzynabed7f72016-01-27 08:02:48 -080038#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080040#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070041#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070043#include <utils/String8.h>
44#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045
Mathias Agopian208059f2009-05-18 15:08:03 -070046#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080047#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049#ifndef INT32_MAX
50#define INT32_MAX ((int32_t)(2147483647))
51#endif
52
53#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevichb6b14232015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
66 if (s > (SIZE_T_MAX - 3)) {
67 abort();
68 }
69 return PAD_SIZE_UNSAFE(s);
70}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080073#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborna4cff882014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Brown13b16042014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100{
101 switch (obj.type) {
102 case BINDER_TYPE_BINDER:
103 if (obj.binder) {
104 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700111 return;
112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
114 if (b != NULL) {
115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
120 case BINDER_TYPE_WEAK_HANDLE: {
121 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
122 if (b != NULL) b.get_refs()->incWeak(who);
123 return;
124 }
125 case BINDER_TYPE_FD: {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800126 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
127 struct stat st;
128 int ret = fstat(obj.handle, &st);
129 if (!ret && S_ISCHR(st.st_mode)) {
Adrian Roos6bb31142015-10-22 16:46:12 -0700130 // If we own an ashmem fd, keep track of how much memory it refers to.
131 int size = ashmem_get_size_region(obj.handle);
132 if (size > 0) {
133 *outAshmemSize += size;
134 }
Adrian Rooscbf37262015-10-22 16:12:53 -0700135 }
136 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 return;
138 }
139 }
140
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800141 ALOGD("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142}
143
Adrian Roos6bb31142015-10-22 16:46:12 -0700144void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 const flat_binder_object& obj, const void* who)
146{
Adrian Roos6bb31142015-10-22 16:46:12 -0700147 acquire_object(proc, obj, who, NULL);
148}
149
150static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700151 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152{
153 switch (obj.type) {
154 case BINDER_TYPE_BINDER:
155 if (obj.binder) {
156 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800157 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700158 }
159 return;
160 case BINDER_TYPE_WEAK_BINDER:
161 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800162 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700163 return;
164 case BINDER_TYPE_HANDLE: {
165 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
166 if (b != NULL) {
167 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
168 b->decStrong(who);
169 }
170 return;
171 }
172 case BINDER_TYPE_WEAK_HANDLE: {
173 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
174 if (b != NULL) b.get_refs()->decWeak(who);
175 return;
176 }
177 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800178 if (obj.cookie != 0) { // owned
179 if (outAshmemSize != NULL) {
Mark Salyzyneab2afc2016-01-27 08:02:48 -0800180 struct stat st;
181 int ret = fstat(obj.handle, &st);
182 if (!ret && S_ISCHR(st.st_mode)) {
183 int size = ashmem_get_size_region(obj.handle);
184 if (size > 0) {
185 *outAshmemSize -= size;
186 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700187 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700188 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800189
190 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700191 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 return;
193 }
194 }
195
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800196 ALOGE("Invalid object type 0x%08x", obj.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197}
198
Adrian Roos6bb31142015-10-22 16:46:12 -0700199void release_object(const sp<ProcessState>& proc,
200 const flat_binder_object& obj, const void* who)
201{
202 release_object(proc, obj, who, NULL);
203}
204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800206 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700207{
208 return out->writeObject(flat, false);
209}
210
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800211status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700212 const sp<IBinder>& binder, Parcel* out)
213{
214 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700216 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 if (binder != NULL) {
218 IBinder *local = binder->localBinder();
219 if (!local) {
220 BpBinder *proxy = binder->remoteBinder();
221 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
225 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
230 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
232 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
234 } else {
235 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = 0;
237 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 return finish_flatten_binder(binder, obj, out);
241}
242
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800243status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 const wp<IBinder>& binder, Parcel* out)
245{
246 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
249 if (binder != NULL) {
250 sp<IBinder> real = binder.promote();
251 if (real != NULL) {
252 IBinder *local = real->localBinder();
253 if (!local) {
254 BpBinder *proxy = real->remoteBinder();
255 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000256 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 const int32_t handle = proxy ? proxy->handle() : 0;
259 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800260 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 } else {
264 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
266 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 }
268 return finish_flatten_binder(real, obj, out);
269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 // XXX How to deal? In order to flatten the given binder,
272 // we need to probe it for information, which requires a primary
273 // reference... but we don't have one.
274 //
275 // The OpenBinder implementation uses a dynamic_cast<> here,
276 // but we can't do that with the different reference counting
277 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000278 ALOGE("Unable to unflatten Binder weak reference!");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 } else {
285 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800286 obj.binder = 0;
287 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700288 return finish_flatten_binder(NULL, obj, out);
289 }
290}
291
292inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800293 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
294 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295{
296 return NO_ERROR;
297}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299status_t unflatten_binder(const sp<ProcessState>& proc,
300 const Parcel& in, sp<IBinder>* out)
301{
302 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 if (flat) {
305 switch (flat->type) {
306 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800307 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700308 return finish_unflatten_binder(NULL, *flat, in);
309 case BINDER_TYPE_HANDLE:
310 *out = proc->getStrongProxyForHandle(flat->handle);
311 return finish_unflatten_binder(
312 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 }
315 return BAD_TYPE;
316}
317
318status_t unflatten_binder(const sp<ProcessState>& proc,
319 const Parcel& in, wp<IBinder>* out)
320{
321 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 if (flat) {
324 switch (flat->type) {
325 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800326 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700327 return finish_unflatten_binder(NULL, *flat, in);
328 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 reinterpret_cast<IBinder*>(flat->cookie),
332 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 } else {
334 *out = NULL;
335 }
336 return finish_unflatten_binder(NULL, *flat, in);
337 case BINDER_TYPE_HANDLE:
338 case BINDER_TYPE_WEAK_HANDLE:
339 *out = proc->getWeakProxyForHandle(flat->handle);
340 return finish_unflatten_binder(
341 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
342 }
343 }
344 return BAD_TYPE;
345}
346
347// ---------------------------------------------------------------------------
348
349Parcel::Parcel()
350{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800351 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 initState();
353}
354
355Parcel::~Parcel()
356{
357 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800358 LOG_ALLOC("Parcel %p: destroyed", this);
359}
360
361size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800362 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
363 size_t size = gParcelGlobalAllocSize;
364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
365 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800366}
367
368size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800369 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
370 size_t count = gParcelGlobalAllocCount;
371 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
372 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373}
374
375const uint8_t* Parcel::data() const
376{
377 return mData;
378}
379
380size_t Parcel::dataSize() const
381{
382 return (mDataSize > mDataPos ? mDataSize : mDataPos);
383}
384
385size_t Parcel::dataAvail() const
386{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700387 size_t result = dataSize() - dataPosition();
388 if (result > INT32_MAX) {
389 abort();
390 }
391 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392}
393
394size_t Parcel::dataPosition() const
395{
396 return mDataPos;
397}
398
399size_t Parcel::dataCapacity() const
400{
401 return mDataCapacity;
402}
403
404status_t Parcel::setDataSize(size_t size)
405{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700406 if (size > INT32_MAX) {
407 // don't accept size_t values which may have come from an
408 // inadvertent conversion from a negative int.
409 return BAD_VALUE;
410 }
411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 status_t err;
413 err = continueWrite(size);
414 if (err == NO_ERROR) {
415 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700416 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 }
418 return err;
419}
420
421void Parcel::setDataPosition(size_t pos) const
422{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700423 if (pos > INT32_MAX) {
424 // don't accept size_t values which may have come from an
425 // inadvertent conversion from a negative int.
426 abort();
427 }
428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 mDataPos = pos;
430 mNextObjectHint = 0;
431}
432
433status_t Parcel::setDataCapacity(size_t size)
434{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700435 if (size > INT32_MAX) {
436 // don't accept size_t values which may have come from an
437 // inadvertent conversion from a negative int.
438 return BAD_VALUE;
439 }
440
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700441 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 return NO_ERROR;
443}
444
445status_t Parcel::setData(const uint8_t* buffer, size_t len)
446{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700447 if (len > INT32_MAX) {
448 // don't accept size_t values which may have come from an
449 // inadvertent conversion from a negative int.
450 return BAD_VALUE;
451 }
452
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 status_t err = restartWrite(len);
454 if (err == NO_ERROR) {
455 memcpy(const_cast<uint8_t*>(data()), buffer, len);
456 mDataSize = len;
457 mFdsKnown = false;
458 }
459 return err;
460}
461
Andreas Huber51faf462011-04-13 10:21:56 -0700462status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463{
464 const sp<ProcessState> proc(ProcessState::self());
465 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700466 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800467 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 size_t size = parcel->mObjectsSize;
469 int startPos = mDataPos;
470 int firstIndex = -1, lastIndex = -2;
471
472 if (len == 0) {
473 return NO_ERROR;
474 }
475
Nick Kralevichb6b14232015-04-02 09:36:02 -0700476 if (len > INT32_MAX) {
477 // don't accept size_t values which may have come from an
478 // inadvertent conversion from a negative int.
479 return BAD_VALUE;
480 }
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 // range checks against the source parcel size
483 if ((offset > parcel->mDataSize)
484 || (len > parcel->mDataSize)
485 || (offset + len > parcel->mDataSize)) {
486 return BAD_VALUE;
487 }
488
489 // Count objects in range
490 for (int i = 0; i < (int) size; i++) {
491 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700492 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 if (firstIndex == -1) {
494 firstIndex = i;
495 }
496 lastIndex = i;
497 }
498 }
499 int numObjects = lastIndex - firstIndex + 1;
500
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700501 if ((mDataSize+len) > mDataCapacity) {
502 // grow data
503 err = growData(len);
504 if (err != NO_ERROR) {
505 return err;
506 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 }
508
509 // append data
510 memcpy(mData + mDataPos, data + offset, len);
511 mDataPos += len;
512 mDataSize += len;
513
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 err = NO_ERROR;
515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 if (numObjects > 0) {
517 // grow objects
518 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700519 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
520 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800521 binder_size_t *objects =
522 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
523 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524 return NO_MEMORY;
525 }
526 mObjects = objects;
527 mObjectsCapacity = newSize;
528 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 // append and acquire objects
531 int idx = mObjectsSize;
532 for (int i = firstIndex; i <= lastIndex; i++) {
533 size_t off = objects[i] - offset + startPos;
534 mObjects[idx++] = off;
535 mObjectsSize++;
536
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700537 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700538 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700539 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541 if (flat->type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700542 // If this is a file descriptor, we need to dup it so the
543 // new Parcel now owns its own fd, and can declare that we
544 // officially know we have fds.
545 flat->handle = dup(flat->handle);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800546 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700547 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400548 if (!mAllowFds) {
549 err = FDS_NOT_ALLOWED;
550 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700551 }
552 }
553 }
554
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400555 return err;
556}
557
Jeff Brown13b16042014-11-11 16:44:25 -0800558bool Parcel::allowFds() const
559{
560 return mAllowFds;
561}
562
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700563bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564{
565 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700566 if (!allowFds) {
567 mAllowFds = false;
568 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400569 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570}
571
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572void Parcel::restoreAllowFds(bool lastValue)
573{
574 mAllowFds = lastValue;
575}
576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700577bool Parcel::hasFileDescriptors() const
578{
579 if (!mFdsKnown) {
580 scanForFds();
581 }
582 return mHasFds;
583}
584
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700585// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586status_t Parcel::writeInterfaceToken(const String16& interface)
587{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700588 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
589 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590 // currently the interface identification token is just its name as a string
591 return writeString16(interface);
592}
593
Mathias Agopian83c04462009-05-22 19:00:22 -0700594bool Parcel::checkInterface(IBinder* binder) const
595{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700596 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700597}
598
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700599bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700600 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700601{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700602 int32_t strictPolicy = readInt32();
603 if (threadState == NULL) {
604 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700605 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700606 if ((threadState->getLastTransactionBinderFlags() &
607 IBinder::FLAG_ONEWAY) != 0) {
608 // For one-way calls, the callee is running entirely
609 // disconnected from the caller, so disable StrictMode entirely.
610 // Not only does disk/network usage not impact the caller, but
611 // there's no way to commuicate back any violations anyway.
612 threadState->setStrictModePolicy(0);
613 } else {
614 threadState->setStrictModePolicy(strictPolicy);
615 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700616 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700617 if (str == interface) {
618 return true;
619 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700620 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700621 String8(interface).string(), String8(str).string());
622 return false;
623 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700624}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700625
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800626const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700627{
628 return mObjects;
629}
630
631size_t Parcel::objectsCount() const
632{
633 return mObjectsSize;
634}
635
636status_t Parcel::errorCheck() const
637{
638 return mError;
639}
640
641void Parcel::setError(status_t err)
642{
643 mError = err;
644}
645
646status_t Parcel::finishWrite(size_t len)
647{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700648 if (len > INT32_MAX) {
649 // don't accept size_t values which may have come from an
650 // inadvertent conversion from a negative int.
651 return BAD_VALUE;
652 }
653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700654 //printf("Finish write of %d\n", len);
655 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700656 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700657 if (mDataPos > mDataSize) {
658 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700659 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700660 }
661 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
662 return NO_ERROR;
663}
664
665status_t Parcel::writeUnpadded(const void* data, size_t len)
666{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700667 if (len > INT32_MAX) {
668 // don't accept size_t values which may have come from an
669 // inadvertent conversion from a negative int.
670 return BAD_VALUE;
671 }
672
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 size_t end = mDataPos + len;
674 if (end < mDataPos) {
675 // integer overflow
676 return BAD_VALUE;
677 }
678
679 if (end <= mDataCapacity) {
680restart_write:
681 memcpy(mData+mDataPos, data, len);
682 return finishWrite(len);
683 }
684
685 status_t err = growData(len);
686 if (err == NO_ERROR) goto restart_write;
687 return err;
688}
689
690status_t Parcel::write(const void* data, size_t len)
691{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700692 if (len > INT32_MAX) {
693 // don't accept size_t values which may have come from an
694 // inadvertent conversion from a negative int.
695 return BAD_VALUE;
696 }
697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 void* const d = writeInplace(len);
699 if (d) {
700 memcpy(d, data, len);
701 return NO_ERROR;
702 }
703 return mError;
704}
705
706void* Parcel::writeInplace(size_t len)
707{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700708 if (len > INT32_MAX) {
709 // don't accept size_t values which may have come from an
710 // inadvertent conversion from a negative int.
711 return NULL;
712 }
713
714 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700715
716 // sanity check for integer overflow
717 if (mDataPos+padded < mDataPos) {
718 return NULL;
719 }
720
721 if ((mDataPos+padded) <= mDataCapacity) {
722restart_write:
723 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
724 uint8_t* const data = mData+mDataPos;
725
726 // Need to pad at end?
727 if (padded != len) {
728#if BYTE_ORDER == BIG_ENDIAN
729 static const uint32_t mask[4] = {
730 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
731 };
732#endif
733#if BYTE_ORDER == LITTLE_ENDIAN
734 static const uint32_t mask[4] = {
735 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
736 };
737#endif
738 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
739 // *reinterpret_cast<void**>(data+padded-4));
740 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
741 }
742
743 finishWrite(padded);
744 return data;
745 }
746
747 status_t err = growData(padded);
748 if (err == NO_ERROR) goto restart_write;
749 return NULL;
750}
751
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800752status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
753 const uint8_t* strData = (uint8_t*)str.data();
754 const size_t strLen= str.length();
755 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
756 if (utf16Len < 0 || utf16Len> std::numeric_limits<int32_t>::max()) {
757 return BAD_VALUE;
758 }
759
760 status_t err = writeInt32(utf16Len);
761 if (err) {
762 return err;
763 }
764
765 // Allocate enough bytes to hold our converted string and its terminating NULL.
766 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
767 if (!dst) {
768 return NO_MEMORY;
769 }
770
771 utf8_to_utf16(strData, strLen, (char16_t*)dst);
772
773 return NO_ERROR;
774}
775
776status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
777 if (!str) {
778 return writeInt32(-1);
779 }
780 return writeUtf8AsUtf16(*str);
781}
782
Casey Dahlinb9872622015-11-25 15:09:45 -0800783status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
784{
785 if (!val) {
786 return writeInt32(-1);
787 }
788
789 return writeByteVector(*val);
790}
791
Casey Dahlin451ff582015-10-19 18:12:18 -0700792status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
793{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700794 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700795 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700796 status = BAD_VALUE;
797 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700798 }
799
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700800 status = writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700801 if (status != OK) {
802 return status;
803 }
804
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700805 void* data = writeInplace(val.size());
806 if (!data) {
807 status = BAD_VALUE;
808 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700809 }
810
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700811 memcpy(data, val.data(), val.size());
812 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700813}
814
815status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
816{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800817 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700818}
819
Casey Dahlinb9872622015-11-25 15:09:45 -0800820status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
821{
822 return writeNullableTypedVector(val, &Parcel::writeInt32);
823}
824
Casey Dahlin451ff582015-10-19 18:12:18 -0700825status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
826{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800827 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700828}
829
Casey Dahlinb9872622015-11-25 15:09:45 -0800830status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
831{
832 return writeNullableTypedVector(val, &Parcel::writeInt64);
833}
834
Casey Dahlin451ff582015-10-19 18:12:18 -0700835status_t Parcel::writeFloatVector(const std::vector<float>& val)
836{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800837 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700838}
839
Casey Dahlinb9872622015-11-25 15:09:45 -0800840status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
841{
842 return writeNullableTypedVector(val, &Parcel::writeFloat);
843}
844
Casey Dahlin451ff582015-10-19 18:12:18 -0700845status_t Parcel::writeDoubleVector(const std::vector<double>& val)
846{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800847 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700848}
849
Casey Dahlinb9872622015-11-25 15:09:45 -0800850status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
851{
852 return writeNullableTypedVector(val, &Parcel::writeDouble);
853}
854
Casey Dahlin451ff582015-10-19 18:12:18 -0700855status_t Parcel::writeBoolVector(const std::vector<bool>& val)
856{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800857 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700858}
859
Casey Dahlinb9872622015-11-25 15:09:45 -0800860status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
861{
862 return writeNullableTypedVector(val, &Parcel::writeBool);
863}
864
Casey Dahlin451ff582015-10-19 18:12:18 -0700865status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
866{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800867 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700868}
869
Casey Dahlinb9872622015-11-25 15:09:45 -0800870status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
871{
872 return writeNullableTypedVector(val, &Parcel::writeChar);
873}
874
Casey Dahlin451ff582015-10-19 18:12:18 -0700875status_t Parcel::writeString16Vector(const std::vector<String16>& val)
876{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800877 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700878}
879
Casey Dahlinb9872622015-11-25 15:09:45 -0800880status_t Parcel::writeString16Vector(
881 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
882{
883 return writeNullableTypedVector(val, &Parcel::writeString16);
884}
885
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800886status_t Parcel::writeUtf8VectorAsUtf16Vector(
887 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
888 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
889}
890
891status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
892 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
893}
894
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700895status_t Parcel::writeInt32(int32_t val)
896{
Andreas Huber84a6d042009-08-17 13:33:27 -0700897 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700898}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800899
900status_t Parcel::writeUint32(uint32_t val)
901{
902 return writeAligned(val);
903}
904
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700905status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700906 if (len > INT32_MAX) {
907 // don't accept size_t values which may have come from an
908 // inadvertent conversion from a negative int.
909 return BAD_VALUE;
910 }
911
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700912 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700913 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700914 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700915 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700916 if (ret == NO_ERROR) {
917 ret = write(val, len * sizeof(*val));
918 }
919 return ret;
920}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700921status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700922 if (len > INT32_MAX) {
923 // don't accept size_t values which may have come from an
924 // inadvertent conversion from a negative int.
925 return BAD_VALUE;
926 }
927
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700928 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700929 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700930 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700931 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700932 if (ret == NO_ERROR) {
933 ret = write(val, len * sizeof(*val));
934 }
935 return ret;
936}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937
Casey Dahlind6848f52015-10-15 15:44:59 -0700938status_t Parcel::writeBool(bool val)
939{
940 return writeInt32(int32_t(val));
941}
942
943status_t Parcel::writeChar(char16_t val)
944{
945 return writeInt32(int32_t(val));
946}
947
948status_t Parcel::writeByte(int8_t val)
949{
950 return writeInt32(int32_t(val));
951}
952
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953status_t Parcel::writeInt64(int64_t val)
954{
Andreas Huber84a6d042009-08-17 13:33:27 -0700955 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956}
957
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700958status_t Parcel::writeUint64(uint64_t val)
959{
960 return writeAligned(val);
961}
962
Serban Constantinescuf683e012013-11-05 16:53:55 +0000963status_t Parcel::writePointer(uintptr_t val)
964{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800965 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +0000966}
967
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700968status_t Parcel::writeFloat(float val)
969{
Andreas Huber84a6d042009-08-17 13:33:27 -0700970 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971}
972
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800973#if defined(__mips__) && defined(__mips_hard_float)
974
975status_t Parcel::writeDouble(double val)
976{
977 union {
978 double d;
979 unsigned long long ll;
980 } u;
981 u.d = val;
982 return writeAligned(u.ll);
983}
984
985#else
986
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987status_t Parcel::writeDouble(double val)
988{
Andreas Huber84a6d042009-08-17 13:33:27 -0700989 return writeAligned(val);
990}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991
Douglas Leungcc1a4bb2013-01-11 15:00:55 -0800992#endif
993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994status_t Parcel::writeCString(const char* str)
995{
996 return write(str, strlen(str)+1);
997}
998
999status_t Parcel::writeString8(const String8& str)
1000{
1001 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001002 // only write string if its length is more than zero characters,
1003 // as readString8 will only read if the length field is non-zero.
1004 // this is slightly different from how writeString16 works.
1005 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006 err = write(str.string(), str.bytes()+1);
1007 }
1008 return err;
1009}
1010
Casey Dahlinb9872622015-11-25 15:09:45 -08001011status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1012{
1013 if (!str) {
1014 return writeInt32(-1);
1015 }
1016
1017 return writeString16(*str);
1018}
1019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001020status_t Parcel::writeString16(const String16& str)
1021{
1022 return writeString16(str.string(), str.size());
1023}
1024
1025status_t Parcel::writeString16(const char16_t* str, size_t len)
1026{
1027 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029 status_t err = writeInt32(len);
1030 if (err == NO_ERROR) {
1031 len *= sizeof(char16_t);
1032 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1033 if (data) {
1034 memcpy(data, str, len);
1035 *reinterpret_cast<char16_t*>(data+len) = 0;
1036 return NO_ERROR;
1037 }
1038 err = mError;
1039 }
1040 return err;
1041}
1042
1043status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1044{
1045 return flatten_binder(ProcessState::self(), val, this);
1046}
1047
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001048status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1049{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001050 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001051}
1052
Casey Dahlinb9872622015-11-25 15:09:45 -08001053status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1054{
1055 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1056}
1057
1058status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
1059 return readNullableTypedVector(val, &Parcel::readStrongBinder);
1060}
1061
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001062status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001063 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001064}
1065
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1067{
1068 return flatten_binder(ProcessState::self(), val, this);
1069}
1070
Casey Dahlinb9872622015-11-25 15:09:45 -08001071status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1072 if (!parcelable) {
1073 return writeInt32(0);
1074 }
1075
1076 return writeParcelable(*parcelable);
1077}
1078
Christopher Wiley97f048d2015-11-19 06:49:05 -08001079status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1080 status_t status = writeInt32(1); // parcelable is not null.
1081 if (status != OK) {
1082 return status;
1083 }
1084 return parcelable.writeToParcel(this);
1085}
1086
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001087status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001088{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001089 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001090 return BAD_TYPE;
1091
1092 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001093 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001094 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001096 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001097 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001099 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1100 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101
1102 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001103 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 return err;
1105 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001106 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001107 return err;
1108}
1109
Jeff Brown93ff1f92011-11-04 19:01:44 -07001110status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111{
1112 flat_binder_object obj;
1113 obj.type = BINDER_TYPE_FD;
1114 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001115 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001116 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001117 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001118 return writeObject(obj, true);
1119}
1120
1121status_t Parcel::writeDupFileDescriptor(int fd)
1122{
Jeff Brownd341c712011-11-04 20:19:33 -07001123 int dupFd = dup(fd);
1124 if (dupFd < 0) {
1125 return -errno;
1126 }
1127 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001128 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001129 close(dupFd);
1130 }
1131 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001132}
1133
Casey Dahlin06673e32015-11-23 13:24:23 -08001134status_t Parcel::writeUniqueFileDescriptor(const ScopedFd& fd) {
1135 return writeDupFileDescriptor(fd.get());
1136}
1137
1138status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<ScopedFd>& val) {
1139 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1140}
1141
Casey Dahlinb9872622015-11-25 15:09:45 -08001142status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<ScopedFd>>& val) {
1143 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1144}
1145
Jeff Brown13b16042014-11-11 16:44:25 -08001146status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001147{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001148 if (len > INT32_MAX) {
1149 // don't accept size_t values which may have come from an
1150 // inadvertent conversion from a negative int.
1151 return BAD_VALUE;
1152 }
1153
Jeff Brown13b16042014-11-11 16:44:25 -08001154 status_t status;
1155 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001156 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001157 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001158 if (status) return status;
1159
1160 void* ptr = writeInplace(len);
1161 if (!ptr) return NO_MEMORY;
1162
Jeff Brown13b16042014-11-11 16:44:25 -08001163 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001164 return NO_ERROR;
1165 }
1166
Steve Block6807e592011-10-20 11:56:00 +01001167 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001168 int fd = ashmem_create_region("Parcel Blob", len);
1169 if (fd < 0) return NO_MEMORY;
1170
1171 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1172 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001173 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001174 } else {
1175 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1176 if (ptr == MAP_FAILED) {
1177 status = -errno;
1178 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001179 if (!mutableCopy) {
1180 result = ashmem_set_prot_region(fd, PROT_READ);
1181 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001182 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001183 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001184 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001185 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001186 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001187 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001188 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001189 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001190 return NO_ERROR;
1191 }
1192 }
1193 }
1194 }
1195 ::munmap(ptr, len);
1196 }
1197 ::close(fd);
1198 return status;
1199}
1200
Jeff Brown13b16042014-11-11 16:44:25 -08001201status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1202{
1203 // Must match up with what's done in writeBlob.
1204 if (!mAllowFds) return FDS_NOT_ALLOWED;
1205 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1206 if (status) return status;
1207 return writeDupFileDescriptor(fd);
1208}
1209
Mathias Agopiane1424282013-07-29 21:24:40 -07001210status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001211{
1212 status_t err;
1213
1214 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001215 const size_t len = val.getFlattenedSize();
1216 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001217
Nick Kralevichb6b14232015-04-02 09:36:02 -07001218 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1219 // don't accept size_t values which may have come from an
1220 // inadvertent conversion from a negative int.
1221 return BAD_VALUE;
1222 }
1223
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001224 err = this->writeInt32(len);
1225 if (err) return err;
1226
1227 err = this->writeInt32(fd_count);
1228 if (err) return err;
1229
1230 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001231 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001232 if (buf == NULL)
1233 return BAD_VALUE;
1234
1235 int* fds = NULL;
1236 if (fd_count) {
1237 fds = new int[fd_count];
1238 }
1239
1240 err = val.flatten(buf, len, fds, fd_count);
1241 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1242 err = this->writeDupFileDescriptor( fds[i] );
1243 }
1244
1245 if (fd_count) {
1246 delete [] fds;
1247 }
1248
1249 return err;
1250}
1251
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001252status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1253{
1254 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1255 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1256 if (enoughData && enoughObjects) {
1257restart_write:
1258 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001259
Christopher Tate98e67d32015-06-03 18:44:15 -07001260 // remember if it's a file descriptor
1261 if (val.type == BINDER_TYPE_FD) {
1262 if (!mAllowFds) {
1263 // fail before modifying our object index
1264 return FDS_NOT_ALLOWED;
1265 }
1266 mHasFds = mFdsKnown = true;
1267 }
1268
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001269 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001270 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001271 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001272 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001273 mObjectsSize++;
1274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276 return finishWrite(sizeof(flat_binder_object));
1277 }
1278
1279 if (!enoughData) {
1280 const status_t err = growData(sizeof(val));
1281 if (err != NO_ERROR) return err;
1282 }
1283 if (!enoughObjects) {
1284 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateed7a50c2015-06-08 14:45:14 -07001285 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001286 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001287 if (objects == NULL) return NO_MEMORY;
1288 mObjects = objects;
1289 mObjectsCapacity = newSize;
1290 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001291
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001292 goto restart_write;
1293}
1294
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001295status_t Parcel::writeNoException()
1296{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001297 binder::Status status;
1298 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001299}
1300
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001301void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001302{
1303 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1304}
1305
1306status_t Parcel::read(void* outData, size_t len) const
1307{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001308 if (len > INT32_MAX) {
1309 // don't accept size_t values which may have come from an
1310 // inadvertent conversion from a negative int.
1311 return BAD_VALUE;
1312 }
1313
1314 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1315 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001316 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001317 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001318 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001319 return NO_ERROR;
1320 }
1321 return NOT_ENOUGH_DATA;
1322}
1323
1324const void* Parcel::readInplace(size_t len) const
1325{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001326 if (len > INT32_MAX) {
1327 // don't accept size_t values which may have come from an
1328 // inadvertent conversion from a negative int.
1329 return NULL;
1330 }
1331
1332 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1333 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001335 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001336 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 return data;
1338 }
1339 return NULL;
1340}
1341
Andreas Huber84a6d042009-08-17 13:33:27 -07001342template<class T>
1343status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001344 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001345
1346 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001348 mDataPos += sizeof(T);
1349 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350 return NO_ERROR;
1351 } else {
1352 return NOT_ENOUGH_DATA;
1353 }
1354}
1355
Andreas Huber84a6d042009-08-17 13:33:27 -07001356template<class T>
1357T Parcel::readAligned() const {
1358 T result;
1359 if (readAligned(&result) != NO_ERROR) {
1360 result = 0;
1361 }
1362
1363 return result;
1364}
1365
1366template<class T>
1367status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001368 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001369
1370 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1371restart_write:
1372 *reinterpret_cast<T*>(mData+mDataPos) = val;
1373 return finishWrite(sizeof(val));
1374 }
1375
1376 status_t err = growData(sizeof(val));
1377 if (err == NO_ERROR) goto restart_write;
1378 return err;
1379}
1380
Casey Dahlin451ff582015-10-19 18:12:18 -07001381status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1382 val->clear();
1383
1384 int32_t size;
1385 status_t status = readInt32(&size);
1386
1387 if (status != OK) {
1388 return status;
1389 }
1390
Christopher Wiley4db672d2015-11-10 09:44:30 -08001391 if (size < 0) {
1392 status = UNEXPECTED_NULL;
1393 return status;
1394 }
1395 if (size_t(size) > dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001396 status = BAD_VALUE;
1397 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001398 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001399
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001400 const void* data = readInplace(size);
1401 if (!data) {
1402 status = BAD_VALUE;
1403 return status;
1404 }
Casey Dahlin451ff582015-10-19 18:12:18 -07001405 val->resize(size);
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001406 memcpy(val->data(), data, size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001407
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001408 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001409}
1410
Casey Dahlinb9872622015-11-25 15:09:45 -08001411status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1412 const int32_t start = dataPosition();
1413 int32_t size;
1414 status_t status = readInt32(&size);
1415 val->reset();
1416
1417 if (status != OK || size < 0) {
1418 return status;
1419 }
1420
1421 setDataPosition(start);
1422 val->reset(new std::vector<int8_t>());
1423
1424 status = readByteVector(val->get());
1425
1426 if (status != OK) {
1427 val->reset();
1428 }
1429
1430 return status;
1431}
1432
1433status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1434 return readNullableTypedVector(val, &Parcel::readInt32);
1435}
1436
Casey Dahlin451ff582015-10-19 18:12:18 -07001437status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001438 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001439}
1440
Casey Dahlinb9872622015-11-25 15:09:45 -08001441status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1442 return readNullableTypedVector(val, &Parcel::readInt64);
1443}
1444
Casey Dahlin451ff582015-10-19 18:12:18 -07001445status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001446 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001447}
1448
Casey Dahlinb9872622015-11-25 15:09:45 -08001449status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1450 return readNullableTypedVector(val, &Parcel::readFloat);
1451}
1452
Casey Dahlin451ff582015-10-19 18:12:18 -07001453status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001454 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001455}
1456
Casey Dahlinb9872622015-11-25 15:09:45 -08001457status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1458 return readNullableTypedVector(val, &Parcel::readDouble);
1459}
1460
Casey Dahlin451ff582015-10-19 18:12:18 -07001461status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001462 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001463}
1464
Casey Dahlinb9872622015-11-25 15:09:45 -08001465status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1466 const int32_t start = dataPosition();
1467 int32_t size;
1468 status_t status = readInt32(&size);
1469 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001470
Casey Dahlinb9872622015-11-25 15:09:45 -08001471 if (status != OK || size < 0) {
1472 return status;
1473 }
1474
1475 setDataPosition(start);
1476 val->reset(new std::vector<bool>());
1477
1478 status = readBoolVector(val->get());
1479
1480 if (status != OK) {
1481 val->reset();
1482 }
1483
1484 return status;
1485}
1486
1487status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001488 int32_t size;
1489 status_t status = readInt32(&size);
1490
1491 if (status != OK) {
1492 return status;
1493 }
1494
1495 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001496 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001497 }
1498
1499 val->resize(size);
1500
1501 /* C++ bool handling means a vector of bools isn't necessarily addressable
1502 * (we might use individual bits)
1503 */
Christopher Wiley97887982015-10-27 16:33:47 -07001504 bool data;
1505 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001506 status = readBool(&data);
1507 (*val)[i] = data;
1508
1509 if (status != OK) {
1510 return status;
1511 }
1512 }
1513
1514 return OK;
1515}
1516
Casey Dahlinb9872622015-11-25 15:09:45 -08001517status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1518 return readNullableTypedVector(val, &Parcel::readChar);
1519}
1520
Casey Dahlin451ff582015-10-19 18:12:18 -07001521status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001522 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001523}
1524
Casey Dahlinb9872622015-11-25 15:09:45 -08001525status_t Parcel::readString16Vector(
1526 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1527 return readNullableTypedVector(val, &Parcel::readString16);
1528}
1529
Casey Dahlin451ff582015-10-19 18:12:18 -07001530status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001531 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001532}
1533
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001534status_t Parcel::readUtf8VectorFromUtf16Vector(
1535 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1536 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1537}
1538
1539status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1540 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1541}
Casey Dahlin451ff582015-10-19 18:12:18 -07001542
Andreas Huber84a6d042009-08-17 13:33:27 -07001543status_t Parcel::readInt32(int32_t *pArg) const
1544{
1545 return readAligned(pArg);
1546}
1547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548int32_t Parcel::readInt32() const
1549{
Andreas Huber84a6d042009-08-17 13:33:27 -07001550 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001551}
1552
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001553status_t Parcel::readUint32(uint32_t *pArg) const
1554{
1555 return readAligned(pArg);
1556}
1557
1558uint32_t Parcel::readUint32() const
1559{
1560 return readAligned<uint32_t>();
1561}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562
1563status_t Parcel::readInt64(int64_t *pArg) const
1564{
Andreas Huber84a6d042009-08-17 13:33:27 -07001565 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001566}
1567
1568
1569int64_t Parcel::readInt64() const
1570{
Andreas Huber84a6d042009-08-17 13:33:27 -07001571 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572}
1573
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001574status_t Parcel::readUint64(uint64_t *pArg) const
1575{
1576 return readAligned(pArg);
1577}
1578
1579uint64_t Parcel::readUint64() const
1580{
1581 return readAligned<uint64_t>();
1582}
1583
Serban Constantinescuf683e012013-11-05 16:53:55 +00001584status_t Parcel::readPointer(uintptr_t *pArg) const
1585{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001586 status_t ret;
1587 binder_uintptr_t ptr;
1588 ret = readAligned(&ptr);
1589 if (!ret)
1590 *pArg = ptr;
1591 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001592}
1593
1594uintptr_t Parcel::readPointer() const
1595{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001596 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001597}
1598
1599
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001600status_t Parcel::readFloat(float *pArg) const
1601{
Andreas Huber84a6d042009-08-17 13:33:27 -07001602 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603}
1604
1605
1606float Parcel::readFloat() const
1607{
Andreas Huber84a6d042009-08-17 13:33:27 -07001608 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001609}
1610
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001611#if defined(__mips__) && defined(__mips_hard_float)
1612
1613status_t Parcel::readDouble(double *pArg) const
1614{
1615 union {
1616 double d;
1617 unsigned long long ll;
1618 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001619 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001620 status_t status;
1621 status = readAligned(&u.ll);
1622 *pArg = u.d;
1623 return status;
1624}
1625
1626double Parcel::readDouble() const
1627{
1628 union {
1629 double d;
1630 unsigned long long ll;
1631 } u;
1632 u.ll = readAligned<unsigned long long>();
1633 return u.d;
1634}
1635
1636#else
1637
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001638status_t Parcel::readDouble(double *pArg) const
1639{
Andreas Huber84a6d042009-08-17 13:33:27 -07001640 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001641}
1642
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001643double Parcel::readDouble() const
1644{
Andreas Huber84a6d042009-08-17 13:33:27 -07001645 return readAligned<double>();
1646}
1647
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001648#endif
1649
Andreas Huber84a6d042009-08-17 13:33:27 -07001650status_t Parcel::readIntPtr(intptr_t *pArg) const
1651{
1652 return readAligned(pArg);
1653}
1654
1655
1656intptr_t Parcel::readIntPtr() const
1657{
1658 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001659}
1660
Casey Dahlind6848f52015-10-15 15:44:59 -07001661status_t Parcel::readBool(bool *pArg) const
1662{
1663 int32_t tmp;
1664 status_t ret = readInt32(&tmp);
1665 *pArg = (tmp != 0);
1666 return ret;
1667}
1668
1669bool Parcel::readBool() const
1670{
1671 return readInt32() != 0;
1672}
1673
1674status_t Parcel::readChar(char16_t *pArg) const
1675{
1676 int32_t tmp;
1677 status_t ret = readInt32(&tmp);
1678 *pArg = char16_t(tmp);
1679 return ret;
1680}
1681
1682char16_t Parcel::readChar() const
1683{
1684 return char16_t(readInt32());
1685}
1686
1687status_t Parcel::readByte(int8_t *pArg) const
1688{
1689 int32_t tmp;
1690 status_t ret = readInt32(&tmp);
1691 *pArg = int8_t(tmp);
1692 return ret;
1693}
1694
1695int8_t Parcel::readByte() const
1696{
1697 return int8_t(readInt32());
1698}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001699
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001700status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1701 size_t utf16Size = 0;
1702 const char16_t* src = readString16Inplace(&utf16Size);
1703 if (!src) {
1704 return UNEXPECTED_NULL;
1705 }
1706
1707 // Save ourselves the trouble, we're done.
1708 if (utf16Size == 0u) {
1709 str->clear();
1710 return NO_ERROR;
1711 }
1712
1713 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size);
1714 if (utf8Size < 0) {
1715 return BAD_VALUE;
1716 }
1717 // Note that while it is probably safe to assume string::resize keeps a
1718 // spare byte around for the trailing null, we're going to be explicit.
1719 str->resize(utf8Size + 1);
1720 utf16_to_utf8(src, utf16Size, &((*str)[0]));
1721 str->resize(utf8Size);
1722 return NO_ERROR;
1723}
1724
1725status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1726 const int32_t start = dataPosition();
1727 int32_t size;
1728 status_t status = readInt32(&size);
1729 str->reset();
1730
1731 if (status != OK || size < 0) {
1732 return status;
1733 }
1734
1735 setDataPosition(start);
1736 str->reset(new std::string());
1737 return readUtf8FromUtf16(str->get());
1738}
1739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740const char* Parcel::readCString() const
1741{
1742 const size_t avail = mDataSize-mDataPos;
1743 if (avail > 0) {
1744 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1745 // is the string's trailing NUL within the parcel's valid bounds?
1746 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1747 if (eos) {
1748 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001749 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001750 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751 return str;
1752 }
1753 }
1754 return NULL;
1755}
1756
1757String8 Parcel::readString8() const
1758{
1759 int32_t size = readInt32();
1760 // watch for potential int overflow adding 1 for trailing NUL
1761 if (size > 0 && size < INT32_MAX) {
1762 const char* str = (const char*)readInplace(size+1);
1763 if (str) return String8(str, size);
1764 }
1765 return String8();
1766}
1767
1768String16 Parcel::readString16() const
1769{
1770 size_t len;
1771 const char16_t* str = readString16Inplace(&len);
1772 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001773 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001774 return String16();
1775}
1776
Casey Dahlinb9872622015-11-25 15:09:45 -08001777status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1778{
1779 const int32_t start = dataPosition();
1780 int32_t size;
1781 status_t status = readInt32(&size);
1782 pArg->reset();
1783
1784 if (status != OK || size < 0) {
1785 return status;
1786 }
1787
1788 setDataPosition(start);
1789 pArg->reset(new String16());
1790
1791 status = readString16(pArg->get());
1792
1793 if (status != OK) {
1794 pArg->reset();
1795 }
1796
1797 return status;
1798}
1799
Casey Dahlin451ff582015-10-19 18:12:18 -07001800status_t Parcel::readString16(String16* pArg) const
1801{
1802 size_t len;
1803 const char16_t* str = readString16Inplace(&len);
1804 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001805 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001806 return 0;
1807 } else {
1808 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001809 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001810 }
1811}
1812
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1814{
1815 int32_t size = readInt32();
1816 // watch for potential int overflow from size+1
1817 if (size >= 0 && size < INT32_MAX) {
1818 *outLen = size;
1819 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1820 if (str != NULL) {
1821 return str;
1822 }
1823 }
1824 *outLen = 0;
1825 return NULL;
1826}
1827
Casey Dahlinf0c13772015-10-27 18:33:56 -07001828status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1829{
1830 return unflatten_binder(ProcessState::self(), *this, val);
1831}
1832
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001833sp<IBinder> Parcel::readStrongBinder() const
1834{
1835 sp<IBinder> val;
Casey Dahlinf0c13772015-10-27 18:33:56 -07001836 readStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001837 return val;
1838}
1839
1840wp<IBinder> Parcel::readWeakBinder() const
1841{
1842 wp<IBinder> val;
1843 unflatten_binder(ProcessState::self(), *this, &val);
1844 return val;
1845}
1846
Christopher Wiley97f048d2015-11-19 06:49:05 -08001847status_t Parcel::readParcelable(Parcelable* parcelable) const {
1848 int32_t have_parcelable = 0;
1849 status_t status = readInt32(&have_parcelable);
1850 if (status != OK) {
1851 return status;
1852 }
1853 if (!have_parcelable) {
1854 return UNEXPECTED_NULL;
1855 }
1856 return parcelable->readFromParcel(this);
1857}
1858
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001859int32_t Parcel::readExceptionCode() const
1860{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001861 binder::Status status;
1862 status.readFromParcel(*this);
1863 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001864}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001865
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001866native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001867{
1868 int numFds, numInts;
1869 status_t err;
1870 err = readInt32(&numFds);
1871 if (err != NO_ERROR) return 0;
1872 err = readInt32(&numInts);
1873 if (err != NO_ERROR) return 0;
1874
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001875 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001876 if (!h) {
1877 return 0;
1878 }
1879
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001880 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Rebecca Schultz Zavin360211f2009-02-13 16:34:38 -08001881 h->data[i] = dup(readFileDescriptor());
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001882 if (h->data[i] < 0) err = BAD_VALUE;
1883 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001884 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001885 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001886 native_handle_close(h);
1887 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001888 h = 0;
1889 }
1890 return h;
1891}
1892
1893
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001894int Parcel::readFileDescriptor() const
1895{
1896 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001897
1898 if (flat && flat->type == BINDER_TYPE_FD) {
1899 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902 return BAD_TYPE;
1903}
1904
Casey Dahlin06673e32015-11-23 13:24:23 -08001905status_t Parcel::readUniqueFileDescriptor(ScopedFd* val) const
1906{
1907 int got = readFileDescriptor();
1908
1909 if (got == BAD_TYPE) {
1910 return BAD_TYPE;
1911 }
1912
1913 val->reset(dup(got));
1914
1915 if (val->get() < 0) {
1916 return BAD_VALUE;
1917 }
1918
1919 return OK;
1920}
1921
1922
Casey Dahlinb9872622015-11-25 15:09:45 -08001923status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<ScopedFd>>* val) const {
1924 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
1925}
1926
Casey Dahlin06673e32015-11-23 13:24:23 -08001927status_t Parcel::readUniqueFileDescriptorVector(std::vector<ScopedFd>* val) const {
1928 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
1929}
1930
Jeff Brown5707dbf2011-09-23 21:17:56 -07001931status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1932{
Jeff Brown13b16042014-11-11 16:44:25 -08001933 int32_t blobType;
1934 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001935 if (status) return status;
1936
Jeff Brown13b16042014-11-11 16:44:25 -08001937 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001938 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001939 const void* ptr = readInplace(len);
1940 if (!ptr) return BAD_VALUE;
1941
Jeff Brown13b16042014-11-11 16:44:25 -08001942 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001943 return NO_ERROR;
1944 }
1945
Steve Block6807e592011-10-20 11:56:00 +01001946 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001947 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001948 int fd = readFileDescriptor();
1949 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1950
Jeff Brown13b16042014-11-11 16:44:25 -08001951 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1952 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001953 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001954
Jeff Brown13b16042014-11-11 16:44:25 -08001955 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001956 return NO_ERROR;
1957}
1958
Mathias Agopiane1424282013-07-29 21:24:40 -07001959status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001960{
1961 // size
1962 const size_t len = this->readInt32();
1963 const size_t fd_count = this->readInt32();
1964
Nick Kralevichb6b14232015-04-02 09:36:02 -07001965 if (len > INT32_MAX) {
1966 // don't accept size_t values which may have come from an
1967 // inadvertent conversion from a negative int.
1968 return BAD_VALUE;
1969 }
1970
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001971 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001972 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001973 if (buf == NULL)
1974 return BAD_VALUE;
1975
1976 int* fds = NULL;
1977 if (fd_count) {
1978 fds = new int[fd_count];
1979 }
1980
1981 status_t err = NO_ERROR;
1982 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hallfee99042014-11-04 08:36:31 -08001983 fds[i] = dup(this->readFileDescriptor());
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001984 if (fds[i] < 0) {
1985 err = BAD_VALUE;
Jesse Hallfee99042014-11-04 08:36:31 -08001986 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1987 i, fds[i], fd_count, strerror(errno));
Jun Jiangabf8a2c2014-04-29 14:22:10 +08001988 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001989 }
1990
1991 if (err == NO_ERROR) {
1992 err = val.unflatten(buf, len, fds, fd_count);
1993 }
1994
1995 if (fd_count) {
1996 delete [] fds;
1997 }
1998
1999 return err;
2000}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002001const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2002{
2003 const size_t DPOS = mDataPos;
2004 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2005 const flat_binder_object* obj
2006 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2007 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002008 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002009 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002010 // the object list, so we don't want to check for it when
2011 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002012 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002013 return obj;
2014 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002017 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018 const size_t N = mObjectsSize;
2019 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002022 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025 // Start at the current hint position, looking for an object at
2026 // the current data position.
2027 if (opos < N) {
2028 while (opos < (N-1) && OBJS[opos] < DPOS) {
2029 opos++;
2030 }
2031 } else {
2032 opos = N-1;
2033 }
2034 if (OBJS[opos] == DPOS) {
2035 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002036 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037 this, DPOS, opos);
2038 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002039 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002040 return obj;
2041 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043 // Look backwards for it...
2044 while (opos > 0 && OBJS[opos] > DPOS) {
2045 opos--;
2046 }
2047 if (OBJS[opos] == DPOS) {
2048 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002049 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002050 this, DPOS, opos);
2051 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002052 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002053 return obj;
2054 }
2055 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002056 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 -07002057 this, DPOS);
2058 }
2059 return NULL;
2060}
2061
2062void Parcel::closeFileDescriptors()
2063{
2064 size_t i = mObjectsSize;
2065 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 }
2068 while (i > 0) {
2069 i--;
2070 const flat_binder_object* flat
2071 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2072 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002073 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002074 close(flat->handle);
2075 }
2076 }
2077}
2078
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002079uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002081 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002082}
2083
2084size_t Parcel::ipcDataSize() const
2085{
2086 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2087}
2088
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002089uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002091 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092}
2093
2094size_t Parcel::ipcObjectsCount() const
2095{
2096 return mObjectsSize;
2097}
2098
2099void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002100 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002101{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002102 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103 freeDataNoInit();
2104 mError = NO_ERROR;
2105 mData = const_cast<uint8_t*>(data);
2106 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002107 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002109 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002110 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 mObjectsSize = mObjectsCapacity = objectsCount;
2112 mNextObjectHint = 0;
2113 mOwner = relFunc;
2114 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002115 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002116 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002117 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002118 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002119 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002120 mObjectsSize = 0;
2121 break;
2122 }
2123 minOffset = offset + sizeof(flat_binder_object);
2124 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002125 scanForFds();
2126}
2127
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002128void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129{
2130 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002131
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132 if (errorCheck() != NO_ERROR) {
2133 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002134 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 } else if (dataSize() > 0) {
2136 const uint8_t* DATA = data();
2137 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002138 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 const size_t N = objectsCount();
2140 for (size_t i=0; i<N; i++) {
2141 const flat_binder_object* flat
2142 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2143 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2144 << TypeCode(flat->type & 0x7f7f7f00)
2145 << " = " << flat->binder;
2146 }
2147 } else {
2148 to << "NULL";
2149 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002151 to << ")";
2152}
2153
2154void Parcel::releaseObjects()
2155{
2156 const sp<ProcessState> proc(ProcessState::self());
2157 size_t i = mObjectsSize;
2158 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002159 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002160 while (i > 0) {
2161 i--;
2162 const flat_binder_object* flat
2163 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002164 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 }
2166}
2167
2168void Parcel::acquireObjects()
2169{
2170 const sp<ProcessState> proc(ProcessState::self());
2171 size_t i = mObjectsSize;
2172 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002173 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 while (i > 0) {
2175 i--;
2176 const flat_binder_object* flat
2177 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002178 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179 }
2180}
2181
2182void Parcel::freeData()
2183{
2184 freeDataNoInit();
2185 initState();
2186}
2187
2188void Parcel::freeDataNoInit()
2189{
2190 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002191 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002192 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2194 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002195 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002197 if (mData) {
2198 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002199 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002200 if (mDataCapacity <= gParcelGlobalAllocSize) {
2201 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2202 } else {
2203 gParcelGlobalAllocSize = 0;
2204 }
2205 if (gParcelGlobalAllocCount > 0) {
2206 gParcelGlobalAllocCount--;
2207 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002208 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002209 free(mData);
2210 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211 if (mObjects) free(mObjects);
2212 }
2213}
2214
2215status_t Parcel::growData(size_t len)
2216{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002217 if (len > INT32_MAX) {
2218 // don't accept size_t values which may have come from an
2219 // inadvertent conversion from a negative int.
2220 return BAD_VALUE;
2221 }
2222
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 size_t newSize = ((mDataSize+len)*3)/2;
2224 return (newSize <= mDataSize)
2225 ? (status_t) NO_MEMORY
2226 : continueWrite(newSize);
2227}
2228
2229status_t Parcel::restartWrite(size_t desired)
2230{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002231 if (desired > INT32_MAX) {
2232 // don't accept size_t values which may have come from an
2233 // inadvertent conversion from a negative int.
2234 return BAD_VALUE;
2235 }
2236
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 if (mOwner) {
2238 freeData();
2239 return continueWrite(desired);
2240 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002241
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 uint8_t* data = (uint8_t*)realloc(mData, desired);
2243 if (!data && desired > mDataCapacity) {
2244 mError = NO_MEMORY;
2245 return NO_MEMORY;
2246 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002249
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002251 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002252 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002253 gParcelGlobalAllocSize += desired;
2254 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002255 if (!mData) {
2256 gParcelGlobalAllocCount++;
2257 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002258 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002259 mData = data;
2260 mDataCapacity = desired;
2261 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002262
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002263 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002264 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2265 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2266
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 free(mObjects);
2268 mObjects = NULL;
2269 mObjectsSize = mObjectsCapacity = 0;
2270 mNextObjectHint = 0;
2271 mHasFds = false;
2272 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002273 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002274
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002275 return NO_ERROR;
2276}
2277
2278status_t Parcel::continueWrite(size_t desired)
2279{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002280 if (desired > INT32_MAX) {
2281 // don't accept size_t values which may have come from an
2282 // inadvertent conversion from a negative int.
2283 return BAD_VALUE;
2284 }
2285
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286 // If shrinking, first adjust for any objects that appear
2287 // after the new data size.
2288 size_t objectsSize = mObjectsSize;
2289 if (desired < mDataSize) {
2290 if (desired == 0) {
2291 objectsSize = 0;
2292 } else {
2293 while (objectsSize > 0) {
2294 if (mObjects[objectsSize-1] < desired)
2295 break;
2296 objectsSize--;
2297 }
2298 }
2299 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 if (mOwner) {
2302 // If the size is going to zero, just release the owner's data.
2303 if (desired == 0) {
2304 freeData();
2305 return NO_ERROR;
2306 }
2307
2308 // If there is a different owner, we need to take
2309 // posession.
2310 uint8_t* data = (uint8_t*)malloc(desired);
2311 if (!data) {
2312 mError = NO_MEMORY;
2313 return NO_MEMORY;
2314 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002315 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002317 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002318 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002320 free(data);
2321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 mError = NO_MEMORY;
2323 return NO_MEMORY;
2324 }
2325
2326 // Little hack to only acquire references on objects
2327 // we will be keeping.
2328 size_t oldObjectsSize = mObjectsSize;
2329 mObjectsSize = objectsSize;
2330 acquireObjects();
2331 mObjectsSize = oldObjectsSize;
2332 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 if (mData) {
2335 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2336 }
2337 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002338 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002339 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002340 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2342 mOwner = NULL;
2343
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002344 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002345 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002346 gParcelGlobalAllocSize += desired;
2347 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002348 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002349
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 mData = data;
2351 mObjects = objects;
2352 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002353 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002354 mDataCapacity = desired;
2355 mObjectsSize = mObjectsCapacity = objectsSize;
2356 mNextObjectHint = 0;
2357
2358 } else if (mData) {
2359 if (objectsSize < mObjectsSize) {
2360 // Need to release refs on any objects we are dropping.
2361 const sp<ProcessState> proc(ProcessState::self());
2362 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2363 const flat_binder_object* flat
2364 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2365 if (flat->type == BINDER_TYPE_FD) {
2366 // will need to rescan because we may have lopped off the only FDs
2367 mFdsKnown = false;
2368 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002369 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002371 binder_size_t* objects =
2372 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 if (objects) {
2374 mObjects = objects;
2375 }
2376 mObjectsSize = objectsSize;
2377 mNextObjectHint = 0;
2378 }
2379
2380 // We own the data, so we can just do a realloc().
2381 if (desired > mDataCapacity) {
2382 uint8_t* data = (uint8_t*)realloc(mData, desired);
2383 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002384 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2385 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002386 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002387 gParcelGlobalAllocSize += desired;
2388 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002389 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 mData = data;
2391 mDataCapacity = desired;
2392 } else if (desired > mDataCapacity) {
2393 mError = NO_MEMORY;
2394 return NO_MEMORY;
2395 }
2396 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002397 if (mDataSize > desired) {
2398 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002400 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 if (mDataPos > desired) {
2402 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002403 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002404 }
2405 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 } else {
2408 // This is the first data. Easy!
2409 uint8_t* data = (uint8_t*)malloc(desired);
2410 if (!data) {
2411 mError = NO_MEMORY;
2412 return NO_MEMORY;
2413 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002415 if(!(mDataCapacity == 0 && mObjects == NULL
2416 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002417 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002419
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002420 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002421 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002422 gParcelGlobalAllocSize += desired;
2423 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002424 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426 mData = data;
2427 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002428 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2429 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 mDataCapacity = desired;
2431 }
2432
2433 return NO_ERROR;
2434}
2435
2436void Parcel::initState()
2437{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002438 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 mError = NO_ERROR;
2440 mData = 0;
2441 mDataSize = 0;
2442 mDataCapacity = 0;
2443 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002444 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2445 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446 mObjects = NULL;
2447 mObjectsSize = 0;
2448 mObjectsCapacity = 0;
2449 mNextObjectHint = 0;
2450 mHasFds = false;
2451 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002452 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002454 mOpenAshmemSize = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455}
2456
2457void Parcel::scanForFds() const
2458{
2459 bool hasFds = false;
2460 for (size_t i=0; i<mObjectsSize; i++) {
2461 const flat_binder_object* flat
2462 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2463 if (flat->type == BINDER_TYPE_FD) {
2464 hasFds = true;
2465 break;
2466 }
2467 }
2468 mHasFds = hasFds;
2469 mFdsKnown = true;
2470}
2471
Dan Sandleraa5c2342015-04-10 10:08:45 -04002472size_t Parcel::getBlobAshmemSize() const
2473{
Adrian Roos6bb31142015-10-22 16:46:12 -07002474 // This used to return the size of all blobs that were written to ashmem, now we're returning
2475 // the ashmem currently referenced by this Parcel, which should be equivalent.
2476 // TODO: Remove method once ABI can be changed.
2477 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002478}
2479
Adrian Rooscbf37262015-10-22 16:12:53 -07002480size_t Parcel::getOpenAshmemSize() const
2481{
2482 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002483}
2484
2485// --- Parcel::Blob ---
2486
2487Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002488 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002489}
2490
2491Parcel::Blob::~Blob() {
2492 release();
2493}
2494
2495void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002496 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002497 ::munmap(mData, mSize);
2498 }
2499 clear();
2500}
2501
Jeff Brown13b16042014-11-11 16:44:25 -08002502void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2503 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002504 mData = data;
2505 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002506 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002507}
2508
2509void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002510 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002511 mData = NULL;
2512 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002513 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002514}
2515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516}; // namespace android