blob: ca6a97debb2ff9a168113e629b24b0c5a54f5f87 [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 -070079namespace android {
80
Dianne Hackborna4cff882014-11-13 17:07:40 -080081static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
82static size_t gParcelGlobalAllocSize = 0;
83static size_t gParcelGlobalAllocCount = 0;
84
Christopher Tatee4e0ae82016-03-24 16:03:44 -070085static size_t gMaxFds = 0;
86
Jeff Brown13b16042014-11-11 16:44:25 -080087// Maximum size of a blob to transfer in-place.
88static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
89
90enum {
91 BLOB_INPLACE = 0,
92 BLOB_ASHMEM_IMMUTABLE = 1,
93 BLOB_ASHMEM_MUTABLE = 2,
94};
95
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070096void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -070097 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070098{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -070099 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700100 case BINDER_TYPE_BINDER:
101 if (obj.binder) {
102 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800103 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 }
105 return;
106 case BINDER_TYPE_WEAK_BINDER:
107 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800108 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700109 return;
110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700112 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
118 case BINDER_TYPE_WEAK_HANDLE: {
119 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700120 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 case BINDER_TYPE_FD: {
Yi Kongfdd8da92018-06-07 17:52:27 -0700124 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700125 // If we own an ashmem fd, keep track of how much memory it refers to.
126 int size = ashmem_get_size_region(obj.handle);
127 if (size > 0) {
128 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700129 }
130 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 return;
132 }
133 }
134
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700135 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136}
137
Adrian Roos6bb31142015-10-22 16:46:12 -0700138void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 const flat_binder_object& obj, const void* who)
140{
Yi Kongfdd8da92018-06-07 17:52:27 -0700141 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700142}
143
144static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700145 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700147 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700148 case BINDER_TYPE_BINDER:
149 if (obj.binder) {
150 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800151 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 }
153 return;
154 case BINDER_TYPE_WEAK_BINDER:
155 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800156 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700157 return;
158 case BINDER_TYPE_HANDLE: {
159 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700160 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
162 b->decStrong(who);
163 }
164 return;
165 }
166 case BINDER_TYPE_WEAK_HANDLE: {
167 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kongfdd8da92018-06-07 17:52:27 -0700168 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 return;
170 }
171 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800172 if (obj.cookie != 0) { // owned
Yi Kongfdd8da92018-06-07 17:52:27 -0700173 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700174 int size = ashmem_get_size_region(obj.handle);
175 if (size > 0) {
Tri Voaa6e1112019-01-29 13:23:46 -0800176 // ashmem size might have changed since last time it was accounted for, e.g.
177 // in acquire_object(). Value of *outAshmemSize is not critical since we are
178 // releasing the object anyway. Check for integer overflow condition.
179 *outAshmemSize -= std::min(*outAshmemSize, static_cast<size_t>(size));
Adrian Roos6bb31142015-10-22 16:46:12 -0700180 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700181 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800182
183 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700184 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185 return;
186 }
187 }
188
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700189 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190}
191
Adrian Roos6bb31142015-10-22 16:46:12 -0700192void release_object(const sp<ProcessState>& proc,
193 const flat_binder_object& obj, const void* who)
194{
Yi Kongfdd8da92018-06-07 17:52:27 -0700195 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700196}
197
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700198inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800199 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700200{
201 return out->writeObject(flat, false);
202}
203
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205 const sp<IBinder>& binder, Parcel* out)
206{
207 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700208
Martijn Coenen2b631742017-05-05 11:16:59 -0700209 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
210 /* minimum priority for all nodes is nice 0 */
211 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
212 } else {
213 /* minimum priority for all nodes is MAX_NICE(19) */
214 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
215 }
216
Yi Kongfdd8da92018-06-07 17:52:27 -0700217 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218 IBinder *local = binder->localBinder();
219 if (!local) {
220 BpBinder *proxy = binder->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700221 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700225 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700227 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800228 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
232 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 }
234 } else {
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 = 0;
237 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 return finish_flatten_binder(binder, obj, out);
241}
242
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800243status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 const wp<IBinder>& binder, Parcel* out)
245{
246 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700247
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kongfdd8da92018-06-07 17:52:27 -0700249 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 sp<IBinder> real = binder.promote();
Yi Kongfdd8da92018-06-07 17:52:27 -0700251 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700252 IBinder *local = real->localBinder();
253 if (!local) {
254 BpBinder *proxy = real->remoteBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700255 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000256 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700259 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800260 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800262 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700263 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800265 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
266 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700267 }
268 return finish_flatten_binder(real, obj, out);
269 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700270
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700271 // XXX How to deal? In order to flatten the given binder,
272 // we need to probe it for information, which requires a primary
273 // reference... but we don't have one.
274 //
275 // The OpenBinder implementation uses a dynamic_cast<> here,
276 // but we can't do that with the different reference counting
277 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000278 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700279 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -0700282 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700285 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800286 obj.binder = 0;
287 obj.cookie = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -0700288 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 }
290}
291
292inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800293 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
294 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295{
296 return NO_ERROR;
297}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299status_t unflatten_binder(const sp<ProcessState>& proc,
300 const Parcel& in, sp<IBinder>* out)
301{
302 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700305 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700306 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800307 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kongfdd8da92018-06-07 17:52:27 -0700308 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 case BINDER_TYPE_HANDLE:
310 *out = proc->getStrongProxyForHandle(flat->handle);
311 return finish_unflatten_binder(
312 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700313 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 }
315 return BAD_TYPE;
316}
317
318status_t unflatten_binder(const sp<ProcessState>& proc,
319 const Parcel& in, wp<IBinder>* out)
320{
321 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700322
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700323 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700324 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800326 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kongfdd8da92018-06-07 17:52:27 -0700327 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800329 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 reinterpret_cast<IBinder*>(flat->cookie),
332 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -0700334 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700336 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337 case BINDER_TYPE_HANDLE:
338 case BINDER_TYPE_WEAK_HANDLE:
339 *out = proc->getWeakProxyForHandle(flat->handle);
340 return finish_unflatten_binder(
341 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
342 }
343 }
344 return BAD_TYPE;
345}
346
347// ---------------------------------------------------------------------------
348
349Parcel::Parcel()
350{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800351 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700352 initState();
353}
354
355Parcel::~Parcel()
356{
357 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800358 LOG_ALLOC("Parcel %p: destroyed", this);
359}
360
361size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800362 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
363 size_t size = gParcelGlobalAllocSize;
364 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
365 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800366}
367
368size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800369 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
370 size_t count = gParcelGlobalAllocCount;
371 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
372 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700373}
374
375const uint8_t* Parcel::data() const
376{
377 return mData;
378}
379
380size_t Parcel::dataSize() const
381{
382 return (mDataSize > mDataPos ? mDataSize : mDataPos);
383}
384
385size_t Parcel::dataAvail() const
386{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700387 size_t result = dataSize() - dataPosition();
388 if (result > INT32_MAX) {
389 abort();
390 }
391 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392}
393
394size_t Parcel::dataPosition() const
395{
396 return mDataPos;
397}
398
399size_t Parcel::dataCapacity() const
400{
401 return mDataCapacity;
402}
403
404status_t Parcel::setDataSize(size_t size)
405{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700406 if (size > INT32_MAX) {
407 // don't accept size_t values which may have come from an
408 // inadvertent conversion from a negative int.
409 return BAD_VALUE;
410 }
411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 status_t err;
413 err = continueWrite(size);
414 if (err == NO_ERROR) {
415 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700416 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 }
418 return err;
419}
420
421void Parcel::setDataPosition(size_t pos) const
422{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700423 if (pos > INT32_MAX) {
424 // don't accept size_t values which may have come from an
425 // inadvertent conversion from a negative int.
426 abort();
427 }
428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 mDataPos = pos;
430 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800431 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432}
433
434status_t Parcel::setDataCapacity(size_t size)
435{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700436 if (size > INT32_MAX) {
437 // don't accept size_t values which may have come from an
438 // inadvertent conversion from a negative int.
439 return BAD_VALUE;
440 }
441
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700442 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700443 return NO_ERROR;
444}
445
446status_t Parcel::setData(const uint8_t* buffer, size_t len)
447{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700448 if (len > INT32_MAX) {
449 // don't accept size_t values which may have come from an
450 // inadvertent conversion from a negative int.
451 return BAD_VALUE;
452 }
453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 status_t err = restartWrite(len);
455 if (err == NO_ERROR) {
456 memcpy(const_cast<uint8_t*>(data()), buffer, len);
457 mDataSize = len;
458 mFdsKnown = false;
459 }
460 return err;
461}
462
Andreas Huber51faf462011-04-13 10:21:56 -0700463status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700466 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800467 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468 size_t size = parcel->mObjectsSize;
469 int startPos = mDataPos;
470 int firstIndex = -1, lastIndex = -2;
471
472 if (len == 0) {
473 return NO_ERROR;
474 }
475
Nick Kralevichb6b14232015-04-02 09:36:02 -0700476 if (len > INT32_MAX) {
477 // don't accept size_t values which may have come from an
478 // inadvertent conversion from a negative int.
479 return BAD_VALUE;
480 }
481
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 // range checks against the source parcel size
483 if ((offset > parcel->mDataSize)
484 || (len > parcel->mDataSize)
485 || (offset + len > parcel->mDataSize)) {
486 return BAD_VALUE;
487 }
488
489 // Count objects in range
490 for (int i = 0; i < (int) size; i++) {
491 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700492 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 if (firstIndex == -1) {
494 firstIndex = i;
495 }
496 lastIndex = i;
497 }
498 }
499 int numObjects = lastIndex - firstIndex + 1;
500
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700501 if ((mDataSize+len) > mDataCapacity) {
502 // grow data
503 err = growData(len);
504 if (err != NO_ERROR) {
505 return err;
506 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 }
508
509 // append data
510 memcpy(mData + mDataPos, data + offset, len);
511 mDataPos += len;
512 mDataSize += len;
513
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400514 err = NO_ERROR;
515
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700516 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200517 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 // grow objects
519 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700520 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700521 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800522 binder_size_t *objects =
523 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -0700524 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700525 return NO_MEMORY;
526 }
527 mObjects = objects;
528 mObjectsCapacity = newSize;
529 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531 // append and acquire objects
532 int idx = mObjectsSize;
533 for (int i = firstIndex; i <= lastIndex; i++) {
534 size_t off = objects[i] - offset + startPos;
535 mObjects[idx++] = off;
536 mObjectsSize++;
537
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700538 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700539 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700540 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700542 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 // If this is a file descriptor, we need to dup it so the
544 // new Parcel now owns its own fd, and can declare that we
545 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800546 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800547 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700548 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549 if (!mAllowFds) {
550 err = FDS_NOT_ALLOWED;
551 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700552 }
553 }
554 }
555
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400556 return err;
557}
558
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700559int Parcel::compareData(const Parcel& other) {
560 size_t size = dataSize();
561 if (size != other.dataSize()) {
562 return size < other.dataSize() ? -1 : 1;
563 }
564 return memcmp(data(), other.data(), size);
565}
566
Jeff Brown13b16042014-11-11 16:44:25 -0800567bool Parcel::allowFds() const
568{
569 return mAllowFds;
570}
571
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700572bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400573{
574 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700575 if (!allowFds) {
576 mAllowFds = false;
577 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700579}
580
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700581void Parcel::restoreAllowFds(bool lastValue)
582{
583 mAllowFds = lastValue;
584}
585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700586bool Parcel::hasFileDescriptors() const
587{
588 if (!mFdsKnown) {
589 scanForFds();
590 }
591 return mHasFds;
592}
593
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700594// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595status_t Parcel::writeInterfaceToken(const String16& interface)
596{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700597 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
598 STRICT_MODE_PENALTY_GATHER);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700599 // currently the interface identification token is just its name as a string
600 return writeString16(interface);
601}
602
Mathias Agopian83c04462009-05-22 19:00:22 -0700603bool Parcel::checkInterface(IBinder* binder) const
604{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700605 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700606}
607
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700608bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700609 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700610{
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700611 int32_t strictPolicy = readInt32();
Yi Kongfdd8da92018-06-07 17:52:27 -0700612 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700613 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700614 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700615 if ((threadState->getLastTransactionBinderFlags() &
616 IBinder::FLAG_ONEWAY) != 0) {
617 // For one-way calls, the callee is running entirely
618 // disconnected from the caller, so disable StrictMode entirely.
619 // Not only does disk/network usage not impact the caller, but
620 // there's no way to commuicate back any violations anyway.
621 threadState->setStrictModePolicy(0);
622 } else {
623 threadState->setStrictModePolicy(strictPolicy);
624 }
Mathias Agopian83c04462009-05-22 19:00:22 -0700625 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700626 if (str == interface) {
627 return true;
628 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700629 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700630 String8(interface).string(), String8(str).string());
631 return false;
632 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700633}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800635const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636{
637 return mObjects;
638}
639
640size_t Parcel::objectsCount() const
641{
642 return mObjectsSize;
643}
644
645status_t Parcel::errorCheck() const
646{
647 return mError;
648}
649
650void Parcel::setError(status_t err)
651{
652 mError = err;
653}
654
655status_t Parcel::finishWrite(size_t len)
656{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700657 if (len > INT32_MAX) {
658 // don't accept size_t values which may have come from an
659 // inadvertent conversion from a negative int.
660 return BAD_VALUE;
661 }
662
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700663 //printf("Finish write of %d\n", len);
664 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700665 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700666 if (mDataPos > mDataSize) {
667 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700668 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700669 }
670 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
671 return NO_ERROR;
672}
673
674status_t Parcel::writeUnpadded(const void* data, size_t len)
675{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700676 if (len > INT32_MAX) {
677 // don't accept size_t values which may have come from an
678 // inadvertent conversion from a negative int.
679 return BAD_VALUE;
680 }
681
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700682 size_t end = mDataPos + len;
683 if (end < mDataPos) {
684 // integer overflow
685 return BAD_VALUE;
686 }
687
688 if (end <= mDataCapacity) {
689restart_write:
690 memcpy(mData+mDataPos, data, len);
691 return finishWrite(len);
692 }
693
694 status_t err = growData(len);
695 if (err == NO_ERROR) goto restart_write;
696 return err;
697}
698
699status_t Parcel::write(const void* data, size_t len)
700{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700701 if (len > INT32_MAX) {
702 // don't accept size_t values which may have come from an
703 // inadvertent conversion from a negative int.
704 return BAD_VALUE;
705 }
706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700707 void* const d = writeInplace(len);
708 if (d) {
709 memcpy(d, data, len);
710 return NO_ERROR;
711 }
712 return mError;
713}
714
715void* Parcel::writeInplace(size_t len)
716{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700717 if (len > INT32_MAX) {
718 // don't accept size_t values which may have come from an
719 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -0700720 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700721 }
722
723 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724
725 // sanity check for integer overflow
726 if (mDataPos+padded < mDataPos) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700727 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700728 }
729
730 if ((mDataPos+padded) <= mDataCapacity) {
731restart_write:
732 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
733 uint8_t* const data = mData+mDataPos;
734
735 // Need to pad at end?
736 if (padded != len) {
737#if BYTE_ORDER == BIG_ENDIAN
738 static const uint32_t mask[4] = {
739 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
740 };
741#endif
742#if BYTE_ORDER == LITTLE_ENDIAN
743 static const uint32_t mask[4] = {
744 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
745 };
746#endif
747 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
748 // *reinterpret_cast<void**>(data+padded-4));
749 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
750 }
751
752 finishWrite(padded);
753 return data;
754 }
755
756 status_t err = growData(padded);
757 if (err == NO_ERROR) goto restart_write;
Yi Kongfdd8da92018-06-07 17:52:27 -0700758 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700759}
760
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800761status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
762 const uint8_t* strData = (uint8_t*)str.data();
763 const size_t strLen= str.length();
764 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100765 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800766 return BAD_VALUE;
767 }
768
769 status_t err = writeInt32(utf16Len);
770 if (err) {
771 return err;
772 }
773
774 // Allocate enough bytes to hold our converted string and its terminating NULL.
775 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
776 if (!dst) {
777 return NO_MEMORY;
778 }
779
Sergio Girof4607432016-07-21 14:46:35 +0100780 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800781
782 return NO_ERROR;
783}
784
785status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
786 if (!str) {
787 return writeInt32(-1);
788 }
789 return writeUtf8AsUtf16(*str);
790}
791
Casey Dahlin185d3442016-02-09 11:08:35 -0800792namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800793
Casey Dahlin185d3442016-02-09 11:08:35 -0800794template<typename T>
795status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700796{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700797 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700798 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700799 status = BAD_VALUE;
800 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700801 }
802
Casey Dahlin185d3442016-02-09 11:08:35 -0800803 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700804 if (status != OK) {
805 return status;
806 }
807
Casey Dahlin185d3442016-02-09 11:08:35 -0800808 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700809 if (!data) {
810 status = BAD_VALUE;
811 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700812 }
813
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700814 memcpy(data, val.data(), val.size());
815 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700816}
817
Casey Dahlin185d3442016-02-09 11:08:35 -0800818template<typename T>
819status_t writeByteVectorInternalPtr(Parcel* parcel,
820 const std::unique_ptr<std::vector<T>>& val)
821{
822 if (!val) {
823 return parcel->writeInt32(-1);
824 }
825
826 return writeByteVectorInternal(parcel, *val);
827}
828
829} // namespace
830
831status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
832 return writeByteVectorInternal(this, val);
833}
834
835status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
836{
837 return writeByteVectorInternalPtr(this, val);
838}
839
840status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
841 return writeByteVectorInternal(this, val);
842}
843
844status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
845{
846 return writeByteVectorInternalPtr(this, val);
847}
848
Casey Dahlin451ff582015-10-19 18:12:18 -0700849status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
850{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800851 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700852}
853
Casey Dahlinb9872622015-11-25 15:09:45 -0800854status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
855{
856 return writeNullableTypedVector(val, &Parcel::writeInt32);
857}
858
Casey Dahlin451ff582015-10-19 18:12:18 -0700859status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
860{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800861 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700862}
863
Casey Dahlinb9872622015-11-25 15:09:45 -0800864status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
865{
866 return writeNullableTypedVector(val, &Parcel::writeInt64);
867}
868
Casey Dahlin451ff582015-10-19 18:12:18 -0700869status_t Parcel::writeFloatVector(const std::vector<float>& val)
870{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800871 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700872}
873
Casey Dahlinb9872622015-11-25 15:09:45 -0800874status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
875{
876 return writeNullableTypedVector(val, &Parcel::writeFloat);
877}
878
Casey Dahlin451ff582015-10-19 18:12:18 -0700879status_t Parcel::writeDoubleVector(const std::vector<double>& val)
880{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800881 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700882}
883
Casey Dahlinb9872622015-11-25 15:09:45 -0800884status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
885{
886 return writeNullableTypedVector(val, &Parcel::writeDouble);
887}
888
Casey Dahlin451ff582015-10-19 18:12:18 -0700889status_t Parcel::writeBoolVector(const std::vector<bool>& val)
890{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800891 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700892}
893
Casey Dahlinb9872622015-11-25 15:09:45 -0800894status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
895{
896 return writeNullableTypedVector(val, &Parcel::writeBool);
897}
898
Casey Dahlin451ff582015-10-19 18:12:18 -0700899status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
900{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800901 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700902}
903
Casey Dahlinb9872622015-11-25 15:09:45 -0800904status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
905{
906 return writeNullableTypedVector(val, &Parcel::writeChar);
907}
908
Casey Dahlin451ff582015-10-19 18:12:18 -0700909status_t Parcel::writeString16Vector(const std::vector<String16>& val)
910{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800911 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700912}
913
Casey Dahlinb9872622015-11-25 15:09:45 -0800914status_t Parcel::writeString16Vector(
915 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
916{
917 return writeNullableTypedVector(val, &Parcel::writeString16);
918}
919
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800920status_t Parcel::writeUtf8VectorAsUtf16Vector(
921 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
922 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
923}
924
925status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
926 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
927}
928
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700929status_t Parcel::writeInt32(int32_t val)
930{
Andreas Huber84a6d042009-08-17 13:33:27 -0700931 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800933
934status_t Parcel::writeUint32(uint32_t val)
935{
936 return writeAligned(val);
937}
938
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700939status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700940 if (len > INT32_MAX) {
941 // don't accept size_t values which may have come from an
942 // inadvertent conversion from a negative int.
943 return BAD_VALUE;
944 }
945
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700946 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700947 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700948 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700949 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700950 if (ret == NO_ERROR) {
951 ret = write(val, len * sizeof(*val));
952 }
953 return ret;
954}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700955status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700956 if (len > INT32_MAX) {
957 // don't accept size_t values which may have come from an
958 // inadvertent conversion from a negative int.
959 return BAD_VALUE;
960 }
961
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700962 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700963 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700964 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700965 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700966 if (ret == NO_ERROR) {
967 ret = write(val, len * sizeof(*val));
968 }
969 return ret;
970}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700971
Casey Dahlind6848f52015-10-15 15:44:59 -0700972status_t Parcel::writeBool(bool val)
973{
974 return writeInt32(int32_t(val));
975}
976
977status_t Parcel::writeChar(char16_t val)
978{
979 return writeInt32(int32_t(val));
980}
981
982status_t Parcel::writeByte(int8_t val)
983{
984 return writeInt32(int32_t(val));
985}
986
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987status_t Parcel::writeInt64(int64_t val)
988{
Andreas Huber84a6d042009-08-17 13:33:27 -0700989 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700990}
991
Ronghua Wu2d13afd2015-03-16 11:11:07 -0700992status_t Parcel::writeUint64(uint64_t val)
993{
994 return writeAligned(val);
995}
996
Serban Constantinescuf683e012013-11-05 16:53:55 +0000997status_t Parcel::writePointer(uintptr_t val)
998{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800999 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001000}
1001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001002status_t Parcel::writeFloat(float val)
1003{
Andreas Huber84a6d042009-08-17 13:33:27 -07001004 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005}
1006
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001007#if defined(__mips__) && defined(__mips_hard_float)
1008
1009status_t Parcel::writeDouble(double val)
1010{
1011 union {
1012 double d;
1013 unsigned long long ll;
1014 } u;
1015 u.d = val;
1016 return writeAligned(u.ll);
1017}
1018
1019#else
1020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021status_t Parcel::writeDouble(double val)
1022{
Andreas Huber84a6d042009-08-17 13:33:27 -07001023 return writeAligned(val);
1024}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001026#endif
1027
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028status_t Parcel::writeCString(const char* str)
1029{
1030 return write(str, strlen(str)+1);
1031}
1032
1033status_t Parcel::writeString8(const String8& str)
1034{
1035 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001036 // only write string if its length is more than zero characters,
1037 // as readString8 will only read if the length field is non-zero.
1038 // this is slightly different from how writeString16 works.
1039 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040 err = write(str.string(), str.bytes()+1);
1041 }
1042 return err;
1043}
1044
Casey Dahlinb9872622015-11-25 15:09:45 -08001045status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1046{
1047 if (!str) {
1048 return writeInt32(-1);
1049 }
1050
1051 return writeString16(*str);
1052}
1053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054status_t Parcel::writeString16(const String16& str)
1055{
1056 return writeString16(str.string(), str.size());
1057}
1058
1059status_t Parcel::writeString16(const char16_t* str, size_t len)
1060{
Yi Kongfdd8da92018-06-07 17:52:27 -07001061 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001062
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001063 status_t err = writeInt32(len);
1064 if (err == NO_ERROR) {
1065 len *= sizeof(char16_t);
1066 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1067 if (data) {
1068 memcpy(data, str, len);
1069 *reinterpret_cast<char16_t*>(data+len) = 0;
1070 return NO_ERROR;
1071 }
1072 err = mError;
1073 }
1074 return err;
1075}
1076
1077status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1078{
1079 return flatten_binder(ProcessState::self(), val, this);
1080}
1081
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001082status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1083{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001084 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001085}
1086
Casey Dahlinb9872622015-11-25 15:09:45 -08001087status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1088{
1089 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1090}
1091
1092status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001093 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001094}
1095
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001096status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001097 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001098}
1099
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1101{
1102 return flatten_binder(ProcessState::self(), val, this);
1103}
1104
Casey Dahlinb9872622015-11-25 15:09:45 -08001105status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1106 if (!parcelable) {
1107 return writeInt32(0);
1108 }
1109
1110 return writeParcelable(*parcelable);
1111}
1112
Christopher Wiley97f048d2015-11-19 06:49:05 -08001113status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1114 status_t status = writeInt32(1); // parcelable is not null.
1115 if (status != OK) {
1116 return status;
1117 }
1118 return parcelable.writeToParcel(this);
1119}
1120
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001121status_t Parcel::writeValue(const binder::Value& value) {
1122 return value.writeToParcel(this);
1123}
1124
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001125status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001126{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001127 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001128 return BAD_TYPE;
1129
1130 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001131 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001132 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001134 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001135 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001137 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1138 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139
1140 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001141 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 return err;
1143 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001144 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001145 return err;
1146}
1147
Jeff Brown93ff1f92011-11-04 19:01:44 -07001148status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001149{
1150 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001151 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001152 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001153 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001154 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001155 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156 return writeObject(obj, true);
1157}
1158
1159status_t Parcel::writeDupFileDescriptor(int fd)
1160{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001161 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001162 if (dupFd < 0) {
1163 return -errno;
1164 }
1165 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001166 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001167 close(dupFd);
1168 }
1169 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001170}
1171
Dianne Hackborn1941a402016-08-29 12:30:43 -07001172status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1173{
1174 writeInt32(0);
1175 return writeFileDescriptor(fd, takeOwnership);
1176}
1177
Ryo Hashimotobf551892018-05-31 16:58:35 +09001178status_t Parcel::writeDupParcelFileDescriptor(int fd)
1179{
1180 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1181 if (dupFd < 0) {
1182 return -errno;
1183 }
1184 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1185 if (err != OK) {
1186 close(dupFd);
1187 }
1188 return err;
1189}
1190
Christopher Wiley2cf19952016-04-11 11:09:37 -07001191status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001192 return writeDupFileDescriptor(fd.get());
1193}
1194
Christopher Wiley2cf19952016-04-11 11:09:37 -07001195status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001196 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1197}
1198
Christopher Wiley2cf19952016-04-11 11:09:37 -07001199status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001200 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1201}
1202
Jeff Brown13b16042014-11-11 16:44:25 -08001203status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001204{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001205 if (len > INT32_MAX) {
1206 // don't accept size_t values which may have come from an
1207 // inadvertent conversion from a negative int.
1208 return BAD_VALUE;
1209 }
1210
Jeff Brown13b16042014-11-11 16:44:25 -08001211 status_t status;
1212 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001213 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001214 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215 if (status) return status;
1216
1217 void* ptr = writeInplace(len);
1218 if (!ptr) return NO_MEMORY;
1219
Jeff Brown13b16042014-11-11 16:44:25 -08001220 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001221 return NO_ERROR;
1222 }
1223
Steve Block6807e592011-10-20 11:56:00 +01001224 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001225 int fd = ashmem_create_region("Parcel Blob", len);
1226 if (fd < 0) return NO_MEMORY;
1227
1228 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1229 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001230 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001231 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -07001232 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001233 if (ptr == MAP_FAILED) {
1234 status = -errno;
1235 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001236 if (!mutableCopy) {
1237 result = ashmem_set_prot_region(fd, PROT_READ);
1238 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001240 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001241 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001242 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001243 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001244 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001246 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 return NO_ERROR;
1248 }
1249 }
1250 }
1251 }
1252 ::munmap(ptr, len);
1253 }
1254 ::close(fd);
1255 return status;
1256}
1257
Jeff Brown13b16042014-11-11 16:44:25 -08001258status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1259{
1260 // Must match up with what's done in writeBlob.
1261 if (!mAllowFds) return FDS_NOT_ALLOWED;
1262 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1263 if (status) return status;
1264 return writeDupFileDescriptor(fd);
1265}
1266
Mathias Agopiane1424282013-07-29 21:24:40 -07001267status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001268{
1269 status_t err;
1270
1271 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001272 const size_t len = val.getFlattenedSize();
1273 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001274
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001275 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001276 // don't accept size_t values which may have come from an
1277 // inadvertent conversion from a negative int.
1278 return BAD_VALUE;
1279 }
1280
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001281 err = this->writeInt32(len);
1282 if (err) return err;
1283
1284 err = this->writeInt32(fd_count);
1285 if (err) return err;
1286
1287 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001288 void* const buf = this->writeInplace(len);
Yi Kongfdd8da92018-06-07 17:52:27 -07001289 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001290 return BAD_VALUE;
1291
Yi Kongfdd8da92018-06-07 17:52:27 -07001292 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001293 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001294 fds = new (std::nothrow) int[fd_count];
1295 if (fds == nullptr) {
1296 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1297 return BAD_VALUE;
1298 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001299 }
1300
1301 err = val.flatten(buf, len, fds, fd_count);
1302 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1303 err = this->writeDupFileDescriptor( fds[i] );
1304 }
1305
1306 if (fd_count) {
1307 delete [] fds;
1308 }
1309
1310 return err;
1311}
1312
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001313status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1314{
1315 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1316 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1317 if (enoughData && enoughObjects) {
1318restart_write:
1319 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001320
Christopher Tate98e67d32015-06-03 18:44:15 -07001321 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001322 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001323 if (!mAllowFds) {
1324 // fail before modifying our object index
1325 return FDS_NOT_ALLOWED;
1326 }
1327 mHasFds = mFdsKnown = true;
1328 }
1329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001331 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001333 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334 mObjectsSize++;
1335 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001336
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337 return finishWrite(sizeof(flat_binder_object));
1338 }
1339
1340 if (!enoughData) {
1341 const status_t err = growData(sizeof(val));
1342 if (err != NO_ERROR) return err;
1343 }
1344 if (!enoughObjects) {
1345 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001346 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001347 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07001348 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349 mObjects = objects;
1350 mObjectsCapacity = newSize;
1351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001353 goto restart_write;
1354}
1355
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001356status_t Parcel::writeNoException()
1357{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001358 binder::Status status;
1359 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001360}
1361
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001362status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1363{
1364 using ::std::map;
1365 using ::android::binder::Value;
1366 using ::android::binder::Map;
1367
1368 Map::const_iterator iter;
1369 status_t ret;
1370
1371 ret = writeInt32(map_in.size());
1372
1373 if (ret != NO_ERROR) {
1374 return ret;
1375 }
1376
1377 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1378 ret = writeValue(Value(iter->first));
1379 if (ret != NO_ERROR) {
1380 return ret;
1381 }
1382
1383 ret = writeValue(iter->second);
1384 if (ret != NO_ERROR) {
1385 return ret;
1386 }
1387 }
1388
1389 return ret;
1390}
1391
1392status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1393{
Yi Kongfdd8da92018-06-07 17:52:27 -07001394 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001395 return writeInt32(-1);
1396 }
1397
1398 return writeMap(*map.get());
1399}
1400
1401status_t Parcel::readMap(::android::binder::Map* map_out)const
1402{
1403 using ::std::map;
1404 using ::android::String16;
1405 using ::android::String8;
1406 using ::android::binder::Value;
1407 using ::android::binder::Map;
1408
1409 status_t ret = NO_ERROR;
1410 int32_t count;
1411
1412 ret = readInt32(&count);
1413 if (ret != NO_ERROR) {
1414 return ret;
1415 }
1416
1417 if (count < 0) {
1418 ALOGE("readMap: Unexpected count: %d", count);
1419 return (count == -1)
1420 ? UNEXPECTED_NULL
1421 : BAD_VALUE;
1422 }
1423
1424 map_out->clear();
1425
1426 while (count--) {
1427 Map::key_type key;
1428 Value value;
1429
1430 ret = readValue(&value);
1431 if (ret != NO_ERROR) {
1432 return ret;
1433 }
1434
1435 if (!value.getString(&key)) {
1436 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1437 return BAD_VALUE;
1438 }
1439
1440 ret = readValue(&value);
1441 if (ret != NO_ERROR) {
1442 return ret;
1443 }
1444
1445 (*map_out)[key] = value;
1446 }
1447
1448 return ret;
1449}
1450
1451status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1452{
1453 const size_t start = dataPosition();
1454 int32_t count;
1455 status_t status = readInt32(&count);
1456 map->reset();
1457
1458 if (status != OK || count == -1) {
1459 return status;
1460 }
1461
1462 setDataPosition(start);
1463 map->reset(new binder::Map());
1464
1465 status = readMap(map->get());
1466
1467 if (status != OK) {
1468 map->reset();
1469 }
1470
1471 return status;
1472}
1473
1474
1475
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001476void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477{
1478 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1479}
1480
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001481status_t Parcel::validateReadData(size_t upperBound) const
1482{
1483 // Don't allow non-object reads on object data
1484 if (mObjectsSorted || mObjectsSize <= 1) {
1485data_sorted:
1486 // Expect to check only against the next object
1487 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1488 // For some reason the current read position is greater than the next object
1489 // hint. Iterate until we find the right object
1490 size_t nextObject = mNextObjectHint;
1491 do {
1492 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1493 // Requested info overlaps with an object
1494 ALOGE("Attempt to read from protected data in Parcel %p", this);
1495 return PERMISSION_DENIED;
1496 }
1497 nextObject++;
1498 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1499 mNextObjectHint = nextObject;
1500 }
1501 return NO_ERROR;
1502 }
1503 // Quickly determine if mObjects is sorted.
1504 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1505 binder_size_t* prevObj = currObj;
1506 while (currObj > mObjects) {
1507 prevObj--;
1508 if(*prevObj > *currObj) {
1509 goto data_unsorted;
1510 }
1511 currObj--;
1512 }
1513 mObjectsSorted = true;
1514 goto data_sorted;
1515
1516data_unsorted:
1517 // Insertion Sort mObjects
1518 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1519 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1520 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1521 binder_size_t temp = *iter0;
1522 binder_size_t* iter1 = iter0 - 1;
1523 while (iter1 >= mObjects && *iter1 > temp) {
1524 *(iter1 + 1) = *iter1;
1525 iter1--;
1526 }
1527 *(iter1 + 1) = temp;
1528 }
1529 mNextObjectHint = 0;
1530 mObjectsSorted = true;
1531 goto data_sorted;
1532}
1533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001534status_t Parcel::read(void* outData, size_t len) const
1535{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001536 if (len > INT32_MAX) {
1537 // don't accept size_t values which may have come from an
1538 // inadvertent conversion from a negative int.
1539 return BAD_VALUE;
1540 }
1541
1542 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1543 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001544 if (mObjectsSize > 0) {
1545 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001546 if(err != NO_ERROR) {
1547 // Still increment the data position by the expected length
1548 mDataPos += pad_size(len);
1549 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1550 return err;
1551 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001552 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001554 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001555 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556 return NO_ERROR;
1557 }
1558 return NOT_ENOUGH_DATA;
1559}
1560
1561const void* Parcel::readInplace(size_t len) const
1562{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001563 if (len > INT32_MAX) {
1564 // don't accept size_t values which may have come from an
1565 // inadvertent conversion from a negative int.
Yi Kongfdd8da92018-06-07 17:52:27 -07001566 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001567 }
1568
1569 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1570 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001571 if (mObjectsSize > 0) {
1572 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001573 if(err != NO_ERROR) {
1574 // Still increment the data position by the expected length
1575 mDataPos += pad_size(len);
1576 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Xin Lif11e2bd2018-06-08 15:11:57 -07001577 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001578 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001579 }
1580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001582 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001583 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 return data;
1585 }
Yi Kongfdd8da92018-06-07 17:52:27 -07001586 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001587}
1588
Andreas Huber84a6d042009-08-17 13:33:27 -07001589template<class T>
1590status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001591 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001592
1593 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001594 if (mObjectsSize > 0) {
1595 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001596 if(err != NO_ERROR) {
1597 // Still increment the data position by the expected length
1598 mDataPos += sizeof(T);
1599 return err;
1600 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001601 }
1602
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001604 mDataPos += sizeof(T);
1605 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001606 return NO_ERROR;
1607 } else {
1608 return NOT_ENOUGH_DATA;
1609 }
1610}
1611
Andreas Huber84a6d042009-08-17 13:33:27 -07001612template<class T>
1613T Parcel::readAligned() const {
1614 T result;
1615 if (readAligned(&result) != NO_ERROR) {
1616 result = 0;
1617 }
1618
1619 return result;
1620}
1621
1622template<class T>
1623status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001624 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001625
1626 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1627restart_write:
1628 *reinterpret_cast<T*>(mData+mDataPos) = val;
1629 return finishWrite(sizeof(val));
1630 }
1631
1632 status_t err = growData(sizeof(val));
1633 if (err == NO_ERROR) goto restart_write;
1634 return err;
1635}
1636
Casey Dahlin185d3442016-02-09 11:08:35 -08001637namespace {
1638
1639template<typename T>
1640status_t readByteVectorInternal(const Parcel* parcel,
1641 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001642 val->clear();
1643
1644 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001645 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001646
1647 if (status != OK) {
1648 return status;
1649 }
1650
Christopher Wiley4db672d2015-11-10 09:44:30 -08001651 if (size < 0) {
1652 status = UNEXPECTED_NULL;
1653 return status;
1654 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001655 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001656 status = BAD_VALUE;
1657 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001658 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001659
Paul Lietar433e87b2016-09-16 10:39:32 -07001660 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001661 if (!data) {
1662 status = BAD_VALUE;
1663 return status;
1664 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001665 val->reserve(size);
1666 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001667
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001668 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001669}
1670
Casey Dahlin185d3442016-02-09 11:08:35 -08001671template<typename T>
1672status_t readByteVectorInternalPtr(
1673 const Parcel* parcel,
1674 std::unique_ptr<std::vector<T>>* val) {
1675 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001676 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001677 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001678 val->reset();
1679
1680 if (status != OK || size < 0) {
1681 return status;
1682 }
1683
Casey Dahlin185d3442016-02-09 11:08:35 -08001684 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001685 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001686
Casey Dahlin185d3442016-02-09 11:08:35 -08001687 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001688
1689 if (status != OK) {
1690 val->reset();
1691 }
1692
1693 return status;
1694}
1695
Casey Dahlin185d3442016-02-09 11:08:35 -08001696} // namespace
1697
1698status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1699 return readByteVectorInternal(this, val);
1700}
1701
1702status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1703 return readByteVectorInternal(this, val);
1704}
1705
1706status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1707 return readByteVectorInternalPtr(this, val);
1708}
1709
1710status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1711 return readByteVectorInternalPtr(this, val);
1712}
1713
Casey Dahlinb9872622015-11-25 15:09:45 -08001714status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1715 return readNullableTypedVector(val, &Parcel::readInt32);
1716}
1717
Casey Dahlin451ff582015-10-19 18:12:18 -07001718status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001719 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001720}
1721
Casey Dahlinb9872622015-11-25 15:09:45 -08001722status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1723 return readNullableTypedVector(val, &Parcel::readInt64);
1724}
1725
Casey Dahlin451ff582015-10-19 18:12:18 -07001726status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001727 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001728}
1729
Casey Dahlinb9872622015-11-25 15:09:45 -08001730status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1731 return readNullableTypedVector(val, &Parcel::readFloat);
1732}
1733
Casey Dahlin451ff582015-10-19 18:12:18 -07001734status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001735 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001736}
1737
Casey Dahlinb9872622015-11-25 15:09:45 -08001738status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1739 return readNullableTypedVector(val, &Parcel::readDouble);
1740}
1741
Casey Dahlin451ff582015-10-19 18:12:18 -07001742status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001743 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001744}
1745
Casey Dahlinb9872622015-11-25 15:09:45 -08001746status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1747 const int32_t start = dataPosition();
1748 int32_t size;
1749 status_t status = readInt32(&size);
1750 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001751
Casey Dahlinb9872622015-11-25 15:09:45 -08001752 if (status != OK || size < 0) {
1753 return status;
1754 }
1755
1756 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001757 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001758
1759 status = readBoolVector(val->get());
1760
1761 if (status != OK) {
1762 val->reset();
1763 }
1764
1765 return status;
1766}
1767
1768status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001769 int32_t size;
1770 status_t status = readInt32(&size);
1771
1772 if (status != OK) {
1773 return status;
1774 }
1775
1776 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001777 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001778 }
1779
1780 val->resize(size);
1781
1782 /* C++ bool handling means a vector of bools isn't necessarily addressable
1783 * (we might use individual bits)
1784 */
Christopher Wiley97887982015-10-27 16:33:47 -07001785 bool data;
1786 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001787 status = readBool(&data);
1788 (*val)[i] = data;
1789
1790 if (status != OK) {
1791 return status;
1792 }
1793 }
1794
1795 return OK;
1796}
1797
Casey Dahlinb9872622015-11-25 15:09:45 -08001798status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1799 return readNullableTypedVector(val, &Parcel::readChar);
1800}
1801
Casey Dahlin451ff582015-10-19 18:12:18 -07001802status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001803 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001804}
1805
Casey Dahlinb9872622015-11-25 15:09:45 -08001806status_t Parcel::readString16Vector(
1807 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1808 return readNullableTypedVector(val, &Parcel::readString16);
1809}
1810
Casey Dahlin451ff582015-10-19 18:12:18 -07001811status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001812 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001813}
1814
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001815status_t Parcel::readUtf8VectorFromUtf16Vector(
1816 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1817 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1818}
1819
1820status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1821 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1822}
Casey Dahlin451ff582015-10-19 18:12:18 -07001823
Andreas Huber84a6d042009-08-17 13:33:27 -07001824status_t Parcel::readInt32(int32_t *pArg) const
1825{
1826 return readAligned(pArg);
1827}
1828
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829int32_t Parcel::readInt32() const
1830{
Andreas Huber84a6d042009-08-17 13:33:27 -07001831 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001832}
1833
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001834status_t Parcel::readUint32(uint32_t *pArg) const
1835{
1836 return readAligned(pArg);
1837}
1838
1839uint32_t Parcel::readUint32() const
1840{
1841 return readAligned<uint32_t>();
1842}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843
1844status_t Parcel::readInt64(int64_t *pArg) const
1845{
Andreas Huber84a6d042009-08-17 13:33:27 -07001846 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847}
1848
1849
1850int64_t Parcel::readInt64() const
1851{
Andreas Huber84a6d042009-08-17 13:33:27 -07001852 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001853}
1854
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001855status_t Parcel::readUint64(uint64_t *pArg) const
1856{
1857 return readAligned(pArg);
1858}
1859
1860uint64_t Parcel::readUint64() const
1861{
1862 return readAligned<uint64_t>();
1863}
1864
Serban Constantinescuf683e012013-11-05 16:53:55 +00001865status_t Parcel::readPointer(uintptr_t *pArg) const
1866{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001867 status_t ret;
1868 binder_uintptr_t ptr;
1869 ret = readAligned(&ptr);
1870 if (!ret)
1871 *pArg = ptr;
1872 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001873}
1874
1875uintptr_t Parcel::readPointer() const
1876{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001877 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001878}
1879
1880
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881status_t Parcel::readFloat(float *pArg) const
1882{
Andreas Huber84a6d042009-08-17 13:33:27 -07001883 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884}
1885
1886
1887float Parcel::readFloat() const
1888{
Andreas Huber84a6d042009-08-17 13:33:27 -07001889 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890}
1891
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001892#if defined(__mips__) && defined(__mips_hard_float)
1893
1894status_t Parcel::readDouble(double *pArg) const
1895{
1896 union {
1897 double d;
1898 unsigned long long ll;
1899 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001900 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001901 status_t status;
1902 status = readAligned(&u.ll);
1903 *pArg = u.d;
1904 return status;
1905}
1906
1907double Parcel::readDouble() const
1908{
1909 union {
1910 double d;
1911 unsigned long long ll;
1912 } u;
1913 u.ll = readAligned<unsigned long long>();
1914 return u.d;
1915}
1916
1917#else
1918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919status_t Parcel::readDouble(double *pArg) const
1920{
Andreas Huber84a6d042009-08-17 13:33:27 -07001921 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922}
1923
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924double Parcel::readDouble() const
1925{
Andreas Huber84a6d042009-08-17 13:33:27 -07001926 return readAligned<double>();
1927}
1928
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001929#endif
1930
Andreas Huber84a6d042009-08-17 13:33:27 -07001931status_t Parcel::readIntPtr(intptr_t *pArg) const
1932{
1933 return readAligned(pArg);
1934}
1935
1936
1937intptr_t Parcel::readIntPtr() const
1938{
1939 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940}
1941
Casey Dahlind6848f52015-10-15 15:44:59 -07001942status_t Parcel::readBool(bool *pArg) const
1943{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001944 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001945 status_t ret = readInt32(&tmp);
1946 *pArg = (tmp != 0);
1947 return ret;
1948}
1949
1950bool Parcel::readBool() const
1951{
1952 return readInt32() != 0;
1953}
1954
1955status_t Parcel::readChar(char16_t *pArg) const
1956{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001957 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001958 status_t ret = readInt32(&tmp);
1959 *pArg = char16_t(tmp);
1960 return ret;
1961}
1962
1963char16_t Parcel::readChar() const
1964{
1965 return char16_t(readInt32());
1966}
1967
1968status_t Parcel::readByte(int8_t *pArg) const
1969{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001970 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001971 status_t ret = readInt32(&tmp);
1972 *pArg = int8_t(tmp);
1973 return ret;
1974}
1975
1976int8_t Parcel::readByte() const
1977{
1978 return int8_t(readInt32());
1979}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001980
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001981status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1982 size_t utf16Size = 0;
1983 const char16_t* src = readString16Inplace(&utf16Size);
1984 if (!src) {
1985 return UNEXPECTED_NULL;
1986 }
1987
1988 // Save ourselves the trouble, we're done.
1989 if (utf16Size == 0u) {
1990 str->clear();
1991 return NO_ERROR;
1992 }
1993
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001994 // Allow for closing '\0'
1995 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1996 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001997 return BAD_VALUE;
1998 }
1999 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002000 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002001 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002002 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2003 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002004 return NO_ERROR;
2005}
2006
2007status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2008 const int32_t start = dataPosition();
2009 int32_t size;
2010 status_t status = readInt32(&size);
2011 str->reset();
2012
2013 if (status != OK || size < 0) {
2014 return status;
2015 }
2016
2017 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002018 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002019 return readUtf8FromUtf16(str->get());
2020}
2021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022const char* Parcel::readCString() const
2023{
2024 const size_t avail = mDataSize-mDataPos;
2025 if (avail > 0) {
2026 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2027 // is the string's trailing NUL within the parcel's valid bounds?
2028 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2029 if (eos) {
2030 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002031 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002032 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033 return str;
2034 }
2035 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002036 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002037}
2038
2039String8 Parcel::readString8() const
2040{
Roshan Pius87b64d22016-07-18 12:51:02 -07002041 String8 retString;
2042 status_t status = readString8(&retString);
2043 if (status != OK) {
2044 // We don't care about errors here, so just return an empty string.
2045 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002047 return retString;
2048}
2049
2050status_t Parcel::readString8(String8* pArg) const
2051{
2052 int32_t size;
2053 status_t status = readInt32(&size);
2054 if (status != OK) {
2055 return status;
2056 }
2057 // watch for potential int overflow from size+1
2058 if (size < 0 || size >= INT32_MAX) {
2059 return BAD_VALUE;
2060 }
2061 // |writeString8| writes nothing for empty string.
2062 if (size == 0) {
2063 *pArg = String8();
2064 return OK;
2065 }
2066 const char* str = (const char*)readInplace(size + 1);
Yi Kongfdd8da92018-06-07 17:52:27 -07002067 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002068 return BAD_VALUE;
2069 }
2070 pArg->setTo(str, size);
2071 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072}
2073
2074String16 Parcel::readString16() const
2075{
2076 size_t len;
2077 const char16_t* str = readString16Inplace(&len);
2078 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002079 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 return String16();
2081}
2082
Casey Dahlinb9872622015-11-25 15:09:45 -08002083status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2084{
2085 const int32_t start = dataPosition();
2086 int32_t size;
2087 status_t status = readInt32(&size);
2088 pArg->reset();
2089
2090 if (status != OK || size < 0) {
2091 return status;
2092 }
2093
2094 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002095 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002096
2097 status = readString16(pArg->get());
2098
2099 if (status != OK) {
2100 pArg->reset();
2101 }
2102
2103 return status;
2104}
2105
Casey Dahlin451ff582015-10-19 18:12:18 -07002106status_t Parcel::readString16(String16* pArg) const
2107{
2108 size_t len;
2109 const char16_t* str = readString16Inplace(&len);
2110 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002111 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002112 return 0;
2113 } else {
2114 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002115 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002116 }
2117}
2118
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2120{
2121 int32_t size = readInt32();
2122 // watch for potential int overflow from size+1
2123 if (size >= 0 && size < INT32_MAX) {
2124 *outLen = size;
2125 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kongfdd8da92018-06-07 17:52:27 -07002126 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 return str;
2128 }
2129 }
2130 *outLen = 0;
Yi Kongfdd8da92018-06-07 17:52:27 -07002131 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132}
2133
Casey Dahlinf0c13772015-10-27 18:33:56 -07002134status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2135{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002136 status_t status = readNullableStrongBinder(val);
2137 if (status == OK && !val->get()) {
2138 status = UNEXPECTED_NULL;
2139 }
2140 return status;
2141}
2142
2143status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2144{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002145 return unflatten_binder(ProcessState::self(), *this, val);
2146}
2147
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148sp<IBinder> Parcel::readStrongBinder() const
2149{
2150 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002151 // Note that a lot of code in Android reads binders by hand with this
2152 // method, and that code has historically been ok with getting nullptr
2153 // back (while ignoring error codes).
2154 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155 return val;
2156}
2157
2158wp<IBinder> Parcel::readWeakBinder() const
2159{
2160 wp<IBinder> val;
2161 unflatten_binder(ProcessState::self(), *this, &val);
2162 return val;
2163}
2164
Christopher Wiley97f048d2015-11-19 06:49:05 -08002165status_t Parcel::readParcelable(Parcelable* parcelable) const {
2166 int32_t have_parcelable = 0;
2167 status_t status = readInt32(&have_parcelable);
2168 if (status != OK) {
2169 return status;
2170 }
2171 if (!have_parcelable) {
2172 return UNEXPECTED_NULL;
2173 }
2174 return parcelable->readFromParcel(this);
2175}
2176
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002177status_t Parcel::readValue(binder::Value* value) const {
2178 return value->readFromParcel(this);
2179}
2180
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002181int32_t Parcel::readExceptionCode() const
2182{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002183 binder::Status status;
2184 status.readFromParcel(*this);
2185 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002186}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002187
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002188native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002189{
2190 int numFds, numInts;
2191 status_t err;
2192 err = readInt32(&numFds);
Yi Kongfdd8da92018-06-07 17:52:27 -07002193 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194 err = readInt32(&numInts);
Yi Kongfdd8da92018-06-07 17:52:27 -07002195 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002196
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002197 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002198 if (!h) {
Yi Kongfdd8da92018-06-07 17:52:27 -07002199 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002200 }
2201
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002202 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002203 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002204 if (h->data[i] < 0) {
2205 for (int j = 0; j < i; j++) {
2206 close(h->data[j]);
2207 }
2208 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002209 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002210 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002211 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002212 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002213 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002214 native_handle_close(h);
2215 native_handle_delete(h);
Yi Kongfdd8da92018-06-07 17:52:27 -07002216 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002217 }
2218 return h;
2219}
2220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221int Parcel::readFileDescriptor() const
2222{
2223 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002224
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002225 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002226 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002227 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002228
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 return BAD_TYPE;
2230}
2231
Dianne Hackborn1941a402016-08-29 12:30:43 -07002232int Parcel::readParcelFileDescriptor() const
2233{
2234 int32_t hasComm = readInt32();
2235 int fd = readFileDescriptor();
2236 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002237 // detach (owned by the binder driver)
2238 int comm = readFileDescriptor();
2239
2240 // warning: this must be kept in sync with:
2241 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2242 enum ParcelFileDescriptorStatus {
2243 DETACHED = 2,
2244 };
2245
2246#if BYTE_ORDER == BIG_ENDIAN
2247 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2248#endif
2249#if BYTE_ORDER == LITTLE_ENDIAN
2250 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2251#endif
2252
2253 ssize_t written = TEMP_FAILURE_RETRY(
2254 ::write(comm, &message, sizeof(message)));
2255
2256 if (written == -1 || written != sizeof(message)) {
2257 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2258 written, strerror(errno));
2259 return BAD_TYPE;
2260 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002261 }
2262 return fd;
2263}
2264
Christopher Wiley2cf19952016-04-11 11:09:37 -07002265status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002266{
2267 int got = readFileDescriptor();
2268
2269 if (got == BAD_TYPE) {
2270 return BAD_TYPE;
2271 }
2272
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002273 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002274
2275 if (val->get() < 0) {
2276 return BAD_VALUE;
2277 }
2278
2279 return OK;
2280}
2281
Ryo Hashimotobf551892018-05-31 16:58:35 +09002282status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2283{
2284 int got = readParcelFileDescriptor();
2285
2286 if (got == BAD_TYPE) {
2287 return BAD_TYPE;
2288 }
2289
2290 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2291
2292 if (val->get() < 0) {
2293 return BAD_VALUE;
2294 }
2295
2296 return OK;
2297}
Casey Dahlin06673e32015-11-23 13:24:23 -08002298
Christopher Wiley2cf19952016-04-11 11:09:37 -07002299status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002300 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2301}
2302
Christopher Wiley2cf19952016-04-11 11:09:37 -07002303status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002304 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2305}
2306
Jeff Brown5707dbf2011-09-23 21:17:56 -07002307status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2308{
Jeff Brown13b16042014-11-11 16:44:25 -08002309 int32_t blobType;
2310 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002311 if (status) return status;
2312
Jeff Brown13b16042014-11-11 16:44:25 -08002313 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002314 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002315 const void* ptr = readInplace(len);
2316 if (!ptr) return BAD_VALUE;
2317
Jeff Brown13b16042014-11-11 16:44:25 -08002318 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002319 return NO_ERROR;
2320 }
2321
Steve Block6807e592011-10-20 11:56:00 +01002322 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002323 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002324 int fd = readFileDescriptor();
2325 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2326
Yi Kongfdd8da92018-06-07 17:52:27 -07002327 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002328 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002329 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002330
Jeff Brown13b16042014-11-11 16:44:25 -08002331 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002332 return NO_ERROR;
2333}
2334
Mathias Agopiane1424282013-07-29 21:24:40 -07002335status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002336{
2337 // size
2338 const size_t len = this->readInt32();
2339 const size_t fd_count = this->readInt32();
2340
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002341 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002342 // don't accept size_t values which may have come from an
2343 // inadvertent conversion from a negative int.
2344 return BAD_VALUE;
2345 }
2346
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002347 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002348 void const* const buf = this->readInplace(pad_size(len));
Yi Kongfdd8da92018-06-07 17:52:27 -07002349 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002350 return BAD_VALUE;
2351
Yi Kongfdd8da92018-06-07 17:52:27 -07002352 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002353 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002354 fds = new (std::nothrow) int[fd_count];
2355 if (fds == nullptr) {
2356 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2357 return BAD_VALUE;
2358 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002359 }
2360
2361 status_t err = NO_ERROR;
2362 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002363 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002364 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002365 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002366 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 -07002367 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2368 // Close all the file descriptors that were dup-ed.
2369 for (size_t j=0; j<i ;j++) {
2370 close(fds[j]);
2371 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002372 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002373 }
2374
2375 if (err == NO_ERROR) {
2376 err = val.unflatten(buf, len, fds, fd_count);
2377 }
2378
2379 if (fd_count) {
2380 delete [] fds;
2381 }
2382
2383 return err;
2384}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2386{
2387 const size_t DPOS = mDataPos;
2388 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2389 const flat_binder_object* obj
2390 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2391 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002392 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002393 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002394 // the object list, so we don't want to check for it when
2395 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002396 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002397 return obj;
2398 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002401 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002402 const size_t N = mObjectsSize;
2403 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002404
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 // Start at the current hint position, looking for an object at
2410 // the current data position.
2411 if (opos < N) {
2412 while (opos < (N-1) && OBJS[opos] < DPOS) {
2413 opos++;
2414 }
2415 } else {
2416 opos = N-1;
2417 }
2418 if (OBJS[opos] == DPOS) {
2419 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002420 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 this, DPOS, opos);
2422 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 return obj;
2425 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 // Look backwards for it...
2428 while (opos > 0 && OBJS[opos] > DPOS) {
2429 opos--;
2430 }
2431 if (OBJS[opos] == DPOS) {
2432 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002433 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434 this, DPOS, opos);
2435 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002436 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 return obj;
2438 }
2439 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002440 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 -07002441 this, DPOS);
2442 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002443 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444}
2445
2446void Parcel::closeFileDescriptors()
2447{
2448 size_t i = mObjectsSize;
2449 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002450 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 }
2452 while (i > 0) {
2453 i--;
2454 const flat_binder_object* flat
2455 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002456 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002457 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 close(flat->handle);
2459 }
2460 }
2461}
2462
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002463uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002465 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466}
2467
2468size_t Parcel::ipcDataSize() const
2469{
2470 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2471}
2472
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002473uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002475 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476}
2477
2478size_t Parcel::ipcObjectsCount() const
2479{
2480 return mObjectsSize;
2481}
2482
2483void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002484 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002486 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 freeDataNoInit();
2488 mError = NO_ERROR;
2489 mData = const_cast<uint8_t*>(data);
2490 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002493 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002494 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002495 mObjectsSize = mObjectsCapacity = objectsCount;
2496 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002497 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 mOwner = relFunc;
2499 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002500 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002501 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002502 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002503 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002504 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002505 mObjectsSize = 0;
2506 break;
2507 }
2508 minOffset = offset + sizeof(flat_binder_object);
2509 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002510 scanForFds();
2511}
2512
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002513void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514{
2515 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002516
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 if (errorCheck() != NO_ERROR) {
2518 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002519 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 } else if (dataSize() > 0) {
2521 const uint8_t* DATA = data();
2522 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002523 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 const size_t N = objectsCount();
2525 for (size_t i=0; i<N; i++) {
2526 const flat_binder_object* flat
2527 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2528 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002529 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 << " = " << flat->binder;
2531 }
2532 } else {
2533 to << "NULL";
2534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 to << ")";
2537}
2538
2539void Parcel::releaseObjects()
2540{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002542 if (i == 0) {
2543 return;
2544 }
2545 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002547 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 while (i > 0) {
2549 i--;
2550 const flat_binder_object* flat
2551 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002552 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 }
2554}
2555
2556void Parcel::acquireObjects()
2557{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002558 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002559 if (i == 0) {
2560 return;
2561 }
2562 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002564 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565 while (i > 0) {
2566 i--;
2567 const flat_binder_object* flat
2568 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002569 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 }
2571}
2572
2573void Parcel::freeData()
2574{
2575 freeDataNoInit();
2576 initState();
2577}
2578
2579void Parcel::freeDataNoInit()
2580{
2581 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002582 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002583 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2585 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002586 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002588 if (mData) {
2589 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002590 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002591 if (mDataCapacity <= gParcelGlobalAllocSize) {
2592 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2593 } else {
2594 gParcelGlobalAllocSize = 0;
2595 }
2596 if (gParcelGlobalAllocCount > 0) {
2597 gParcelGlobalAllocCount--;
2598 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002599 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002600 free(mData);
2601 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 if (mObjects) free(mObjects);
2603 }
2604}
2605
2606status_t Parcel::growData(size_t len)
2607{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002608 if (len > INT32_MAX) {
2609 // don't accept size_t values which may have come from an
2610 // inadvertent conversion from a negative int.
2611 return BAD_VALUE;
2612 }
2613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 size_t newSize = ((mDataSize+len)*3)/2;
2615 return (newSize <= mDataSize)
2616 ? (status_t) NO_MEMORY
2617 : continueWrite(newSize);
2618}
2619
2620status_t Parcel::restartWrite(size_t desired)
2621{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002622 if (desired > INT32_MAX) {
2623 // don't accept size_t values which may have come from an
2624 // inadvertent conversion from a negative int.
2625 return BAD_VALUE;
2626 }
2627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 if (mOwner) {
2629 freeData();
2630 return continueWrite(desired);
2631 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633 uint8_t* data = (uint8_t*)realloc(mData, desired);
2634 if (!data && desired > mDataCapacity) {
2635 mError = NO_MEMORY;
2636 return NO_MEMORY;
2637 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002638
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002640
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002641 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002642 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002643 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002644 gParcelGlobalAllocSize += desired;
2645 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002646 if (!mData) {
2647 gParcelGlobalAllocCount++;
2648 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002649 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 mData = data;
2651 mDataCapacity = desired;
2652 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002653
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002654 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002655 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2656 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2657
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002658 free(mObjects);
Yi Kongfdd8da92018-06-07 17:52:27 -07002659 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 mObjectsSize = mObjectsCapacity = 0;
2661 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002662 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663 mHasFds = false;
2664 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002665 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002667 return NO_ERROR;
2668}
2669
2670status_t Parcel::continueWrite(size_t desired)
2671{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002672 if (desired > INT32_MAX) {
2673 // don't accept size_t values which may have come from an
2674 // inadvertent conversion from a negative int.
2675 return BAD_VALUE;
2676 }
2677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 // If shrinking, first adjust for any objects that appear
2679 // after the new data size.
2680 size_t objectsSize = mObjectsSize;
2681 if (desired < mDataSize) {
2682 if (desired == 0) {
2683 objectsSize = 0;
2684 } else {
2685 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002686 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 break;
2688 objectsSize--;
2689 }
2690 }
2691 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 if (mOwner) {
2694 // If the size is going to zero, just release the owner's data.
2695 if (desired == 0) {
2696 freeData();
2697 return NO_ERROR;
2698 }
2699
2700 // If there is a different owner, we need to take
2701 // posession.
2702 uint8_t* data = (uint8_t*)malloc(desired);
2703 if (!data) {
2704 mError = NO_MEMORY;
2705 return NO_MEMORY;
2706 }
Yi Kongfdd8da92018-06-07 17:52:27 -07002707 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002708
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002710 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002711 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002712 free(data);
2713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002714 mError = NO_MEMORY;
2715 return NO_MEMORY;
2716 }
2717
2718 // Little hack to only acquire references on objects
2719 // we will be keeping.
2720 size_t oldObjectsSize = mObjectsSize;
2721 mObjectsSize = objectsSize;
2722 acquireObjects();
2723 mObjectsSize = oldObjectsSize;
2724 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 if (mData) {
2727 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2728 }
2729 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002730 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002731 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002732 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kongfdd8da92018-06-07 17:52:27 -07002734 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002736 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002737 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002738 gParcelGlobalAllocSize += desired;
2739 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002740 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002741
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 mData = data;
2743 mObjects = objects;
2744 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002745 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 mDataCapacity = desired;
2747 mObjectsSize = mObjectsCapacity = objectsSize;
2748 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002749 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750
2751 } else if (mData) {
2752 if (objectsSize < mObjectsSize) {
2753 // Need to release refs on any objects we are dropping.
2754 const sp<ProcessState> proc(ProcessState::self());
2755 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2756 const flat_binder_object* flat
2757 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002758 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 // will need to rescan because we may have lopped off the only FDs
2760 mFdsKnown = false;
2761 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002762 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002763 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002764 binder_size_t* objects =
2765 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766 if (objects) {
2767 mObjects = objects;
2768 }
2769 mObjectsSize = objectsSize;
2770 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002771 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 }
2773
2774 // We own the data, so we can just do a realloc().
2775 if (desired > mDataCapacity) {
2776 uint8_t* data = (uint8_t*)realloc(mData, desired);
2777 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002778 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2779 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002780 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002781 gParcelGlobalAllocSize += desired;
2782 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002783 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 mData = data;
2785 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002786 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 mError = NO_MEMORY;
2788 return NO_MEMORY;
2789 }
2790 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002791 if (mDataSize > desired) {
2792 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002793 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002794 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002795 if (mDataPos > desired) {
2796 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002797 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 }
2799 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002800
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 } else {
2802 // This is the first data. Easy!
2803 uint8_t* data = (uint8_t*)malloc(desired);
2804 if (!data) {
2805 mError = NO_MEMORY;
2806 return NO_MEMORY;
2807 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002808
Yi Kongfdd8da92018-06-07 17:52:27 -07002809 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002811 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002813
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002814 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002815 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002816 gParcelGlobalAllocSize += desired;
2817 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002818 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002819
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820 mData = data;
2821 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002822 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2823 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824 mDataCapacity = desired;
2825 }
2826
2827 return NO_ERROR;
2828}
2829
2830void Parcel::initState()
2831{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002832 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002833 mError = NO_ERROR;
Yi Kongfdd8da92018-06-07 17:52:27 -07002834 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 mDataSize = 0;
2836 mDataCapacity = 0;
2837 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002838 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2839 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kongfdd8da92018-06-07 17:52:27 -07002840 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 mObjectsSize = 0;
2842 mObjectsCapacity = 0;
2843 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002844 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002845 mHasFds = false;
2846 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002847 mAllowFds = true;
Yi Kongfdd8da92018-06-07 17:52:27 -07002848 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002849 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002850
2851 // racing multiple init leads only to multiple identical write
2852 if (gMaxFds == 0) {
2853 struct rlimit result;
2854 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2855 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002856 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002857 } else {
2858 ALOGW("Unable to getrlimit: %s", strerror(errno));
2859 gMaxFds = 1024;
2860 }
2861 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002862}
2863
2864void Parcel::scanForFds() const
2865{
2866 bool hasFds = false;
2867 for (size_t i=0; i<mObjectsSize; i++) {
2868 const flat_binder_object* flat
2869 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002870 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002871 hasFds = true;
2872 break;
2873 }
2874 }
2875 mHasFds = hasFds;
2876 mFdsKnown = true;
2877}
2878
Dan Sandleraa5c2342015-04-10 10:08:45 -04002879size_t Parcel::getBlobAshmemSize() const
2880{
Adrian Roos6bb31142015-10-22 16:46:12 -07002881 // This used to return the size of all blobs that were written to ashmem, now we're returning
2882 // the ashmem currently referenced by this Parcel, which should be equivalent.
2883 // TODO: Remove method once ABI can be changed.
2884 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002885}
2886
Adrian Rooscbf37262015-10-22 16:12:53 -07002887size_t Parcel::getOpenAshmemSize() const
2888{
2889 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002890}
2891
2892// --- Parcel::Blob ---
2893
2894Parcel::Blob::Blob() :
Yi Kongfdd8da92018-06-07 17:52:27 -07002895 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002896}
2897
2898Parcel::Blob::~Blob() {
2899 release();
2900}
2901
2902void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002903 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002904 ::munmap(mData, mSize);
2905 }
2906 clear();
2907}
2908
Jeff Brown13b16042014-11-11 16:44:25 -08002909void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2910 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002911 mData = data;
2912 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002913 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002914}
2915
2916void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002917 mFd = -1;
Yi Kongfdd8da92018-06-07 17:52:27 -07002918 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002919 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002920 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002921}
2922
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002923}; // namespace android