blob: bc1a71cac8faa9928c00f0e789afdf5e2648c6f1 [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 Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079// XXX This can be made public if we want to provide
80// support for typed data.
81struct small_flat_data
82{
83 uint32_t type;
84 uint32_t data;
85};
86
87namespace android {
88
Dianne Hackborna4cff882014-11-13 17:07:40 -080089static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
90static size_t gParcelGlobalAllocSize = 0;
91static size_t gParcelGlobalAllocCount = 0;
92
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093static size_t gMaxFds = 0;
94
Jeff Brown13b16042014-11-11 16:44:25 -080095// Maximum size of a blob to transfer in-place.
96static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
97
98enum {
99 BLOB_INPLACE = 0,
100 BLOB_ASHMEM_IMMUTABLE = 1,
101 BLOB_ASHMEM_MUTABLE = 2,
102};
103
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104void acquire_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700105 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700107 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 case BINDER_TYPE_BINDER:
109 if (obj.binder) {
110 LOG_REFS("Parcel %p acquiring reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800111 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 }
113 return;
114 case BINDER_TYPE_WEAK_BINDER:
115 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800116 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700117 return;
118 case BINDER_TYPE_HANDLE: {
119 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700120 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
122 b->incStrong(who);
123 }
124 return;
125 }
126 case BINDER_TYPE_WEAK_HANDLE: {
127 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700128 if (b != nullptr) b.get_refs()->incWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 return;
130 }
131 case BINDER_TYPE_FD: {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000132 if ((obj.cookie != 0) && (outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700133 // If we own an ashmem fd, keep track of how much memory it refers to.
134 int size = ashmem_get_size_region(obj.handle);
135 if (size > 0) {
136 *outAshmemSize += size;
Adrian Rooscbf37262015-10-22 16:12:53 -0700137 }
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 return;
140 }
141 }
142
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700143 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700144}
145
Adrian Roos6bb31142015-10-22 16:46:12 -0700146void acquire_object(const sp<ProcessState>& proc,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 const flat_binder_object& obj, const void* who)
148{
Yi Kong91635562018-06-07 14:38:36 -0700149 acquire_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700150}
151
152static void release_object(const sp<ProcessState>& proc,
Adrian Rooscbf37262015-10-22 16:12:53 -0700153 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154{
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700155 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_BINDER:
157 if (obj.binder) {
158 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800159 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 }
161 return;
162 case BINDER_TYPE_WEAK_BINDER:
163 if (obj.binder)
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800164 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
170 b->decStrong(who);
171 }
172 return;
173 }
174 case BINDER_TYPE_WEAK_HANDLE: {
175 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700176 if (b != nullptr) b.get_refs()->decWeak(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177 return;
178 }
179 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800180 if (obj.cookie != 0) { // owned
Jorim Jaggi150b4ef2018-07-13 11:18:30 +0000181 if ((outAshmemSize != nullptr) && ashmem_valid(obj.handle)) {
Mark Salyzyn80589362016-08-23 16:15:04 -0700182 int size = ashmem_get_size_region(obj.handle);
183 if (size > 0) {
184 *outAshmemSize -= size;
Adrian Roos6bb31142015-10-22 16:46:12 -0700185 }
Adrian Roos6bb31142015-10-22 16:46:12 -0700186 }
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800187
188 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700189 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 return;
191 }
192 }
193
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700194 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700195}
196
Adrian Roos6bb31142015-10-22 16:46:12 -0700197void release_object(const sp<ProcessState>& proc,
198 const flat_binder_object& obj, const void* who)
199{
Yi Kong91635562018-06-07 14:38:36 -0700200 release_object(proc, obj, who, nullptr);
Adrian Roos6bb31142015-10-22 16:46:12 -0700201}
202
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700203inline static status_t finish_flatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800204 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700205{
206 return out->writeObject(flat, false);
207}
208
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 const sp<IBinder>& binder, Parcel* out)
211{
212 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700213
Martijn Coenen2b631742017-05-05 11:16:59 -0700214 if (IPCThreadState::self()->backgroundSchedulingDisabled()) {
215 /* minimum priority for all nodes is nice 0 */
216 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 } else {
218 /* minimum priority for all nodes is MAX_NICE(19) */
219 obj.flags = 0x13 | FLAT_BINDER_FLAG_ACCEPTS_FDS;
220 }
221
Yi Kong91635562018-06-07 14:38:36 -0700222 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700223 IBinder *local = binder->localBinder();
224 if (!local) {
225 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700226 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700228 }
229 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700230 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800231 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800233 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700234 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700235 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
237 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700238 }
239 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.binder = 0;
242 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700244
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700245 return finish_flatten_binder(binder, obj, out);
246}
247
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800248status_t flatten_binder(const sp<ProcessState>& /*proc*/,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 const wp<IBinder>& binder, Parcel* out)
250{
251 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Yi Kong91635562018-06-07 14:38:36 -0700254 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 sp<IBinder> real = binder.promote();
Yi Kong91635562018-06-07 14:38:36 -0700256 if (real != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 IBinder *local = real->localBinder();
258 if (!local) {
259 BpBinder *proxy = real->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700260 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000261 ALOGE("null proxy");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262 }
263 const int32_t handle = proxy ? proxy->handle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700264 obj.hdr.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800265 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700266 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800267 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700268 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700269 obj.hdr.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800270 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
271 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272 }
273 return finish_flatten_binder(real, obj, out);
274 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700275
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 // XXX How to deal? In order to flatten the given binder,
277 // we need to probe it for information, which requires a primary
278 // reference... but we don't have one.
279 //
280 // The OpenBinder implementation uses a dynamic_cast<> here,
281 // but we can't do that with the different reference counting
282 // implementation we are using.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000283 ALOGE("Unable to unflatten Binder weak reference!");
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700284 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800285 obj.binder = 0;
286 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700287 return finish_flatten_binder(nullptr, obj, out);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700289 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700290 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800291 obj.binder = 0;
292 obj.cookie = 0;
Yi Kong91635562018-06-07 14:38:36 -0700293 return finish_flatten_binder(nullptr, obj, out);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700294 }
295}
296
297inline static status_t finish_unflatten_binder(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800298 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
299 const Parcel& /*in*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300{
301 return NO_ERROR;
302}
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304status_t unflatten_binder(const sp<ProcessState>& proc,
305 const Parcel& in, sp<IBinder>* out)
306{
307 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700308
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700310 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700311 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800312 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700313 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700314 case BINDER_TYPE_HANDLE:
315 *out = proc->getStrongProxyForHandle(flat->handle);
316 return finish_unflatten_binder(
317 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700318 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700319 }
320 return BAD_TYPE;
321}
322
323status_t unflatten_binder(const sp<ProcessState>& proc,
324 const Parcel& in, wp<IBinder>* out)
325{
326 const flat_binder_object* flat = in.readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700329 switch (flat->hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700330 case BINDER_TYPE_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800331 *out = reinterpret_cast<IBinder*>(flat->cookie);
Yi Kong91635562018-06-07 14:38:36 -0700332 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700333 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800334 if (flat->binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700335 out->set_object_and_refs(
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800336 reinterpret_cast<IBinder*>(flat->cookie),
337 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 } else {
Yi Kong91635562018-06-07 14:38:36 -0700339 *out = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700340 }
Yi Kong91635562018-06-07 14:38:36 -0700341 return finish_unflatten_binder(nullptr, *flat, in);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700342 case BINDER_TYPE_HANDLE:
343 case BINDER_TYPE_WEAK_HANDLE:
344 *out = proc->getWeakProxyForHandle(flat->handle);
345 return finish_unflatten_binder(
346 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
347 }
348 }
349 return BAD_TYPE;
350}
351
352// ---------------------------------------------------------------------------
353
354Parcel::Parcel()
355{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700357 initState();
358}
359
360Parcel::~Parcel()
361{
362 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800363 LOG_ALLOC("Parcel %p: destroyed", this);
364}
365
366size_t Parcel::getGlobalAllocSize() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800367 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
368 size_t size = gParcelGlobalAllocSize;
369 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
370 return size;
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800371}
372
373size_t Parcel::getGlobalAllocCount() {
Dianne Hackborna4cff882014-11-13 17:07:40 -0800374 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
375 size_t count = gParcelGlobalAllocCount;
376 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
377 return count;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378}
379
380const uint8_t* Parcel::data() const
381{
382 return mData;
383}
384
385size_t Parcel::dataSize() const
386{
387 return (mDataSize > mDataPos ? mDataSize : mDataPos);
388}
389
390size_t Parcel::dataAvail() const
391{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700392 size_t result = dataSize() - dataPosition();
393 if (result > INT32_MAX) {
394 abort();
395 }
396 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700397}
398
399size_t Parcel::dataPosition() const
400{
401 return mDataPos;
402}
403
404size_t Parcel::dataCapacity() const
405{
406 return mDataCapacity;
407}
408
409status_t Parcel::setDataSize(size_t size)
410{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700411 if (size > INT32_MAX) {
412 // don't accept size_t values which may have come from an
413 // inadvertent conversion from a negative int.
414 return BAD_VALUE;
415 }
416
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 status_t err;
418 err = continueWrite(size);
419 if (err == NO_ERROR) {
420 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700421 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 }
423 return err;
424}
425
426void Parcel::setDataPosition(size_t pos) const
427{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700428 if (pos > INT32_MAX) {
429 // don't accept size_t values which may have come from an
430 // inadvertent conversion from a negative int.
431 abort();
432 }
433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434 mDataPos = pos;
435 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800436 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437}
438
439status_t Parcel::setDataCapacity(size_t size)
440{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700441 if (size > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700447 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 return NO_ERROR;
449}
450
451status_t Parcel::setData(const uint8_t* buffer, size_t len)
452{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700453 if (len > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 return BAD_VALUE;
457 }
458
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 status_t err = restartWrite(len);
460 if (err == NO_ERROR) {
461 memcpy(const_cast<uint8_t*>(data()), buffer, len);
462 mDataSize = len;
463 mFdsKnown = false;
464 }
465 return err;
466}
467
Andreas Huber51faf462011-04-13 10:21:56 -0700468status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469{
470 const sp<ProcessState> proc(ProcessState::self());
471 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700472 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800473 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 size_t size = parcel->mObjectsSize;
475 int startPos = mDataPos;
476 int firstIndex = -1, lastIndex = -2;
477
478 if (len == 0) {
479 return NO_ERROR;
480 }
481
Nick Kralevichb6b14232015-04-02 09:36:02 -0700482 if (len > INT32_MAX) {
483 // don't accept size_t values which may have come from an
484 // inadvertent conversion from a negative int.
485 return BAD_VALUE;
486 }
487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // range checks against the source parcel size
489 if ((offset > parcel->mDataSize)
490 || (len > parcel->mDataSize)
491 || (offset + len > parcel->mDataSize)) {
492 return BAD_VALUE;
493 }
494
495 // Count objects in range
496 for (int i = 0; i < (int) size; i++) {
497 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700498 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700499 if (firstIndex == -1) {
500 firstIndex = i;
501 }
502 lastIndex = i;
503 }
504 }
505 int numObjects = lastIndex - firstIndex + 1;
506
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700507 if ((mDataSize+len) > mDataCapacity) {
508 // grow data
509 err = growData(len);
510 if (err != NO_ERROR) {
511 return err;
512 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700513 }
514
515 // append data
516 memcpy(mData + mDataPos, data + offset, len);
517 mDataPos += len;
518 mDataSize += len;
519
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400520 err = NO_ERROR;
521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 if (numObjects > 0) {
523 // grow objects
524 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateed7a50c2015-06-08 14:45:14 -0700525 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -0700526 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800527 binder_size_t *objects =
528 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700529 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return NO_MEMORY;
531 }
532 mObjects = objects;
533 mObjectsCapacity = newSize;
534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 // append and acquire objects
537 int idx = mObjectsSize;
538 for (int i = firstIndex; i <= lastIndex; i++) {
539 size_t off = objects[i] - offset + startPos;
540 mObjects[idx++] = off;
541 mObjectsSize++;
542
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700543 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700544 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Rooscbf37262015-10-22 16:12:53 -0700545 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700546
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700547 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700548 // If this is a file descriptor, we need to dup it so the
549 // new Parcel now owns its own fd, and can declare that we
550 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800551 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800552 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 if (!mAllowFds) {
555 err = FDS_NOT_ALLOWED;
556 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557 }
558 }
559 }
560
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400561 return err;
562}
563
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700564int Parcel::compareData(const Parcel& other) {
565 size_t size = dataSize();
566 if (size != other.dataSize()) {
567 return size < other.dataSize() ? -1 : 1;
568 }
569 return memcmp(data(), other.data(), size);
570}
571
Jeff Brown13b16042014-11-11 16:44:25 -0800572bool Parcel::allowFds() const
573{
574 return mAllowFds;
575}
576
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700577bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400578{
579 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700580 if (!allowFds) {
581 mAllowFds = false;
582 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400583 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700586void Parcel::restoreAllowFds(bool lastValue)
587{
588 mAllowFds = lastValue;
589}
590
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700591bool Parcel::hasFileDescriptors() const
592{
593 if (!mFdsKnown) {
594 scanForFds();
595 }
596 return mHasFds;
597}
598
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700599// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600status_t Parcel::writeInterfaceToken(const String16& interface)
601{
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700602 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
603 STRICT_MODE_PENALTY_GATHER);
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100604 writeInt32(IPCThreadState::self()->getWorkSource());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700605 // currently the interface identification token is just its name as a string
606 return writeString16(interface);
607}
608
Mathias Agopian83c04462009-05-22 19:00:22 -0700609bool Parcel::checkInterface(IBinder* binder) const
610{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700611 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700612}
613
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700614bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700615 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700616{
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100617 // StrictModePolicy.
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700618 int32_t strictPolicy = readInt32();
Yi Kong91635562018-06-07 14:38:36 -0700619 if (threadState == nullptr) {
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700620 threadState = IPCThreadState::self();
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700621 }
Brad Fitzpatrick52736032010-08-30 16:01:16 -0700622 if ((threadState->getLastTransactionBinderFlags() &
623 IBinder::FLAG_ONEWAY) != 0) {
624 // For one-way calls, the callee is running entirely
625 // disconnected from the caller, so disable StrictMode entirely.
626 // Not only does disk/network usage not impact the caller, but
627 // there's no way to commuicate back any violations anyway.
628 threadState->setStrictModePolicy(0);
629 } else {
630 threadState->setStrictModePolicy(strictPolicy);
631 }
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100632 // WorkSource.
633 int32_t workSource = readInt32();
634 threadState->setWorkSource(workSource);
635 // Interface descriptor.
Mathias Agopian83c04462009-05-22 19:00:22 -0700636 const String16 str(readString16());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700637 if (str == interface) {
638 return true;
639 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700640 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700641 String8(interface).string(), String8(str).string());
642 return false;
643 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700644}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700645
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800646const binder_size_t* Parcel::objects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700647{
648 return mObjects;
649}
650
651size_t Parcel::objectsCount() const
652{
653 return mObjectsSize;
654}
655
656status_t Parcel::errorCheck() const
657{
658 return mError;
659}
660
661void Parcel::setError(status_t err)
662{
663 mError = err;
664}
665
666status_t Parcel::finishWrite(size_t len)
667{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700668 if (len > INT32_MAX) {
669 // don't accept size_t values which may have come from an
670 // inadvertent conversion from a negative int.
671 return BAD_VALUE;
672 }
673
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700674 //printf("Finish write of %d\n", len);
675 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700676 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700677 if (mDataPos > mDataSize) {
678 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700679 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 }
681 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
682 return NO_ERROR;
683}
684
685status_t Parcel::writeUnpadded(const void* data, size_t len)
686{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700687 if (len > INT32_MAX) {
688 // don't accept size_t values which may have come from an
689 // inadvertent conversion from a negative int.
690 return BAD_VALUE;
691 }
692
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693 size_t end = mDataPos + len;
694 if (end < mDataPos) {
695 // integer overflow
696 return BAD_VALUE;
697 }
698
699 if (end <= mDataCapacity) {
700restart_write:
701 memcpy(mData+mDataPos, data, len);
702 return finishWrite(len);
703 }
704
705 status_t err = growData(len);
706 if (err == NO_ERROR) goto restart_write;
707 return err;
708}
709
710status_t Parcel::write(const void* data, size_t len)
711{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700712 if (len > INT32_MAX) {
713 // don't accept size_t values which may have come from an
714 // inadvertent conversion from a negative int.
715 return BAD_VALUE;
716 }
717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700718 void* const d = writeInplace(len);
719 if (d) {
720 memcpy(d, data, len);
721 return NO_ERROR;
722 }
723 return mError;
724}
725
726void* Parcel::writeInplace(size_t len)
727{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700728 if (len > INT32_MAX) {
729 // don't accept size_t values which may have come from an
730 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700731 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700732 }
733
734 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735
736 // sanity check for integer overflow
737 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700738 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700739 }
740
741 if ((mDataPos+padded) <= mDataCapacity) {
742restart_write:
743 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
744 uint8_t* const data = mData+mDataPos;
745
746 // Need to pad at end?
747 if (padded != len) {
748#if BYTE_ORDER == BIG_ENDIAN
749 static const uint32_t mask[4] = {
750 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
751 };
752#endif
753#if BYTE_ORDER == LITTLE_ENDIAN
754 static const uint32_t mask[4] = {
755 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
756 };
757#endif
758 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
759 // *reinterpret_cast<void**>(data+padded-4));
760 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
761 }
762
763 finishWrite(padded);
764 return data;
765 }
766
767 status_t err = growData(padded);
768 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700769 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700770}
771
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800772status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
773 const uint8_t* strData = (uint8_t*)str.data();
774 const size_t strLen= str.length();
775 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100776 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800777 return BAD_VALUE;
778 }
779
780 status_t err = writeInt32(utf16Len);
781 if (err) {
782 return err;
783 }
784
785 // Allocate enough bytes to hold our converted string and its terminating NULL.
786 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
787 if (!dst) {
788 return NO_MEMORY;
789 }
790
Sergio Girof4607432016-07-21 14:46:35 +0100791 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800792
793 return NO_ERROR;
794}
795
796status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) {
797 if (!str) {
798 return writeInt32(-1);
799 }
800 return writeUtf8AsUtf16(*str);
801}
802
Casey Dahlin185d3442016-02-09 11:08:35 -0800803namespace {
Casey Dahlinb9872622015-11-25 15:09:45 -0800804
Casey Dahlin185d3442016-02-09 11:08:35 -0800805template<typename T>
806status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
Casey Dahlin451ff582015-10-19 18:12:18 -0700807{
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700808 status_t status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700809 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700810 status = BAD_VALUE;
811 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700812 }
813
Casey Dahlin185d3442016-02-09 11:08:35 -0800814 status = parcel->writeInt32(val.size());
Casey Dahlin451ff582015-10-19 18:12:18 -0700815 if (status != OK) {
816 return status;
817 }
818
Casey Dahlin185d3442016-02-09 11:08:35 -0800819 void* data = parcel->writeInplace(val.size());
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700820 if (!data) {
821 status = BAD_VALUE;
822 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700823 }
824
Christopher Wileyf0fc52b2015-10-31 13:22:15 -0700825 memcpy(data, val.data(), val.size());
826 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -0700827}
828
Casey Dahlin185d3442016-02-09 11:08:35 -0800829template<typename T>
830status_t writeByteVectorInternalPtr(Parcel* parcel,
831 const std::unique_ptr<std::vector<T>>& val)
832{
833 if (!val) {
834 return parcel->writeInt32(-1);
835 }
836
837 return writeByteVectorInternal(parcel, *val);
838}
839
840} // namespace
841
842status_t Parcel::writeByteVector(const std::vector<int8_t>& val) {
843 return writeByteVectorInternal(this, val);
844}
845
846status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val)
847{
848 return writeByteVectorInternalPtr(this, val);
849}
850
851status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) {
852 return writeByteVectorInternal(this, val);
853}
854
855status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val)
856{
857 return writeByteVectorInternalPtr(this, val);
858}
859
Casey Dahlin451ff582015-10-19 18:12:18 -0700860status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
861{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800862 return writeTypedVector(val, &Parcel::writeInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -0700863}
864
Casey Dahlinb9872622015-11-25 15:09:45 -0800865status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val)
866{
867 return writeNullableTypedVector(val, &Parcel::writeInt32);
868}
869
Casey Dahlin451ff582015-10-19 18:12:18 -0700870status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
871{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800872 return writeTypedVector(val, &Parcel::writeInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -0700873}
874
Casey Dahlinb9872622015-11-25 15:09:45 -0800875status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val)
876{
877 return writeNullableTypedVector(val, &Parcel::writeInt64);
878}
879
Casey Dahlin451ff582015-10-19 18:12:18 -0700880status_t Parcel::writeFloatVector(const std::vector<float>& val)
881{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800882 return writeTypedVector(val, &Parcel::writeFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -0700883}
884
Casey Dahlinb9872622015-11-25 15:09:45 -0800885status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val)
886{
887 return writeNullableTypedVector(val, &Parcel::writeFloat);
888}
889
Casey Dahlin451ff582015-10-19 18:12:18 -0700890status_t Parcel::writeDoubleVector(const std::vector<double>& val)
891{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800892 return writeTypedVector(val, &Parcel::writeDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -0700893}
894
Casey Dahlinb9872622015-11-25 15:09:45 -0800895status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val)
896{
897 return writeNullableTypedVector(val, &Parcel::writeDouble);
898}
899
Casey Dahlin451ff582015-10-19 18:12:18 -0700900status_t Parcel::writeBoolVector(const std::vector<bool>& val)
901{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800902 return writeTypedVector(val, &Parcel::writeBool);
Casey Dahlin451ff582015-10-19 18:12:18 -0700903}
904
Casey Dahlinb9872622015-11-25 15:09:45 -0800905status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val)
906{
907 return writeNullableTypedVector(val, &Parcel::writeBool);
908}
909
Casey Dahlin451ff582015-10-19 18:12:18 -0700910status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
911{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800912 return writeTypedVector(val, &Parcel::writeChar);
Casey Dahlin451ff582015-10-19 18:12:18 -0700913}
914
Casey Dahlinb9872622015-11-25 15:09:45 -0800915status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val)
916{
917 return writeNullableTypedVector(val, &Parcel::writeChar);
918}
919
Casey Dahlin451ff582015-10-19 18:12:18 -0700920status_t Parcel::writeString16Vector(const std::vector<String16>& val)
921{
Christopher Wiley03d1eb62015-11-19 06:42:40 -0800922 return writeTypedVector(val, &Parcel::writeString16);
Casey Dahlin451ff582015-10-19 18:12:18 -0700923}
924
Casey Dahlinb9872622015-11-25 15:09:45 -0800925status_t Parcel::writeString16Vector(
926 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val)
927{
928 return writeNullableTypedVector(val, &Parcel::writeString16);
929}
930
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800931status_t Parcel::writeUtf8VectorAsUtf16Vector(
932 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) {
933 return writeNullableTypedVector(val, &Parcel::writeUtf8AsUtf16);
934}
935
936status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) {
937 return writeTypedVector(val, &Parcel::writeUtf8AsUtf16);
938}
939
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700940status_t Parcel::writeInt32(int32_t val)
941{
Andreas Huber84a6d042009-08-17 13:33:27 -0700942 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700943}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800944
945status_t Parcel::writeUint32(uint32_t val)
946{
947 return writeAligned(val);
948}
949
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700950status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700951 if (len > INT32_MAX) {
952 // don't accept size_t values which may have come from an
953 // inadvertent conversion from a negative int.
954 return BAD_VALUE;
955 }
956
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700957 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700958 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700959 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700960 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700961 if (ret == NO_ERROR) {
962 ret = write(val, len * sizeof(*val));
963 }
964 return ret;
965}
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700966status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700967 if (len > INT32_MAX) {
968 // don't accept size_t values which may have come from an
969 // inadvertent conversion from a negative int.
970 return BAD_VALUE;
971 }
972
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700973 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700974 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700975 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700976 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -0700977 if (ret == NO_ERROR) {
978 ret = write(val, len * sizeof(*val));
979 }
980 return ret;
981}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700982
Casey Dahlind6848f52015-10-15 15:44:59 -0700983status_t Parcel::writeBool(bool val)
984{
985 return writeInt32(int32_t(val));
986}
987
988status_t Parcel::writeChar(char16_t val)
989{
990 return writeInt32(int32_t(val));
991}
992
993status_t Parcel::writeByte(int8_t val)
994{
995 return writeInt32(int32_t(val));
996}
997
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998status_t Parcel::writeInt64(int64_t val)
999{
Andreas Huber84a6d042009-08-17 13:33:27 -07001000 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001001}
1002
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001003status_t Parcel::writeUint64(uint64_t val)
1004{
1005 return writeAligned(val);
1006}
1007
Serban Constantinescuf683e012013-11-05 16:53:55 +00001008status_t Parcel::writePointer(uintptr_t val)
1009{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001010 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001011}
1012
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013status_t Parcel::writeFloat(float val)
1014{
Andreas Huber84a6d042009-08-17 13:33:27 -07001015 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001016}
1017
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001018#if defined(__mips__) && defined(__mips_hard_float)
1019
1020status_t Parcel::writeDouble(double val)
1021{
1022 union {
1023 double d;
1024 unsigned long long ll;
1025 } u;
1026 u.d = val;
1027 return writeAligned(u.ll);
1028}
1029
1030#else
1031
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001032status_t Parcel::writeDouble(double val)
1033{
Andreas Huber84a6d042009-08-17 13:33:27 -07001034 return writeAligned(val);
1035}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001036
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001037#endif
1038
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039status_t Parcel::writeCString(const char* str)
1040{
1041 return write(str, strlen(str)+1);
1042}
1043
1044status_t Parcel::writeString8(const String8& str)
1045{
1046 status_t err = writeInt32(str.bytes());
Pravat Dalbeherad1dff8d2010-12-15 08:40:00 +01001047 // only write string if its length is more than zero characters,
1048 // as readString8 will only read if the length field is non-zero.
1049 // this is slightly different from how writeString16 works.
1050 if (str.bytes() > 0 && err == NO_ERROR) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001051 err = write(str.string(), str.bytes()+1);
1052 }
1053 return err;
1054}
1055
Casey Dahlinb9872622015-11-25 15:09:45 -08001056status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
1057{
1058 if (!str) {
1059 return writeInt32(-1);
1060 }
1061
1062 return writeString16(*str);
1063}
1064
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001065status_t Parcel::writeString16(const String16& str)
1066{
1067 return writeString16(str.string(), str.size());
1068}
1069
1070status_t Parcel::writeString16(const char16_t* str, size_t len)
1071{
Yi Kong91635562018-06-07 14:38:36 -07001072 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001073
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001074 status_t err = writeInt32(len);
1075 if (err == NO_ERROR) {
1076 len *= sizeof(char16_t);
1077 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1078 if (data) {
1079 memcpy(data, str, len);
1080 *reinterpret_cast<char16_t*>(data+len) = 0;
1081 return NO_ERROR;
1082 }
1083 err = mError;
1084 }
1085 return err;
1086}
1087
1088status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1089{
1090 return flatten_binder(ProcessState::self(), val, this);
1091}
1092
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001093status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1094{
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001095 return writeTypedVector(val, &Parcel::writeStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001096}
1097
Casey Dahlinb9872622015-11-25 15:09:45 -08001098status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val)
1099{
1100 return writeNullableTypedVector(val, &Parcel::writeStrongBinder);
1101}
1102
1103status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const {
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001104 return readNullableTypedVector(val, &Parcel::readNullableStrongBinder);
Casey Dahlinb9872622015-11-25 15:09:45 -08001105}
1106
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001107status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001108 return readTypedVector(val, &Parcel::readStrongBinder);
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001109}
1110
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001111status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1112{
1113 return flatten_binder(ProcessState::self(), val, this);
1114}
1115
Casey Dahlinb9872622015-11-25 15:09:45 -08001116status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1117 if (!parcelable) {
1118 return writeInt32(0);
1119 }
1120
1121 return writeParcelable(*parcelable);
1122}
1123
Christopher Wiley97f048d2015-11-19 06:49:05 -08001124status_t Parcel::writeParcelable(const Parcelable& parcelable) {
1125 status_t status = writeInt32(1); // parcelable is not null.
1126 if (status != OK) {
1127 return status;
1128 }
1129 return parcelable.writeToParcel(this);
1130}
1131
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001132status_t Parcel::writeValue(const binder::Value& value) {
1133 return value.writeToParcel(this);
1134}
1135
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001136status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001137{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001138 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001139 return BAD_TYPE;
1140
1141 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001145 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001146 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1149 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150
1151 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001152 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 return err;
1154 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001155 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001156 return err;
1157}
1158
Jeff Brown93ff1f92011-11-04 19:01:44 -07001159status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160{
1161 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001162 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001163 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001164 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001166 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167 return writeObject(obj, true);
1168}
1169
1170status_t Parcel::writeDupFileDescriptor(int fd)
1171{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001172 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001173 if (dupFd < 0) {
1174 return -errno;
1175 }
1176 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001177 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001178 close(dupFd);
1179 }
1180 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001181}
1182
Dianne Hackborn1941a402016-08-29 12:30:43 -07001183status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1184{
1185 writeInt32(0);
1186 return writeFileDescriptor(fd, takeOwnership);
1187}
1188
Ryo Hashimotobf551892018-05-31 16:58:35 +09001189status_t Parcel::writeDupParcelFileDescriptor(int fd)
1190{
1191 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1192 if (dupFd < 0) {
1193 return -errno;
1194 }
1195 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1196 if (err != OK) {
1197 close(dupFd);
1198 }
1199 return err;
1200}
1201
Christopher Wiley2cf19952016-04-11 11:09:37 -07001202status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001203 return writeDupFileDescriptor(fd.get());
1204}
1205
Christopher Wiley2cf19952016-04-11 11:09:37 -07001206status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001207 return writeTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1208}
1209
Christopher Wiley2cf19952016-04-11 11:09:37 -07001210status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) {
Casey Dahlinb9872622015-11-25 15:09:45 -08001211 return writeNullableTypedVector(val, &Parcel::writeUniqueFileDescriptor);
1212}
1213
Jeff Brown13b16042014-11-11 16:44:25 -08001214status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001215{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001216 if (len > INT32_MAX) {
1217 // don't accept size_t values which may have come from an
1218 // inadvertent conversion from a negative int.
1219 return BAD_VALUE;
1220 }
1221
Jeff Brown13b16042014-11-11 16:44:25 -08001222 status_t status;
1223 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001224 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001225 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001226 if (status) return status;
1227
1228 void* ptr = writeInplace(len);
1229 if (!ptr) return NO_MEMORY;
1230
Jeff Brown13b16042014-11-11 16:44:25 -08001231 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001232 return NO_ERROR;
1233 }
1234
Steve Block6807e592011-10-20 11:56:00 +01001235 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001236 int fd = ashmem_create_region("Parcel Blob", len);
1237 if (fd < 0) return NO_MEMORY;
1238
1239 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1240 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001241 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001242 } else {
Yi Kong91635562018-06-07 14:38:36 -07001243 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 if (ptr == MAP_FAILED) {
1245 status = -errno;
1246 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001247 if (!mutableCopy) {
1248 result = ashmem_set_prot_region(fd, PROT_READ);
1249 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001250 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001251 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001253 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001254 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001255 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001257 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258 return NO_ERROR;
1259 }
1260 }
1261 }
1262 }
1263 ::munmap(ptr, len);
1264 }
1265 ::close(fd);
1266 return status;
1267}
1268
Jeff Brown13b16042014-11-11 16:44:25 -08001269status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1270{
1271 // Must match up with what's done in writeBlob.
1272 if (!mAllowFds) return FDS_NOT_ALLOWED;
1273 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1274 if (status) return status;
1275 return writeDupFileDescriptor(fd);
1276}
1277
Mathias Agopiane1424282013-07-29 21:24:40 -07001278status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001279{
1280 status_t err;
1281
1282 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001283 const size_t len = val.getFlattenedSize();
1284 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001285
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001286 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001287 // don't accept size_t values which may have come from an
1288 // inadvertent conversion from a negative int.
1289 return BAD_VALUE;
1290 }
1291
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001292 err = this->writeInt32(len);
1293 if (err) return err;
1294
1295 err = this->writeInt32(fd_count);
1296 if (err) return err;
1297
1298 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001299 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001300 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301 return BAD_VALUE;
1302
Yi Kong91635562018-06-07 14:38:36 -07001303 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001304 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001305 fds = new (std::nothrow) int[fd_count];
1306 if (fds == nullptr) {
1307 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1308 return BAD_VALUE;
1309 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001310 }
1311
1312 err = val.flatten(buf, len, fds, fd_count);
1313 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1314 err = this->writeDupFileDescriptor( fds[i] );
1315 }
1316
1317 if (fd_count) {
1318 delete [] fds;
1319 }
1320
1321 return err;
1322}
1323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001324status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1325{
1326 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1327 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1328 if (enoughData && enoughObjects) {
1329restart_write:
1330 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001331
Christopher Tate98e67d32015-06-03 18:44:15 -07001332 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001333 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001334 if (!mAllowFds) {
1335 // fail before modifying our object index
1336 return FDS_NOT_ALLOWED;
1337 }
1338 mHasFds = mFdsKnown = true;
1339 }
1340
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001341 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001342 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001343 mObjects[mObjectsSize] = mDataPos;
Adrian Rooscbf37262015-10-22 16:12:53 -07001344 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 mObjectsSize++;
1346 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 return finishWrite(sizeof(flat_binder_object));
1349 }
1350
1351 if (!enoughData) {
1352 const status_t err = growData(sizeof(val));
1353 if (err != NO_ERROR) return err;
1354 }
1355 if (!enoughObjects) {
1356 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tate44235112016-11-03 13:32:41 -07001357 if (newSize*sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001358 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001359 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 mObjects = objects;
1361 mObjectsCapacity = newSize;
1362 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364 goto restart_write;
1365}
1366
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001367status_t Parcel::writeNoException()
1368{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001369 binder::Status status;
1370 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001371}
1372
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001373status_t Parcel::writeMap(const ::android::binder::Map& map_in)
1374{
1375 using ::std::map;
1376 using ::android::binder::Value;
1377 using ::android::binder::Map;
1378
1379 Map::const_iterator iter;
1380 status_t ret;
1381
1382 ret = writeInt32(map_in.size());
1383
1384 if (ret != NO_ERROR) {
1385 return ret;
1386 }
1387
1388 for (iter = map_in.begin(); iter != map_in.end(); ++iter) {
1389 ret = writeValue(Value(iter->first));
1390 if (ret != NO_ERROR) {
1391 return ret;
1392 }
1393
1394 ret = writeValue(iter->second);
1395 if (ret != NO_ERROR) {
1396 return ret;
1397 }
1398 }
1399
1400 return ret;
1401}
1402
1403status_t Parcel::writeNullableMap(const std::unique_ptr<binder::Map>& map)
1404{
Yi Kong91635562018-06-07 14:38:36 -07001405 if (map == nullptr) {
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001406 return writeInt32(-1);
1407 }
1408
1409 return writeMap(*map.get());
1410}
1411
1412status_t Parcel::readMap(::android::binder::Map* map_out)const
1413{
1414 using ::std::map;
1415 using ::android::String16;
1416 using ::android::String8;
1417 using ::android::binder::Value;
1418 using ::android::binder::Map;
1419
1420 status_t ret = NO_ERROR;
1421 int32_t count;
1422
1423 ret = readInt32(&count);
1424 if (ret != NO_ERROR) {
1425 return ret;
1426 }
1427
1428 if (count < 0) {
1429 ALOGE("readMap: Unexpected count: %d", count);
1430 return (count == -1)
1431 ? UNEXPECTED_NULL
1432 : BAD_VALUE;
1433 }
1434
1435 map_out->clear();
1436
1437 while (count--) {
1438 Map::key_type key;
1439 Value value;
1440
1441 ret = readValue(&value);
1442 if (ret != NO_ERROR) {
1443 return ret;
1444 }
1445
1446 if (!value.getString(&key)) {
1447 ALOGE("readMap: Key type not a string (parcelType = %d)", value.parcelType());
1448 return BAD_VALUE;
1449 }
1450
1451 ret = readValue(&value);
1452 if (ret != NO_ERROR) {
1453 return ret;
1454 }
1455
1456 (*map_out)[key] = value;
1457 }
1458
1459 return ret;
1460}
1461
1462status_t Parcel::readNullableMap(std::unique_ptr<binder::Map>* map) const
1463{
1464 const size_t start = dataPosition();
1465 int32_t count;
1466 status_t status = readInt32(&count);
1467 map->reset();
1468
1469 if (status != OK || count == -1) {
1470 return status;
1471 }
1472
1473 setDataPosition(start);
1474 map->reset(new binder::Map());
1475
1476 status = readMap(map->get());
1477
1478 if (status != OK) {
1479 map->reset();
1480 }
1481
1482 return status;
1483}
1484
1485
1486
Colin Cross6f4f3ab2014-02-05 17:42:44 -08001487void Parcel::remove(size_t /*start*/, size_t /*amt*/)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001488{
1489 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1490}
1491
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001492status_t Parcel::validateReadData(size_t upperBound) const
1493{
1494 // Don't allow non-object reads on object data
1495 if (mObjectsSorted || mObjectsSize <= 1) {
1496data_sorted:
1497 // Expect to check only against the next object
1498 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1499 // For some reason the current read position is greater than the next object
1500 // hint. Iterate until we find the right object
1501 size_t nextObject = mNextObjectHint;
1502 do {
1503 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1504 // Requested info overlaps with an object
1505 ALOGE("Attempt to read from protected data in Parcel %p", this);
1506 return PERMISSION_DENIED;
1507 }
1508 nextObject++;
1509 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1510 mNextObjectHint = nextObject;
1511 }
1512 return NO_ERROR;
1513 }
1514 // Quickly determine if mObjects is sorted.
1515 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1516 binder_size_t* prevObj = currObj;
1517 while (currObj > mObjects) {
1518 prevObj--;
1519 if(*prevObj > *currObj) {
1520 goto data_unsorted;
1521 }
1522 currObj--;
1523 }
1524 mObjectsSorted = true;
1525 goto data_sorted;
1526
1527data_unsorted:
1528 // Insertion Sort mObjects
1529 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1530 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1531 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1532 binder_size_t temp = *iter0;
1533 binder_size_t* iter1 = iter0 - 1;
1534 while (iter1 >= mObjects && *iter1 > temp) {
1535 *(iter1 + 1) = *iter1;
1536 iter1--;
1537 }
1538 *(iter1 + 1) = temp;
1539 }
1540 mNextObjectHint = 0;
1541 mObjectsSorted = true;
1542 goto data_sorted;
1543}
1544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545status_t Parcel::read(void* outData, size_t len) const
1546{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001547 if (len > INT32_MAX) {
1548 // don't accept size_t values which may have come from an
1549 // inadvertent conversion from a negative int.
1550 return BAD_VALUE;
1551 }
1552
1553 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1554 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001555 if (mObjectsSize > 0) {
1556 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001557 if(err != NO_ERROR) {
1558 // Still increment the data position by the expected length
1559 mDataPos += pad_size(len);
1560 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1561 return err;
1562 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001563 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001565 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001566 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001567 return NO_ERROR;
1568 }
1569 return NOT_ENOUGH_DATA;
1570}
1571
1572const void* Parcel::readInplace(size_t len) const
1573{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001574 if (len > INT32_MAX) {
1575 // don't accept size_t values which may have come from an
1576 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001577 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001578 }
1579
1580 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1581 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001582 if (mObjectsSize > 0) {
1583 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001584 if(err != NO_ERROR) {
1585 // Still increment the data position by the expected length
1586 mDataPos += pad_size(len);
1587 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001588 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001589 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001590 }
1591
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001592 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001593 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001594 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595 return data;
1596 }
Yi Kong91635562018-06-07 14:38:36 -07001597 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598}
1599
Andreas Huber84a6d042009-08-17 13:33:27 -07001600template<class T>
1601status_t Parcel::readAligned(T *pArg) const {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001602 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001603
1604 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001605 if (mObjectsSize > 0) {
1606 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001607 if(err != NO_ERROR) {
1608 // Still increment the data position by the expected length
1609 mDataPos += sizeof(T);
1610 return err;
1611 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001612 }
1613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 mDataPos += sizeof(T);
1616 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617 return NO_ERROR;
1618 } else {
1619 return NOT_ENOUGH_DATA;
1620 }
1621}
1622
Andreas Huber84a6d042009-08-17 13:33:27 -07001623template<class T>
1624T Parcel::readAligned() const {
1625 T result;
1626 if (readAligned(&result) != NO_ERROR) {
1627 result = 0;
1628 }
1629
1630 return result;
1631}
1632
1633template<class T>
1634status_t Parcel::writeAligned(T val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001635 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001636
1637 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1638restart_write:
1639 *reinterpret_cast<T*>(mData+mDataPos) = val;
1640 return finishWrite(sizeof(val));
1641 }
1642
1643 status_t err = growData(sizeof(val));
1644 if (err == NO_ERROR) goto restart_write;
1645 return err;
1646}
1647
Casey Dahlin185d3442016-02-09 11:08:35 -08001648namespace {
1649
1650template<typename T>
1651status_t readByteVectorInternal(const Parcel* parcel,
1652 std::vector<T>* val) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001653 val->clear();
1654
1655 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001656 status_t status = parcel->readInt32(&size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001657
1658 if (status != OK) {
1659 return status;
1660 }
1661
Christopher Wiley4db672d2015-11-10 09:44:30 -08001662 if (size < 0) {
1663 status = UNEXPECTED_NULL;
1664 return status;
1665 }
Casey Dahlin185d3442016-02-09 11:08:35 -08001666 if (size_t(size) > parcel->dataAvail()) {
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001667 status = BAD_VALUE;
1668 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001669 }
Christopher Wiley4db672d2015-11-10 09:44:30 -08001670
Paul Lietar433e87b2016-09-16 10:39:32 -07001671 T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001672 if (!data) {
1673 status = BAD_VALUE;
1674 return status;
1675 }
Paul Lietar433e87b2016-09-16 10:39:32 -07001676 val->reserve(size);
1677 val->insert(val->end(), data, data + size);
Casey Dahlin451ff582015-10-19 18:12:18 -07001678
Christopher Wileyf0fc52b2015-10-31 13:22:15 -07001679 return status;
Casey Dahlin451ff582015-10-19 18:12:18 -07001680}
1681
Casey Dahlin185d3442016-02-09 11:08:35 -08001682template<typename T>
1683status_t readByteVectorInternalPtr(
1684 const Parcel* parcel,
1685 std::unique_ptr<std::vector<T>>* val) {
1686 const int32_t start = parcel->dataPosition();
Casey Dahlinb9872622015-11-25 15:09:45 -08001687 int32_t size;
Casey Dahlin185d3442016-02-09 11:08:35 -08001688 status_t status = parcel->readInt32(&size);
Casey Dahlinb9872622015-11-25 15:09:45 -08001689 val->reset();
1690
1691 if (status != OK || size < 0) {
1692 return status;
1693 }
1694
Casey Dahlin185d3442016-02-09 11:08:35 -08001695 parcel->setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001696 val->reset(new (std::nothrow) std::vector<T>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001697
Casey Dahlin185d3442016-02-09 11:08:35 -08001698 status = readByteVectorInternal(parcel, val->get());
Casey Dahlinb9872622015-11-25 15:09:45 -08001699
1700 if (status != OK) {
1701 val->reset();
1702 }
1703
1704 return status;
1705}
1706
Casey Dahlin185d3442016-02-09 11:08:35 -08001707} // namespace
1708
1709status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1710 return readByteVectorInternal(this, val);
1711}
1712
1713status_t Parcel::readByteVector(std::vector<uint8_t>* val) const {
1714 return readByteVectorInternal(this, val);
1715}
1716
1717status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const {
1718 return readByteVectorInternalPtr(this, val);
1719}
1720
1721status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const {
1722 return readByteVectorInternalPtr(this, val);
1723}
1724
Casey Dahlinb9872622015-11-25 15:09:45 -08001725status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const {
1726 return readNullableTypedVector(val, &Parcel::readInt32);
1727}
1728
Casey Dahlin451ff582015-10-19 18:12:18 -07001729status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001730 return readTypedVector(val, &Parcel::readInt32);
Casey Dahlin451ff582015-10-19 18:12:18 -07001731}
1732
Casey Dahlinb9872622015-11-25 15:09:45 -08001733status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const {
1734 return readNullableTypedVector(val, &Parcel::readInt64);
1735}
1736
Casey Dahlin451ff582015-10-19 18:12:18 -07001737status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001738 return readTypedVector(val, &Parcel::readInt64);
Casey Dahlin451ff582015-10-19 18:12:18 -07001739}
1740
Casey Dahlinb9872622015-11-25 15:09:45 -08001741status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const {
1742 return readNullableTypedVector(val, &Parcel::readFloat);
1743}
1744
Casey Dahlin451ff582015-10-19 18:12:18 -07001745status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001746 return readTypedVector(val, &Parcel::readFloat);
Casey Dahlin451ff582015-10-19 18:12:18 -07001747}
1748
Casey Dahlinb9872622015-11-25 15:09:45 -08001749status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const {
1750 return readNullableTypedVector(val, &Parcel::readDouble);
1751}
1752
Casey Dahlin451ff582015-10-19 18:12:18 -07001753status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001754 return readTypedVector(val, &Parcel::readDouble);
Casey Dahlin451ff582015-10-19 18:12:18 -07001755}
1756
Casey Dahlinb9872622015-11-25 15:09:45 -08001757status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const {
1758 const int32_t start = dataPosition();
1759 int32_t size;
1760 status_t status = readInt32(&size);
1761 val->reset();
Casey Dahlin451ff582015-10-19 18:12:18 -07001762
Casey Dahlinb9872622015-11-25 15:09:45 -08001763 if (status != OK || size < 0) {
1764 return status;
1765 }
1766
1767 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001768 val->reset(new (std::nothrow) std::vector<bool>());
Casey Dahlinb9872622015-11-25 15:09:45 -08001769
1770 status = readBoolVector(val->get());
1771
1772 if (status != OK) {
1773 val->reset();
1774 }
1775
1776 return status;
1777}
1778
1779status_t Parcel::readBoolVector(std::vector<bool>* val) const {
Casey Dahlin451ff582015-10-19 18:12:18 -07001780 int32_t size;
1781 status_t status = readInt32(&size);
1782
1783 if (status != OK) {
1784 return status;
1785 }
1786
1787 if (size < 0) {
Christopher Wiley4db672d2015-11-10 09:44:30 -08001788 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001789 }
1790
1791 val->resize(size);
1792
1793 /* C++ bool handling means a vector of bools isn't necessarily addressable
1794 * (we might use individual bits)
1795 */
Christopher Wiley97887982015-10-27 16:33:47 -07001796 bool data;
1797 for (int32_t i = 0; i < size; ++i) {
Casey Dahlin451ff582015-10-19 18:12:18 -07001798 status = readBool(&data);
1799 (*val)[i] = data;
1800
1801 if (status != OK) {
1802 return status;
1803 }
1804 }
1805
1806 return OK;
1807}
1808
Casey Dahlinb9872622015-11-25 15:09:45 -08001809status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const {
1810 return readNullableTypedVector(val, &Parcel::readChar);
1811}
1812
Casey Dahlin451ff582015-10-19 18:12:18 -07001813status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001814 return readTypedVector(val, &Parcel::readChar);
Casey Dahlin451ff582015-10-19 18:12:18 -07001815}
1816
Casey Dahlinb9872622015-11-25 15:09:45 -08001817status_t Parcel::readString16Vector(
1818 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const {
1819 return readNullableTypedVector(val, &Parcel::readString16);
1820}
1821
Casey Dahlin451ff582015-10-19 18:12:18 -07001822status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley03d1eb62015-11-19 06:42:40 -08001823 return readTypedVector(val, &Parcel::readString16);
Casey Dahlin451ff582015-10-19 18:12:18 -07001824}
1825
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001826status_t Parcel::readUtf8VectorFromUtf16Vector(
1827 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const {
1828 return readNullableTypedVector(val, &Parcel::readUtf8FromUtf16);
1829}
1830
1831status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const {
1832 return readTypedVector(val, &Parcel::readUtf8FromUtf16);
1833}
Casey Dahlin451ff582015-10-19 18:12:18 -07001834
Andreas Huber84a6d042009-08-17 13:33:27 -07001835status_t Parcel::readInt32(int32_t *pArg) const
1836{
1837 return readAligned(pArg);
1838}
1839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840int32_t Parcel::readInt32() const
1841{
Andreas Huber84a6d042009-08-17 13:33:27 -07001842 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843}
1844
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001845status_t Parcel::readUint32(uint32_t *pArg) const
1846{
1847 return readAligned(pArg);
1848}
1849
1850uint32_t Parcel::readUint32() const
1851{
1852 return readAligned<uint32_t>();
1853}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001854
1855status_t Parcel::readInt64(int64_t *pArg) const
1856{
Andreas Huber84a6d042009-08-17 13:33:27 -07001857 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001858}
1859
1860
1861int64_t Parcel::readInt64() const
1862{
Andreas Huber84a6d042009-08-17 13:33:27 -07001863 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864}
1865
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001866status_t Parcel::readUint64(uint64_t *pArg) const
1867{
1868 return readAligned(pArg);
1869}
1870
1871uint64_t Parcel::readUint64() const
1872{
1873 return readAligned<uint64_t>();
1874}
1875
Serban Constantinescuf683e012013-11-05 16:53:55 +00001876status_t Parcel::readPointer(uintptr_t *pArg) const
1877{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001878 status_t ret;
1879 binder_uintptr_t ptr;
1880 ret = readAligned(&ptr);
1881 if (!ret)
1882 *pArg = ptr;
1883 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001884}
1885
1886uintptr_t Parcel::readPointer() const
1887{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001888 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001889}
1890
1891
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892status_t Parcel::readFloat(float *pArg) const
1893{
Andreas Huber84a6d042009-08-17 13:33:27 -07001894 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001895}
1896
1897
1898float Parcel::readFloat() const
1899{
Andreas Huber84a6d042009-08-17 13:33:27 -07001900 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001901}
1902
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001903#if defined(__mips__) && defined(__mips_hard_float)
1904
1905status_t Parcel::readDouble(double *pArg) const
1906{
1907 union {
1908 double d;
1909 unsigned long long ll;
1910 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001911 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001912 status_t status;
1913 status = readAligned(&u.ll);
1914 *pArg = u.d;
1915 return status;
1916}
1917
1918double Parcel::readDouble() const
1919{
1920 union {
1921 double d;
1922 unsigned long long ll;
1923 } u;
1924 u.ll = readAligned<unsigned long long>();
1925 return u.d;
1926}
1927
1928#else
1929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930status_t Parcel::readDouble(double *pArg) const
1931{
Andreas Huber84a6d042009-08-17 13:33:27 -07001932 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001933}
1934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001935double Parcel::readDouble() const
1936{
Andreas Huber84a6d042009-08-17 13:33:27 -07001937 return readAligned<double>();
1938}
1939
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001940#endif
1941
Andreas Huber84a6d042009-08-17 13:33:27 -07001942status_t Parcel::readIntPtr(intptr_t *pArg) const
1943{
1944 return readAligned(pArg);
1945}
1946
1947
1948intptr_t Parcel::readIntPtr() const
1949{
1950 return readAligned<intptr_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951}
1952
Casey Dahlind6848f52015-10-15 15:44:59 -07001953status_t Parcel::readBool(bool *pArg) const
1954{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001955 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001956 status_t ret = readInt32(&tmp);
1957 *pArg = (tmp != 0);
1958 return ret;
1959}
1960
1961bool Parcel::readBool() const
1962{
1963 return readInt32() != 0;
1964}
1965
1966status_t Parcel::readChar(char16_t *pArg) const
1967{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001968 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001969 status_t ret = readInt32(&tmp);
1970 *pArg = char16_t(tmp);
1971 return ret;
1972}
1973
1974char16_t Parcel::readChar() const
1975{
1976 return char16_t(readInt32());
1977}
1978
1979status_t Parcel::readByte(int8_t *pArg) const
1980{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001981 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001982 status_t ret = readInt32(&tmp);
1983 *pArg = int8_t(tmp);
1984 return ret;
1985}
1986
1987int8_t Parcel::readByte() const
1988{
1989 return int8_t(readInt32());
1990}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001991
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001992status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1993 size_t utf16Size = 0;
1994 const char16_t* src = readString16Inplace(&utf16Size);
1995 if (!src) {
1996 return UNEXPECTED_NULL;
1997 }
1998
1999 // Save ourselves the trouble, we're done.
2000 if (utf16Size == 0u) {
2001 str->clear();
2002 return NO_ERROR;
2003 }
2004
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002005 // Allow for closing '\0'
2006 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2007 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002008 return BAD_VALUE;
2009 }
2010 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002011 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002012 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002013 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2014 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002015 return NO_ERROR;
2016}
2017
2018status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const {
2019 const int32_t start = dataPosition();
2020 int32_t size;
2021 status_t status = readInt32(&size);
2022 str->reset();
2023
2024 if (status != OK || size < 0) {
2025 return status;
2026 }
2027
2028 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002029 str->reset(new (std::nothrow) std::string());
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002030 return readUtf8FromUtf16(str->get());
2031}
2032
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002033const char* Parcel::readCString() const
2034{
2035 const size_t avail = mDataSize-mDataPos;
2036 if (avail > 0) {
2037 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2038 // is the string's trailing NUL within the parcel's valid bounds?
2039 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2040 if (eos) {
2041 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002042 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002043 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 return str;
2045 }
2046 }
Yi Kong91635562018-06-07 14:38:36 -07002047 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048}
2049
2050String8 Parcel::readString8() const
2051{
Roshan Pius87b64d22016-07-18 12:51:02 -07002052 String8 retString;
2053 status_t status = readString8(&retString);
2054 if (status != OK) {
2055 // We don't care about errors here, so just return an empty string.
2056 return String8();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002057 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002058 return retString;
2059}
2060
2061status_t Parcel::readString8(String8* pArg) const
2062{
2063 int32_t size;
2064 status_t status = readInt32(&size);
2065 if (status != OK) {
2066 return status;
2067 }
2068 // watch for potential int overflow from size+1
2069 if (size < 0 || size >= INT32_MAX) {
2070 return BAD_VALUE;
2071 }
2072 // |writeString8| writes nothing for empty string.
2073 if (size == 0) {
2074 *pArg = String8();
2075 return OK;
2076 }
2077 const char* str = (const char*)readInplace(size + 1);
Yi Kong91635562018-06-07 14:38:36 -07002078 if (str == nullptr) {
Roshan Pius87b64d22016-07-18 12:51:02 -07002079 return BAD_VALUE;
2080 }
2081 pArg->setTo(str, size);
2082 return OK;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002083}
2084
2085String16 Parcel::readString16() const
2086{
2087 size_t len;
2088 const char16_t* str = readString16Inplace(&len);
2089 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002090 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091 return String16();
2092}
2093
Casey Dahlinb9872622015-11-25 15:09:45 -08002094status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
2095{
2096 const int32_t start = dataPosition();
2097 int32_t size;
2098 status_t status = readInt32(&size);
2099 pArg->reset();
2100
2101 if (status != OK || size < 0) {
2102 return status;
2103 }
2104
2105 setDataPosition(start);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002106 pArg->reset(new (std::nothrow) String16());
Casey Dahlinb9872622015-11-25 15:09:45 -08002107
2108 status = readString16(pArg->get());
2109
2110 if (status != OK) {
2111 pArg->reset();
2112 }
2113
2114 return status;
2115}
2116
Casey Dahlin451ff582015-10-19 18:12:18 -07002117status_t Parcel::readString16(String16* pArg) const
2118{
2119 size_t len;
2120 const char16_t* str = readString16Inplace(&len);
2121 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002122 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002123 return 0;
2124 } else {
2125 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002126 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002127 }
2128}
2129
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002130const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2131{
2132 int32_t size = readInt32();
2133 // watch for potential int overflow from size+1
2134 if (size >= 0 && size < INT32_MAX) {
2135 *outLen = size;
2136 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Yi Kong91635562018-06-07 14:38:36 -07002137 if (str != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002138 return str;
2139 }
2140 }
2141 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002142 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002143}
2144
Casey Dahlinf0c13772015-10-27 18:33:56 -07002145status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2146{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002147 status_t status = readNullableStrongBinder(val);
2148 if (status == OK && !val->get()) {
2149 status = UNEXPECTED_NULL;
2150 }
2151 return status;
2152}
2153
2154status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2155{
Casey Dahlinf0c13772015-10-27 18:33:56 -07002156 return unflatten_binder(ProcessState::self(), *this, val);
2157}
2158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002159sp<IBinder> Parcel::readStrongBinder() const
2160{
2161 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002162 // Note that a lot of code in Android reads binders by hand with this
2163 // method, and that code has historically been ok with getting nullptr
2164 // back (while ignoring error codes).
2165 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166 return val;
2167}
2168
2169wp<IBinder> Parcel::readWeakBinder() const
2170{
2171 wp<IBinder> val;
2172 unflatten_binder(ProcessState::self(), *this, &val);
2173 return val;
2174}
2175
Christopher Wiley97f048d2015-11-19 06:49:05 -08002176status_t Parcel::readParcelable(Parcelable* parcelable) const {
2177 int32_t have_parcelable = 0;
2178 status_t status = readInt32(&have_parcelable);
2179 if (status != OK) {
2180 return status;
2181 }
2182 if (!have_parcelable) {
2183 return UNEXPECTED_NULL;
2184 }
2185 return parcelable->readFromParcel(this);
2186}
2187
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08002188status_t Parcel::readValue(binder::Value* value) const {
2189 return value->readFromParcel(this);
2190}
2191
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002192int32_t Parcel::readExceptionCode() const
2193{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002194 binder::Status status;
2195 status.readFromParcel(*this);
2196 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002197}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002198
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002199native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002200{
2201 int numFds, numInts;
2202 status_t err;
2203 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002204 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002205 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002206 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002207
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002208 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002209 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002210 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002211 }
2212
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002213 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002214 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002215 if (h->data[i] < 0) {
2216 for (int j = 0; j < i; j++) {
2217 close(h->data[j]);
2218 }
2219 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002220 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002221 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002222 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002223 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002224 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002225 native_handle_close(h);
2226 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002227 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002228 }
2229 return h;
2230}
2231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232int Parcel::readFileDescriptor() const
2233{
2234 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002235
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002236 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002237 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002240 return BAD_TYPE;
2241}
2242
Dianne Hackborn1941a402016-08-29 12:30:43 -07002243int Parcel::readParcelFileDescriptor() const
2244{
2245 int32_t hasComm = readInt32();
2246 int fd = readFileDescriptor();
2247 if (hasComm != 0) {
2248 // skip
2249 readFileDescriptor();
2250 }
2251 return fd;
2252}
2253
Christopher Wiley2cf19952016-04-11 11:09:37 -07002254status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002255{
2256 int got = readFileDescriptor();
2257
2258 if (got == BAD_TYPE) {
2259 return BAD_TYPE;
2260 }
2261
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002262 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002263
2264 if (val->get() < 0) {
2265 return BAD_VALUE;
2266 }
2267
2268 return OK;
2269}
2270
Ryo Hashimotobf551892018-05-31 16:58:35 +09002271status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2272{
2273 int got = readParcelFileDescriptor();
2274
2275 if (got == BAD_TYPE) {
2276 return BAD_TYPE;
2277 }
2278
2279 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2280
2281 if (val->get() < 0) {
2282 return BAD_VALUE;
2283 }
2284
2285 return OK;
2286}
Casey Dahlin06673e32015-11-23 13:24:23 -08002287
Christopher Wiley2cf19952016-04-11 11:09:37 -07002288status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const {
Casey Dahlinb9872622015-11-25 15:09:45 -08002289 return readNullableTypedVector(val, &Parcel::readUniqueFileDescriptor);
2290}
2291
Christopher Wiley2cf19952016-04-11 11:09:37 -07002292status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002293 return readTypedVector(val, &Parcel::readUniqueFileDescriptor);
2294}
2295
Jeff Brown5707dbf2011-09-23 21:17:56 -07002296status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2297{
Jeff Brown13b16042014-11-11 16:44:25 -08002298 int32_t blobType;
2299 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002300 if (status) return status;
2301
Jeff Brown13b16042014-11-11 16:44:25 -08002302 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002303 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002304 const void* ptr = readInplace(len);
2305 if (!ptr) return BAD_VALUE;
2306
Jeff Brown13b16042014-11-11 16:44:25 -08002307 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002308 return NO_ERROR;
2309 }
2310
Steve Block6807e592011-10-20 11:56:00 +01002311 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002312 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002313 int fd = readFileDescriptor();
2314 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2315
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002316 if (!ashmem_valid(fd)) {
2317 ALOGE("invalid fd");
2318 return BAD_VALUE;
2319 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002320 int size = ashmem_get_size_region(fd);
2321 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002322 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002323 return BAD_VALUE;
2324 }
Yi Kong91635562018-06-07 14:38:36 -07002325 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002326 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002327 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002328
Jeff Brown13b16042014-11-11 16:44:25 -08002329 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002330 return NO_ERROR;
2331}
2332
Mathias Agopiane1424282013-07-29 21:24:40 -07002333status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002334{
2335 // size
2336 const size_t len = this->readInt32();
2337 const size_t fd_count = this->readInt32();
2338
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002339 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002340 // don't accept size_t values which may have come from an
2341 // inadvertent conversion from a negative int.
2342 return BAD_VALUE;
2343 }
2344
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002345 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002346 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002347 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002348 return BAD_VALUE;
2349
Yi Kong91635562018-06-07 14:38:36 -07002350 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002351 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002352 fds = new (std::nothrow) int[fd_count];
2353 if (fds == nullptr) {
2354 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2355 return BAD_VALUE;
2356 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002357 }
2358
2359 status_t err = NO_ERROR;
2360 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002361 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002362 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002363 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002364 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 -07002365 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2366 // Close all the file descriptors that were dup-ed.
2367 for (size_t j=0; j<i ;j++) {
2368 close(fds[j]);
2369 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002370 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002371 }
2372
2373 if (err == NO_ERROR) {
2374 err = val.unflatten(buf, len, fds, fd_count);
2375 }
2376
2377 if (fd_count) {
2378 delete [] fds;
2379 }
2380
2381 return err;
2382}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2384{
2385 const size_t DPOS = mDataPos;
2386 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2387 const flat_binder_object* obj
2388 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2389 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002390 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002391 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 // the object list, so we don't want to check for it when
2393 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002394 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395 return obj;
2396 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002397
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002398 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002399 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 const size_t N = mObjectsSize;
2401 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002404 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 // Start at the current hint position, looking for an object at
2408 // the current data position.
2409 if (opos < N) {
2410 while (opos < (N-1) && OBJS[opos] < DPOS) {
2411 opos++;
2412 }
2413 } else {
2414 opos = N-1;
2415 }
2416 if (OBJS[opos] == DPOS) {
2417 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002418 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002419 this, DPOS, opos);
2420 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002421 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002422 return obj;
2423 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 // Look backwards for it...
2426 while (opos > 0 && OBJS[opos] > DPOS) {
2427 opos--;
2428 }
2429 if (OBJS[opos] == DPOS) {
2430 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 this, DPOS, opos);
2433 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002434 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 return obj;
2436 }
2437 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002438 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 -07002439 this, DPOS);
2440 }
Yi Kong91635562018-06-07 14:38:36 -07002441 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442}
2443
2444void Parcel::closeFileDescriptors()
2445{
2446 size_t i = mObjectsSize;
2447 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002448 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 }
2450 while (i > 0) {
2451 i--;
2452 const flat_binder_object* flat
2453 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002454 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002455 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 close(flat->handle);
2457 }
2458 }
2459}
2460
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002461uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002463 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464}
2465
2466size_t Parcel::ipcDataSize() const
2467{
2468 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2469}
2470
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002471uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002473 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474}
2475
2476size_t Parcel::ipcObjectsCount() const
2477{
2478 return mObjectsSize;
2479}
2480
2481void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002482 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002483{
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002484 binder_size_t minOffset = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002485 freeDataNoInit();
2486 mError = NO_ERROR;
2487 mData = const_cast<uint8_t*>(data);
2488 mDataSize = mDataCapacity = dataSize;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002492 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 mObjectsSize = mObjectsCapacity = objectsCount;
2494 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002495 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 mOwner = relFunc;
2497 mOwnerCookie = relCookie;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002498 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002499 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002500 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002501 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002502 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002503 mObjectsSize = 0;
2504 break;
2505 }
2506 minOffset = offset + sizeof(flat_binder_object);
2507 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 scanForFds();
2509}
2510
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002511void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512{
2513 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002514
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 if (errorCheck() != NO_ERROR) {
2516 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002517 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002518 } else if (dataSize() > 0) {
2519 const uint8_t* DATA = data();
2520 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002521 const binder_size_t* OBJS = objects();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 const size_t N = objectsCount();
2523 for (size_t i=0; i<N; i++) {
2524 const flat_binder_object* flat
2525 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2526 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002527 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528 << " = " << flat->binder;
2529 }
2530 } else {
2531 to << "NULL";
2532 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 to << ")";
2535}
2536
2537void Parcel::releaseObjects()
2538{
2539 const sp<ProcessState> proc(ProcessState::self());
2540 size_t i = mObjectsSize;
2541 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002542 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 while (i > 0) {
2544 i--;
2545 const flat_binder_object* flat
2546 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002547 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 }
2549}
2550
2551void Parcel::acquireObjects()
2552{
2553 const sp<ProcessState> proc(ProcessState::self());
2554 size_t i = mObjectsSize;
2555 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002556 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 while (i > 0) {
2558 i--;
2559 const flat_binder_object* flat
2560 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Rooscbf37262015-10-22 16:12:53 -07002561 acquire_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 }
2563}
2564
2565void Parcel::freeData()
2566{
2567 freeDataNoInit();
2568 initState();
2569}
2570
2571void Parcel::freeDataNoInit()
2572{
2573 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002574 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002575 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2577 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002578 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002580 if (mData) {
2581 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002582 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin48fd7b42015-09-10 13:46:02 -07002583 if (mDataCapacity <= gParcelGlobalAllocSize) {
2584 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
2585 } else {
2586 gParcelGlobalAllocSize = 0;
2587 }
2588 if (gParcelGlobalAllocCount > 0) {
2589 gParcelGlobalAllocCount--;
2590 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002591 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002592 free(mData);
2593 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 if (mObjects) free(mObjects);
2595 }
2596}
2597
2598status_t Parcel::growData(size_t len)
2599{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002600 if (len > INT32_MAX) {
2601 // don't accept size_t values which may have come from an
2602 // inadvertent conversion from a negative int.
2603 return BAD_VALUE;
2604 }
2605
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 size_t newSize = ((mDataSize+len)*3)/2;
2607 return (newSize <= mDataSize)
2608 ? (status_t) NO_MEMORY
2609 : continueWrite(newSize);
2610}
2611
2612status_t Parcel::restartWrite(size_t desired)
2613{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002614 if (desired > INT32_MAX) {
2615 // don't accept size_t values which may have come from an
2616 // inadvertent conversion from a negative int.
2617 return BAD_VALUE;
2618 }
2619
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 if (mOwner) {
2621 freeData();
2622 return continueWrite(desired);
2623 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 uint8_t* data = (uint8_t*)realloc(mData, desired);
2626 if (!data && desired > mDataCapacity) {
2627 mError = NO_MEMORY;
2628 return NO_MEMORY;
2629 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002630
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002631 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002634 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002635 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002636 gParcelGlobalAllocSize += desired;
2637 gParcelGlobalAllocSize -= mDataCapacity;
Colin Cross83ec65e2015-12-08 17:15:50 -08002638 if (!mData) {
2639 gParcelGlobalAllocCount++;
2640 }
Dianne Hackborna4cff882014-11-13 17:07:40 -08002641 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 mData = data;
2643 mDataCapacity = desired;
2644 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002647 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2648 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2649
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002651 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652 mObjectsSize = mObjectsCapacity = 0;
2653 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002654 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655 mHasFds = false;
2656 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002657 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659 return NO_ERROR;
2660}
2661
2662status_t Parcel::continueWrite(size_t desired)
2663{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002664 if (desired > INT32_MAX) {
2665 // don't accept size_t values which may have come from an
2666 // inadvertent conversion from a negative int.
2667 return BAD_VALUE;
2668 }
2669
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670 // If shrinking, first adjust for any objects that appear
2671 // after the new data size.
2672 size_t objectsSize = mObjectsSize;
2673 if (desired < mDataSize) {
2674 if (desired == 0) {
2675 objectsSize = 0;
2676 } else {
2677 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002678 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 break;
2680 objectsSize--;
2681 }
2682 }
2683 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002684
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002685 if (mOwner) {
2686 // If the size is going to zero, just release the owner's data.
2687 if (desired == 0) {
2688 freeData();
2689 return NO_ERROR;
2690 }
2691
2692 // If there is a different owner, we need to take
2693 // posession.
2694 uint8_t* data = (uint8_t*)malloc(desired);
2695 if (!data) {
2696 mError = NO_MEMORY;
2697 return NO_MEMORY;
2698 }
Yi Kong91635562018-06-07 14:38:36 -07002699 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002700
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002702 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002703 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002704 free(data);
2705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002706 mError = NO_MEMORY;
2707 return NO_MEMORY;
2708 }
2709
2710 // Little hack to only acquire references on objects
2711 // we will be keeping.
2712 size_t oldObjectsSize = mObjectsSize;
2713 mObjectsSize = objectsSize;
2714 acquireObjects();
2715 mObjectsSize = oldObjectsSize;
2716 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002718 if (mData) {
2719 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2720 }
2721 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002722 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002723 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002724 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002725 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
Yi Kong91635562018-06-07 14:38:36 -07002726 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002727
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002728 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002729 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002730 gParcelGlobalAllocSize += desired;
2731 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002732 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 mData = data;
2735 mObjects = objects;
2736 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002737 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 mDataCapacity = desired;
2739 mObjectsSize = mObjectsCapacity = objectsSize;
2740 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002741 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742
2743 } else if (mData) {
2744 if (objectsSize < mObjectsSize) {
2745 // Need to release refs on any objects we are dropping.
2746 const sp<ProcessState> proc(ProcessState::self());
2747 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2748 const flat_binder_object* flat
2749 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002750 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002751 // will need to rescan because we may have lopped off the only FDs
2752 mFdsKnown = false;
2753 }
Adrian Rooscbf37262015-10-22 16:12:53 -07002754 release_object(proc, *flat, this, &mOpenAshmemSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 }
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002756 binder_size_t* objects =
2757 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 if (objects) {
2759 mObjects = objects;
2760 }
2761 mObjectsSize = objectsSize;
2762 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002763 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002764 }
2765
2766 // We own the data, so we can just do a realloc().
2767 if (desired > mDataCapacity) {
2768 uint8_t* data = (uint8_t*)realloc(mData, desired);
2769 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002770 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2771 desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002772 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002773 gParcelGlobalAllocSize += desired;
2774 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002775 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002776 mData = data;
2777 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002778 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002779 mError = NO_MEMORY;
2780 return NO_MEMORY;
2781 }
2782 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002783 if (mDataSize > desired) {
2784 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002785 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002786 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002787 if (mDataPos > desired) {
2788 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002789 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 }
2791 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002792
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 } else {
2794 // This is the first data. Easy!
2795 uint8_t* data = (uint8_t*)malloc(desired);
2796 if (!data) {
2797 mError = NO_MEMORY;
2798 return NO_MEMORY;
2799 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002800
Yi Kong91635562018-06-07 14:38:36 -07002801 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002803 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002805
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002806 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborna4cff882014-11-13 17:07:40 -08002807 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002808 gParcelGlobalAllocSize += desired;
2809 gParcelGlobalAllocCount++;
Dianne Hackborna4cff882014-11-13 17:07:40 -08002810 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002811
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812 mData = data;
2813 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002814 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2815 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 mDataCapacity = desired;
2817 }
2818
2819 return NO_ERROR;
2820}
2821
2822void Parcel::initState()
2823{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002824 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002826 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002827 mDataSize = 0;
2828 mDataCapacity = 0;
2829 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002830 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2831 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07002832 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002833 mObjectsSize = 0;
2834 mObjectsCapacity = 0;
2835 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002836 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 mHasFds = false;
2838 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002839 mAllowFds = true;
Yi Kong91635562018-06-07 14:38:36 -07002840 mOwner = nullptr;
Adrian Rooscbf37262015-10-22 16:12:53 -07002841 mOpenAshmemSize = 0;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002842
2843 // racing multiple init leads only to multiple identical write
2844 if (gMaxFds == 0) {
2845 struct rlimit result;
2846 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2847 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002848 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002849 } else {
2850 ALOGW("Unable to getrlimit: %s", strerror(errno));
2851 gMaxFds = 1024;
2852 }
2853 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854}
2855
2856void Parcel::scanForFds() const
2857{
2858 bool hasFds = false;
2859 for (size_t i=0; i<mObjectsSize; i++) {
2860 const flat_binder_object* flat
2861 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002862 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 hasFds = true;
2864 break;
2865 }
2866 }
2867 mHasFds = hasFds;
2868 mFdsKnown = true;
2869}
2870
Dan Sandleraa5c2342015-04-10 10:08:45 -04002871size_t Parcel::getBlobAshmemSize() const
2872{
Adrian Roos6bb31142015-10-22 16:46:12 -07002873 // This used to return the size of all blobs that were written to ashmem, now we're returning
2874 // the ashmem currently referenced by this Parcel, which should be equivalent.
2875 // TODO: Remove method once ABI can be changed.
2876 return mOpenAshmemSize;
Dan Sandleraa5c2342015-04-10 10:08:45 -04002877}
2878
Adrian Rooscbf37262015-10-22 16:12:53 -07002879size_t Parcel::getOpenAshmemSize() const
2880{
2881 return mOpenAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002882}
2883
2884// --- Parcel::Blob ---
2885
2886Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002887 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002888}
2889
2890Parcel::Blob::~Blob() {
2891 release();
2892}
2893
2894void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002895 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002896 ::munmap(mData, mSize);
2897 }
2898 clear();
2899}
2900
Jeff Brown13b16042014-11-11 16:44:25 -08002901void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2902 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002903 mData = data;
2904 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002905 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002906}
2907
2908void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002909 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002910 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002911 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002912 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002913}
2914
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002915}; // namespace android