blob: f4885d1097172a4a48a3247a89c418c368a845ee [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);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 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);
Yi Kong91635562018-06-07 14:38:36 -0700128 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 return;
130 }
131 case BINDER_TYPE_FD: {
Yi Kong91635562018-06-07 14:38:36 -0700132 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700133 // 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{
Yi Kong91635562018-06-07 14:38:36 -0700149 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700150}
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);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 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);
Yi Kong91635562018-06-07 14:38:36 -0700176 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Yi Kong91635562018-06-07 14:38:36 -0700181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 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{
Yi Kong91635562018-06-07 14:38:36 -0700200 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700201}
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
Yi Kong91635562018-06-07 14:38:36 -0700222 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 IBinder *local = binder->localBinder();
224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700226 if (proxy == nullptr) {
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;
Yi Kong91635562018-06-07 14:38:36 -0700254 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700256 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 IBinder *local = real->localBinder();
258 if (!local) {
259 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700260 if (proxy == nullptr) {
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;
Yi Kong91635562018-06-07 14:38:36 -0700287 return finish_flatten_binder(nullptr, 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;
Yi Kong91635562018-06-07 14:38:36 -0700293 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 }
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);
Yi Kong91635562018-06-07 14:38:36 -0700313 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 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);
Yi Kong91635562018-06-07 14:38:36 -0700332 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 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 {
Yi Kong91635562018-06-07 14:38:36 -0700339 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 }
Yi Kong91635562018-06-07 14:38:36 -0700341 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 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;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800436 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437}
438
439status_t Parcel::setDataCapacity(size_t size)
440{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700441 if (size > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700447 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 return NO_ERROR;
449}
450
451status_t Parcel::setData(const uint8_t* buffer, size_t len)
452{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700453 if (len > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 return BAD_VALUE;
457 }
458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 status_t err = restartWrite(len);
460 if (err == NO_ERROR) {
461 memcpy(const_cast<uint8_t*>(data()), buffer, len);
462 mDataSize = len;
463 mFdsKnown = false;
464 }
465 return err;
466}
467
Andreas Huber51faf462011-04-13 10:21:56 -0700468status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469{
470 const sp<ProcessState> proc(ProcessState::self());
471 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700472 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800473 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 size_t size = parcel->mObjectsSize;
475 int startPos = mDataPos;
476 int firstIndex = -1, lastIndex = -2;
477
478 if (len == 0) {
479 return NO_ERROR;
480 }
481
Nick Kralevichb6b14232015-04-02 09:36:02 -0700482 if (len > INT32_MAX) {
483 // don't accept size_t values which may have come from an
484 // inadvertent conversion from a negative int.
485 return BAD_VALUE;
486 }
487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // range checks against the source parcel size
489 if ((offset > parcel->mDataSize)
490 || (len > parcel->mDataSize)
491 || (offset + len > parcel->mDataSize)) {
492 return BAD_VALUE;
493 }
494
495 // Count objects in range
496 for (int i = 0; i < (int) size; i++) {
497 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700498 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 if (firstIndex == -1) {
500 firstIndex = i;
501 }
502 lastIndex = i;
503 }
504 }
505 int numObjects = lastIndex - firstIndex + 1;
506
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700507 if ((mDataSize+len) > mDataCapacity) {
508 // grow data
509 err = growData(len);
510 if (err != NO_ERROR) {
511 return err;
512 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 }
514
515 // append data
516 memcpy(mData + mDataPos, data + offset, len);
517 mDataPos += len;
518 mDataSize += len;
519
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400520 err = NO_ERROR;
521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 if (numObjects > 0) {
523 // grow objects
524 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700525 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700526 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800527 binder_size_t *objects =
528 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700529 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return NO_MEMORY;
531 }
532 mObjects = objects;
533 mObjectsCapacity = newSize;
534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // append and acquire objects
537 int idx = mObjectsSize;
538 for (int i = firstIndex; i <= lastIndex; i++) {
539 size_t off = objects[i] - offset + startPos;
540 mObjects[idx++] = off;
541 mObjectsSize++;
542
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700545 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700547 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700548 // If this is a file descriptor, we need to dup it so the
549 // new Parcel now owns its own fd, and can declare that we
550 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800551 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 if (!mAllowFds) {
555 err = FDS_NOT_ALLOWED;
556 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 }
558 }
559 }
560
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return err;
562}
563
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700564int Parcel::compareData(const Parcel& other) {
565 size_t size = dataSize();
566 if (size != other.dataSize()) {
567 return size < other.dataSize() ? -1 : 1;
568 }
569 return memcmp(data(), other.data(), size);
570}
571
Jeff Brown13b16042014-11-11 16:44:25 -0800572bool Parcel::allowFds() const
573{
574 return mAllowFds;
575}
576
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700577bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578{
579 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580 if (!allowFds) {
581 mAllowFds = false;
582 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586void Parcel::restoreAllowFds(bool lastValue)
587{
588 mAllowFds = lastValue;
589}
590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591bool Parcel::hasFileDescriptors() const
592{
593 if (!mFdsKnown) {
594 scanForFds();
595 }
596 return mHasFds;
597}
598
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600status_t Parcel::writeInterfaceToken(const String16& interface)
601{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700602 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
603 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700604 // currently the interface identification token is just its name as a string
605 return writeString16(interface);
606}
607
Mathias Agopian83c04462009-05-22 19:00:22 -0700608bool Parcel::checkInterface(IBinder* binder) const
609{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700610 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700611}
612
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700613bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700614 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700615{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700616 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700617 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700618 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700619 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700620 if ((threadState->getLastTransactionBinderFlags() &
621 IBinder::FLAG_ONEWAY) != 0) {
622 // For one-way calls, the callee is running entirely
623 // disconnected from the caller, so disable StrictMode entirely.
624 // Not only does disk/network usage not impact the caller, but
625 // there's no way to commuicate back any violations anyway.
626 threadState->setStrictModePolicy(0);
627 } else {
628 threadState->setStrictModePolicy(strictPolicy);
629 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700630 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700631 if (str == interface) {
632 return true;
633 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700634 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635 String8(interface).string(), String8(str).string());
636 return false;
637 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700638}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800640const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641{
642 return mObjects;
643}
644
645size_t Parcel::objectsCount() const
646{
647 return mObjectsSize;
648}
649
650status_t Parcel::errorCheck() const
651{
652 return mError;
653}
654
655void Parcel::setError(status_t err)
656{
657 mError = err;
658}
659
660status_t Parcel::finishWrite(size_t len)
661{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700662 if (len > INT32_MAX) {
663 // don't accept size_t values which may have come from an
664 // inadvertent conversion from a negative int.
665 return BAD_VALUE;
666 }
667
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700668 //printf("Finish write of %d\n", len);
669 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700670 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671 if (mDataPos > mDataSize) {
672 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700673 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 }
675 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
676 return NO_ERROR;
677}
678
679status_t Parcel::writeUnpadded(const void* data, size_t len)
680{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700681 if (len > INT32_MAX) {
682 // don't accept size_t values which may have come from an
683 // inadvertent conversion from a negative int.
684 return BAD_VALUE;
685 }
686
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687 size_t end = mDataPos + len;
688 if (end < mDataPos) {
689 // integer overflow
690 return BAD_VALUE;
691 }
692
693 if (end <= mDataCapacity) {
694restart_write:
695 memcpy(mData+mDataPos, data, len);
696 return finishWrite(len);
697 }
698
699 status_t err = growData(len);
700 if (err == NO_ERROR) goto restart_write;
701 return err;
702}
703
704status_t Parcel::write(const void* data, size_t len)
705{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700706 if (len > INT32_MAX) {
707 // don't accept size_t values which may have come from an
708 // inadvertent conversion from a negative int.
709 return BAD_VALUE;
710 }
711
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712 void* const d = writeInplace(len);
713 if (d) {
714 memcpy(d, data, len);
715 return NO_ERROR;
716 }
717 return mError;
718}
719
720void* Parcel::writeInplace(size_t len)
721{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700722 if (len > INT32_MAX) {
723 // don't accept size_t values which may have come from an
724 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700725 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700726 }
727
728 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700729
730 // sanity check for integer overflow
731 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700732 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700733 }
734
735 if ((mDataPos+padded) <= mDataCapacity) {
736restart_write:
737 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
738 uint8_t* const data = mData+mDataPos;
739
740 // Need to pad at end?
741 if (padded != len) {
742#if BYTE_ORDER == BIG_ENDIAN
743 static const uint32_t mask[4] = {
744 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
745 };
746#endif
747#if BYTE_ORDER == LITTLE_ENDIAN
748 static const uint32_t mask[4] = {
749 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
750 };
751#endif
752 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
753 // *reinterpret_cast<void**>(data+padded-4));
754 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
755 }
756
757 finishWrite(padded);
758 return data;
759 }
760
761 status_t err = growData(padded);
762 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700763 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700764}
765
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800766status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
767 const uint8_t* strData = (uint8_t*)str.data();
768 const size_t strLen= str.length();
769 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100770 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800771 return BAD_VALUE;
772 }
773
774 status_t err = writeInt32(utf16Len);
775 if (err) {
776 return err;
777 }
778
779 // Allocate enough bytes to hold our converted string and its terminating NULL.
780 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
781 if (!dst) {
782 return NO_MEMORY;
783 }
784
Sergio Girof4607432016-07-21 14:46:35 +0100785 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800786
787 return NO_ERROR;
788}
789
790status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
791 if (!str) {
792 return writeInt32(-1);
793 }
794 return writeUtf8AsUtf16(*str);
795}
796
Casey Dahlin185d3442016-02-09 11:08:35 -0800797namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800798
Casey Dahlin185d3442016-02-09 11:08:35 -0800799template<typename T>
800status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700801{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700802 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700803 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700804 status = BAD_VALUE;
805 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700806 }
807
Casey Dahlin185d3442016-02-09 11:08:35 -0800808 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700809 if (status != OK) {
810 return status;
811 }
812
Casey Dahlin185d3442016-02-09 11:08:35 -0800813 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700814 if (!data) {
815 status = BAD_VALUE;
816 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700817 }
818
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700819 memcpy(data, val.data(), val.size());
820 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700821}
822
Casey Dahlin185d3442016-02-09 11:08:35 -0800823template<typename T>
824status_t writeByteVectorInternalPtr(Parcel* parcel,
825 const std::unique_ptr<std::vector<T>>& val)
826{
827 if (!val) {
828 return parcel->writeInt32(-1);
829 }
830
831 return writeByteVectorInternal(parcel, *val);
832}
833
834} // namespace
835
836status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
837 return writeByteVectorInternal(this, val);
838}
839
840status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
841{
842 return writeByteVectorInternalPtr(this, val);
843}
844
845status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
846 return writeByteVectorInternal(this, val);
847}
848
849status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
850{
851 return writeByteVectorInternalPtr(this, val);
852}
853
Casey Dahlin451ff582015-10-19 18:12:18 -0700854status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
855{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800856 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700857}
858
Casey Dahlinb9872622015-11-25 15:09:45 -0800859status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
860{
861 return writeNullableTypedVector(val, &Parcel::writeInt32);
862}
863
Casey Dahlin451ff582015-10-19 18:12:18 -0700864status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
865{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800866 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700867}
868
Casey Dahlinb9872622015-11-25 15:09:45 -0800869status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
870{
871 return writeNullableTypedVector(val, &Parcel::writeInt64);
872}
873
Casey Dahlin451ff582015-10-19 18:12:18 -0700874status_t Parcel::writeFloatVector(const std::vector<float>& val)
875{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800876 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700877}
878
Casey Dahlinb9872622015-11-25 15:09:45 -0800879status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
880{
881 return writeNullableTypedVector(val, &Parcel::writeFloat);
882}
883
Casey Dahlin451ff582015-10-19 18:12:18 -0700884status_t Parcel::writeDoubleVector(const std::vector<double>& val)
885{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800886 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700887}
888
Casey Dahlinb9872622015-11-25 15:09:45 -0800889status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
890{
891 return writeNullableTypedVector(val, &Parcel::writeDouble);
892}
893
Casey Dahlin451ff582015-10-19 18:12:18 -0700894status_t Parcel::writeBoolVector(const std::vector<bool>& val)
895{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800896 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700897}
898
Casey Dahlinb9872622015-11-25 15:09:45 -0800899status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
900{
901 return writeNullableTypedVector(val, &Parcel::writeBool);
902}
903
Casey Dahlin451ff582015-10-19 18:12:18 -0700904status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
905{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800906 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700907}
908
Casey Dahlinb9872622015-11-25 15:09:45 -0800909status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
910{
911 return writeNullableTypedVector(val, &Parcel::writeChar);
912}
913
Casey Dahlin451ff582015-10-19 18:12:18 -0700914status_t Parcel::writeString16Vector(const std::vector<String16>& val)
915{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800916 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700917}
918
Casey Dahlinb9872622015-11-25 15:09:45 -0800919status_t Parcel::writeString16Vector(
920 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
921{
922 return writeNullableTypedVector(val, &Parcel::writeString16);
923}
924
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800925status_t Parcel::writeUtf8VectorAsUtf16Vector(
926 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
927 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
928}
929
930status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
931 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
932}
933
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700934status_t Parcel::writeInt32(int32_t val)
935{
Andreas Huber84a6d042009-08-17 13:33:27 -0700936 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700937}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800938
939status_t Parcel::writeUint32(uint32_t val)
940{
941 return writeAligned(val);
942}
943
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700944status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700945 if (len > INT32_MAX) {
946 // don't accept size_t values which may have come from an
947 // inadvertent conversion from a negative int.
948 return BAD_VALUE;
949 }
950
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700951 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700952 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700953 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700954 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700955 if (ret == NO_ERROR) {
956 ret = write(val, len * sizeof(*val));
957 }
958 return ret;
959}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700960status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700961 if (len > INT32_MAX) {
962 // don't accept size_t values which may have come from an
963 // inadvertent conversion from a negative int.
964 return BAD_VALUE;
965 }
966
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700967 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700968 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700969 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700970 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700971 if (ret == NO_ERROR) {
972 ret = write(val, len * sizeof(*val));
973 }
974 return ret;
975}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976
Casey Dahlind6848f52015-10-15 15:44:59 -0700977status_t Parcel::writeBool(bool val)
978{
979 return writeInt32(int32_t(val));
980}
981
982status_t Parcel::writeChar(char16_t val)
983{
984 return writeInt32(int32_t(val));
985}
986
987status_t Parcel::writeByte(int8_t val)
988{
989 return writeInt32(int32_t(val));
990}
991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700992status_t Parcel::writeInt64(int64_t val)
993{
Andreas Huber84a6d042009-08-17 13:33:27 -0700994 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700995}
996
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700997status_t Parcel::writeUint64(uint64_t val)
998{
999 return writeAligned(val);
1000}
1001
Serban Constantinescuf683e012013-11-05 16:53:55 +00001002status_t Parcel::writePointer(uintptr_t val)
1003{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001004 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001005}
1006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001007status_t Parcel::writeFloat(float val)
1008{
Andreas Huber84a6d042009-08-17 13:33:27 -07001009 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010}
1011
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001012#if defined(__mips__) && defined(__mips_hard_float)
1013
1014status_t Parcel::writeDouble(double val)
1015{
1016 union {
1017 double d;
1018 unsigned long long ll;
1019 } u;
1020 u.d = val;
1021 return writeAligned(u.ll);
1022}
1023
1024#else
1025
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001026status_t Parcel::writeDouble(double val)
1027{
Andreas Huber84a6d042009-08-17 13:33:27 -07001028 return writeAligned(val);
1029}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001031#endif
1032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001033status_t Parcel::writeCString(const char* str)
1034{
1035 return write(str, strlen(str)+1);
1036}
1037
1038status_t Parcel::writeString8(const String8& str)
1039{
1040 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001041 // only write string if its length is more than zero characters,
1042 // as readString8 will only read if the length field is non-zero.
1043 // this is slightly different from how writeString16 works.
1044 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045 err = write(str.string(), str.bytes()+1);
1046 }
1047 return err;
1048}
1049
Casey Dahlinb9872622015-11-25 15:09:45 -08001050status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1051{
1052 if (!str) {
1053 return writeInt32(-1);
1054 }
1055
1056 return writeString16(*str);
1057}
1058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059status_t Parcel::writeString16(const String16& str)
1060{
1061 return writeString16(str.string(), str.size());
1062}
1063
1064status_t Parcel::writeString16(const char16_t* str, size_t len)
1065{
Yi Kong91635562018-06-07 14:38:36 -07001066 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068 status_t err = writeInt32(len);
1069 if (err == NO_ERROR) {
1070 len *= sizeof(char16_t);
1071 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1072 if (data) {
1073 memcpy(data, str, len);
1074 *reinterpret_cast<char16_t*>(data+len) = 0;
1075 return NO_ERROR;
1076 }
1077 err = mError;
1078 }
1079 return err;
1080}
1081
1082status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1083{
1084 return flatten_binder(ProcessState::self(), val, this);
1085}
1086
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001087status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1088{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001089 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001090}
1091
Casey Dahlinb9872622015-11-25 15:09:45 -08001092status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1093{
1094 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1095}
1096
1097status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001098 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001099}
1100
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001101status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001102 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001103}
1104
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1106{
1107 return flatten_binder(ProcessState::self(), val, this);
1108}
1109
Casey Dahlinb9872622015-11-25 15:09:45 -08001110status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1111 if (!parcelable) {
1112 return writeInt32(0);
1113 }
1114
1115 return writeParcelable(*parcelable);
1116}
1117
Christopher Wiley97f048d2015-11-19 06:49:05 -08001118status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1119 status_t status = writeInt32(1); // parcelable is not null.
1120 if (status != OK) {
1121 return status;
1122 }
1123 return parcelable.writeToParcel(this);
1124}
1125
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001126status_t Parcel::writeValue(const binder::Value& value) {
1127 return value.writeToParcel(this);
1128}
1129
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001130status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001131{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001132 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001133 return BAD_TYPE;
1134
1135 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001136 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001139 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001140 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1143 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
1145 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001146 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 return err;
1148 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001149 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001150 return err;
1151}
1152
Jeff Brown93ff1f92011-11-04 19:01:44 -07001153status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154{
1155 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001156 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001157 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001158 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001159 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001160 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001161 return writeObject(obj, true);
1162}
1163
1164status_t Parcel::writeDupFileDescriptor(int fd)
1165{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001166 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001167 if (dupFd < 0) {
1168 return -errno;
1169 }
1170 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001171 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001172 close(dupFd);
1173 }
1174 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001175}
1176
Dianne Hackborn1941a402016-08-29 12:30:43 -07001177status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1178{
1179 writeInt32(0);
1180 return writeFileDescriptor(fd, takeOwnership);
1181}
1182
Christopher Wiley2cf19952016-04-11 11:09:37 -07001183status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001184 return writeDupFileDescriptor(fd.get());
1185}
1186
Christopher Wiley2cf19952016-04-11 11:09:37 -07001187status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001188 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1189}
1190
Christopher Wiley2cf19952016-04-11 11:09:37 -07001191status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001192 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1193}
1194
Jeff Brown13b16042014-11-11 16:44:25 -08001195status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001196{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001197 if (len > INT32_MAX) {
1198 // don't accept size_t values which may have come from an
1199 // inadvertent conversion from a negative int.
1200 return BAD_VALUE;
1201 }
1202
Jeff Brown13b16042014-11-11 16:44:25 -08001203 status_t status;
1204 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001205 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001206 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001207 if (status) return status;
1208
1209 void* ptr = writeInplace(len);
1210 if (!ptr) return NO_MEMORY;
1211
Jeff Brown13b16042014-11-11 16:44:25 -08001212 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001213 return NO_ERROR;
1214 }
1215
Steve Block6807e592011-10-20 11:56:00 +01001216 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217 int fd = ashmem_create_region("Parcel Blob", len);
1218 if (fd < 0) return NO_MEMORY;
1219
1220 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1221 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001222 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001223 } else {
Yi Kong91635562018-06-07 14:38:36 -07001224 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001225 if (ptr == MAP_FAILED) {
1226 status = -errno;
1227 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001228 if (!mutableCopy) {
1229 result = ashmem_set_prot_region(fd, PROT_READ);
1230 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001231 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001232 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001234 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001236 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001238 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 return NO_ERROR;
1240 }
1241 }
1242 }
1243 }
1244 ::munmap(ptr, len);
1245 }
1246 ::close(fd);
1247 return status;
1248}
1249
Jeff Brown13b16042014-11-11 16:44:25 -08001250status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1251{
1252 // Must match up with what's done in writeBlob.
1253 if (!mAllowFds) return FDS_NOT_ALLOWED;
1254 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1255 if (status) return status;
1256 return writeDupFileDescriptor(fd);
1257}
1258
Mathias Agopiane1424282013-07-29 21:24:40 -07001259status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001260{
1261 status_t err;
1262
1263 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001264 const size_t len = val.getFlattenedSize();
1265 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001266
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001267 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001268 // don't accept size_t values which may have come from an
1269 // inadvertent conversion from a negative int.
1270 return BAD_VALUE;
1271 }
1272
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001273 err = this->writeInt32(len);
1274 if (err) return err;
1275
1276 err = this->writeInt32(fd_count);
1277 if (err) return err;
1278
1279 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001280 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001281 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001282 return BAD_VALUE;
1283
Yi Kong91635562018-06-07 14:38:36 -07001284 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001285 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001286 fds = new (std::nothrow) int[fd_count];
1287 if (fds == nullptr) {
1288 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1289 return BAD_VALUE;
1290 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001291 }
1292
1293 err = val.flatten(buf, len, fds, fd_count);
1294 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1295 err = this->writeDupFileDescriptor( fds[i] );
1296 }
1297
1298 if (fd_count) {
1299 delete [] fds;
1300 }
1301
1302 return err;
1303}
1304
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001305status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1306{
1307 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1308 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1309 if (enoughData && enoughObjects) {
1310restart_write:
1311 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001312
Christopher Tate98e67d32015-06-03 18:44:15 -07001313 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001314 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001315 if (!mAllowFds) {
1316 // fail before modifying our object index
1317 return FDS_NOT_ALLOWED;
1318 }
1319 mHasFds = mFdsKnown = true;
1320 }
1321
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001322 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001323 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001325 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326 mObjectsSize++;
1327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001329 return finishWrite(sizeof(flat_binder_object));
1330 }
1331
1332 if (!enoughData) {
1333 const status_t err = growData(sizeof(val));
1334 if (err != NO_ERROR) return err;
1335 }
1336 if (!enoughObjects) {
1337 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001338 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001339 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001340 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341 mObjects = objects;
1342 mObjectsCapacity = newSize;
1343 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 goto restart_write;
1346}
1347
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001348status_t Parcel::writeNoException()
1349{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001350 binder::Status status;
1351 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001352}
1353
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001354status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1355{
1356 using ::std::map;
1357 using ::android::binder::Value;
1358 using ::android::binder::Map;
1359
1360 Map::const_iterator iter;
1361 status_t ret;
1362
1363 ret = writeInt32(map_in.size());
1364
1365 if (ret != NO_ERROR) {
1366 return ret;
1367 }
1368
1369 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1370 ret = writeValue(Value(iter->first));
1371 if (ret != NO_ERROR) {
1372 return ret;
1373 }
1374
1375 ret = writeValue(iter->second);
1376 if (ret != NO_ERROR) {
1377 return ret;
1378 }
1379 }
1380
1381 return ret;
1382}
1383
1384status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1385{
Yi Kong91635562018-06-07 14:38:36 -07001386 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001387 return writeInt32(-1);
1388 }
1389
1390 return writeMap(*map.get());
1391}
1392
1393status_t Parcel::readMap(::android::binder::Map* map_out)const
1394{
1395 using ::std::map;
1396 using ::android::String16;
1397 using ::android::String8;
1398 using ::android::binder::Value;
1399 using ::android::binder::Map;
1400
1401 status_t ret = NO_ERROR;
1402 int32_t count;
1403
1404 ret = readInt32(&count);
1405 if (ret != NO_ERROR) {
1406 return ret;
1407 }
1408
1409 if (count < 0) {
1410 ALOGE("readMap: Unexpected count: %d", count);
1411 return (count == -1)
1412 ? UNEXPECTED_NULL
1413 : BAD_VALUE;
1414 }
1415
1416 map_out->clear();
1417
1418 while (count--) {
1419 Map::key_type key;
1420 Value value;
1421
1422 ret = readValue(&value);
1423 if (ret != NO_ERROR) {
1424 return ret;
1425 }
1426
1427 if (!value.getString(&key)) {
1428 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1429 return BAD_VALUE;
1430 }
1431
1432 ret = readValue(&value);
1433 if (ret != NO_ERROR) {
1434 return ret;
1435 }
1436
1437 (*map_out)[key] = value;
1438 }
1439
1440 return ret;
1441}
1442
1443status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1444{
1445 const size_t start = dataPosition();
1446 int32_t count;
1447 status_t status = readInt32(&count);
1448 map->reset();
1449
1450 if (status != OK || count == -1) {
1451 return status;
1452 }
1453
1454 setDataPosition(start);
1455 map->reset(new binder::Map());
1456
1457 status = readMap(map->get());
1458
1459 if (status != OK) {
1460 map->reset();
1461 }
1462
1463 return status;
1464}
1465
1466
1467
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001468void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469{
1470 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1471}
1472
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001473status_t Parcel::validateReadData(size_t upperBound) const
1474{
1475 // Don't allow non-object reads on object data
1476 if (mObjectsSorted || mObjectsSize <= 1) {
1477data_sorted:
1478 // Expect to check only against the next object
1479 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1480 // For some reason the current read position is greater than the next object
1481 // hint. Iterate until we find the right object
1482 size_t nextObject = mNextObjectHint;
1483 do {
1484 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1485 // Requested info overlaps with an object
1486 ALOGE("Attempt to read from protected data in Parcel %p", this);
1487 return PERMISSION_DENIED;
1488 }
1489 nextObject++;
1490 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1491 mNextObjectHint = nextObject;
1492 }
1493 return NO_ERROR;
1494 }
1495 // Quickly determine if mObjects is sorted.
1496 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1497 binder_size_t* prevObj = currObj;
1498 while (currObj > mObjects) {
1499 prevObj--;
1500 if(*prevObj > *currObj) {
1501 goto data_unsorted;
1502 }
1503 currObj--;
1504 }
1505 mObjectsSorted = true;
1506 goto data_sorted;
1507
1508data_unsorted:
1509 // Insertion Sort mObjects
1510 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1511 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1512 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1513 binder_size_t temp = *iter0;
1514 binder_size_t* iter1 = iter0 - 1;
1515 while (iter1 >= mObjects && *iter1 > temp) {
1516 *(iter1 + 1) = *iter1;
1517 iter1--;
1518 }
1519 *(iter1 + 1) = temp;
1520 }
1521 mNextObjectHint = 0;
1522 mObjectsSorted = true;
1523 goto data_sorted;
1524}
1525
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526status_t Parcel::read(void* outData, size_t len) const
1527{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001528 if (len > INT32_MAX) {
1529 // don't accept size_t values which may have come from an
1530 // inadvertent conversion from a negative int.
1531 return BAD_VALUE;
1532 }
1533
1534 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1535 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001536 if (mObjectsSize > 0) {
1537 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001538 if(err != NO_ERROR) {
1539 // Still increment the data position by the expected length
1540 mDataPos += pad_size(len);
1541 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1542 return err;
1543 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001546 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001547 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001548 return NO_ERROR;
1549 }
1550 return NOT_ENOUGH_DATA;
1551}
1552
1553const void* Parcel::readInplace(size_t len) const
1554{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001555 if (len > INT32_MAX) {
1556 // don't accept size_t values which may have come from an
1557 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001558 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001559 }
1560
1561 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1562 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001563 if (mObjectsSize > 0) {
1564 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001565 if(err != NO_ERROR) {
1566 // Still increment the data position by the expected length
1567 mDataPos += pad_size(len);
1568 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001569 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001570 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001571 }
1572
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001573 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001574 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001575 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576 return data;
1577 }
Yi Kong91635562018-06-07 14:38:36 -07001578 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579}
1580
Andreas Huber84a6d042009-08-17 13:33:27 -07001581template<class T>
1582status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001583 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001584
1585 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001586 if (mObjectsSize > 0) {
1587 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001588 if(err != NO_ERROR) {
1589 // Still increment the data position by the expected length
1590 mDataPos += sizeof(T);
1591 return err;
1592 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001593 }
1594
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001596 mDataPos += sizeof(T);
1597 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 return NO_ERROR;
1599 } else {
1600 return NOT_ENOUGH_DATA;
1601 }
1602}
1603
Andreas Huber84a6d042009-08-17 13:33:27 -07001604template<class T>
1605T Parcel::readAligned() const {
1606 T result;
1607 if (readAligned(&result) != NO_ERROR) {
1608 result = 0;
1609 }
1610
1611 return result;
1612}
1613
1614template<class T>
1615status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001616 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001617
1618 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1619restart_write:
1620 *reinterpret_cast<T*>(mData+mDataPos) = val;
1621 return finishWrite(sizeof(val));
1622 }
1623
1624 status_t err = growData(sizeof(val));
1625 if (err == NO_ERROR) goto restart_write;
1626 return err;
1627}
1628
Casey Dahlin185d3442016-02-09 11:08:35 -08001629namespace {
1630
1631template<typename T>
1632status_t readByteVectorInternal(const Parcel* parcel,
1633 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001634 val->clear();
1635
1636 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001637 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001638
1639 if (status != OK) {
1640 return status;
1641 }
1642
Christopher Wiley4db672d2015-11-10 09:44:30 -08001643 if (size < 0) {
1644 status = UNEXPECTED_NULL;
1645 return status;
1646 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001647 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001648 status = BAD_VALUE;
1649 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001650 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001651
Paul Lietar433e87b2016-09-16 10:39:32 -07001652 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001653 if (!data) {
1654 status = BAD_VALUE;
1655 return status;
1656 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001657 val->reserve(size);
1658 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001659
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001660 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001661}
1662
Casey Dahlin185d3442016-02-09 11:08:35 -08001663template<typename T>
1664status_t readByteVectorInternalPtr(
1665 const Parcel* parcel,
1666 std::unique_ptr<std::vector<T>>* val) {
1667 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001668 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001669 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001670 val->reset();
1671
1672 if (status != OK || size < 0) {
1673 return status;
1674 }
1675
Casey Dahlin185d3442016-02-09 11:08:35 -08001676 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001677 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001678
Casey Dahlin185d3442016-02-09 11:08:35 -08001679 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001680
1681 if (status != OK) {
1682 val->reset();
1683 }
1684
1685 return status;
1686}
1687
Casey Dahlin185d3442016-02-09 11:08:35 -08001688} // namespace
1689
1690status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1691 return readByteVectorInternal(this, val);
1692}
1693
1694status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1695 return readByteVectorInternal(this, val);
1696}
1697
1698status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1699 return readByteVectorInternalPtr(this, val);
1700}
1701
1702status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1703 return readByteVectorInternalPtr(this, val);
1704}
1705
Casey Dahlinb9872622015-11-25 15:09:45 -08001706status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1707 return readNullableTypedVector(val, &Parcel::readInt32);
1708}
1709
Casey Dahlin451ff582015-10-19 18:12:18 -07001710status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001711 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001712}
1713
Casey Dahlinb9872622015-11-25 15:09:45 -08001714status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1715 return readNullableTypedVector(val, &Parcel::readInt64);
1716}
1717
Casey Dahlin451ff582015-10-19 18:12:18 -07001718status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001719 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001720}
1721
Casey Dahlinb9872622015-11-25 15:09:45 -08001722status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1723 return readNullableTypedVector(val, &Parcel::readFloat);
1724}
1725
Casey Dahlin451ff582015-10-19 18:12:18 -07001726status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001727 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001728}
1729
Casey Dahlinb9872622015-11-25 15:09:45 -08001730status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1731 return readNullableTypedVector(val, &Parcel::readDouble);
1732}
1733
Casey Dahlin451ff582015-10-19 18:12:18 -07001734status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001735 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001736}
1737
Casey Dahlinb9872622015-11-25 15:09:45 -08001738status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1739 const int32_t start = dataPosition();
1740 int32_t size;
1741 status_t status = readInt32(&size);
1742 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001743
Casey Dahlinb9872622015-11-25 15:09:45 -08001744 if (status != OK || size < 0) {
1745 return status;
1746 }
1747
1748 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001749 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001750
1751 status = readBoolVector(val->get());
1752
1753 if (status != OK) {
1754 val->reset();
1755 }
1756
1757 return status;
1758}
1759
1760status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001761 int32_t size;
1762 status_t status = readInt32(&size);
1763
1764 if (status != OK) {
1765 return status;
1766 }
1767
1768 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001769 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001770 }
1771
1772 val->resize(size);
1773
1774 /* C++ bool handling means a vector of bools isn't necessarily addressable
1775 * (we might use individual bits)
1776 */
Christopher Wiley97887982015-10-27 16:33:47 -07001777 bool data;
1778 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001779 status = readBool(&data);
1780 (*val)[i] = data;
1781
1782 if (status != OK) {
1783 return status;
1784 }
1785 }
1786
1787 return OK;
1788}
1789
Casey Dahlinb9872622015-11-25 15:09:45 -08001790status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1791 return readNullableTypedVector(val, &Parcel::readChar);
1792}
1793
Casey Dahlin451ff582015-10-19 18:12:18 -07001794status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001795 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001796}
1797
Casey Dahlinb9872622015-11-25 15:09:45 -08001798status_t Parcel::readString16Vector(
1799 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1800 return readNullableTypedVector(val, &Parcel::readString16);
1801}
1802
Casey Dahlin451ff582015-10-19 18:12:18 -07001803status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001804 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001805}
1806
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001807status_t Parcel::readUtf8VectorFromUtf16Vector(
1808 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1809 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1810}
1811
1812status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1813 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1814}
Casey Dahlin451ff582015-10-19 18:12:18 -07001815
Andreas Huber84a6d042009-08-17 13:33:27 -07001816status_t Parcel::readInt32(int32_t *pArg) const
1817{
1818 return readAligned(pArg);
1819}
1820
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821int32_t Parcel::readInt32() const
1822{
Andreas Huber84a6d042009-08-17 13:33:27 -07001823 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001824}
1825
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001826status_t Parcel::readUint32(uint32_t *pArg) const
1827{
1828 return readAligned(pArg);
1829}
1830
1831uint32_t Parcel::readUint32() const
1832{
1833 return readAligned<uint32_t>();
1834}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001835
1836status_t Parcel::readInt64(int64_t *pArg) const
1837{
Andreas Huber84a6d042009-08-17 13:33:27 -07001838 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001839}
1840
1841
1842int64_t Parcel::readInt64() const
1843{
Andreas Huber84a6d042009-08-17 13:33:27 -07001844 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001845}
1846
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001847status_t Parcel::readUint64(uint64_t *pArg) const
1848{
1849 return readAligned(pArg);
1850}
1851
1852uint64_t Parcel::readUint64() const
1853{
1854 return readAligned<uint64_t>();
1855}
1856
Serban Constantinescuf683e012013-11-05 16:53:55 +00001857status_t Parcel::readPointer(uintptr_t *pArg) const
1858{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001859 status_t ret;
1860 binder_uintptr_t ptr;
1861 ret = readAligned(&ptr);
1862 if (!ret)
1863 *pArg = ptr;
1864 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001865}
1866
1867uintptr_t Parcel::readPointer() const
1868{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001869 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001870}
1871
1872
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873status_t Parcel::readFloat(float *pArg) const
1874{
Andreas Huber84a6d042009-08-17 13:33:27 -07001875 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001876}
1877
1878
1879float Parcel::readFloat() const
1880{
Andreas Huber84a6d042009-08-17 13:33:27 -07001881 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882}
1883
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001884#if defined(__mips__) && defined(__mips_hard_float)
1885
1886status_t Parcel::readDouble(double *pArg) const
1887{
1888 union {
1889 double d;
1890 unsigned long long ll;
1891 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001892 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001893 status_t status;
1894 status = readAligned(&u.ll);
1895 *pArg = u.d;
1896 return status;
1897}
1898
1899double Parcel::readDouble() const
1900{
1901 union {
1902 double d;
1903 unsigned long long ll;
1904 } u;
1905 u.ll = readAligned<unsigned long long>();
1906 return u.d;
1907}
1908
1909#else
1910
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911status_t Parcel::readDouble(double *pArg) const
1912{
Andreas Huber84a6d042009-08-17 13:33:27 -07001913 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914}
1915
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916double Parcel::readDouble() const
1917{
Andreas Huber84a6d042009-08-17 13:33:27 -07001918 return readAligned<double>();
1919}
1920
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001921#endif
1922
Andreas Huber84a6d042009-08-17 13:33:27 -07001923status_t Parcel::readIntPtr(intptr_t *pArg) const
1924{
1925 return readAligned(pArg);
1926}
1927
1928
1929intptr_t Parcel::readIntPtr() const
1930{
1931 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001932}
1933
Casey Dahlind6848f52015-10-15 15:44:59 -07001934status_t Parcel::readBool(bool *pArg) const
1935{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001936 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001937 status_t ret = readInt32(&tmp);
1938 *pArg = (tmp != 0);
1939 return ret;
1940}
1941
1942bool Parcel::readBool() const
1943{
1944 return readInt32() != 0;
1945}
1946
1947status_t Parcel::readChar(char16_t *pArg) const
1948{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001949 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001950 status_t ret = readInt32(&tmp);
1951 *pArg = char16_t(tmp);
1952 return ret;
1953}
1954
1955char16_t Parcel::readChar() const
1956{
1957 return char16_t(readInt32());
1958}
1959
1960status_t Parcel::readByte(int8_t *pArg) const
1961{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001962 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001963 status_t ret = readInt32(&tmp);
1964 *pArg = int8_t(tmp);
1965 return ret;
1966}
1967
1968int8_t Parcel::readByte() const
1969{
1970 return int8_t(readInt32());
1971}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001972
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001973status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1974 size_t utf16Size = 0;
1975 const char16_t* src = readString16Inplace(&utf16Size);
1976 if (!src) {
1977 return UNEXPECTED_NULL;
1978 }
1979
1980 // Save ourselves the trouble, we're done.
1981 if (utf16Size == 0u) {
1982 str->clear();
1983 return NO_ERROR;
1984 }
1985
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001986 // Allow for closing '\0'
1987 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1988 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001989 return BAD_VALUE;
1990 }
1991 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001992 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001993 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001994 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1995 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001996 return NO_ERROR;
1997}
1998
1999status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2000 const int32_t start = dataPosition();
2001 int32_t size;
2002 status_t status = readInt32(&size);
2003 str->reset();
2004
2005 if (status != OK || size < 0) {
2006 return status;
2007 }
2008
2009 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002010 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002011 return readUtf8FromUtf16(str->get());
2012}
2013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014const char* Parcel::readCString() const
2015{
2016 const size_t avail = mDataSize-mDataPos;
2017 if (avail > 0) {
2018 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2019 // is the string's trailing NUL within the parcel's valid bounds?
2020 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2021 if (eos) {
2022 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002023 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002024 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025 return str;
2026 }
2027 }
Yi Kong91635562018-06-07 14:38:36 -07002028 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029}
2030
2031String8 Parcel::readString8() const
2032{
Roshan Pius87b64d22016-07-18 12:51:02 -07002033 String8 retString;
2034 status_t status = readString8(&retString);
2035 if (status != OK) {
2036 // We don't care about errors here, so just return an empty string.
2037 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002038 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002039 return retString;
2040}
2041
2042status_t Parcel::readString8(String8* pArg) const
2043{
2044 int32_t size;
2045 status_t status = readInt32(&size);
2046 if (status != OK) {
2047 return status;
2048 }
2049 // watch for potential int overflow from size+1
2050 if (size < 0 || size >= INT32_MAX) {
2051 return BAD_VALUE;
2052 }
2053 // |writeString8| writes nothing for empty string.
2054 if (size == 0) {
2055 *pArg = String8();
2056 return OK;
2057 }
2058 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002059 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002060 return BAD_VALUE;
2061 }
2062 pArg->setTo(str, size);
2063 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064}
2065
2066String16 Parcel::readString16() const
2067{
2068 size_t len;
2069 const char16_t* str = readString16Inplace(&len);
2070 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002071 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072 return String16();
2073}
2074
Casey Dahlinb9872622015-11-25 15:09:45 -08002075status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2076{
2077 const int32_t start = dataPosition();
2078 int32_t size;
2079 status_t status = readInt32(&size);
2080 pArg->reset();
2081
2082 if (status != OK || size < 0) {
2083 return status;
2084 }
2085
2086 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002087 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002088
2089 status = readString16(pArg->get());
2090
2091 if (status != OK) {
2092 pArg->reset();
2093 }
2094
2095 return status;
2096}
2097
Casey Dahlin451ff582015-10-19 18:12:18 -07002098status_t Parcel::readString16(String16* pArg) const
2099{
2100 size_t len;
2101 const char16_t* str = readString16Inplace(&len);
2102 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002103 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002104 return 0;
2105 } else {
2106 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002107 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002108 }
2109}
2110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2112{
2113 int32_t size = readInt32();
2114 // watch for potential int overflow from size+1
2115 if (size >= 0 && size < INT32_MAX) {
2116 *outLen = size;
2117 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002118 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 return str;
2120 }
2121 }
2122 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002123 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124}
2125
Casey Dahlinf0c13772015-10-27 18:33:56 -07002126status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2127{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002128 status_t status = readNullableStrongBinder(val);
2129 if (status == OK && !val->get()) {
2130 status = UNEXPECTED_NULL;
2131 }
2132 return status;
2133}
2134
2135status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2136{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002137 return unflatten_binder(ProcessState::self(), *this, val);
2138}
2139
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002140sp<IBinder> Parcel::readStrongBinder() const
2141{
2142 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002143 // Note that a lot of code in Android reads binders by hand with this
2144 // method, and that code has historically been ok with getting nullptr
2145 // back (while ignoring error codes).
2146 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 return val;
2148}
2149
2150wp<IBinder> Parcel::readWeakBinder() const
2151{
2152 wp<IBinder> val;
2153 unflatten_binder(ProcessState::self(), *this, &val);
2154 return val;
2155}
2156
Christopher Wiley97f048d2015-11-19 06:49:05 -08002157status_t Parcel::readParcelable(Parcelable* parcelable) const {
2158 int32_t have_parcelable = 0;
2159 status_t status = readInt32(&have_parcelable);
2160 if (status != OK) {
2161 return status;
2162 }
2163 if (!have_parcelable) {
2164 return UNEXPECTED_NULL;
2165 }
2166 return parcelable->readFromParcel(this);
2167}
2168
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002169status_t Parcel::readValue(binder::Value* value) const {
2170 return value->readFromParcel(this);
2171}
2172
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002173int32_t Parcel::readExceptionCode() const
2174{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002175 binder::Status status;
2176 status.readFromParcel(*this);
2177 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002178}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002179
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002180native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002181{
2182 int numFds, numInts;
2183 status_t err;
2184 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002185 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002186 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002187 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002188
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002189 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002190 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002191 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002192 }
2193
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002195 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002196 if (h->data[i] < 0) {
2197 for (int j = 0; j < i; j++) {
2198 close(h->data[j]);
2199 }
2200 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002201 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002202 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002203 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002204 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002205 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002206 native_handle_close(h);
2207 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002208 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002209 }
2210 return h;
2211}
2212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213int Parcel::readFileDescriptor() const
2214{
2215 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002216
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002217 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002218 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 return BAD_TYPE;
2222}
2223
Dianne Hackborn1941a402016-08-29 12:30:43 -07002224int Parcel::readParcelFileDescriptor() const
2225{
2226 int32_t hasComm = readInt32();
2227 int fd = readFileDescriptor();
2228 if (hasComm != 0) {
2229 // skip
2230 readFileDescriptor();
2231 }
2232 return fd;
2233}
2234
Christopher Wiley2cf19952016-04-11 11:09:37 -07002235status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002236{
2237 int got = readFileDescriptor();
2238
2239 if (got == BAD_TYPE) {
2240 return BAD_TYPE;
2241 }
2242
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002243 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002244
2245 if (val->get() < 0) {
2246 return BAD_VALUE;
2247 }
2248
2249 return OK;
2250}
2251
2252
Christopher Wiley2cf19952016-04-11 11:09:37 -07002253status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002254 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2255}
2256
Christopher Wiley2cf19952016-04-11 11:09:37 -07002257status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002258 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2259}
2260
Jeff Brown5707dbf2011-09-23 21:17:56 -07002261status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2262{
Jeff Brown13b16042014-11-11 16:44:25 -08002263 int32_t blobType;
2264 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002265 if (status) return status;
2266
Jeff Brown13b16042014-11-11 16:44:25 -08002267 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002268 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002269 const void* ptr = readInplace(len);
2270 if (!ptr) return BAD_VALUE;
2271
Jeff Brown13b16042014-11-11 16:44:25 -08002272 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002273 return NO_ERROR;
2274 }
2275
Steve Block6807e592011-10-20 11:56:00 +01002276 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002277 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002278 int fd = readFileDescriptor();
2279 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2280
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002281 if (!ashmem_valid(fd)) {
2282 ALOGE("invalid fd");
2283 return BAD_VALUE;
2284 }
2285 int size = ashmem_get_size_region(fd);
2286 if (size < 0 || size_t(size) < len) {
2287 ALOGE("request size %zu does not match fd size %d", len, size);
2288 return BAD_VALUE;
2289 }
Yi Kong91635562018-06-07 14:38:36 -07002290 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002291 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002292 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002293
Jeff Brown13b16042014-11-11 16:44:25 -08002294 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002295 return NO_ERROR;
2296}
2297
Mathias Agopiane1424282013-07-29 21:24:40 -07002298status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002299{
2300 // size
2301 const size_t len = this->readInt32();
2302 const size_t fd_count = this->readInt32();
2303
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002304 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002305 // don't accept size_t values which may have come from an
2306 // inadvertent conversion from a negative int.
2307 return BAD_VALUE;
2308 }
2309
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002310 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002311 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002312 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002313 return BAD_VALUE;
2314
Yi Kong91635562018-06-07 14:38:36 -07002315 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002316 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002317 fds = new (std::nothrow) int[fd_count];
2318 if (fds == nullptr) {
2319 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2320 return BAD_VALUE;
2321 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002322 }
2323
2324 status_t err = NO_ERROR;
2325 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002326 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002327 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002328 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002329 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 -07002330 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2331 // Close all the file descriptors that were dup-ed.
2332 for (size_t j=0; j<i ;j++) {
2333 close(fds[j]);
2334 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002335 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002336 }
2337
2338 if (err == NO_ERROR) {
2339 err = val.unflatten(buf, len, fds, fd_count);
2340 }
2341
2342 if (fd_count) {
2343 delete [] fds;
2344 }
2345
2346 return err;
2347}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2349{
2350 const size_t DPOS = mDataPos;
2351 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2352 const flat_binder_object* obj
2353 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2354 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002355 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002356 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 // the object list, so we don't want to check for it when
2358 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002359 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 return obj;
2361 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002363 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002364 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 const size_t N = mObjectsSize;
2366 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002369 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002372 // Start at the current hint position, looking for an object at
2373 // the current data position.
2374 if (opos < N) {
2375 while (opos < (N-1) && OBJS[opos] < DPOS) {
2376 opos++;
2377 }
2378 } else {
2379 opos = N-1;
2380 }
2381 if (OBJS[opos] == DPOS) {
2382 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002383 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384 this, DPOS, opos);
2385 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002386 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387 return obj;
2388 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002389
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 // Look backwards for it...
2391 while (opos > 0 && OBJS[opos] > DPOS) {
2392 opos--;
2393 }
2394 if (OBJS[opos] == DPOS) {
2395 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002396 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 this, DPOS, opos);
2398 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 return obj;
2401 }
2402 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002403 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 -07002404 this, DPOS);
2405 }
Yi Kong91635562018-06-07 14:38:36 -07002406 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407}
2408
2409void Parcel::closeFileDescriptors()
2410{
2411 size_t i = mObjectsSize;
2412 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002413 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 }
2415 while (i > 0) {
2416 i--;
2417 const flat_binder_object* flat
2418 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002419 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 close(flat->handle);
2422 }
2423 }
2424}
2425
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002426uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002428 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429}
2430
2431size_t Parcel::ipcDataSize() const
2432{
2433 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2434}
2435
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002436uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002438 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439}
2440
2441size_t Parcel::ipcObjectsCount() const
2442{
2443 return mObjectsSize;
2444}
2445
2446void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002447 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002448{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002449 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002450 freeDataNoInit();
2451 mError = NO_ERROR;
2452 mData = const_cast<uint8_t*>(data);
2453 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002454 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002457 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 mObjectsSize = mObjectsCapacity = objectsCount;
2459 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002460 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002461 mOwner = relFunc;
2462 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002463 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002464 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002465 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002466 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002467 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002468 mObjectsSize = 0;
2469 break;
2470 }
2471 minOffset = offset + sizeof(flat_binder_object);
2472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 scanForFds();
2474}
2475
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002476void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477{
2478 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 if (errorCheck() != NO_ERROR) {
2481 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002482 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483 } else if (dataSize() > 0) {
2484 const uint8_t* DATA = data();
2485 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002486 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 const size_t N = objectsCount();
2488 for (size_t i=0; i<N; i++) {
2489 const flat_binder_object* flat
2490 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2491 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002492 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 << " = " << flat->binder;
2494 }
2495 } else {
2496 to << "NULL";
2497 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 to << ")";
2500}
2501
2502void Parcel::releaseObjects()
2503{
2504 const sp<ProcessState> proc(ProcessState::self());
2505 size_t i = mObjectsSize;
2506 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002507 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 while (i > 0) {
2509 i--;
2510 const flat_binder_object* flat
2511 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002512 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 }
2514}
2515
2516void Parcel::acquireObjects()
2517{
2518 const sp<ProcessState> proc(ProcessState::self());
2519 size_t i = mObjectsSize;
2520 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002521 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 while (i > 0) {
2523 i--;
2524 const flat_binder_object* flat
2525 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002526 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 }
2528}
2529
2530void Parcel::freeData()
2531{
2532 freeDataNoInit();
2533 initState();
2534}
2535
2536void Parcel::freeDataNoInit()
2537{
2538 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002539 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002540 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2542 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002543 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002545 if (mData) {
2546 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002547 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002548 if (mDataCapacity <= gParcelGlobalAllocSize) {
2549 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2550 } else {
2551 gParcelGlobalAllocSize = 0;
2552 }
2553 if (gParcelGlobalAllocCount > 0) {
2554 gParcelGlobalAllocCount--;
2555 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002556 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002557 free(mData);
2558 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 if (mObjects) free(mObjects);
2560 }
2561}
2562
2563status_t Parcel::growData(size_t len)
2564{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002565 if (len > INT32_MAX) {
2566 // don't accept size_t values which may have come from an
2567 // inadvertent conversion from a negative int.
2568 return BAD_VALUE;
2569 }
2570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 size_t newSize = ((mDataSize+len)*3)/2;
2572 return (newSize <= mDataSize)
2573 ? (status_t) NO_MEMORY
2574 : continueWrite(newSize);
2575}
2576
2577status_t Parcel::restartWrite(size_t desired)
2578{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002579 if (desired > INT32_MAX) {
2580 // don't accept size_t values which may have come from an
2581 // inadvertent conversion from a negative int.
2582 return BAD_VALUE;
2583 }
2584
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585 if (mOwner) {
2586 freeData();
2587 return continueWrite(desired);
2588 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 uint8_t* data = (uint8_t*)realloc(mData, desired);
2591 if (!data && desired > mDataCapacity) {
2592 mError = NO_MEMORY;
2593 return NO_MEMORY;
2594 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002595
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002597
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002599 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002600 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002601 gParcelGlobalAllocSize += desired;
2602 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002603 if (!mData) {
2604 gParcelGlobalAllocCount++;
2605 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002606 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 mData = data;
2608 mDataCapacity = desired;
2609 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002610
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002612 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2613 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2614
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002616 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002617 mObjectsSize = mObjectsCapacity = 0;
2618 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002619 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 mHasFds = false;
2621 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002622 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624 return NO_ERROR;
2625}
2626
2627status_t Parcel::continueWrite(size_t desired)
2628{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002629 if (desired > INT32_MAX) {
2630 // don't accept size_t values which may have come from an
2631 // inadvertent conversion from a negative int.
2632 return BAD_VALUE;
2633 }
2634
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 // If shrinking, first adjust for any objects that appear
2636 // after the new data size.
2637 size_t objectsSize = mObjectsSize;
2638 if (desired < mDataSize) {
2639 if (desired == 0) {
2640 objectsSize = 0;
2641 } else {
2642 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002643 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 break;
2645 objectsSize--;
2646 }
2647 }
2648 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002649
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 if (mOwner) {
2651 // If the size is going to zero, just release the owner's data.
2652 if (desired == 0) {
2653 freeData();
2654 return NO_ERROR;
2655 }
2656
2657 // If there is a different owner, we need to take
2658 // posession.
2659 uint8_t* data = (uint8_t*)malloc(desired);
2660 if (!data) {
2661 mError = NO_MEMORY;
2662 return NO_MEMORY;
2663 }
Yi Kong91635562018-06-07 14:38:36 -07002664 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002665
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002667 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002669 free(data);
2670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 mError = NO_MEMORY;
2672 return NO_MEMORY;
2673 }
2674
2675 // Little hack to only acquire references on objects
2676 // we will be keeping.
2677 size_t oldObjectsSize = mObjectsSize;
2678 mObjectsSize = objectsSize;
2679 acquireObjects();
2680 mObjectsSize = oldObjectsSize;
2681 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002682
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002683 if (mData) {
2684 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2685 }
2686 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002687 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002689 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002690 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002691 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002693 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002694 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002695 gParcelGlobalAllocSize += desired;
2696 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002697 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002698
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 mData = data;
2700 mObjects = objects;
2701 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002702 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 mDataCapacity = desired;
2704 mObjectsSize = mObjectsCapacity = objectsSize;
2705 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002706 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002707
2708 } else if (mData) {
2709 if (objectsSize < mObjectsSize) {
2710 // Need to release refs on any objects we are dropping.
2711 const sp<ProcessState> proc(ProcessState::self());
2712 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2713 const flat_binder_object* flat
2714 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002715 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002716 // will need to rescan because we may have lopped off the only FDs
2717 mFdsKnown = false;
2718 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002719 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002720 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002721 binder_size_t* objects =
2722 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723 if (objects) {
2724 mObjects = objects;
2725 }
2726 mObjectsSize = objectsSize;
2727 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002728 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002729 }
2730
2731 // We own the data, so we can just do a realloc().
2732 if (desired > mDataCapacity) {
2733 uint8_t* data = (uint8_t*)realloc(mData, desired);
2734 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002735 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2736 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002737 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002738 gParcelGlobalAllocSize += desired;
2739 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002740 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 mData = data;
2742 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002743 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002744 mError = NO_MEMORY;
2745 return NO_MEMORY;
2746 }
2747 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002748 if (mDataSize > desired) {
2749 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002750 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002751 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002752 if (mDataPos > desired) {
2753 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002754 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 }
2756 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002757
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 } else {
2759 // This is the first data. Easy!
2760 uint8_t* data = (uint8_t*)malloc(desired);
2761 if (!data) {
2762 mError = NO_MEMORY;
2763 return NO_MEMORY;
2764 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002765
Yi Kong91635562018-06-07 14:38:36 -07002766 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002768 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002770
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002771 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002772 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002773 gParcelGlobalAllocSize += desired;
2774 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002775 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002776
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002777 mData = data;
2778 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002779 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2780 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002781 mDataCapacity = desired;
2782 }
2783
2784 return NO_ERROR;
2785}
2786
2787void Parcel::initState()
2788{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002789 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002791 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792 mDataSize = 0;
2793 mDataCapacity = 0;
2794 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002795 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2796 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002797 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 mObjectsSize = 0;
2799 mObjectsCapacity = 0;
2800 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002801 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 mHasFds = false;
2803 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002804 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002805 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002806 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002807
2808 // racing multiple init leads only to multiple identical write
2809 if (gMaxFds == 0) {
2810 struct rlimit result;
2811 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2812 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002813 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002814 } else {
2815 ALOGW("Unable to getrlimit: %s", strerror(errno));
2816 gMaxFds = 1024;
2817 }
2818 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002819}
2820
2821void Parcel::scanForFds() const
2822{
2823 bool hasFds = false;
2824 for (size_t i=0; i<mObjectsSize; i++) {
2825 const flat_binder_object* flat
2826 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002827 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002828 hasFds = true;
2829 break;
2830 }
2831 }
2832 mHasFds = hasFds;
2833 mFdsKnown = true;
2834}
2835
Dan Sandleraa5c2342015-04-10 10:08:45 -04002836size_t Parcel::getBlobAshmemSize() const
2837{
Adrian Roos6bb31142015-10-22 16:46:12 -07002838 // This used to return the size of all blobs that were written to ashmem, now we're returning
2839 // the ashmem currently referenced by this Parcel, which should be equivalent.
2840 // TODO: Remove method once ABI can be changed.
2841 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002842}
2843
Adrian Rooscbf37262015-10-22 16:12:53 -07002844size_t Parcel::getOpenAshmemSize() const
2845{
2846 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002847}
2848
2849// --- Parcel::Blob ---
2850
2851Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002852 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002853}
2854
2855Parcel::Blob::~Blob() {
2856 release();
2857}
2858
2859void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002860 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002861 ::munmap(mData, mSize);
2862 }
2863 clear();
2864}
2865
Jeff Brown13b16042014-11-11 16:44:25 -08002866void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2867 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002868 mData = data;
2869 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002870 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002871}
2872
2873void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002874 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002875 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002876 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002877 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002878}
2879
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880}; // namespace android