blob: e221c6d6ff80c573c8002278e8464b7bbd9d8f30 [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>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080023#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/mman.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080028#include <sys/stat.h>
29#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070030#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080031#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070032
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Binder.h>
34#include <binder/BpBinder.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080035#include <binder/IPCThreadState.h>
36#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070037#include <binder/ProcessState.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080038#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070039#include <binder/TextOutput.h>
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -080040#include <binder/Value.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041
Mark Salyzynabed7f72016-01-27 08:02:48 -080042#include <cutils/ashmem.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070043#include <utils/Debug.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070045#include <utils/Log.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String8.h>
48#include <utils/String16.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Mathias Agopian208059f2009-05-18 15:08:03 -070050#include <private/binder/binder_module.h>
Dianne Hackborn7e790af2014-11-11 12:22:53 -080051#include <private/binder/Static.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070052
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070053#ifndef INT32_MAX
54#define INT32_MAX ((int32_t)(2147483647))
55#endif
56
57#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
67#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
69static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey0c1f5cb2014-12-18 10:26:57 -080077#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700107 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
120 if (b != NULL) {
121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
128 if (b != NULL) b.get_refs()->incWeak(who);
129 return;
130 }
131 case BINDER_TYPE_FD: {
Mark Salyzyn80589362016-08-23 16:15:04 -0700132 if ((obj.cookie != 0) && (outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700143 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Adrian Roos6bb31142015-10-22 16:46:12 -0700149 acquire_object(proc, obj, who, NULL);
150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700155 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
168 if (b != NULL) {
169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
176 if (b != NULL) b.get_refs()->decWeak(who);
177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Mark Salyzyn80589362016-08-23 16:15:04 -0700181 if ((outAshmemSize != NULL) && ashmem_valid(obj.handle)) {
182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
200 release_object(proc, obj, who, NULL);
201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Martijn Coenen2b631742017-05-05 11:16:59 -0700214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 } else {
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 }
221
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700222 if (binder != NULL) {
223 IBinder *local = binder->localBinder();
224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
226 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700235 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
237 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
239 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.binder = 0;
242 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 return finish_flatten_binder(binder, obj, out);
246}
247
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800248status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 const wp<IBinder>& binder, Parcel* out)
250{
251 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
254 if (binder != NULL) {
255 sp<IBinder> real = binder.promote();
256 if (real != NULL) {
257 IBinder *local = real->localBinder();
258 if (!local) {
259 BpBinder *proxy = real->remoteBinder();
260 if (proxy == NULL) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000261 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800265 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700269 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
271 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 }
273 return finish_flatten_binder(real, obj, out);
274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 // XXX How to deal? In order to flatten the given binder,
277 // we need to probe it for information, which requires a primary
278 // reference... but we don't have one.
279 //
280 // The OpenBinder implementation uses a dynamic_cast<> here,
281 // but we can't do that with the different reference counting
282 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000283 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700284 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800285 obj.binder = 0;
286 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 return finish_flatten_binder(NULL, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700290 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 obj.binder = 0;
292 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 return finish_flatten_binder(NULL, obj, out);
294 }
295}
296
297inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800298 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
299 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300{
301 return NO_ERROR;
302}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304status_t unflatten_binder(const sp<ProcessState>& proc,
305 const Parcel& in, sp<IBinder>* out)
306{
307 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700310 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800312 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 return finish_unflatten_binder(NULL, *flat, in);
314 case BINDER_TYPE_HANDLE:
315 *out = proc->getStrongProxyForHandle(flat->handle);
316 return finish_unflatten_binder(
317 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700318 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 }
320 return BAD_TYPE;
321}
322
323status_t unflatten_binder(const sp<ProcessState>& proc,
324 const Parcel& in, wp<IBinder>* out)
325{
326 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700329 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 *out = reinterpret_cast<IBinder*>(flat->cookie);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700332 return finish_unflatten_binder(NULL, *flat, in);
333 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800336 reinterpret_cast<IBinder*>(flat->cookie),
337 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 } else {
339 *out = NULL;
340 }
341 return finish_unflatten_binder(NULL, *flat, in);
342 case BINDER_TYPE_HANDLE:
343 case BINDER_TYPE_WEAK_HANDLE:
344 *out = proc->getWeakProxyForHandle(flat->handle);
345 return finish_unflatten_binder(
346 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
347 }
348 }
349 return BAD_TYPE;
350}
351
352// ---------------------------------------------------------------------------
353
354Parcel::Parcel()
355{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700357 initState();
358}
359
360Parcel::~Parcel()
361{
362 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363 LOG_ALLOC("Parcel %p: destroyed", this);
364}
365
366size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t size = gParcelGlobalAllocSize;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
370 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800371}
372
373size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800374 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
375 size_t count = gParcelGlobalAllocCount;
376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
377 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378}
379
380const uint8_t* Parcel::data() const
381{
382 return mData;
383}
384
385size_t Parcel::dataSize() const
386{
387 return (mDataSize > mDataPos ? mDataSize : mDataPos);
388}
389
390size_t Parcel::dataAvail() const
391{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700392 size_t result = dataSize() - dataPosition();
393 if (result > INT32_MAX) {
394 abort();
395 }
396 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397}
398
399size_t Parcel::dataPosition() const
400{
401 return mDataPos;
402}
403
404size_t Parcel::dataCapacity() const
405{
406 return mDataCapacity;
407}
408
409status_t Parcel::setDataSize(size_t size)
410{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700411 if (size > INT32_MAX) {
412 // don't accept size_t values which may have come from an
413 // inadvertent conversion from a negative int.
414 return BAD_VALUE;
415 }
416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 status_t err;
418 err = continueWrite(size);
419 if (err == NO_ERROR) {
420 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700421 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423 return err;
424}
425
426void Parcel::setDataPosition(size_t pos) const
427{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700428 if (pos > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
431 abort();
432 }
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 mDataPos = pos;
435 mNextObjectHint = 0;
436}
437
438status_t Parcel::setDataCapacity(size_t size)
439{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700440 if (size > INT32_MAX) {
441 // don't accept size_t values which may have come from an
442 // inadvertent conversion from a negative int.
443 return BAD_VALUE;
444 }
445
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700446 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700447 return NO_ERROR;
448}
449
450status_t Parcel::setData(const uint8_t* buffer, size_t len)
451{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700452 if (len > INT32_MAX) {
453 // don't accept size_t values which may have come from an
454 // inadvertent conversion from a negative int.
455 return BAD_VALUE;
456 }
457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700458 status_t err = restartWrite(len);
459 if (err == NO_ERROR) {
460 memcpy(const_cast<uint8_t*>(data()), buffer, len);
461 mDataSize = len;
462 mFdsKnown = false;
463 }
464 return err;
465}
466
Andreas Huber51faf462011-04-13 10:21:56 -0700467status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468{
469 const sp<ProcessState> proc(ProcessState::self());
470 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700471 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800472 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 size_t size = parcel->mObjectsSize;
474 int startPos = mDataPos;
475 int firstIndex = -1, lastIndex = -2;
476
477 if (len == 0) {
478 return NO_ERROR;
479 }
480
Nick Kralevichb6b14232015-04-02 09:36:02 -0700481 if (len > INT32_MAX) {
482 // don't accept size_t values which may have come from an
483 // inadvertent conversion from a negative int.
484 return BAD_VALUE;
485 }
486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 // range checks against the source parcel size
488 if ((offset > parcel->mDataSize)
489 || (len > parcel->mDataSize)
490 || (offset + len > parcel->mDataSize)) {
491 return BAD_VALUE;
492 }
493
494 // Count objects in range
495 for (int i = 0; i < (int) size; i++) {
496 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700497 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 if (firstIndex == -1) {
499 firstIndex = i;
500 }
501 lastIndex = i;
502 }
503 }
504 int numObjects = lastIndex - firstIndex + 1;
505
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700506 if ((mDataSize+len) > mDataCapacity) {
507 // grow data
508 err = growData(len);
509 if (err != NO_ERROR) {
510 return err;
511 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700512 }
513
514 // append data
515 memcpy(mData + mDataPos, data + offset, len);
516 mDataPos += len;
517 mDataSize += len;
518
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400519 err = NO_ERROR;
520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521 if (numObjects > 0) {
522 // grow objects
523 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700524 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700525 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800526 binder_size_t *objects =
527 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
528 if (objects == (binder_size_t*)0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700529 return NO_MEMORY;
530 }
531 mObjects = objects;
532 mObjectsCapacity = newSize;
533 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535 // append and acquire objects
536 int idx = mObjectsSize;
537 for (int i = firstIndex; i <= lastIndex; i++) {
538 size_t off = objects[i] - offset + startPos;
539 mObjects[idx++] = off;
540 mObjectsSize++;
541
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700542 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700544 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700546 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700547 // If this is a file descriptor, we need to dup it so the
548 // new Parcel now owns its own fd, and can declare that we
549 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800550 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800551 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400553 if (!mAllowFds) {
554 err = FDS_NOT_ALLOWED;
555 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700556 }
557 }
558 }
559
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400560 return err;
561}
562
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700563int Parcel::compareData(const Parcel& other) {
564 size_t size = dataSize();
565 if (size != other.dataSize()) {
566 return size < other.dataSize() ? -1 : 1;
567 }
568 return memcmp(data(), other.data(), size);
569}
570
Jeff Brown13b16042014-11-11 16:44:25 -0800571bool Parcel::allowFds() const
572{
573 return mAllowFds;
574}
575
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700576bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400577{
578 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700579 if (!allowFds) {
580 mAllowFds = false;
581 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400582 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700583}
584
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700585void Parcel::restoreAllowFds(bool lastValue)
586{
587 mAllowFds = lastValue;
588}
589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700590bool Parcel::hasFileDescriptors() const
591{
592 if (!mFdsKnown) {
593 scanForFds();
594 }
595 return mHasFds;
596}
597
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700598// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599status_t Parcel::writeInterfaceToken(const String16& interface)
600{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700601 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
602 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700603 // currently the interface identification token is just its name as a string
604 return writeString16(interface);
605}
606
Mathias Agopian83c04462009-05-22 19:00:22 -0700607bool Parcel::checkInterface(IBinder* binder) const
608{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700609 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700610}
611
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700612bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700613 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700615 int32_t strictPolicy = readInt32();
616 if (threadState == NULL) {
617 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700618 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700619 if ((threadState->getLastTransactionBinderFlags() &
620 IBinder::FLAG_ONEWAY) != 0) {
621 // For one-way calls, the callee is running entirely
622 // disconnected from the caller, so disable StrictMode entirely.
623 // Not only does disk/network usage not impact the caller, but
624 // there's no way to commuicate back any violations anyway.
625 threadState->setStrictModePolicy(0);
626 } else {
627 threadState->setStrictModePolicy(strictPolicy);
628 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700629 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630 if (str == interface) {
631 return true;
632 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700633 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634 String8(interface).string(), String8(str).string());
635 return false;
636 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700637}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800639const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700640{
641 return mObjects;
642}
643
644size_t Parcel::objectsCount() const
645{
646 return mObjectsSize;
647}
648
649status_t Parcel::errorCheck() const
650{
651 return mError;
652}
653
654void Parcel::setError(status_t err)
655{
656 mError = err;
657}
658
659status_t Parcel::finishWrite(size_t len)
660{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700661 if (len > INT32_MAX) {
662 // don't accept size_t values which may have come from an
663 // inadvertent conversion from a negative int.
664 return BAD_VALUE;
665 }
666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700667 //printf("Finish write of %d\n", len);
668 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700669 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700670 if (mDataPos > mDataSize) {
671 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700672 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700673 }
674 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
675 return NO_ERROR;
676}
677
678status_t Parcel::writeUnpadded(const void* data, size_t len)
679{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700680 if (len > INT32_MAX) {
681 // don't accept size_t values which may have come from an
682 // inadvertent conversion from a negative int.
683 return BAD_VALUE;
684 }
685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700686 size_t end = mDataPos + len;
687 if (end < mDataPos) {
688 // integer overflow
689 return BAD_VALUE;
690 }
691
692 if (end <= mDataCapacity) {
693restart_write:
694 memcpy(mData+mDataPos, data, len);
695 return finishWrite(len);
696 }
697
698 status_t err = growData(len);
699 if (err == NO_ERROR) goto restart_write;
700 return err;
701}
702
703status_t Parcel::write(const void* data, size_t len)
704{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700705 if (len > INT32_MAX) {
706 // don't accept size_t values which may have come from an
707 // inadvertent conversion from a negative int.
708 return BAD_VALUE;
709 }
710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711 void* const d = writeInplace(len);
712 if (d) {
713 memcpy(d, data, len);
714 return NO_ERROR;
715 }
716 return mError;
717}
718
719void* Parcel::writeInplace(size_t len)
720{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700721 if (len > INT32_MAX) {
722 // don't accept size_t values which may have come from an
723 // inadvertent conversion from a negative int.
724 return NULL;
725 }
726
727 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728
729 // sanity check for integer overflow
730 if (mDataPos+padded < mDataPos) {
731 return NULL;
732 }
733
734 if ((mDataPos+padded) <= mDataCapacity) {
735restart_write:
736 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
737 uint8_t* const data = mData+mDataPos;
738
739 // Need to pad at end?
740 if (padded != len) {
741#if BYTE_ORDER == BIG_ENDIAN
742 static const uint32_t mask[4] = {
743 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
744 };
745#endif
746#if BYTE_ORDER == LITTLE_ENDIAN
747 static const uint32_t mask[4] = {
748 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
749 };
750#endif
751 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
752 // *reinterpret_cast<void**>(data+padded-4));
753 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
754 }
755
756 finishWrite(padded);
757 return data;
758 }
759
760 status_t err = growData(padded);
761 if (err == NO_ERROR) goto restart_write;
762 return NULL;
763}
764
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800765status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
766 const uint8_t* strData = (uint8_t*)str.data();
767 const size_t strLen= str.length();
768 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100769 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800770 return BAD_VALUE;
771 }
772
773 status_t err = writeInt32(utf16Len);
774 if (err) {
775 return err;
776 }
777
778 // Allocate enough bytes to hold our converted string and its terminating NULL.
779 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
780 if (!dst) {
781 return NO_MEMORY;
782 }
783
Sergio Girof4607432016-07-21 14:46:35 +0100784 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800785
786 return NO_ERROR;
787}
788
789status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
790 if (!str) {
791 return writeInt32(-1);
792 }
793 return writeUtf8AsUtf16(*str);
794}
795
Casey Dahlin185d3442016-02-09 11:08:35 -0800796namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800797
Casey Dahlin185d3442016-02-09 11:08:35 -0800798template<typename T>
799status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700800{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700801 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700802 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700803 status = BAD_VALUE;
804 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700805 }
806
Casey Dahlin185d3442016-02-09 11:08:35 -0800807 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700808 if (status != OK) {
809 return status;
810 }
811
Casey Dahlin185d3442016-02-09 11:08:35 -0800812 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700813 if (!data) {
814 status = BAD_VALUE;
815 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700816 }
817
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700818 memcpy(data, val.data(), val.size());
819 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700820}
821
Casey Dahlin185d3442016-02-09 11:08:35 -0800822template<typename T>
823status_t writeByteVectorInternalPtr(Parcel* parcel,
824 const std::unique_ptr<std::vector<T>>& val)
825{
826 if (!val) {
827 return parcel->writeInt32(-1);
828 }
829
830 return writeByteVectorInternal(parcel, *val);
831}
832
833} // namespace
834
835status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
836 return writeByteVectorInternal(this, val);
837}
838
839status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
840{
841 return writeByteVectorInternalPtr(this, val);
842}
843
844status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
845 return writeByteVectorInternal(this, val);
846}
847
848status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
849{
850 return writeByteVectorInternalPtr(this, val);
851}
852
Casey Dahlin451ff582015-10-19 18:12:18 -0700853status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
854{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800855 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700856}
857
Casey Dahlinb9872622015-11-25 15:09:45 -0800858status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
859{
860 return writeNullableTypedVector(val, &Parcel::writeInt32);
861}
862
Casey Dahlin451ff582015-10-19 18:12:18 -0700863status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
864{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800865 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700866}
867
Casey Dahlinb9872622015-11-25 15:09:45 -0800868status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
869{
870 return writeNullableTypedVector(val, &Parcel::writeInt64);
871}
872
Casey Dahlin451ff582015-10-19 18:12:18 -0700873status_t Parcel::writeFloatVector(const std::vector<float>& val)
874{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800875 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700876}
877
Casey Dahlinb9872622015-11-25 15:09:45 -0800878status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
879{
880 return writeNullableTypedVector(val, &Parcel::writeFloat);
881}
882
Casey Dahlin451ff582015-10-19 18:12:18 -0700883status_t Parcel::writeDoubleVector(const std::vector<double>& val)
884{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800885 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700886}
887
Casey Dahlinb9872622015-11-25 15:09:45 -0800888status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
889{
890 return writeNullableTypedVector(val, &Parcel::writeDouble);
891}
892
Casey Dahlin451ff582015-10-19 18:12:18 -0700893status_t Parcel::writeBoolVector(const std::vector<bool>& val)
894{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800895 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700896}
897
Casey Dahlinb9872622015-11-25 15:09:45 -0800898status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
899{
900 return writeNullableTypedVector(val, &Parcel::writeBool);
901}
902
Casey Dahlin451ff582015-10-19 18:12:18 -0700903status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
904{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800905 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700906}
907
Casey Dahlinb9872622015-11-25 15:09:45 -0800908status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
909{
910 return writeNullableTypedVector(val, &Parcel::writeChar);
911}
912
Casey Dahlin451ff582015-10-19 18:12:18 -0700913status_t Parcel::writeString16Vector(const std::vector<String16>& val)
914{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800915 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700916}
917
Casey Dahlinb9872622015-11-25 15:09:45 -0800918status_t Parcel::writeString16Vector(
919 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
920{
921 return writeNullableTypedVector(val, &Parcel::writeString16);
922}
923
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800924status_t Parcel::writeUtf8VectorAsUtf16Vector(
925 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
926 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
927}
928
929status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
930 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
931}
932
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700933status_t Parcel::writeInt32(int32_t val)
934{
Andreas Huber84a6d042009-08-17 13:33:27 -0700935 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700936}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800937
938status_t Parcel::writeUint32(uint32_t val)
939{
940 return writeAligned(val);
941}
942
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700943status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700944 if (len > INT32_MAX) {
945 // don't accept size_t values which may have come from an
946 // inadvertent conversion from a negative int.
947 return BAD_VALUE;
948 }
949
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700950 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700951 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700952 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700953 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700954 if (ret == NO_ERROR) {
955 ret = write(val, len * sizeof(*val));
956 }
957 return ret;
958}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700959status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700960 if (len > INT32_MAX) {
961 // don't accept size_t values which may have come from an
962 // inadvertent conversion from a negative int.
963 return BAD_VALUE;
964 }
965
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700966 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700967 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700968 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700969 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700970 if (ret == NO_ERROR) {
971 ret = write(val, len * sizeof(*val));
972 }
973 return ret;
974}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975
Casey Dahlind6848f52015-10-15 15:44:59 -0700976status_t Parcel::writeBool(bool val)
977{
978 return writeInt32(int32_t(val));
979}
980
981status_t Parcel::writeChar(char16_t val)
982{
983 return writeInt32(int32_t(val));
984}
985
986status_t Parcel::writeByte(int8_t val)
987{
988 return writeInt32(int32_t(val));
989}
990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991status_t Parcel::writeInt64(int64_t val)
992{
Andreas Huber84a6d042009-08-17 13:33:27 -0700993 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700994}
995
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700996status_t Parcel::writeUint64(uint64_t val)
997{
998 return writeAligned(val);
999}
1000
Serban Constantinescuf683e012013-11-05 16:53:55 +00001001status_t Parcel::writePointer(uintptr_t val)
1002{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001003 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001004}
1005
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001006status_t Parcel::writeFloat(float val)
1007{
Andreas Huber84a6d042009-08-17 13:33:27 -07001008 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001009}
1010
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001011#if defined(__mips__) && defined(__mips_hard_float)
1012
1013status_t Parcel::writeDouble(double val)
1014{
1015 union {
1016 double d;
1017 unsigned long long ll;
1018 } u;
1019 u.d = val;
1020 return writeAligned(u.ll);
1021}
1022
1023#else
1024
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025status_t Parcel::writeDouble(double val)
1026{
Andreas Huber84a6d042009-08-17 13:33:27 -07001027 return writeAligned(val);
1028}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001029
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001030#endif
1031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032status_t Parcel::writeCString(const char* str)
1033{
1034 return write(str, strlen(str)+1);
1035}
1036
1037status_t Parcel::writeString8(const String8& str)
1038{
1039 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001040 // only write string if its length is more than zero characters,
1041 // as readString8 will only read if the length field is non-zero.
1042 // this is slightly different from how writeString16 works.
1043 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001044 err = write(str.string(), str.bytes()+1);
1045 }
1046 return err;
1047}
1048
Casey Dahlinb9872622015-11-25 15:09:45 -08001049status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1050{
1051 if (!str) {
1052 return writeInt32(-1);
1053 }
1054
1055 return writeString16(*str);
1056}
1057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058status_t Parcel::writeString16(const String16& str)
1059{
1060 return writeString16(str.string(), str.size());
1061}
1062
1063status_t Parcel::writeString16(const char16_t* str, size_t len)
1064{
1065 if (str == NULL) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001066
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001067 status_t err = writeInt32(len);
1068 if (err == NO_ERROR) {
1069 len *= sizeof(char16_t);
1070 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1071 if (data) {
1072 memcpy(data, str, len);
1073 *reinterpret_cast<char16_t*>(data+len) = 0;
1074 return NO_ERROR;
1075 }
1076 err = mError;
1077 }
1078 return err;
1079}
1080
1081status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1082{
1083 return flatten_binder(ProcessState::self(), val, this);
1084}
1085
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001086status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1087{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001088 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001089}
1090
Casey Dahlinb9872622015-11-25 15:09:45 -08001091status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1092{
1093 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1094}
1095
1096status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001097 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001098}
1099
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001100status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001101 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001102}
1103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1105{
1106 return flatten_binder(ProcessState::self(), val, this);
1107}
1108
Casey Dahlinb9872622015-11-25 15:09:45 -08001109status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1110 if (!parcelable) {
1111 return writeInt32(0);
1112 }
1113
1114 return writeParcelable(*parcelable);
1115}
1116
Christopher Wiley97f048d2015-11-19 06:49:05 -08001117status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1118 status_t status = writeInt32(1); // parcelable is not null.
1119 if (status != OK) {
1120 return status;
1121 }
1122 return parcelable.writeToParcel(this);
1123}
1124
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001125status_t Parcel::writeValue(const binder::Value& value) {
1126 return value.writeToParcel(this);
1127}
1128
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001129status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001130{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001131 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001132 return BAD_TYPE;
1133
1134 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001135 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001136 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001138 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001139 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001141 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1142 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143
1144 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001145 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 return err;
1147 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149 return err;
1150}
1151
Jeff Brown93ff1f92011-11-04 19:01:44 -07001152status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001153{
1154 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001155 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001157 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001158 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001159 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 return writeObject(obj, true);
1161}
1162
1163status_t Parcel::writeDupFileDescriptor(int fd)
1164{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001165 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001166 if (dupFd < 0) {
1167 return -errno;
1168 }
1169 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001170 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001171 close(dupFd);
1172 }
1173 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174}
1175
Dianne Hackborn1941a402016-08-29 12:30:43 -07001176status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1177{
1178 writeInt32(0);
1179 return writeFileDescriptor(fd, takeOwnership);
1180}
1181
Ryo Hashimotobf551892018-05-31 16:58:35 +09001182status_t Parcel::writeDupParcelFileDescriptor(int fd)
1183{
1184 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1185 if (dupFd < 0) {
1186 return -errno;
1187 }
1188 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1189 if (err != OK) {
1190 close(dupFd);
1191 }
1192 return err;
1193}
1194
Christopher Wiley2cf19952016-04-11 11:09:37 -07001195status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001196 return writeDupFileDescriptor(fd.get());
1197}
1198
Christopher Wiley2cf19952016-04-11 11:09:37 -07001199status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001200 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1201}
1202
Christopher Wiley2cf19952016-04-11 11:09:37 -07001203status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001204 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1205}
1206
Jeff Brown13b16042014-11-11 16:44:25 -08001207status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001208{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001209 if (len > INT32_MAX) {
1210 // don't accept size_t values which may have come from an
1211 // inadvertent conversion from a negative int.
1212 return BAD_VALUE;
1213 }
1214
Jeff Brown13b16042014-11-11 16:44:25 -08001215 status_t status;
1216 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001217 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001218 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001219 if (status) return status;
1220
1221 void* ptr = writeInplace(len);
1222 if (!ptr) return NO_MEMORY;
1223
Jeff Brown13b16042014-11-11 16:44:25 -08001224 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001225 return NO_ERROR;
1226 }
1227
Steve Block6807e592011-10-20 11:56:00 +01001228 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 int fd = ashmem_create_region("Parcel Blob", len);
1230 if (fd < 0) return NO_MEMORY;
1231
1232 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1233 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001234 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235 } else {
1236 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1237 if (ptr == MAP_FAILED) {
1238 status = -errno;
1239 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001240 if (!mutableCopy) {
1241 result = ashmem_set_prot_region(fd, PROT_READ);
1242 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001244 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001246 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001248 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001249 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001250 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001251 return NO_ERROR;
1252 }
1253 }
1254 }
1255 }
1256 ::munmap(ptr, len);
1257 }
1258 ::close(fd);
1259 return status;
1260}
1261
Jeff Brown13b16042014-11-11 16:44:25 -08001262status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1263{
1264 // Must match up with what's done in writeBlob.
1265 if (!mAllowFds) return FDS_NOT_ALLOWED;
1266 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1267 if (status) return status;
1268 return writeDupFileDescriptor(fd);
1269}
1270
Mathias Agopiane1424282013-07-29 21:24:40 -07001271status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272{
1273 status_t err;
1274
1275 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001276 const size_t len = val.getFlattenedSize();
1277 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001278
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001279 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001280 // don't accept size_t values which may have come from an
1281 // inadvertent conversion from a negative int.
1282 return BAD_VALUE;
1283 }
1284
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001285 err = this->writeInt32(len);
1286 if (err) return err;
1287
1288 err = this->writeInt32(fd_count);
1289 if (err) return err;
1290
1291 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001292 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001293 if (buf == NULL)
1294 return BAD_VALUE;
1295
1296 int* fds = NULL;
1297 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001298 fds = new (std::nothrow) int[fd_count];
1299 if (fds == nullptr) {
1300 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1301 return BAD_VALUE;
1302 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001303 }
1304
1305 err = val.flatten(buf, len, fds, fd_count);
1306 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1307 err = this->writeDupFileDescriptor( fds[i] );
1308 }
1309
1310 if (fd_count) {
1311 delete [] fds;
1312 }
1313
1314 return err;
1315}
1316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1318{
1319 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1320 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1321 if (enoughData && enoughObjects) {
1322restart_write:
1323 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001324
Christopher Tate98e67d32015-06-03 18:44:15 -07001325 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001326 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001327 if (!mAllowFds) {
1328 // fail before modifying our object index
1329 return FDS_NOT_ALLOWED;
1330 }
1331 mHasFds = mFdsKnown = true;
1332 }
1333
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001335 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001336 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001337 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001338 mObjectsSize++;
1339 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001340
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341 return finishWrite(sizeof(flat_binder_object));
1342 }
1343
1344 if (!enoughData) {
1345 const status_t err = growData(sizeof(val));
1346 if (err != NO_ERROR) return err;
1347 }
1348 if (!enoughObjects) {
1349 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001350 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001351 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001352 if (objects == NULL) return NO_MEMORY;
1353 mObjects = objects;
1354 mObjectsCapacity = newSize;
1355 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001357 goto restart_write;
1358}
1359
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001360status_t Parcel::writeNoException()
1361{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001362 binder::Status status;
1363 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001364}
1365
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001366status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1367{
1368 using ::std::map;
1369 using ::android::binder::Value;
1370 using ::android::binder::Map;
1371
1372 Map::const_iterator iter;
1373 status_t ret;
1374
1375 ret = writeInt32(map_in.size());
1376
1377 if (ret != NO_ERROR) {
1378 return ret;
1379 }
1380
1381 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1382 ret = writeValue(Value(iter->first));
1383 if (ret != NO_ERROR) {
1384 return ret;
1385 }
1386
1387 ret = writeValue(iter->second);
1388 if (ret != NO_ERROR) {
1389 return ret;
1390 }
1391 }
1392
1393 return ret;
1394}
1395
1396status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1397{
1398 if (map == NULL) {
1399 return writeInt32(-1);
1400 }
1401
1402 return writeMap(*map.get());
1403}
1404
1405status_t Parcel::readMap(::android::binder::Map* map_out)const
1406{
1407 using ::std::map;
1408 using ::android::String16;
1409 using ::android::String8;
1410 using ::android::binder::Value;
1411 using ::android::binder::Map;
1412
1413 status_t ret = NO_ERROR;
1414 int32_t count;
1415
1416 ret = readInt32(&count);
1417 if (ret != NO_ERROR) {
1418 return ret;
1419 }
1420
1421 if (count < 0) {
1422 ALOGE("readMap: Unexpected count: %d", count);
1423 return (count == -1)
1424 ? UNEXPECTED_NULL
1425 : BAD_VALUE;
1426 }
1427
1428 map_out->clear();
1429
1430 while (count--) {
1431 Map::key_type key;
1432 Value value;
1433
1434 ret = readValue(&value);
1435 if (ret != NO_ERROR) {
1436 return ret;
1437 }
1438
1439 if (!value.getString(&key)) {
1440 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1441 return BAD_VALUE;
1442 }
1443
1444 ret = readValue(&value);
1445 if (ret != NO_ERROR) {
1446 return ret;
1447 }
1448
1449 (*map_out)[key] = value;
1450 }
1451
1452 return ret;
1453}
1454
1455status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1456{
1457 const size_t start = dataPosition();
1458 int32_t count;
1459 status_t status = readInt32(&count);
1460 map->reset();
1461
1462 if (status != OK || count == -1) {
1463 return status;
1464 }
1465
1466 setDataPosition(start);
1467 map->reset(new binder::Map());
1468
1469 status = readMap(map->get());
1470
1471 if (status != OK) {
1472 map->reset();
1473 }
1474
1475 return status;
1476}
1477
1478
1479
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001480void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481{
1482 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1483}
1484
1485status_t Parcel::read(void* outData, size_t len) const
1486{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001487 if (len > INT32_MAX) {
1488 // don't accept size_t values which may have come from an
1489 // inadvertent conversion from a negative int.
1490 return BAD_VALUE;
1491 }
1492
1493 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1494 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001495 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001496 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001497 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 return NO_ERROR;
1499 }
1500 return NOT_ENOUGH_DATA;
1501}
1502
1503const void* Parcel::readInplace(size_t len) const
1504{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001505 if (len > INT32_MAX) {
1506 // don't accept size_t values which may have come from an
1507 // inadvertent conversion from a negative int.
1508 return NULL;
1509 }
1510
1511 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1512 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001514 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001515 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 return data;
1517 }
1518 return NULL;
1519}
1520
Andreas Huber84a6d042009-08-17 13:33:27 -07001521template<class T>
1522status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001523 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001524
1525 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001527 mDataPos += sizeof(T);
1528 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001529 return NO_ERROR;
1530 } else {
1531 return NOT_ENOUGH_DATA;
1532 }
1533}
1534
Andreas Huber84a6d042009-08-17 13:33:27 -07001535template<class T>
1536T Parcel::readAligned() const {
1537 T result;
1538 if (readAligned(&result) != NO_ERROR) {
1539 result = 0;
1540 }
1541
1542 return result;
1543}
1544
1545template<class T>
1546status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001547 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001548
1549 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1550restart_write:
1551 *reinterpret_cast<T*>(mData+mDataPos) = val;
1552 return finishWrite(sizeof(val));
1553 }
1554
1555 status_t err = growData(sizeof(val));
1556 if (err == NO_ERROR) goto restart_write;
1557 return err;
1558}
1559
Casey Dahlin185d3442016-02-09 11:08:35 -08001560namespace {
1561
1562template<typename T>
1563status_t readByteVectorInternal(const Parcel* parcel,
1564 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001565 val->clear();
1566
1567 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001568 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001569
1570 if (status != OK) {
1571 return status;
1572 }
1573
Christopher Wiley4db672d2015-11-10 09:44:30 -08001574 if (size < 0) {
1575 status = UNEXPECTED_NULL;
1576 return status;
1577 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001578 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001579 status = BAD_VALUE;
1580 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001581 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001582
Paul Lietar433e87b2016-09-16 10:39:32 -07001583 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001584 if (!data) {
1585 status = BAD_VALUE;
1586 return status;
1587 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001588 val->reserve(size);
1589 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001590
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001591 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001592}
1593
Casey Dahlin185d3442016-02-09 11:08:35 -08001594template<typename T>
1595status_t readByteVectorInternalPtr(
1596 const Parcel* parcel,
1597 std::unique_ptr<std::vector<T>>* val) {
1598 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001599 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001600 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001601 val->reset();
1602
1603 if (status != OK || size < 0) {
1604 return status;
1605 }
1606
Casey Dahlin185d3442016-02-09 11:08:35 -08001607 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001608 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001609
Casey Dahlin185d3442016-02-09 11:08:35 -08001610 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001611
1612 if (status != OK) {
1613 val->reset();
1614 }
1615
1616 return status;
1617}
1618
Casey Dahlin185d3442016-02-09 11:08:35 -08001619} // namespace
1620
1621status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1622 return readByteVectorInternal(this, val);
1623}
1624
1625status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1626 return readByteVectorInternal(this, val);
1627}
1628
1629status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1630 return readByteVectorInternalPtr(this, val);
1631}
1632
1633status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1634 return readByteVectorInternalPtr(this, val);
1635}
1636
Casey Dahlinb9872622015-11-25 15:09:45 -08001637status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1638 return readNullableTypedVector(val, &Parcel::readInt32);
1639}
1640
Casey Dahlin451ff582015-10-19 18:12:18 -07001641status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001642 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001643}
1644
Casey Dahlinb9872622015-11-25 15:09:45 -08001645status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1646 return readNullableTypedVector(val, &Parcel::readInt64);
1647}
1648
Casey Dahlin451ff582015-10-19 18:12:18 -07001649status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001650 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001651}
1652
Casey Dahlinb9872622015-11-25 15:09:45 -08001653status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1654 return readNullableTypedVector(val, &Parcel::readFloat);
1655}
1656
Casey Dahlin451ff582015-10-19 18:12:18 -07001657status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001658 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001659}
1660
Casey Dahlinb9872622015-11-25 15:09:45 -08001661status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1662 return readNullableTypedVector(val, &Parcel::readDouble);
1663}
1664
Casey Dahlin451ff582015-10-19 18:12:18 -07001665status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001666 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001667}
1668
Casey Dahlinb9872622015-11-25 15:09:45 -08001669status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1670 const int32_t start = dataPosition();
1671 int32_t size;
1672 status_t status = readInt32(&size);
1673 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001674
Casey Dahlinb9872622015-11-25 15:09:45 -08001675 if (status != OK || size < 0) {
1676 return status;
1677 }
1678
1679 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001680 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001681
1682 status = readBoolVector(val->get());
1683
1684 if (status != OK) {
1685 val->reset();
1686 }
1687
1688 return status;
1689}
1690
1691status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001692 int32_t size;
1693 status_t status = readInt32(&size);
1694
1695 if (status != OK) {
1696 return status;
1697 }
1698
1699 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001700 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001701 }
1702
1703 val->resize(size);
1704
1705 /* C++ bool handling means a vector of bools isn't necessarily addressable
1706 * (we might use individual bits)
1707 */
Christopher Wiley97887982015-10-27 16:33:47 -07001708 bool data;
1709 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001710 status = readBool(&data);
1711 (*val)[i] = data;
1712
1713 if (status != OK) {
1714 return status;
1715 }
1716 }
1717
1718 return OK;
1719}
1720
Casey Dahlinb9872622015-11-25 15:09:45 -08001721status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1722 return readNullableTypedVector(val, &Parcel::readChar);
1723}
1724
Casey Dahlin451ff582015-10-19 18:12:18 -07001725status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001726 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001727}
1728
Casey Dahlinb9872622015-11-25 15:09:45 -08001729status_t Parcel::readString16Vector(
1730 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1731 return readNullableTypedVector(val, &Parcel::readString16);
1732}
1733
Casey Dahlin451ff582015-10-19 18:12:18 -07001734status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001735 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001736}
1737
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001738status_t Parcel::readUtf8VectorFromUtf16Vector(
1739 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1740 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1741}
1742
1743status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1744 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1745}
Casey Dahlin451ff582015-10-19 18:12:18 -07001746
Andreas Huber84a6d042009-08-17 13:33:27 -07001747status_t Parcel::readInt32(int32_t *pArg) const
1748{
1749 return readAligned(pArg);
1750}
1751
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001752int32_t Parcel::readInt32() const
1753{
Andreas Huber84a6d042009-08-17 13:33:27 -07001754 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755}
1756
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001757status_t Parcel::readUint32(uint32_t *pArg) const
1758{
1759 return readAligned(pArg);
1760}
1761
1762uint32_t Parcel::readUint32() const
1763{
1764 return readAligned<uint32_t>();
1765}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766
1767status_t Parcel::readInt64(int64_t *pArg) const
1768{
Andreas Huber84a6d042009-08-17 13:33:27 -07001769 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001770}
1771
1772
1773int64_t Parcel::readInt64() const
1774{
Andreas Huber84a6d042009-08-17 13:33:27 -07001775 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001776}
1777
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001778status_t Parcel::readUint64(uint64_t *pArg) const
1779{
1780 return readAligned(pArg);
1781}
1782
1783uint64_t Parcel::readUint64() const
1784{
1785 return readAligned<uint64_t>();
1786}
1787
Serban Constantinescuf683e012013-11-05 16:53:55 +00001788status_t Parcel::readPointer(uintptr_t *pArg) const
1789{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001790 status_t ret;
1791 binder_uintptr_t ptr;
1792 ret = readAligned(&ptr);
1793 if (!ret)
1794 *pArg = ptr;
1795 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001796}
1797
1798uintptr_t Parcel::readPointer() const
1799{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001800 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001801}
1802
1803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804status_t Parcel::readFloat(float *pArg) const
1805{
Andreas Huber84a6d042009-08-17 13:33:27 -07001806 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001807}
1808
1809
1810float Parcel::readFloat() const
1811{
Andreas Huber84a6d042009-08-17 13:33:27 -07001812 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001813}
1814
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001815#if defined(__mips__) && defined(__mips_hard_float)
1816
1817status_t Parcel::readDouble(double *pArg) const
1818{
1819 union {
1820 double d;
1821 unsigned long long ll;
1822 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001823 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001824 status_t status;
1825 status = readAligned(&u.ll);
1826 *pArg = u.d;
1827 return status;
1828}
1829
1830double Parcel::readDouble() const
1831{
1832 union {
1833 double d;
1834 unsigned long long ll;
1835 } u;
1836 u.ll = readAligned<unsigned long long>();
1837 return u.d;
1838}
1839
1840#else
1841
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001842status_t Parcel::readDouble(double *pArg) const
1843{
Andreas Huber84a6d042009-08-17 13:33:27 -07001844 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001845}
1846
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847double Parcel::readDouble() const
1848{
Andreas Huber84a6d042009-08-17 13:33:27 -07001849 return readAligned<double>();
1850}
1851
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001852#endif
1853
Andreas Huber84a6d042009-08-17 13:33:27 -07001854status_t Parcel::readIntPtr(intptr_t *pArg) const
1855{
1856 return readAligned(pArg);
1857}
1858
1859
1860intptr_t Parcel::readIntPtr() const
1861{
1862 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001863}
1864
Casey Dahlind6848f52015-10-15 15:44:59 -07001865status_t Parcel::readBool(bool *pArg) const
1866{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001867 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001868 status_t ret = readInt32(&tmp);
1869 *pArg = (tmp != 0);
1870 return ret;
1871}
1872
1873bool Parcel::readBool() const
1874{
1875 return readInt32() != 0;
1876}
1877
1878status_t Parcel::readChar(char16_t *pArg) const
1879{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001880 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001881 status_t ret = readInt32(&tmp);
1882 *pArg = char16_t(tmp);
1883 return ret;
1884}
1885
1886char16_t Parcel::readChar() const
1887{
1888 return char16_t(readInt32());
1889}
1890
1891status_t Parcel::readByte(int8_t *pArg) const
1892{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001893 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001894 status_t ret = readInt32(&tmp);
1895 *pArg = int8_t(tmp);
1896 return ret;
1897}
1898
1899int8_t Parcel::readByte() const
1900{
1901 return int8_t(readInt32());
1902}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001904status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1905 size_t utf16Size = 0;
1906 const char16_t* src = readString16Inplace(&utf16Size);
1907 if (!src) {
1908 return UNEXPECTED_NULL;
1909 }
1910
1911 // Save ourselves the trouble, we're done.
1912 if (utf16Size == 0u) {
1913 str->clear();
1914 return NO_ERROR;
1915 }
1916
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001917 // Allow for closing '\0'
1918 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1919 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001920 return BAD_VALUE;
1921 }
1922 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001923 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001924 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001925 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1926 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001927 return NO_ERROR;
1928}
1929
1930status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1931 const int32_t start = dataPosition();
1932 int32_t size;
1933 status_t status = readInt32(&size);
1934 str->reset();
1935
1936 if (status != OK || size < 0) {
1937 return status;
1938 }
1939
1940 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001941 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001942 return readUtf8FromUtf16(str->get());
1943}
1944
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001945const char* Parcel::readCString() const
1946{
1947 const size_t avail = mDataSize-mDataPos;
1948 if (avail > 0) {
1949 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1950 // is the string's trailing NUL within the parcel's valid bounds?
1951 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1952 if (eos) {
1953 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001954 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001955 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956 return str;
1957 }
1958 }
1959 return NULL;
1960}
1961
1962String8 Parcel::readString8() const
1963{
Roshan Pius87b64d22016-07-18 12:51:02 -07001964 String8 retString;
1965 status_t status = readString8(&retString);
1966 if (status != OK) {
1967 // We don't care about errors here, so just return an empty string.
1968 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001969 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001970 return retString;
1971}
1972
1973status_t Parcel::readString8(String8* pArg) const
1974{
1975 int32_t size;
1976 status_t status = readInt32(&size);
1977 if (status != OK) {
1978 return status;
1979 }
1980 // watch for potential int overflow from size+1
1981 if (size < 0 || size >= INT32_MAX) {
1982 return BAD_VALUE;
1983 }
1984 // |writeString8| writes nothing for empty string.
1985 if (size == 0) {
1986 *pArg = String8();
1987 return OK;
1988 }
1989 const char* str = (const char*)readInplace(size + 1);
1990 if (str == NULL) {
1991 return BAD_VALUE;
1992 }
1993 pArg->setTo(str, size);
1994 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001995}
1996
1997String16 Parcel::readString16() const
1998{
1999 size_t len;
2000 const char16_t* str = readString16Inplace(&len);
2001 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002002 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002003 return String16();
2004}
2005
Casey Dahlinb9872622015-11-25 15:09:45 -08002006status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2007{
2008 const int32_t start = dataPosition();
2009 int32_t size;
2010 status_t status = readInt32(&size);
2011 pArg->reset();
2012
2013 if (status != OK || size < 0) {
2014 return status;
2015 }
2016
2017 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002018 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002019
2020 status = readString16(pArg->get());
2021
2022 if (status != OK) {
2023 pArg->reset();
2024 }
2025
2026 return status;
2027}
2028
Casey Dahlin451ff582015-10-19 18:12:18 -07002029status_t Parcel::readString16(String16* pArg) const
2030{
2031 size_t len;
2032 const char16_t* str = readString16Inplace(&len);
2033 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002034 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002035 return 0;
2036 } else {
2037 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002038 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002039 }
2040}
2041
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2043{
2044 int32_t size = readInt32();
2045 // watch for potential int overflow from size+1
2046 if (size >= 0 && size < INT32_MAX) {
2047 *outLen = size;
2048 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
2049 if (str != NULL) {
2050 return str;
2051 }
2052 }
2053 *outLen = 0;
2054 return NULL;
2055}
2056
Casey Dahlinf0c13772015-10-27 18:33:56 -07002057status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2058{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002059 status_t status = readNullableStrongBinder(val);
2060 if (status == OK && !val->get()) {
2061 status = UNEXPECTED_NULL;
2062 }
2063 return status;
2064}
2065
2066status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2067{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002068 return unflatten_binder(ProcessState::self(), *this, val);
2069}
2070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071sp<IBinder> Parcel::readStrongBinder() const
2072{
2073 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002074 // Note that a lot of code in Android reads binders by hand with this
2075 // method, and that code has historically been ok with getting nullptr
2076 // back (while ignoring error codes).
2077 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 return val;
2079}
2080
2081wp<IBinder> Parcel::readWeakBinder() const
2082{
2083 wp<IBinder> val;
2084 unflatten_binder(ProcessState::self(), *this, &val);
2085 return val;
2086}
2087
Christopher Wiley97f048d2015-11-19 06:49:05 -08002088status_t Parcel::readParcelable(Parcelable* parcelable) const {
2089 int32_t have_parcelable = 0;
2090 status_t status = readInt32(&have_parcelable);
2091 if (status != OK) {
2092 return status;
2093 }
2094 if (!have_parcelable) {
2095 return UNEXPECTED_NULL;
2096 }
2097 return parcelable->readFromParcel(this);
2098}
2099
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002100status_t Parcel::readValue(binder::Value* value) const {
2101 return value->readFromParcel(this);
2102}
2103
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002104int32_t Parcel::readExceptionCode() const
2105{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002106 binder::Status status;
2107 status.readFromParcel(*this);
2108 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002109}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002110
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002111native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002112{
2113 int numFds, numInts;
2114 status_t err;
2115 err = readInt32(&numFds);
2116 if (err != NO_ERROR) return 0;
2117 err = readInt32(&numInts);
2118 if (err != NO_ERROR) return 0;
2119
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002120 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002121 if (!h) {
2122 return 0;
2123 }
2124
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002125 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002126 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002127 if (h->data[i] < 0) {
2128 for (int j = 0; j < i; j++) {
2129 close(h->data[j]);
2130 }
2131 native_handle_delete(h);
2132 return 0;
2133 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002134 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002135 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002136 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002137 native_handle_close(h);
2138 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002139 h = 0;
2140 }
2141 return h;
2142}
2143
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144int Parcel::readFileDescriptor() const
2145{
2146 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002147
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002148 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002149 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002150 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 return BAD_TYPE;
2153}
2154
Dianne Hackborn1941a402016-08-29 12:30:43 -07002155int Parcel::readParcelFileDescriptor() const
2156{
2157 int32_t hasComm = readInt32();
2158 int fd = readFileDescriptor();
2159 if (hasComm != 0) {
2160 // skip
2161 readFileDescriptor();
2162 }
2163 return fd;
2164}
2165
Christopher Wiley2cf19952016-04-11 11:09:37 -07002166status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002167{
2168 int got = readFileDescriptor();
2169
2170 if (got == BAD_TYPE) {
2171 return BAD_TYPE;
2172 }
2173
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002174 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002175
2176 if (val->get() < 0) {
2177 return BAD_VALUE;
2178 }
2179
2180 return OK;
2181}
2182
Ryo Hashimotobf551892018-05-31 16:58:35 +09002183status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2184{
2185 int got = readParcelFileDescriptor();
2186
2187 if (got == BAD_TYPE) {
2188 return BAD_TYPE;
2189 }
2190
2191 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2192
2193 if (val->get() < 0) {
2194 return BAD_VALUE;
2195 }
2196
2197 return OK;
2198}
Casey Dahlin06673e32015-11-23 13:24:23 -08002199
Christopher Wiley2cf19952016-04-11 11:09:37 -07002200status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002201 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2202}
2203
Christopher Wiley2cf19952016-04-11 11:09:37 -07002204status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002205 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2206}
2207
Jeff Brown5707dbf2011-09-23 21:17:56 -07002208status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2209{
Jeff Brown13b16042014-11-11 16:44:25 -08002210 int32_t blobType;
2211 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002212 if (status) return status;
2213
Jeff Brown13b16042014-11-11 16:44:25 -08002214 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002215 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002216 const void* ptr = readInplace(len);
2217 if (!ptr) return BAD_VALUE;
2218
Jeff Brown13b16042014-11-11 16:44:25 -08002219 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002220 return NO_ERROR;
2221 }
2222
Steve Block6807e592011-10-20 11:56:00 +01002223 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002224 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002225 int fd = readFileDescriptor();
2226 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2227
Jeff Brown13b16042014-11-11 16:44:25 -08002228 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2229 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002230 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002231
Jeff Brown13b16042014-11-11 16:44:25 -08002232 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002233 return NO_ERROR;
2234}
2235
Mathias Agopiane1424282013-07-29 21:24:40 -07002236status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002237{
2238 // size
2239 const size_t len = this->readInt32();
2240 const size_t fd_count = this->readInt32();
2241
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002242 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002243 // don't accept size_t values which may have come from an
2244 // inadvertent conversion from a negative int.
2245 return BAD_VALUE;
2246 }
2247
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002248 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002249 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002250 if (buf == NULL)
2251 return BAD_VALUE;
2252
2253 int* fds = NULL;
2254 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002255 fds = new (std::nothrow) int[fd_count];
2256 if (fds == nullptr) {
2257 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2258 return BAD_VALUE;
2259 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002260 }
2261
2262 status_t err = NO_ERROR;
2263 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002264 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002265 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002266 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002267 ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002268 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2269 // Close all the file descriptors that were dup-ed.
2270 for (size_t j=0; j<i ;j++) {
2271 close(fds[j]);
2272 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002273 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002274 }
2275
2276 if (err == NO_ERROR) {
2277 err = val.unflatten(buf, len, fds, fd_count);
2278 }
2279
2280 if (fd_count) {
2281 delete [] fds;
2282 }
2283
2284 return err;
2285}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002286const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2287{
2288 const size_t DPOS = mDataPos;
2289 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2290 const flat_binder_object* obj
2291 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2292 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002293 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002294 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 // the object list, so we don't want to check for it when
2296 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002297 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 return obj;
2299 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002302 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002303 const size_t N = mObjectsSize;
2304 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002307 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002309
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002310 // Start at the current hint position, looking for an object at
2311 // the current data position.
2312 if (opos < N) {
2313 while (opos < (N-1) && OBJS[opos] < DPOS) {
2314 opos++;
2315 }
2316 } else {
2317 opos = N-1;
2318 }
2319 if (OBJS[opos] == DPOS) {
2320 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002321 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322 this, DPOS, opos);
2323 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002324 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002325 return obj;
2326 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 // Look backwards for it...
2329 while (opos > 0 && OBJS[opos] > DPOS) {
2330 opos--;
2331 }
2332 if (OBJS[opos] == DPOS) {
2333 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002334 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 this, DPOS, opos);
2336 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 return obj;
2339 }
2340 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002341 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 -07002342 this, DPOS);
2343 }
2344 return NULL;
2345}
2346
2347void Parcel::closeFileDescriptors()
2348{
2349 size_t i = mObjectsSize;
2350 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002351 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 }
2353 while (i > 0) {
2354 i--;
2355 const flat_binder_object* flat
2356 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002357 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002358 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 close(flat->handle);
2360 }
2361 }
2362}
2363
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002364uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002366 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367}
2368
2369size_t Parcel::ipcDataSize() const
2370{
2371 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2372}
2373
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002374uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002376 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377}
2378
2379size_t Parcel::ipcObjectsCount() const
2380{
2381 return mObjectsSize;
2382}
2383
2384void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002385 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002387 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 freeDataNoInit();
2389 mError = NO_ERROR;
2390 mData = const_cast<uint8_t*>(data);
2391 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002392 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002395 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396 mObjectsSize = mObjectsCapacity = objectsCount;
2397 mNextObjectHint = 0;
2398 mOwner = relFunc;
2399 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002400 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002401 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002402 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002403 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002404 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002405 mObjectsSize = 0;
2406 break;
2407 }
2408 minOffset = offset + sizeof(flat_binder_object);
2409 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 scanForFds();
2411}
2412
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002413void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414{
2415 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002417 if (errorCheck() != NO_ERROR) {
2418 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002419 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 } else if (dataSize() > 0) {
2421 const uint8_t* DATA = data();
2422 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002423 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 const size_t N = objectsCount();
2425 for (size_t i=0; i<N; i++) {
2426 const flat_binder_object* flat
2427 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2428 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002429 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 << " = " << flat->binder;
2431 }
2432 } else {
2433 to << "NULL";
2434 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002436 to << ")";
2437}
2438
2439void Parcel::releaseObjects()
2440{
2441 const sp<ProcessState> proc(ProcessState::self());
2442 size_t i = mObjectsSize;
2443 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002444 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002445 while (i > 0) {
2446 i--;
2447 const flat_binder_object* flat
2448 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002449 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 }
2451}
2452
2453void Parcel::acquireObjects()
2454{
2455 const sp<ProcessState> proc(ProcessState::self());
2456 size_t i = mObjectsSize;
2457 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002458 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 while (i > 0) {
2460 i--;
2461 const flat_binder_object* flat
2462 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002463 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 }
2465}
2466
2467void Parcel::freeData()
2468{
2469 freeDataNoInit();
2470 initState();
2471}
2472
2473void Parcel::freeDataNoInit()
2474{
2475 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002476 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002477 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002478 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2479 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002480 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002482 if (mData) {
2483 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002484 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002485 if (mDataCapacity <= gParcelGlobalAllocSize) {
2486 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2487 } else {
2488 gParcelGlobalAllocSize = 0;
2489 }
2490 if (gParcelGlobalAllocCount > 0) {
2491 gParcelGlobalAllocCount--;
2492 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002493 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002494 free(mData);
2495 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 if (mObjects) free(mObjects);
2497 }
2498}
2499
2500status_t Parcel::growData(size_t len)
2501{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002502 if (len > INT32_MAX) {
2503 // don't accept size_t values which may have come from an
2504 // inadvertent conversion from a negative int.
2505 return BAD_VALUE;
2506 }
2507
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 size_t newSize = ((mDataSize+len)*3)/2;
2509 return (newSize <= mDataSize)
2510 ? (status_t) NO_MEMORY
2511 : continueWrite(newSize);
2512}
2513
2514status_t Parcel::restartWrite(size_t desired)
2515{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002516 if (desired > INT32_MAX) {
2517 // don't accept size_t values which may have come from an
2518 // inadvertent conversion from a negative int.
2519 return BAD_VALUE;
2520 }
2521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 if (mOwner) {
2523 freeData();
2524 return continueWrite(desired);
2525 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 uint8_t* data = (uint8_t*)realloc(mData, desired);
2528 if (!data && desired > mDataCapacity) {
2529 mError = NO_MEMORY;
2530 return NO_MEMORY;
2531 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002534
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002536 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002537 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002538 gParcelGlobalAllocSize += desired;
2539 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002540 if (!mData) {
2541 gParcelGlobalAllocCount++;
2542 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002543 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mData = data;
2545 mDataCapacity = desired;
2546 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002547
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002549 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2550 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 free(mObjects);
2553 mObjects = NULL;
2554 mObjectsSize = mObjectsCapacity = 0;
2555 mNextObjectHint = 0;
2556 mHasFds = false;
2557 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002558 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002559
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 return NO_ERROR;
2561}
2562
2563status_t Parcel::continueWrite(size_t desired)
2564{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002565 if (desired > INT32_MAX) {
2566 // don't accept size_t values which may have come from an
2567 // inadvertent conversion from a negative int.
2568 return BAD_VALUE;
2569 }
2570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 // If shrinking, first adjust for any objects that appear
2572 // after the new data size.
2573 size_t objectsSize = mObjectsSize;
2574 if (desired < mDataSize) {
2575 if (desired == 0) {
2576 objectsSize = 0;
2577 } else {
2578 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002579 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002580 break;
2581 objectsSize--;
2582 }
2583 }
2584 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 if (mOwner) {
2587 // If the size is going to zero, just release the owner's data.
2588 if (desired == 0) {
2589 freeData();
2590 return NO_ERROR;
2591 }
2592
2593 // If there is a different owner, we need to take
2594 // posession.
2595 uint8_t* data = (uint8_t*)malloc(desired);
2596 if (!data) {
2597 mError = NO_MEMORY;
2598 return NO_MEMORY;
2599 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002600 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002603 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002605 free(data);
2606
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 mError = NO_MEMORY;
2608 return NO_MEMORY;
2609 }
2610
2611 // Little hack to only acquire references on objects
2612 // we will be keeping.
2613 size_t oldObjectsSize = mObjectsSize;
2614 mObjectsSize = objectsSize;
2615 acquireObjects();
2616 mObjectsSize = oldObjectsSize;
2617 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002618
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002619 if (mData) {
2620 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2621 }
2622 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002623 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002625 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2627 mOwner = NULL;
2628
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002629 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002630 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002631 gParcelGlobalAllocSize += desired;
2632 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002633 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002634
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 mData = data;
2636 mObjects = objects;
2637 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002638 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 mDataCapacity = desired;
2640 mObjectsSize = mObjectsCapacity = objectsSize;
2641 mNextObjectHint = 0;
2642
2643 } else if (mData) {
2644 if (objectsSize < mObjectsSize) {
2645 // Need to release refs on any objects we are dropping.
2646 const sp<ProcessState> proc(ProcessState::self());
2647 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2648 const flat_binder_object* flat
2649 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002650 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002651 // will need to rescan because we may have lopped off the only FDs
2652 mFdsKnown = false;
2653 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002654 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002656 binder_size_t* objects =
2657 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 if (objects) {
2659 mObjects = objects;
2660 }
2661 mObjectsSize = objectsSize;
2662 mNextObjectHint = 0;
2663 }
2664
2665 // We own the data, so we can just do a realloc().
2666 if (desired > mDataCapacity) {
2667 uint8_t* data = (uint8_t*)realloc(mData, desired);
2668 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002669 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2670 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002671 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002672 gParcelGlobalAllocSize += desired;
2673 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002674 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002675 mData = data;
2676 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002677 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 mError = NO_MEMORY;
2679 return NO_MEMORY;
2680 }
2681 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002682 if (mDataSize > desired) {
2683 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002684 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002685 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 if (mDataPos > desired) {
2687 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002688 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 }
2690 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002691
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 } else {
2693 // This is the first data. Easy!
2694 uint8_t* data = (uint8_t*)malloc(desired);
2695 if (!data) {
2696 mError = NO_MEMORY;
2697 return NO_MEMORY;
2698 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002699
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 if(!(mDataCapacity == 0 && mObjects == NULL
2701 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002702 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002704
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002705 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002706 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002707 gParcelGlobalAllocSize += desired;
2708 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002709 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002710
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002711 mData = data;
2712 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002713 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2714 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 mDataCapacity = desired;
2716 }
2717
2718 return NO_ERROR;
2719}
2720
2721void Parcel::initState()
2722{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002723 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002724 mError = NO_ERROR;
2725 mData = 0;
2726 mDataSize = 0;
2727 mDataCapacity = 0;
2728 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002729 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2730 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002731 mObjects = NULL;
2732 mObjectsSize = 0;
2733 mObjectsCapacity = 0;
2734 mNextObjectHint = 0;
2735 mHasFds = false;
2736 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002737 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002739 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002740
2741 // racing multiple init leads only to multiple identical write
2742 if (gMaxFds == 0) {
2743 struct rlimit result;
2744 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2745 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002746 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002747 } else {
2748 ALOGW("Unable to getrlimit: %s", strerror(errno));
2749 gMaxFds = 1024;
2750 }
2751 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752}
2753
2754void Parcel::scanForFds() const
2755{
2756 bool hasFds = false;
2757 for (size_t i=0; i<mObjectsSize; i++) {
2758 const flat_binder_object* flat
2759 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002760 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 hasFds = true;
2762 break;
2763 }
2764 }
2765 mHasFds = hasFds;
2766 mFdsKnown = true;
2767}
2768
Dan Sandleraa5c2342015-04-10 10:08:45 -04002769size_t Parcel::getBlobAshmemSize() const
2770{
Adrian Roos6bb31142015-10-22 16:46:12 -07002771 // This used to return the size of all blobs that were written to ashmem, now we're returning
2772 // the ashmem currently referenced by this Parcel, which should be equivalent.
2773 // TODO: Remove method once ABI can be changed.
2774 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002775}
2776
Adrian Rooscbf37262015-10-22 16:12:53 -07002777size_t Parcel::getOpenAshmemSize() const
2778{
2779 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002780}
2781
2782// --- Parcel::Blob ---
2783
2784Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002785 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002786}
2787
2788Parcel::Blob::~Blob() {
2789 release();
2790}
2791
2792void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002793 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002794 ::munmap(mData, mSize);
2795 }
2796 clear();
2797}
2798
Jeff Brown13b16042014-11-11 16:44:25 -08002799void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2800 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002801 mData = data;
2802 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002803 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002804}
2805
2806void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002807 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002808 mData = NULL;
2809 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002810 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002811}
2812
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813}; // namespace android