blob: f739f0702457c7dfbcef6170d90f93e785563d49 [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
Christopher Wiley2cf19952016-04-11 11:09:37 -07001182status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001183 return writeDupFileDescriptor(fd.get());
1184}
1185
Christopher Wiley2cf19952016-04-11 11:09:37 -07001186status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001187 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1188}
1189
Christopher Wiley2cf19952016-04-11 11:09:37 -07001190status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001191 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1192}
1193
Jeff Brown13b16042014-11-11 16:44:25 -08001194status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001195{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001196 if (len > INT32_MAX) {
1197 // don't accept size_t values which may have come from an
1198 // inadvertent conversion from a negative int.
1199 return BAD_VALUE;
1200 }
1201
Jeff Brown13b16042014-11-11 16:44:25 -08001202 status_t status;
1203 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001204 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001205 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001206 if (status) return status;
1207
1208 void* ptr = writeInplace(len);
1209 if (!ptr) return NO_MEMORY;
1210
Jeff Brown13b16042014-11-11 16:44:25 -08001211 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001212 return NO_ERROR;
1213 }
1214
Steve Block6807e592011-10-20 11:56:00 +01001215 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001216 int fd = ashmem_create_region("Parcel Blob", len);
1217 if (fd < 0) return NO_MEMORY;
1218
1219 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1220 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001221 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001222 } else {
1223 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1224 if (ptr == MAP_FAILED) {
1225 status = -errno;
1226 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001227 if (!mutableCopy) {
1228 result = ashmem_set_prot_region(fd, PROT_READ);
1229 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001230 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001231 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001233 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001235 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001237 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 return NO_ERROR;
1239 }
1240 }
1241 }
1242 }
1243 ::munmap(ptr, len);
1244 }
1245 ::close(fd);
1246 return status;
1247}
1248
Jeff Brown13b16042014-11-11 16:44:25 -08001249status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1250{
1251 // Must match up with what's done in writeBlob.
1252 if (!mAllowFds) return FDS_NOT_ALLOWED;
1253 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1254 if (status) return status;
1255 return writeDupFileDescriptor(fd);
1256}
1257
Mathias Agopiane1424282013-07-29 21:24:40 -07001258status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001259{
1260 status_t err;
1261
1262 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001263 const size_t len = val.getFlattenedSize();
1264 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001265
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001266 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001267 // don't accept size_t values which may have come from an
1268 // inadvertent conversion from a negative int.
1269 return BAD_VALUE;
1270 }
1271
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001272 err = this->writeInt32(len);
1273 if (err) return err;
1274
1275 err = this->writeInt32(fd_count);
1276 if (err) return err;
1277
1278 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07001279 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001280 if (buf == NULL)
1281 return BAD_VALUE;
1282
1283 int* fds = NULL;
1284 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001285 fds = new (std::nothrow) int[fd_count];
1286 if (fds == nullptr) {
1287 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1288 return BAD_VALUE;
1289 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001290 }
1291
1292 err = val.flatten(buf, len, fds, fd_count);
1293 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1294 err = this->writeDupFileDescriptor( fds[i] );
1295 }
1296
1297 if (fd_count) {
1298 delete [] fds;
1299 }
1300
1301 return err;
1302}
1303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1305{
1306 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1307 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1308 if (enoughData && enoughObjects) {
1309restart_write:
1310 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001311
Christopher Tate98e67d32015-06-03 18:44:15 -07001312 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001313 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001314 if (!mAllowFds) {
1315 // fail before modifying our object index
1316 return FDS_NOT_ALLOWED;
1317 }
1318 mHasFds = mFdsKnown = true;
1319 }
1320
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001321 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001322 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001323 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001324 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001325 mObjectsSize++;
1326 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001328 return finishWrite(sizeof(flat_binder_object));
1329 }
1330
1331 if (!enoughData) {
1332 const status_t err = growData(sizeof(val));
1333 if (err != NO_ERROR) return err;
1334 }
1335 if (!enoughObjects) {
1336 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001337 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001338 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001339 if (objects == NULL) return NO_MEMORY;
1340 mObjects = objects;
1341 mObjectsCapacity = newSize;
1342 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 goto restart_write;
1345}
1346
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001347status_t Parcel::writeNoException()
1348{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001349 binder::Status status;
1350 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001351}
1352
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001353status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1354{
1355 using ::std::map;
1356 using ::android::binder::Value;
1357 using ::android::binder::Map;
1358
1359 Map::const_iterator iter;
1360 status_t ret;
1361
1362 ret = writeInt32(map_in.size());
1363
1364 if (ret != NO_ERROR) {
1365 return ret;
1366 }
1367
1368 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1369 ret = writeValue(Value(iter->first));
1370 if (ret != NO_ERROR) {
1371 return ret;
1372 }
1373
1374 ret = writeValue(iter->second);
1375 if (ret != NO_ERROR) {
1376 return ret;
1377 }
1378 }
1379
1380 return ret;
1381}
1382
1383status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1384{
1385 if (map == NULL) {
1386 return writeInt32(-1);
1387 }
1388
1389 return writeMap(*map.get());
1390}
1391
1392status_t Parcel::readMap(::android::binder::Map* map_out)const
1393{
1394 using ::std::map;
1395 using ::android::String16;
1396 using ::android::String8;
1397 using ::android::binder::Value;
1398 using ::android::binder::Map;
1399
1400 status_t ret = NO_ERROR;
1401 int32_t count;
1402
1403 ret = readInt32(&count);
1404 if (ret != NO_ERROR) {
1405 return ret;
1406 }
1407
1408 if (count < 0) {
1409 ALOGE("readMap: Unexpected count: %d", count);
1410 return (count == -1)
1411 ? UNEXPECTED_NULL
1412 : BAD_VALUE;
1413 }
1414
1415 map_out->clear();
1416
1417 while (count--) {
1418 Map::key_type key;
1419 Value value;
1420
1421 ret = readValue(&value);
1422 if (ret != NO_ERROR) {
1423 return ret;
1424 }
1425
1426 if (!value.getString(&key)) {
1427 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1428 return BAD_VALUE;
1429 }
1430
1431 ret = readValue(&value);
1432 if (ret != NO_ERROR) {
1433 return ret;
1434 }
1435
1436 (*map_out)[key] = value;
1437 }
1438
1439 return ret;
1440}
1441
1442status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1443{
1444 const size_t start = dataPosition();
1445 int32_t count;
1446 status_t status = readInt32(&count);
1447 map->reset();
1448
1449 if (status != OK || count == -1) {
1450 return status;
1451 }
1452
1453 setDataPosition(start);
1454 map->reset(new binder::Map());
1455
1456 status = readMap(map->get());
1457
1458 if (status != OK) {
1459 map->reset();
1460 }
1461
1462 return status;
1463}
1464
1465
1466
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001467void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468{
1469 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1470}
1471
1472status_t Parcel::read(void* outData, size_t len) const
1473{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001474 if (len > INT32_MAX) {
1475 // don't accept size_t values which may have come from an
1476 // inadvertent conversion from a negative int.
1477 return BAD_VALUE;
1478 }
1479
1480 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1481 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001482 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001483 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001484 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485 return NO_ERROR;
1486 }
1487 return NOT_ENOUGH_DATA;
1488}
1489
1490const void* Parcel::readInplace(size_t len) const
1491{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001492 if (len > INT32_MAX) {
1493 // don't accept size_t values which may have come from an
1494 // inadvertent conversion from a negative int.
1495 return NULL;
1496 }
1497
1498 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1499 && len <= pad_size(len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001501 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001502 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503 return data;
1504 }
1505 return NULL;
1506}
1507
Andreas Huber84a6d042009-08-17 13:33:27 -07001508template<class T>
1509status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001510 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001511
1512 if ((mDataPos+sizeof(T)) <= mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001513 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001514 mDataPos += sizeof(T);
1515 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001516 return NO_ERROR;
1517 } else {
1518 return NOT_ENOUGH_DATA;
1519 }
1520}
1521
Andreas Huber84a6d042009-08-17 13:33:27 -07001522template<class T>
1523T Parcel::readAligned() const {
1524 T result;
1525 if (readAligned(&result) != NO_ERROR) {
1526 result = 0;
1527 }
1528
1529 return result;
1530}
1531
1532template<class T>
1533status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001534 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001535
1536 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1537restart_write:
1538 *reinterpret_cast<T*>(mData+mDataPos) = val;
1539 return finishWrite(sizeof(val));
1540 }
1541
1542 status_t err = growData(sizeof(val));
1543 if (err == NO_ERROR) goto restart_write;
1544 return err;
1545}
1546
Casey Dahlin185d3442016-02-09 11:08:35 -08001547namespace {
1548
1549template<typename T>
1550status_t readByteVectorInternal(const Parcel* parcel,
1551 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001552 val->clear();
1553
1554 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001555 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001556
1557 if (status != OK) {
1558 return status;
1559 }
1560
Christopher Wiley4db672d2015-11-10 09:44:30 -08001561 if (size < 0) {
1562 status = UNEXPECTED_NULL;
1563 return status;
1564 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001565 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001566 status = BAD_VALUE;
1567 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001568 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001569
Paul Lietar433e87b2016-09-16 10:39:32 -07001570 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001571 if (!data) {
1572 status = BAD_VALUE;
1573 return status;
1574 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001575 val->reserve(size);
1576 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001577
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001578 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001579}
1580
Casey Dahlin185d3442016-02-09 11:08:35 -08001581template<typename T>
1582status_t readByteVectorInternalPtr(
1583 const Parcel* parcel,
1584 std::unique_ptr<std::vector<T>>* val) {
1585 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001586 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001587 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001588 val->reset();
1589
1590 if (status != OK || size < 0) {
1591 return status;
1592 }
1593
Casey Dahlin185d3442016-02-09 11:08:35 -08001594 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001595 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001596
Casey Dahlin185d3442016-02-09 11:08:35 -08001597 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001598
1599 if (status != OK) {
1600 val->reset();
1601 }
1602
1603 return status;
1604}
1605
Casey Dahlin185d3442016-02-09 11:08:35 -08001606} // namespace
1607
1608status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1609 return readByteVectorInternal(this, val);
1610}
1611
1612status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1613 return readByteVectorInternal(this, val);
1614}
1615
1616status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1617 return readByteVectorInternalPtr(this, val);
1618}
1619
1620status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1621 return readByteVectorInternalPtr(this, val);
1622}
1623
Casey Dahlinb9872622015-11-25 15:09:45 -08001624status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1625 return readNullableTypedVector(val, &Parcel::readInt32);
1626}
1627
Casey Dahlin451ff582015-10-19 18:12:18 -07001628status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001629 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001630}
1631
Casey Dahlinb9872622015-11-25 15:09:45 -08001632status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1633 return readNullableTypedVector(val, &Parcel::readInt64);
1634}
1635
Casey Dahlin451ff582015-10-19 18:12:18 -07001636status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001637 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001638}
1639
Casey Dahlinb9872622015-11-25 15:09:45 -08001640status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1641 return readNullableTypedVector(val, &Parcel::readFloat);
1642}
1643
Casey Dahlin451ff582015-10-19 18:12:18 -07001644status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001645 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001646}
1647
Casey Dahlinb9872622015-11-25 15:09:45 -08001648status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1649 return readNullableTypedVector(val, &Parcel::readDouble);
1650}
1651
Casey Dahlin451ff582015-10-19 18:12:18 -07001652status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001653 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001654}
1655
Casey Dahlinb9872622015-11-25 15:09:45 -08001656status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1657 const int32_t start = dataPosition();
1658 int32_t size;
1659 status_t status = readInt32(&size);
1660 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001661
Casey Dahlinb9872622015-11-25 15:09:45 -08001662 if (status != OK || size < 0) {
1663 return status;
1664 }
1665
1666 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001667 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001668
1669 status = readBoolVector(val->get());
1670
1671 if (status != OK) {
1672 val->reset();
1673 }
1674
1675 return status;
1676}
1677
1678status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001679 int32_t size;
1680 status_t status = readInt32(&size);
1681
1682 if (status != OK) {
1683 return status;
1684 }
1685
1686 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001687 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001688 }
1689
1690 val->resize(size);
1691
1692 /* C++ bool handling means a vector of bools isn't necessarily addressable
1693 * (we might use individual bits)
1694 */
Christopher Wiley97887982015-10-27 16:33:47 -07001695 bool data;
1696 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001697 status = readBool(&data);
1698 (*val)[i] = data;
1699
1700 if (status != OK) {
1701 return status;
1702 }
1703 }
1704
1705 return OK;
1706}
1707
Casey Dahlinb9872622015-11-25 15:09:45 -08001708status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1709 return readNullableTypedVector(val, &Parcel::readChar);
1710}
1711
Casey Dahlin451ff582015-10-19 18:12:18 -07001712status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001713 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001714}
1715
Casey Dahlinb9872622015-11-25 15:09:45 -08001716status_t Parcel::readString16Vector(
1717 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1718 return readNullableTypedVector(val, &Parcel::readString16);
1719}
1720
Casey Dahlin451ff582015-10-19 18:12:18 -07001721status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001722 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001723}
1724
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001725status_t Parcel::readUtf8VectorFromUtf16Vector(
1726 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1727 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1728}
1729
1730status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1731 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1732}
Casey Dahlin451ff582015-10-19 18:12:18 -07001733
Andreas Huber84a6d042009-08-17 13:33:27 -07001734status_t Parcel::readInt32(int32_t *pArg) const
1735{
1736 return readAligned(pArg);
1737}
1738
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001739int32_t Parcel::readInt32() const
1740{
Andreas Huber84a6d042009-08-17 13:33:27 -07001741 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742}
1743
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001744status_t Parcel::readUint32(uint32_t *pArg) const
1745{
1746 return readAligned(pArg);
1747}
1748
1749uint32_t Parcel::readUint32() const
1750{
1751 return readAligned<uint32_t>();
1752}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753
1754status_t Parcel::readInt64(int64_t *pArg) const
1755{
Andreas Huber84a6d042009-08-17 13:33:27 -07001756 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001757}
1758
1759
1760int64_t Parcel::readInt64() const
1761{
Andreas Huber84a6d042009-08-17 13:33:27 -07001762 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763}
1764
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001765status_t Parcel::readUint64(uint64_t *pArg) const
1766{
1767 return readAligned(pArg);
1768}
1769
1770uint64_t Parcel::readUint64() const
1771{
1772 return readAligned<uint64_t>();
1773}
1774
Serban Constantinescuf683e012013-11-05 16:53:55 +00001775status_t Parcel::readPointer(uintptr_t *pArg) const
1776{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001777 status_t ret;
1778 binder_uintptr_t ptr;
1779 ret = readAligned(&ptr);
1780 if (!ret)
1781 *pArg = ptr;
1782 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001783}
1784
1785uintptr_t Parcel::readPointer() const
1786{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001787 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001788}
1789
1790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791status_t Parcel::readFloat(float *pArg) const
1792{
Andreas Huber84a6d042009-08-17 13:33:27 -07001793 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001794}
1795
1796
1797float Parcel::readFloat() const
1798{
Andreas Huber84a6d042009-08-17 13:33:27 -07001799 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001800}
1801
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001802#if defined(__mips__) && defined(__mips_hard_float)
1803
1804status_t Parcel::readDouble(double *pArg) const
1805{
1806 union {
1807 double d;
1808 unsigned long long ll;
1809 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001810 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001811 status_t status;
1812 status = readAligned(&u.ll);
1813 *pArg = u.d;
1814 return status;
1815}
1816
1817double Parcel::readDouble() const
1818{
1819 union {
1820 double d;
1821 unsigned long long ll;
1822 } u;
1823 u.ll = readAligned<unsigned long long>();
1824 return u.d;
1825}
1826
1827#else
1828
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829status_t Parcel::readDouble(double *pArg) const
1830{
Andreas Huber84a6d042009-08-17 13:33:27 -07001831 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832}
1833
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001834double Parcel::readDouble() const
1835{
Andreas Huber84a6d042009-08-17 13:33:27 -07001836 return readAligned<double>();
1837}
1838
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001839#endif
1840
Andreas Huber84a6d042009-08-17 13:33:27 -07001841status_t Parcel::readIntPtr(intptr_t *pArg) const
1842{
1843 return readAligned(pArg);
1844}
1845
1846
1847intptr_t Parcel::readIntPtr() const
1848{
1849 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001850}
1851
Casey Dahlind6848f52015-10-15 15:44:59 -07001852status_t Parcel::readBool(bool *pArg) const
1853{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001854 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001855 status_t ret = readInt32(&tmp);
1856 *pArg = (tmp != 0);
1857 return ret;
1858}
1859
1860bool Parcel::readBool() const
1861{
1862 return readInt32() != 0;
1863}
1864
1865status_t Parcel::readChar(char16_t *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 = char16_t(tmp);
1870 return ret;
1871}
1872
1873char16_t Parcel::readChar() const
1874{
1875 return char16_t(readInt32());
1876}
1877
1878status_t Parcel::readByte(int8_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 = int8_t(tmp);
1883 return ret;
1884}
1885
1886int8_t Parcel::readByte() const
1887{
1888 return int8_t(readInt32());
1889}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001891status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1892 size_t utf16Size = 0;
1893 const char16_t* src = readString16Inplace(&utf16Size);
1894 if (!src) {
1895 return UNEXPECTED_NULL;
1896 }
1897
1898 // Save ourselves the trouble, we're done.
1899 if (utf16Size == 0u) {
1900 str->clear();
1901 return NO_ERROR;
1902 }
1903
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001904 // Allow for closing '\0'
1905 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1906 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001907 return BAD_VALUE;
1908 }
1909 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001910 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001911 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001912 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1913 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001914 return NO_ERROR;
1915}
1916
1917status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
1918 const int32_t start = dataPosition();
1919 int32_t size;
1920 status_t status = readInt32(&size);
1921 str->reset();
1922
1923 if (status != OK || size < 0) {
1924 return status;
1925 }
1926
1927 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001928 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001929 return readUtf8FromUtf16(str->get());
1930}
1931
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932const char* Parcel::readCString() const
1933{
1934 const size_t avail = mDataSize-mDataPos;
1935 if (avail > 0) {
1936 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1937 // is the string's trailing NUL within the parcel's valid bounds?
1938 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1939 if (eos) {
1940 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001941 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001942 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001943 return str;
1944 }
1945 }
1946 return NULL;
1947}
1948
1949String8 Parcel::readString8() const
1950{
Roshan Pius87b64d22016-07-18 12:51:02 -07001951 String8 retString;
1952 status_t status = readString8(&retString);
1953 if (status != OK) {
1954 // We don't care about errors here, so just return an empty string.
1955 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001957 return retString;
1958}
1959
1960status_t Parcel::readString8(String8* pArg) const
1961{
1962 int32_t size;
1963 status_t status = readInt32(&size);
1964 if (status != OK) {
1965 return status;
1966 }
1967 // watch for potential int overflow from size+1
1968 if (size < 0 || size >= INT32_MAX) {
1969 return BAD_VALUE;
1970 }
1971 // |writeString8| writes nothing for empty string.
1972 if (size == 0) {
1973 *pArg = String8();
1974 return OK;
1975 }
1976 const char* str = (const char*)readInplace(size + 1);
1977 if (str == NULL) {
1978 return BAD_VALUE;
1979 }
1980 pArg->setTo(str, size);
1981 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982}
1983
1984String16 Parcel::readString16() const
1985{
1986 size_t len;
1987 const char16_t* str = readString16Inplace(&len);
1988 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001989 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990 return String16();
1991}
1992
Casey Dahlinb9872622015-11-25 15:09:45 -08001993status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1994{
1995 const int32_t start = dataPosition();
1996 int32_t size;
1997 status_t status = readInt32(&size);
1998 pArg->reset();
1999
2000 if (status != OK || size < 0) {
2001 return status;
2002 }
2003
2004 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002005 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002006
2007 status = readString16(pArg->get());
2008
2009 if (status != OK) {
2010 pArg->reset();
2011 }
2012
2013 return status;
2014}
2015
Casey Dahlin451ff582015-10-19 18:12:18 -07002016status_t Parcel::readString16(String16* pArg) const
2017{
2018 size_t len;
2019 const char16_t* str = readString16Inplace(&len);
2020 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002021 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002022 return 0;
2023 } else {
2024 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002025 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002026 }
2027}
2028
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2030{
2031 int32_t size = readInt32();
2032 // watch for potential int overflow from size+1
2033 if (size >= 0 && size < INT32_MAX) {
2034 *outLen = size;
2035 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
2036 if (str != NULL) {
2037 return str;
2038 }
2039 }
2040 *outLen = 0;
2041 return NULL;
2042}
2043
Casey Dahlinf0c13772015-10-27 18:33:56 -07002044status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2045{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002046 status_t status = readNullableStrongBinder(val);
2047 if (status == OK && !val->get()) {
2048 status = UNEXPECTED_NULL;
2049 }
2050 return status;
2051}
2052
2053status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2054{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002055 return unflatten_binder(ProcessState::self(), *this, val);
2056}
2057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002058sp<IBinder> Parcel::readStrongBinder() const
2059{
2060 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002061 // Note that a lot of code in Android reads binders by hand with this
2062 // method, and that code has historically been ok with getting nullptr
2063 // back (while ignoring error codes).
2064 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 return val;
2066}
2067
2068wp<IBinder> Parcel::readWeakBinder() const
2069{
2070 wp<IBinder> val;
2071 unflatten_binder(ProcessState::self(), *this, &val);
2072 return val;
2073}
2074
Christopher Wiley97f048d2015-11-19 06:49:05 -08002075status_t Parcel::readParcelable(Parcelable* parcelable) const {
2076 int32_t have_parcelable = 0;
2077 status_t status = readInt32(&have_parcelable);
2078 if (status != OK) {
2079 return status;
2080 }
2081 if (!have_parcelable) {
2082 return UNEXPECTED_NULL;
2083 }
2084 return parcelable->readFromParcel(this);
2085}
2086
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002087status_t Parcel::readValue(binder::Value* value) const {
2088 return value->readFromParcel(this);
2089}
2090
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002091int32_t Parcel::readExceptionCode() const
2092{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002093 binder::Status status;
2094 status.readFromParcel(*this);
2095 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002096}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002097
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002098native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002099{
2100 int numFds, numInts;
2101 status_t err;
2102 err = readInt32(&numFds);
2103 if (err != NO_ERROR) return 0;
2104 err = readInt32(&numInts);
2105 if (err != NO_ERROR) return 0;
2106
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002107 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002108 if (!h) {
2109 return 0;
2110 }
2111
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002112 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002113 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002114 if (h->data[i] < 0) {
2115 for (int j = 0; j < i; j++) {
2116 close(h->data[j]);
2117 }
2118 native_handle_delete(h);
2119 return 0;
2120 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002121 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002122 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002123 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002124 native_handle_close(h);
2125 native_handle_delete(h);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002126 h = 0;
2127 }
2128 return h;
2129}
2130
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131int Parcel::readFileDescriptor() const
2132{
2133 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002134
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002135 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002136 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002139 return BAD_TYPE;
2140}
2141
Dianne Hackborn1941a402016-08-29 12:30:43 -07002142int Parcel::readParcelFileDescriptor() const
2143{
2144 int32_t hasComm = readInt32();
2145 int fd = readFileDescriptor();
2146 if (hasComm != 0) {
2147 // skip
2148 readFileDescriptor();
2149 }
2150 return fd;
2151}
2152
Christopher Wiley2cf19952016-04-11 11:09:37 -07002153status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002154{
2155 int got = readFileDescriptor();
2156
2157 if (got == BAD_TYPE) {
2158 return BAD_TYPE;
2159 }
2160
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002161 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002162
2163 if (val->get() < 0) {
2164 return BAD_VALUE;
2165 }
2166
2167 return OK;
2168}
2169
2170
Christopher Wiley2cf19952016-04-11 11:09:37 -07002171status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002172 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2173}
2174
Christopher Wiley2cf19952016-04-11 11:09:37 -07002175status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002176 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2177}
2178
Jeff Brown5707dbf2011-09-23 21:17:56 -07002179status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2180{
Jeff Brown13b16042014-11-11 16:44:25 -08002181 int32_t blobType;
2182 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002183 if (status) return status;
2184
Jeff Brown13b16042014-11-11 16:44:25 -08002185 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002186 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002187 const void* ptr = readInplace(len);
2188 if (!ptr) return BAD_VALUE;
2189
Jeff Brown13b16042014-11-11 16:44:25 -08002190 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002191 return NO_ERROR;
2192 }
2193
Steve Block6807e592011-10-20 11:56:00 +01002194 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002195 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002196 int fd = readFileDescriptor();
2197 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2198
Jeff Brown13b16042014-11-11 16:44:25 -08002199 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
2200 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002201 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002202
Jeff Brown13b16042014-11-11 16:44:25 -08002203 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002204 return NO_ERROR;
2205}
2206
Mathias Agopiane1424282013-07-29 21:24:40 -07002207status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002208{
2209 // size
2210 const size_t len = this->readInt32();
2211 const size_t fd_count = this->readInt32();
2212
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002213 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002214 // don't accept size_t values which may have come from an
2215 // inadvertent conversion from a negative int.
2216 return BAD_VALUE;
2217 }
2218
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002219 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002220 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002221 if (buf == NULL)
2222 return BAD_VALUE;
2223
2224 int* fds = NULL;
2225 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002226 fds = new (std::nothrow) int[fd_count];
2227 if (fds == nullptr) {
2228 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2229 return BAD_VALUE;
2230 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002231 }
2232
2233 status_t err = NO_ERROR;
2234 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002235 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002236 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002237 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002238 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 -07002239 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2240 // Close all the file descriptors that were dup-ed.
2241 for (size_t j=0; j<i ;j++) {
2242 close(fds[j]);
2243 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002244 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002245 }
2246
2247 if (err == NO_ERROR) {
2248 err = val.unflatten(buf, len, fds, fd_count);
2249 }
2250
2251 if (fd_count) {
2252 delete [] fds;
2253 }
2254
2255 return err;
2256}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2258{
2259 const size_t DPOS = mDataPos;
2260 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2261 const flat_binder_object* obj
2262 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2263 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002264 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002265 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002266 // the object list, so we don't want to check for it when
2267 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002268 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 return obj;
2270 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002271
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002273 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 const size_t N = mObjectsSize;
2275 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002276
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002278 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002279 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002280
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 // Start at the current hint position, looking for an object at
2282 // the current data position.
2283 if (opos < N) {
2284 while (opos < (N-1) && OBJS[opos] < DPOS) {
2285 opos++;
2286 }
2287 } else {
2288 opos = N-1;
2289 }
2290 if (OBJS[opos] == DPOS) {
2291 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002292 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293 this, DPOS, opos);
2294 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002295 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 return obj;
2297 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 // Look backwards for it...
2300 while (opos > 0 && OBJS[opos] > DPOS) {
2301 opos--;
2302 }
2303 if (OBJS[opos] == DPOS) {
2304 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002305 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306 this, DPOS, opos);
2307 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002308 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 return obj;
2310 }
2311 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002312 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 -07002313 this, DPOS);
2314 }
2315 return NULL;
2316}
2317
2318void Parcel::closeFileDescriptors()
2319{
2320 size_t i = mObjectsSize;
2321 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002322 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 }
2324 while (i > 0) {
2325 i--;
2326 const flat_binder_object* flat
2327 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002328 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002329 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330 close(flat->handle);
2331 }
2332 }
2333}
2334
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002335uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002337 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338}
2339
2340size_t Parcel::ipcDataSize() const
2341{
2342 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2343}
2344
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002345uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002347 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348}
2349
2350size_t Parcel::ipcObjectsCount() const
2351{
2352 return mObjectsSize;
2353}
2354
2355void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002356 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002358 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 freeDataNoInit();
2360 mError = NO_ERROR;
2361 mData = const_cast<uint8_t*>(data);
2362 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002363 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002366 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002367 mObjectsSize = mObjectsCapacity = objectsCount;
2368 mNextObjectHint = 0;
2369 mOwner = relFunc;
2370 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002371 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002372 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002373 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002374 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002375 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002376 mObjectsSize = 0;
2377 break;
2378 }
2379 minOffset = offset + sizeof(flat_binder_object);
2380 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 scanForFds();
2382}
2383
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002384void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385{
2386 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002388 if (errorCheck() != NO_ERROR) {
2389 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002390 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 } else if (dataSize() > 0) {
2392 const uint8_t* DATA = data();
2393 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002394 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 const size_t N = objectsCount();
2396 for (size_t i=0; i<N; i++) {
2397 const flat_binder_object* flat
2398 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2399 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002400 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002401 << " = " << flat->binder;
2402 }
2403 } else {
2404 to << "NULL";
2405 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 to << ")";
2408}
2409
2410void Parcel::releaseObjects()
2411{
2412 const sp<ProcessState> proc(ProcessState::self());
2413 size_t i = mObjectsSize;
2414 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002415 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416 while (i > 0) {
2417 i--;
2418 const flat_binder_object* flat
2419 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002420 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 }
2422}
2423
2424void Parcel::acquireObjects()
2425{
2426 const sp<ProcessState> proc(ProcessState::self());
2427 size_t i = mObjectsSize;
2428 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002429 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 while (i > 0) {
2431 i--;
2432 const flat_binder_object* flat
2433 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002434 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 }
2436}
2437
2438void Parcel::freeData()
2439{
2440 freeDataNoInit();
2441 initState();
2442}
2443
2444void Parcel::freeDataNoInit()
2445{
2446 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002447 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2450 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002451 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002452 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002453 if (mData) {
2454 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002455 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002456 if (mDataCapacity <= gParcelGlobalAllocSize) {
2457 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2458 } else {
2459 gParcelGlobalAllocSize = 0;
2460 }
2461 if (gParcelGlobalAllocCount > 0) {
2462 gParcelGlobalAllocCount--;
2463 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002464 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002465 free(mData);
2466 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 if (mObjects) free(mObjects);
2468 }
2469}
2470
2471status_t Parcel::growData(size_t len)
2472{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002473 if (len > INT32_MAX) {
2474 // don't accept size_t values which may have come from an
2475 // inadvertent conversion from a negative int.
2476 return BAD_VALUE;
2477 }
2478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 size_t newSize = ((mDataSize+len)*3)/2;
2480 return (newSize <= mDataSize)
2481 ? (status_t) NO_MEMORY
2482 : continueWrite(newSize);
2483}
2484
2485status_t Parcel::restartWrite(size_t desired)
2486{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002487 if (desired > INT32_MAX) {
2488 // don't accept size_t values which may have come from an
2489 // inadvertent conversion from a negative int.
2490 return BAD_VALUE;
2491 }
2492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 if (mOwner) {
2494 freeData();
2495 return continueWrite(desired);
2496 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 uint8_t* data = (uint8_t*)realloc(mData, desired);
2499 if (!data && desired > mDataCapacity) {
2500 mError = NO_MEMORY;
2501 return NO_MEMORY;
2502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002507 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002508 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002509 gParcelGlobalAllocSize += desired;
2510 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002511 if (!mData) {
2512 gParcelGlobalAllocCount++;
2513 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002514 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 mData = data;
2516 mDataCapacity = desired;
2517 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2521 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 free(mObjects);
2524 mObjects = NULL;
2525 mObjectsSize = mObjectsCapacity = 0;
2526 mNextObjectHint = 0;
2527 mHasFds = false;
2528 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002529 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 return NO_ERROR;
2532}
2533
2534status_t Parcel::continueWrite(size_t desired)
2535{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002536 if (desired > INT32_MAX) {
2537 // don't accept size_t values which may have come from an
2538 // inadvertent conversion from a negative int.
2539 return BAD_VALUE;
2540 }
2541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542 // If shrinking, first adjust for any objects that appear
2543 // after the new data size.
2544 size_t objectsSize = mObjectsSize;
2545 if (desired < mDataSize) {
2546 if (desired == 0) {
2547 objectsSize = 0;
2548 } else {
2549 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002550 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 break;
2552 objectsSize--;
2553 }
2554 }
2555 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002556
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 if (mOwner) {
2558 // If the size is going to zero, just release the owner's data.
2559 if (desired == 0) {
2560 freeData();
2561 return NO_ERROR;
2562 }
2563
2564 // If there is a different owner, we need to take
2565 // posession.
2566 uint8_t* data = (uint8_t*)malloc(desired);
2567 if (!data) {
2568 mError = NO_MEMORY;
2569 return NO_MEMORY;
2570 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002571 binder_size_t* objects = NULL;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002574 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002576 free(data);
2577
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578 mError = NO_MEMORY;
2579 return NO_MEMORY;
2580 }
2581
2582 // Little hack to only acquire references on objects
2583 // we will be keeping.
2584 size_t oldObjectsSize = mObjectsSize;
2585 mObjectsSize = objectsSize;
2586 acquireObjects();
2587 mObjectsSize = oldObjectsSize;
2588 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 if (mData) {
2591 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2592 }
2593 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002594 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002595 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002596 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002597 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2598 mOwner = NULL;
2599
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002600 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002601 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002602 gParcelGlobalAllocSize += desired;
2603 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002604 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mData = data;
2607 mObjects = objects;
2608 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002609 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 mDataCapacity = desired;
2611 mObjectsSize = mObjectsCapacity = objectsSize;
2612 mNextObjectHint = 0;
2613
2614 } else if (mData) {
2615 if (objectsSize < mObjectsSize) {
2616 // Need to release refs on any objects we are dropping.
2617 const sp<ProcessState> proc(ProcessState::self());
2618 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2619 const flat_binder_object* flat
2620 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002621 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 // will need to rescan because we may have lopped off the only FDs
2623 mFdsKnown = false;
2624 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002625 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002627 binder_size_t* objects =
2628 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002629 if (objects) {
2630 mObjects = objects;
2631 }
2632 mObjectsSize = objectsSize;
2633 mNextObjectHint = 0;
2634 }
2635
2636 // We own the data, so we can just do a realloc().
2637 if (desired > mDataCapacity) {
2638 uint8_t* data = (uint8_t*)realloc(mData, desired);
2639 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002640 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2641 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002642 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002643 gParcelGlobalAllocSize += desired;
2644 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002645 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 mData = data;
2647 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002648 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mError = NO_MEMORY;
2650 return NO_MEMORY;
2651 }
2652 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002653 if (mDataSize > desired) {
2654 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002655 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002656 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657 if (mDataPos > desired) {
2658 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002659 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 }
2661 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663 } else {
2664 // This is the first data. Easy!
2665 uint8_t* data = (uint8_t*)malloc(desired);
2666 if (!data) {
2667 mError = NO_MEMORY;
2668 return NO_MEMORY;
2669 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 if(!(mDataCapacity == 0 && mObjects == NULL
2672 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002673 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002674 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002675
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002676 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002677 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002678 gParcelGlobalAllocSize += desired;
2679 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002680 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 mData = data;
2683 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002684 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2685 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002686 mDataCapacity = desired;
2687 }
2688
2689 return NO_ERROR;
2690}
2691
2692void Parcel::initState()
2693{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002694 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mError = NO_ERROR;
2696 mData = 0;
2697 mDataSize = 0;
2698 mDataCapacity = 0;
2699 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002700 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2701 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002702 mObjects = NULL;
2703 mObjectsSize = 0;
2704 mObjectsCapacity = 0;
2705 mNextObjectHint = 0;
2706 mHasFds = false;
2707 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002708 mAllowFds = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709 mOwner = NULL;
Adrian Rooscbf37262015-10-22 16:12:53 -07002710 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002711
2712 // racing multiple init leads only to multiple identical write
2713 if (gMaxFds == 0) {
2714 struct rlimit result;
2715 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2716 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002717 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002718 } else {
2719 ALOGW("Unable to getrlimit: %s", strerror(errno));
2720 gMaxFds = 1024;
2721 }
2722 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723}
2724
2725void Parcel::scanForFds() const
2726{
2727 bool hasFds = false;
2728 for (size_t i=0; i<mObjectsSize; i++) {
2729 const flat_binder_object* flat
2730 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002731 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 hasFds = true;
2733 break;
2734 }
2735 }
2736 mHasFds = hasFds;
2737 mFdsKnown = true;
2738}
2739
Dan Sandleraa5c2342015-04-10 10:08:45 -04002740size_t Parcel::getBlobAshmemSize() const
2741{
Adrian Roos6bb31142015-10-22 16:46:12 -07002742 // This used to return the size of all blobs that were written to ashmem, now we're returning
2743 // the ashmem currently referenced by this Parcel, which should be equivalent.
2744 // TODO: Remove method once ABI can be changed.
2745 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002746}
2747
Adrian Rooscbf37262015-10-22 16:12:53 -07002748size_t Parcel::getOpenAshmemSize() const
2749{
2750 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002751}
2752
2753// --- Parcel::Blob ---
2754
2755Parcel::Blob::Blob() :
Jeff Brown13b16042014-11-11 16:44:25 -08002756 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002757}
2758
2759Parcel::Blob::~Blob() {
2760 release();
2761}
2762
2763void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002764 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002765 ::munmap(mData, mSize);
2766 }
2767 clear();
2768}
2769
Jeff Brown13b16042014-11-11 16:44:25 -08002770void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2771 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002772 mData = data;
2773 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002774 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002775}
2776
2777void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002778 mFd = -1;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002779 mData = NULL;
2780 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002781 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002782}
2783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784}; // namespace android