blob: 16944a6221730bafe930873d2f3e8908f61a8acb [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>
Andrei Homescua9cca9c2022-05-03 04:48:01 +000028#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
31#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>
Steven Moreland6e5a7752019-08-05 20:30:14 -070038#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080039#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070040#include <binder/TextOutput.h>
41
Frederick Mayle69a0c992022-05-26 20:38:39 +000042#include <android-base/scopeguard.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.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>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000047#include <utils/String8.h>
48#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Andrei Homescu24ad36e2022-08-04 01:33:33 +000050#include "OS.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000054
55// A lot of code in this file uses definitions from the
56// Linux kernel header for Binder <linux/android/binder.h>
57// which is included indirectly via "binder_module.h".
58// Non-Linux OSes do not have that header, so libbinder should be
59// built for those targets without kernel binder support, i.e.,
60// without BINDER_WITH_KERNEL_IPC. For this reason, all code in this
61// file that depends on kernel binder, including the header itself,
62// is conditional on BINDER_WITH_KERNEL_IPC.
63#ifdef BINDER_WITH_KERNEL_IPC
64#include <linux/sched.h>
Steven Moreland6ba5a252021-05-04 22:49:00 +000065#include "binder_module.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000066#else // BINDER_WITH_KERNEL_IPC
67// Needed by {read,write}Pointer
68typedef uintptr_t binder_uintptr_t;
69#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000072// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080073#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000074// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
76// ---------------------------------------------------------------------------
77
Nick Kralevichb6b14232015-04-02 09:36:02 -070078// This macro should never be used at runtime, as a too large value
79// of s could cause an integer overflow. Instead, you should always
80// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000081#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070082
83static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070084 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070085 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070086 }
87 return PAD_SIZE_UNSAFE(s);
88}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070089
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070090// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060091#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070093namespace android {
94
Steven Moreland7b102262019-08-01 15:48:43 -070095// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000096#ifdef __LP64__
97static_assert(sizeof(Parcel) == 120);
98#else
99static_assert(sizeof(Parcel) == 60);
100#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700101
Jeff Sharkey8994c182020-09-11 12:07:10 -0600102static std::atomic<size_t> gParcelGlobalAllocCount;
103static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800104
Andrei Homescu1519b982022-06-09 02:04:44 +0000105// Maximum number of file descriptors per Parcel.
106constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700107
Jeff Brown13b16042014-11-11 16:44:25 -0800108// Maximum size of a blob to transfer in-place.
109static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
110
111enum {
112 BLOB_INPLACE = 0,
113 BLOB_ASHMEM_IMMUTABLE = 1,
114 BLOB_ASHMEM_MUTABLE = 2,
115};
116
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000117#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700118static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
119 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700120 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 case BINDER_TYPE_BINDER:
122 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000123 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800124 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 }
126 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 case BINDER_TYPE_HANDLE: {
128 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700129 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
131 b->incStrong(who);
132 }
133 return;
134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 return;
137 }
138 }
139
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700140 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141}
142
Steven Morelandc673f1f2021-10-07 18:23:35 -0700143static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
144 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700145 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_BINDER:
147 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000148 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800149 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 }
151 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_HANDLE: {
153 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700154 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
156 b->decStrong(who);
157 }
158 return;
159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800161 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800162 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700163 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 }
166 }
167
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700168 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000170#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171
Frederick Mayle69a0c992022-05-26 20:38:39 +0000172static int toRawFd(const std::variant<base::unique_fd, base::borrowed_fd>& v) {
173 return std::visit([](const auto& fd) { return fd.get(); }, v);
174}
175
Frederick Mayled3c595a2022-05-09 23:02:54 +0000176Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
177 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
178}
179
Steven Moreland34b48cb2020-12-01 22:45:38 +0000180status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700181{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700182 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700183 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000184 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185}
186
Steven Morelanda86a3562019-08-01 23:28:34 +0000187status_t Parcel::finishUnflattenBinder(
188 const sp<IBinder>& binder, sp<IBinder>* out) const
189{
190 int32_t stability;
191 status_t status = readInt32(&stability);
192 if (status != OK) return status;
193
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000194 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
195 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000196 if (status != OK) return status;
197
198 *out = binder;
199 return OK;
200}
201
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000202#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000203static constexpr inline int schedPolicyMask(int policy, int priority) {
204 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
205}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000206#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000207
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500208status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
209 BBinder* local = nullptr;
210 if (binder) local = binder->localBinder();
211 if (local) local->setParceled();
212
Frederick Mayled3c595a2022-05-09 23:02:54 +0000213 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000214 if (binder) {
215 status_t status = writeInt32(1); // non-null
216 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700217 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000218 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000219 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
220 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000221 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700222 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000223 if (status != OK) return status;
224 } else {
225 status_t status = writeInt32(0); // null
226 if (status != OK) return status;
227 }
228 return finishFlattenBinder(binder);
229 }
230
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000231#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700233
Steven Morelandbf1915b2020-07-16 22:43:02 +0000234 int schedBits = 0;
235 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
236 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700237 }
238
Yi Kong91635562018-06-07 14:38:36 -0700239 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 if (!local) {
241 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700242 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000243 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000244 } else {
245 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700246 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000247 return INVALID_OPERATION;
248 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
Steven Moreland99157622021-09-13 16:27:34 -0700250 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700251 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800252 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000253 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800255 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000257 int policy = local->getMinSchedulerPolicy();
258 int priority = local->getMinSchedulerPriority();
259
260 if (policy != 0 || priority != 0) {
261 // override value, since it is set explicitly
262 schedBits = schedPolicyMask(policy, priority);
263 }
Steven Moreland49c27532022-03-01 10:21:07 +0000264 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800265 if (local->isRequestingSid()) {
266 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
267 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000268 if (local->isInheritRt()) {
269 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
270 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700271 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
273 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 }
275 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700276 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000277 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800278 obj.binder = 0;
279 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700281
Steven Morelandbf1915b2020-07-16 22:43:02 +0000282 obj.flags |= schedBits;
283
Steven Moreland34b48cb2020-12-01 22:45:38 +0000284 status_t status = writeObject(obj, false);
285 if (status != OK) return status;
286
287 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000288#else // BINDER_WITH_KERNEL_IPC
289 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
290 return INVALID_OPERATION;
291#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292}
293
Steven Morelanda86a3562019-08-01 23:28:34 +0000294status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000296 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700297 int32_t isPresent;
298 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000299 if (status != OK) return status;
300
301 sp<IBinder> binder;
302
Steven Moreland5623d1a2021-09-10 15:45:34 -0700303 if (isPresent & 1) {
304 uint64_t addr;
305 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000306 if (status_t status =
307 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
308 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000309 status != OK)
310 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000311 if (status_t status =
312 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
313 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700314 status != OK)
315 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000316 }
317
318 return finishUnflattenBinder(binder, out);
319 }
320
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000321#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000322 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700325 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000326 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000327 sp<IBinder> binder =
328 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000329 return finishUnflattenBinder(binder, out);
330 }
331 case BINDER_TYPE_HANDLE: {
332 sp<IBinder> binder =
333 ProcessState::self()->getStrongProxyForHandle(flat->handle);
334 return finishUnflattenBinder(binder, out);
335 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700336 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337 }
338 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000339#else // BINDER_WITH_KERNEL_IPC
340 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
341 return INVALID_OPERATION;
342#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343}
344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345// ---------------------------------------------------------------------------
346
347Parcel::Parcel()
348{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700350 initState();
351}
352
353Parcel::~Parcel()
354{
355 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: destroyed", this);
357}
358
359size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600360 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800361}
362
363size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600364 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365}
366
367const uint8_t* Parcel::data() const
368{
369 return mData;
370}
371
372size_t Parcel::dataSize() const
373{
374 return (mDataSize > mDataPos ? mDataSize : mDataPos);
375}
376
Pawan Waghd886a442023-01-21 02:16:38 +0000377size_t Parcel::dataBufferSize() const {
378 return mDataSize;
379}
380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381size_t Parcel::dataAvail() const
382{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700383 size_t result = dataSize() - dataPosition();
384 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700385 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700386 }
387 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700388}
389
390size_t Parcel::dataPosition() const
391{
392 return mDataPos;
393}
394
395size_t Parcel::dataCapacity() const
396{
397 return mDataCapacity;
398}
399
400status_t Parcel::setDataSize(size_t size)
401{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700402 if (size > INT32_MAX) {
403 // don't accept size_t values which may have come from an
404 // inadvertent conversion from a negative int.
405 return BAD_VALUE;
406 }
407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700408 status_t err;
409 err = continueWrite(size);
410 if (err == NO_ERROR) {
411 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700412 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700413 }
414 return err;
415}
416
417void Parcel::setDataPosition(size_t pos) const
418{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700419 if (pos > INT32_MAX) {
420 // don't accept size_t values which may have come from an
421 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700422 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700423 }
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000426 if (const auto* kernelFields = maybeKernelFields()) {
427 kernelFields->mNextObjectHint = 0;
428 kernelFields->mObjectsSorted = false;
429 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700430}
431
432status_t Parcel::setDataCapacity(size_t size)
433{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700434 if (size > INT32_MAX) {
435 // don't accept size_t values which may have come from an
436 // inadvertent conversion from a negative int.
437 return BAD_VALUE;
438 }
439
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700440 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700441 return NO_ERROR;
442}
443
444status_t Parcel::setData(const uint8_t* buffer, size_t len)
445{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700446 if (len > INT32_MAX) {
447 // don't accept size_t values which may have come from an
448 // inadvertent conversion from a negative int.
449 return BAD_VALUE;
450 }
451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 status_t err = restartWrite(len);
453 if (err == NO_ERROR) {
454 memcpy(const_cast<uint8_t*>(data()), buffer, len);
455 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000456 if (auto* kernelFields = maybeKernelFields()) {
457 kernelFields->mFdsKnown = false;
458 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700459 }
460 return err;
461}
462
Frederick Mayled3c595a2022-05-09 23:02:54 +0000463status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
464 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700465 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
466 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000467 return BAD_TYPE;
468 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000469 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
470 ALOGE("Cannot append Parcels from different sessions");
471 return BAD_TYPE;
472 }
Steven Moreland67753c32021-04-02 18:45:19 +0000473
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000475 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700476 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477
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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700495 if ((mDataSize+len) > mDataCapacity) {
496 // grow data
497 err = growData(len);
498 if (err != NO_ERROR) {
499 return err;
500 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501 }
502
503 // append data
504 memcpy(mData + mDataPos, data + offset, len);
505 mDataPos += len;
506 mDataSize += len;
507
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400508 err = NO_ERROR;
509
Frederick Mayled3c595a2022-05-09 23:02:54 +0000510 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000511#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000512 auto* otherKernelFields = parcel->maybeKernelFields();
513 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
514
515 const binder_size_t* objects = otherKernelFields->mObjects;
516 size_t size = otherKernelFields->mObjectsSize;
517 // Count objects in range
518 int firstIndex = -1, lastIndex = -2;
519 for (int i = 0; i < (int)size; i++) {
520 size_t off = objects[i];
521 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
522 if (firstIndex == -1) {
523 firstIndex = i;
524 }
525 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700526 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000528 int numObjects = lastIndex - firstIndex + 1;
529 if (numObjects > 0) {
530 const sp<ProcessState> proc(ProcessState::self());
531 // grow objects
532 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
533 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
534 return NO_MEMORY; // overflow
535 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
536 return NO_MEMORY; // overflow
537 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
538 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
539 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
540 newSize * sizeof(binder_size_t));
541 if (objects == (binder_size_t*)nullptr) {
542 return NO_MEMORY;
543 }
544 kernelFields->mObjects = objects;
545 kernelFields->mObjectsCapacity = newSize;
546 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700547
Frederick Mayled3c595a2022-05-09 23:02:54 +0000548 // append and acquire objects
549 int idx = kernelFields->mObjectsSize;
550 for (int i = firstIndex; i <= lastIndex; i++) {
551 size_t off = objects[i] - offset + startPos;
552 kernelFields->mObjects[idx++] = off;
553 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700554
Frederick Mayled3c595a2022-05-09 23:02:54 +0000555 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
556 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557
Frederick Mayled3c595a2022-05-09 23:02:54 +0000558 if (flat->hdr.type == BINDER_TYPE_FD) {
559 // If this is a file descriptor, we need to dup it so the
560 // new Parcel now owns its own fd, and can declare that we
561 // officially know we have fds.
562 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
563 flat->cookie = 1;
564 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
565 if (!mAllowFds) {
566 err = FDS_NOT_ALLOWED;
567 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400568 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569 }
570 }
Andrei Homescu88012462022-07-07 04:30:05 +0000571#else
572 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
573 return INVALID_OPERATION;
574#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000575 } else {
576 auto* rpcFields = maybeRpcFields();
577 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
578 auto* otherRpcFields = parcel->maybeRpcFields();
579 if (otherRpcFields == nullptr) {
580 return BAD_TYPE;
581 }
582 if (rpcFields->mSession != otherRpcFields->mSession) {
583 return BAD_TYPE;
584 }
585
586 const size_t savedDataPos = mDataPos;
587 base::ScopeGuard scopeGuard = [&]() { mDataPos = savedDataPos; };
588
589 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
590 if (otherRpcFields->mFds != nullptr) {
591 if (rpcFields->mFds == nullptr) {
592 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
593 }
594 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
595 }
596 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
597 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
598 if (offset <= objPos && objPos < offset + len) {
599 size_t newDataPos = objPos - offset + startPos;
600 rpcFields->mObjectPositions.push_back(newDataPos);
601
602 mDataPos = newDataPos;
603 int32_t objectType;
604 if (status_t status = readInt32(&objectType); status != OK) {
605 return status;
606 }
607 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
608 continue;
609 }
610
611 if (!mAllowFds) {
612 return FDS_NOT_ALLOWED;
613 }
614
615 // Read FD, duplicate, and add to list.
616 int32_t fdIndex;
617 if (status_t status = readInt32(&fdIndex); status != OK) {
618 return status;
619 }
Andrei Homescufc221502022-10-08 03:51:17 +0000620 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000621 // To match kernel binder behavior, we always dup, even if the
622 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000623 int newFd = -1;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000624 if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) {
Andrei Homescufc221502022-10-08 03:51:17 +0000625 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
626 }
627 rpcFields->mFds->emplace_back(base::unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000628 // Fixup the index in the data.
629 mDataPos = newDataPos + 4;
630 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
631 return status;
632 }
633 }
634 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635 }
636
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400637 return err;
638}
639
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700640int Parcel::compareData(const Parcel& other) {
641 size_t size = dataSize();
642 if (size != other.dataSize()) {
643 return size < other.dataSize() ? -1 : 1;
644 }
645 return memcmp(data(), other.data(), size);
646}
647
Bernardo Rufino897b1652021-10-08 10:30:20 +0100648status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
649 size_t len, int* result) const {
650 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
651 // Don't accept size_t values which may have come from an inadvertent conversion from a
652 // negative int.
653 return BAD_VALUE;
654 }
655 size_t thisLimit;
656 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
657 return BAD_VALUE;
658 }
659 size_t otherLimit;
660 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
661 return BAD_VALUE;
662 }
663 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
664 return NO_ERROR;
665}
666
Jeff Brown13b16042014-11-11 16:44:25 -0800667bool Parcel::allowFds() const
668{
669 return mAllowFds;
670}
671
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700672bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400673{
674 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700675 if (!allowFds) {
676 mAllowFds = false;
677 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400678 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679}
680
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700681void Parcel::restoreAllowFds(bool lastValue)
682{
683 mAllowFds = lastValue;
684}
685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700686bool Parcel::hasFileDescriptors() const
687{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000688 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000689 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000690 }
691 auto* kernelFields = maybeKernelFields();
692 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700693 scanForFds();
694 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000695 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700696}
697
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000698std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
699 std::vector<sp<IBinder>> ret;
700
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000701#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000702 const auto* kernelFields = maybeKernelFields();
703 if (kernelFields == nullptr) {
704 return ret;
705 }
706
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000707 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000708 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
709 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000710 const flat_binder_object* flat =
711 reinterpret_cast<const flat_binder_object*>(mData + offset);
712 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
713
714 setDataPosition(offset);
715
716 sp<IBinder> binder = readStrongBinder();
717 if (binder != nullptr) ret.push_back(binder);
718 }
719
720 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000721#endif // BINDER_WITH_KERNEL_IPC
722
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000723 return ret;
724}
725
726std::vector<int> Parcel::debugReadAllFileDescriptors() const {
727 std::vector<int> ret;
728
Frederick Mayle69a0c992022-05-26 20:38:39 +0000729 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000730#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000731 size_t initPosition = dataPosition();
732 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
733 binder_size_t offset = kernelFields->mObjects[i];
734 const flat_binder_object* flat =
735 reinterpret_cast<const flat_binder_object*>(mData + offset);
736 if (flat->hdr.type != BINDER_TYPE_FD) continue;
737
738 setDataPosition(offset);
739
740 int fd = readFileDescriptor();
741 LOG_ALWAYS_FATAL_IF(fd == -1);
742 ret.push_back(fd);
743 }
744 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000745#else
746 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
747#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000748 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
749 for (const auto& fd : *rpcFields->mFds) {
750 ret.push_back(toRawFd(fd));
751 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000752 }
753
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000754 return ret;
755}
756
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100757status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100758 if (len > INT32_MAX || offset > INT32_MAX) {
759 // Don't accept size_t values which may have come from an inadvertent conversion from a
760 // negative int.
761 return BAD_VALUE;
762 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100763 size_t limit;
764 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100765 return BAD_VALUE;
766 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100767 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000768 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000769#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000770 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
771 size_t pos = kernelFields->mObjects[i];
772 if (pos < offset) continue;
773 if (pos + sizeof(flat_binder_object) > offset + len) {
774 if (kernelFields->mObjectsSorted) {
775 break;
776 } else {
777 continue;
778 }
779 }
780 const flat_binder_object* flat =
781 reinterpret_cast<const flat_binder_object*>(mData + pos);
782 if (flat->hdr.type == BINDER_TYPE_FD) {
783 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000784 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000785 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100786 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000787#else
788 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
789 return INVALID_OPERATION;
790#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000791 } else if (const auto* rpcFields = maybeRpcFields()) {
792 for (uint32_t pos : rpcFields->mObjectPositions) {
793 if (offset <= pos && pos < limit) {
794 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
795 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
796 *result = true;
797 break;
798 }
799 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100800 }
801 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100802 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100803}
804
Steven Morelandf183fdd2020-10-27 00:12:12 +0000805void Parcel::markSensitive() const
806{
807 mDeallocZero = true;
808}
809
Steven Moreland5553ac42020-11-11 02:14:45 +0000810void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000811 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
812
Steven Moreland5553ac42020-11-11 02:14:45 +0000813 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700814 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000815 }
816}
817
Steven Morelandc9939062021-05-05 17:57:41 +0000818void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000819 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
820 "format must be set before data is written OR on IPC data");
821
Frederick Mayled3c595a2022-05-09 23:02:54 +0000822 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000823}
824
825bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000826 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000827}
828
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000829void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 auto* kernelFields = maybeKernelFields();
831 if (kernelFields == nullptr) {
832 return;
833 }
834
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000835 // Only update the request headers once. We only want to point
836 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000837 if (!kernelFields->mRequestHeaderPresent) {
838 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
839 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000840 }
841}
842
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000843#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandb6c7e222021-02-18 19:20:14 +0000844#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700845constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700846#elif defined(__ANDROID_RECOVERY__)
847constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700848#else
849constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
850#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000851#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700852
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700853// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700854status_t Parcel::writeInterfaceToken(const String16& interface)
855{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000856 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000857}
858
859status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000860 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000861#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000862 const IPCThreadState* threadState = IPCThreadState::self();
863 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
864 updateWorkSourceRequestHeaderPosition();
865 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
866 : IPCThreadState::kUnsetWorkSource);
867 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000868#else // BINDER_WITH_KERNEL_IPC
869 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
870 return INVALID_OPERATION;
871#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000872 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700874 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000875 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700876}
877
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000878bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
879{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000880 auto* kernelFields = maybeKernelFields();
881 if (kernelFields == nullptr) {
882 return false;
883 }
884 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000885 return false;
886 }
887
888 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000889 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000890 status_t err = writeInt32(uid);
891 setDataPosition(initialPosition);
892 return err == NO_ERROR;
893}
894
Steven Morelandf1b1e492019-05-06 15:05:13 -0700895uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000896{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000897 auto* kernelFields = maybeKernelFields();
898 if (kernelFields == nullptr) {
899 return false;
900 }
901 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000902 return IPCThreadState::kUnsetWorkSource;
903 }
904
905 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000906 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000907 uid_t uid = readInt32();
908 setDataPosition(initialPosition);
909 return uid;
910}
911
Mathias Agopian83c04462009-05-22 19:00:22 -0700912bool Parcel::checkInterface(IBinder* binder) const
913{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700914 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700915}
916
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700917bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700918 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000920 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700921}
922
923bool Parcel::enforceInterface(const char16_t* interface,
924 size_t len,
925 IPCThreadState* threadState) const
926{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000927 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000928#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000929 // StrictModePolicy.
930 int32_t strictPolicy = readInt32();
931 if (threadState == nullptr) {
932 threadState = IPCThreadState::self();
933 }
934 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
935 // For one-way calls, the callee is running entirely
936 // disconnected from the caller, so disable StrictMode entirely.
937 // Not only does disk/network usage not impact the caller, but
938 // there's no way to communicate back violations anyway.
939 threadState->setStrictModePolicy(0);
940 } else {
941 threadState->setStrictModePolicy(strictPolicy);
942 }
943 // WorkSource.
944 updateWorkSourceRequestHeaderPosition();
945 int32_t workSource = readInt32();
946 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
947 // vendor header
948 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000949
950 // fuzzers skip this check, because it is for protecting the underlying ABI, but
951 // we don't want it to reduce our coverage
952 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000953 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
954 header);
955 return false;
956 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000957#else // BINDER_WITH_KERNEL_IPC
958 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
959 (void)threadState;
960 return false;
961#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700962 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000963
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100964 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700965 size_t parcel_interface_len;
966 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
967 if (len == parcel_interface_len &&
968 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700969 return true;
970 } else {
Steven Morelande99d8822023-06-02 21:56:39 +0000971 if (mServiceFuzzing) {
972 // ignore. Theoretically, this could cause a few false positives, because
973 // people could assume things about getInterfaceDescriptor if they pass
974 // this point, but it would be extremely fragile. It's more important that
975 // we fuzz with the above things read from the Parcel.
976 return true;
977 } else {
Steven Moreland3a667492023-06-02 21:41:39 +0000978 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000979 String8(interface, len).c_str(),
980 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +0000981 return false;
Steven Moreland3a667492023-06-02 21:41:39 +0000982 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700983 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700984}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985
Pawan Wagh104654a2022-11-15 21:18:42 +0000986void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
987 mEnforceNoDataAvail = enforceNoDataAvail;
988}
989
Steven Moreland3a667492023-06-02 21:41:39 +0000990void Parcel::setServiceFuzzing() {
991 mServiceFuzzing = true;
992}
993
Steven Moreland418914a2023-06-28 21:47:14 +0000994bool Parcel::isServiceFuzzing() const {
995 return mServiceFuzzing;
996}
997
Jooyung Hand23f9502021-12-23 14:39:57 +0900998binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +0000999 if (!mEnforceNoDataAvail) {
1000 return binder::Status::ok();
1001 }
1002
Jooyung Hand23f9502021-12-23 14:39:57 +09001003 const auto n = dataAvail();
1004 if (n == 0) {
1005 return binder::Status::ok();
1006 }
1007 return binder::Status::
1008 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1009 String8::format("Parcel data not fully consumed, unread size: %zu",
1010 n));
1011}
1012
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001013size_t Parcel::objectsCount() const
1014{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001015 if (const auto* kernelFields = maybeKernelFields()) {
1016 return kernelFields->mObjectsSize;
1017 }
1018 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019}
1020
1021status_t Parcel::errorCheck() const
1022{
1023 return mError;
1024}
1025
1026void Parcel::setError(status_t err)
1027{
1028 mError = err;
1029}
1030
1031status_t Parcel::finishWrite(size_t len)
1032{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001033 if (len > INT32_MAX) {
1034 // don't accept size_t values which may have come from an
1035 // inadvertent conversion from a negative int.
1036 return BAD_VALUE;
1037 }
1038
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039 //printf("Finish write of %d\n", len);
1040 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001041 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001042 if (mDataPos > mDataSize) {
1043 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001044 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001045 }
1046 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1047 return NO_ERROR;
1048}
1049
1050status_t Parcel::writeUnpadded(const void* data, size_t len)
1051{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001052 if (len > INT32_MAX) {
1053 // don't accept size_t values which may have come from an
1054 // inadvertent conversion from a negative int.
1055 return BAD_VALUE;
1056 }
1057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058 size_t end = mDataPos + len;
1059 if (end < mDataPos) {
1060 // integer overflow
1061 return BAD_VALUE;
1062 }
1063
1064 if (end <= mDataCapacity) {
1065restart_write:
1066 memcpy(mData+mDataPos, data, len);
1067 return finishWrite(len);
1068 }
1069
1070 status_t err = growData(len);
1071 if (err == NO_ERROR) goto restart_write;
1072 return err;
1073}
1074
1075status_t Parcel::write(const void* data, size_t len)
1076{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001077 if (len > INT32_MAX) {
1078 // don't accept size_t values which may have come from an
1079 // inadvertent conversion from a negative int.
1080 return BAD_VALUE;
1081 }
1082
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001083 void* const d = writeInplace(len);
1084 if (d) {
1085 memcpy(d, data, len);
1086 return NO_ERROR;
1087 }
1088 return mError;
1089}
1090
1091void* Parcel::writeInplace(size_t len)
1092{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001093 if (len > INT32_MAX) {
1094 // don't accept size_t values which may have come from an
1095 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001096 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001097 }
1098
1099 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001101 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001103 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104 }
1105
1106 if ((mDataPos+padded) <= mDataCapacity) {
1107restart_write:
1108 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1109 uint8_t* const data = mData+mDataPos;
1110
1111 // Need to pad at end?
1112 if (padded != len) {
1113#if BYTE_ORDER == BIG_ENDIAN
1114 static const uint32_t mask[4] = {
1115 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1116 };
1117#endif
1118#if BYTE_ORDER == LITTLE_ENDIAN
1119 static const uint32_t mask[4] = {
1120 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1121 };
1122#endif
1123 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1124 // *reinterpret_cast<void**>(data+padded-4));
1125 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1126 }
1127
1128 finishWrite(padded);
1129 return data;
1130 }
1131
1132 status_t err = growData(padded);
1133 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001134 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001135}
1136
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001137status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1138 const uint8_t* strData = (uint8_t*)str.data();
1139 const size_t strLen= str.length();
1140 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001141 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001142 return BAD_VALUE;
1143 }
1144
1145 status_t err = writeInt32(utf16Len);
1146 if (err) {
1147 return err;
1148 }
1149
1150 // Allocate enough bytes to hold our converted string and its terminating NULL.
1151 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1152 if (!dst) {
1153 return NO_MEMORY;
1154 }
1155
Sergio Girof4607432016-07-21 14:46:35 +01001156 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001157
1158 return NO_ERROR;
1159}
1160
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001161
Andy Hung49198cf2020-11-18 11:02:39 -08001162status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1163status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001164
Andy Hung49198cf2020-11-18 11:02:39 -08001165status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1166status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001167
Andy Hung49198cf2020-11-18 11:02:39 -08001168status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1169status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1170status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1171status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1172status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1173status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1174status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1175status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1176status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1177status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1178status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1179status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1180status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1181status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1182status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1183status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1184status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1185status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1186status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1187status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1188status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1189status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1190status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1191status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1192status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1193status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1194status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001195
Andy Hung49198cf2020-11-18 11:02:39 -08001196status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001197status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001198 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001199status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001200 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001201status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001202 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001203status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001204 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1205status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001206
Andy Hung49198cf2020-11-18 11:02:39 -08001207status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1208status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1209status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1210
1211status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1212status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1213status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1214
1215status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1216
1217status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1218status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1219
1220status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1221status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1222
1223status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1224status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1225status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1226status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1227status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1228status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1229status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1230status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1231status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1232status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1233status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1234status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1235status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1236status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1237status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1238status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1239status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1240status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1241status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1242status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1243status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1244status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1245status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1246status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1247status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1248status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1249status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1250
1251status_t Parcel::readString16Vector(
1252 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1253status_t Parcel::readString16Vector(
1254 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1255status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1256status_t Parcel::readUtf8VectorFromUtf16Vector(
1257 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1258status_t Parcel::readUtf8VectorFromUtf16Vector(
1259 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1260status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1261
1262status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1263status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1264status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1265
1266status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1267status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1268status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1269
1270status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001271
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272status_t Parcel::writeInt32(int32_t val)
1273{
Andreas Huber84a6d042009-08-17 13:33:27 -07001274 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001275}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001276
1277status_t Parcel::writeUint32(uint32_t val)
1278{
1279 return writeAligned(val);
1280}
1281
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001282status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001283 if (len > INT32_MAX) {
1284 // don't accept size_t values which may have come from an
1285 // inadvertent conversion from a negative int.
1286 return BAD_VALUE;
1287 }
1288
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001289 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001290 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001291 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001292 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001293 if (ret == NO_ERROR) {
1294 ret = write(val, len * sizeof(*val));
1295 }
1296 return ret;
1297}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001298status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001299 if (len > INT32_MAX) {
1300 // don't accept size_t values which may have come from an
1301 // inadvertent conversion from a negative int.
1302 return BAD_VALUE;
1303 }
1304
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001305 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001306 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001307 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001308 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001309 if (ret == NO_ERROR) {
1310 ret = write(val, len * sizeof(*val));
1311 }
1312 return ret;
1313}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314
Casey Dahlind6848f52015-10-15 15:44:59 -07001315status_t Parcel::writeBool(bool val)
1316{
1317 return writeInt32(int32_t(val));
1318}
1319
1320status_t Parcel::writeChar(char16_t val)
1321{
1322 return writeInt32(int32_t(val));
1323}
1324
1325status_t Parcel::writeByte(int8_t val)
1326{
1327 return writeInt32(int32_t(val));
1328}
1329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330status_t Parcel::writeInt64(int64_t val)
1331{
Andreas Huber84a6d042009-08-17 13:33:27 -07001332 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001333}
1334
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001335status_t Parcel::writeUint64(uint64_t val)
1336{
1337 return writeAligned(val);
1338}
1339
Serban Constantinescuf683e012013-11-05 16:53:55 +00001340status_t Parcel::writePointer(uintptr_t val)
1341{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001342 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001343}
1344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345status_t Parcel::writeFloat(float val)
1346{
Andreas Huber84a6d042009-08-17 13:33:27 -07001347 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348}
1349
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001350#if defined(__mips__) && defined(__mips_hard_float)
1351
1352status_t Parcel::writeDouble(double val)
1353{
1354 union {
1355 double d;
1356 unsigned long long ll;
1357 } u;
1358 u.d = val;
1359 return writeAligned(u.ll);
1360}
1361
1362#else
1363
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364status_t Parcel::writeDouble(double val)
1365{
Andreas Huber84a6d042009-08-17 13:33:27 -07001366 return writeAligned(val);
1367}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001369#endif
1370
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001371status_t Parcel::writeCString(const char* str)
1372{
1373 return write(str, strlen(str)+1);
1374}
1375
1376status_t Parcel::writeString8(const String8& str)
1377{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001378 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001379}
1380
1381status_t Parcel::writeString8(const char* str, size_t len)
1382{
1383 if (str == nullptr) return writeInt32(-1);
1384
Jeff Sharkey18220902020-11-05 08:36:20 -07001385 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001386 status_t err = writeInt32(len);
1387 if (err == NO_ERROR) {
1388 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1389 if (data) {
1390 memcpy(data, str, len);
1391 *reinterpret_cast<char*>(data+len) = 0;
1392 return NO_ERROR;
1393 }
1394 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001395 }
1396 return err;
1397}
1398
1399status_t Parcel::writeString16(const String16& str)
1400{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001401 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402}
1403
1404status_t Parcel::writeString16(const char16_t* str, size_t len)
1405{
Yi Kong91635562018-06-07 14:38:36 -07001406 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001407
Jeff Sharkey18220902020-11-05 08:36:20 -07001408 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001409 status_t err = writeInt32(len);
1410 if (err == NO_ERROR) {
1411 len *= sizeof(char16_t);
1412 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1413 if (data) {
1414 memcpy(data, str, len);
1415 *reinterpret_cast<char16_t*>(data+len) = 0;
1416 return NO_ERROR;
1417 }
1418 err = mError;
1419 }
1420 return err;
1421}
1422
1423status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1424{
Steven Morelanda86a3562019-08-01 23:28:34 +00001425 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426}
1427
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001428
Casey Dahlinb9872622015-11-25 15:09:45 -08001429status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1430 if (!parcelable) {
1431 return writeInt32(0);
1432 }
1433
1434 return writeParcelable(*parcelable);
1435}
1436
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001437status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001438{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001439 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001440 return BAD_TYPE;
1441
1442 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001443 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001444 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001446 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001447 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001449 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1450 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451
1452 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001453 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 return err;
1455 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001456 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001457 return err;
1458}
1459
Frederick Mayle69a0c992022-05-26 20:38:39 +00001460status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1461 if (auto* rpcFields = maybeRpcFields()) {
1462 std::variant<base::unique_fd, base::borrowed_fd> fdVariant;
1463 if (takeOwnership) {
1464 fdVariant = base::unique_fd(fd);
1465 } else {
1466 fdVariant = base::borrowed_fd(fd);
1467 }
1468 if (!mAllowFds) {
1469 return FDS_NOT_ALLOWED;
1470 }
1471 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1472 case RpcSession::FileDescriptorTransportMode::NONE: {
1473 return FDS_NOT_ALLOWED;
1474 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001475 case RpcSession::FileDescriptorTransportMode::UNIX:
1476 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001477 if (rpcFields->mFds == nullptr) {
1478 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1479 }
1480 size_t dataPos = mDataPos;
1481 if (dataPos > UINT32_MAX) {
1482 return NO_MEMORY;
1483 }
1484 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1485 return err;
1486 }
1487 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1488 return err;
1489 }
1490 rpcFields->mObjectPositions.push_back(dataPos);
1491 rpcFields->mFds->push_back(std::move(fdVariant));
1492 return OK;
1493 }
1494 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001495 }
1496
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001497#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001498 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001499 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001500 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001501 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001502 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001503 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001504 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001505#else // BINDER_WITH_KERNEL_IPC
1506 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1507 (void)fd;
1508 (void)takeOwnership;
1509 return INVALID_OPERATION;
1510#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511}
1512
1513status_t Parcel::writeDupFileDescriptor(int fd)
1514{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001515 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001516 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001517 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001518 }
1519 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001520 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001521 close(dupFd);
1522 }
1523 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001524}
1525
Dianne Hackborn1941a402016-08-29 12:30:43 -07001526status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1527{
1528 writeInt32(0);
1529 return writeFileDescriptor(fd, takeOwnership);
1530}
1531
Ryo Hashimotobf551892018-05-31 16:58:35 +09001532status_t Parcel::writeDupParcelFileDescriptor(int fd)
1533{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001534 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001535 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001536 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001537 }
1538 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1539 if (err != OK) {
1540 close(dupFd);
1541 }
1542 return err;
1543}
1544
Christopher Wiley2cf19952016-04-11 11:09:37 -07001545status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001546 return writeDupFileDescriptor(fd.get());
1547}
1548
Jeff Brown13b16042014-11-11 16:44:25 -08001549status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001550{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001551 if (len > INT32_MAX) {
1552 // don't accept size_t values which may have come from an
1553 // inadvertent conversion from a negative int.
1554 return BAD_VALUE;
1555 }
1556
Jeff Brown13b16042014-11-11 16:44:25 -08001557 status_t status;
1558 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001559 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001560 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001561 if (status) return status;
1562
1563 void* ptr = writeInplace(len);
1564 if (!ptr) return NO_MEMORY;
1565
Jeff Brown13b16042014-11-11 16:44:25 -08001566 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001567 return NO_ERROR;
1568 }
1569
Steve Block6807e592011-10-20 11:56:00 +01001570 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001571 int fd = ashmem_create_region("Parcel Blob", len);
1572 if (fd < 0) return NO_MEMORY;
1573
1574 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1575 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001576 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001577 } else {
Yi Kong91635562018-06-07 14:38:36 -07001578 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001579 if (ptr == MAP_FAILED) {
1580 status = -errno;
1581 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001582 if (!mutableCopy) {
1583 result = ashmem_set_prot_region(fd, PROT_READ);
1584 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001585 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001586 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001587 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001588 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001589 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001590 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001591 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001592 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001593 return NO_ERROR;
1594 }
1595 }
1596 }
1597 }
1598 ::munmap(ptr, len);
1599 }
1600 ::close(fd);
1601 return status;
1602}
1603
Jeff Brown13b16042014-11-11 16:44:25 -08001604status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1605{
1606 // Must match up with what's done in writeBlob.
1607 if (!mAllowFds) return FDS_NOT_ALLOWED;
1608 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1609 if (status) return status;
1610 return writeDupFileDescriptor(fd);
1611}
1612
Mathias Agopiane1424282013-07-29 21:24:40 -07001613status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001614{
1615 status_t err;
1616
1617 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001618 const size_t len = val.getFlattenedSize();
1619 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001620
Andrei Homescu1519b982022-06-09 02:04:44 +00001621 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001622 // don't accept size_t values which may have come from an
1623 // inadvertent conversion from a negative int.
1624 return BAD_VALUE;
1625 }
1626
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001627 err = this->writeInt32(len);
1628 if (err) return err;
1629
1630 err = this->writeInt32(fd_count);
1631 if (err) return err;
1632
1633 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001634 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001635 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001636 return BAD_VALUE;
1637
Yi Kong91635562018-06-07 14:38:36 -07001638 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001639 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001640 fds = new (std::nothrow) int[fd_count];
1641 if (fds == nullptr) {
1642 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1643 return BAD_VALUE;
1644 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001645 }
1646
1647 err = val.flatten(buf, len, fds, fd_count);
1648 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1649 err = this->writeDupFileDescriptor( fds[i] );
1650 }
1651
1652 if (fd_count) {
1653 delete [] fds;
1654 }
1655
1656 return err;
1657}
1658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001659status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1660{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001661 auto* kernelFields = maybeKernelFields();
1662 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1663
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001664#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001665 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001666 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001667 if (enoughData && enoughObjects) {
1668restart_write:
1669 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001670
Christopher Tate98e67d32015-06-03 18:44:15 -07001671 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001672 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001673 if (!mAllowFds) {
1674 // fail before modifying our object index
1675 return FDS_NOT_ALLOWED;
1676 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001677 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001678 }
1679
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001680 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001681 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001682 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001683 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001684 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001685 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001686
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001687 return finishWrite(sizeof(flat_binder_object));
1688 }
1689
1690 if (!enoughData) {
1691 const status_t err = growData(sizeof(val));
1692 if (err != NO_ERROR) return err;
1693 }
1694 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001695 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1696 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1697 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001698 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001699 binder_size_t* objects =
1700 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001701 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001702 kernelFields->mObjects = objects;
1703 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001704 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001705
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001706 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001707#else // BINDER_WITH_KERNEL_IPC
1708 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1709 (void)val;
1710 (void)nullMetaData;
1711 return INVALID_OPERATION;
1712#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001713}
1714
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001715status_t Parcel::writeNoException()
1716{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001717 binder::Status status;
1718 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001719}
1720
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001721status_t Parcel::validateReadData(size_t upperBound) const
1722{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001723 const auto* kernelFields = maybeKernelFields();
1724 if (kernelFields == nullptr) {
1725 // Can't validate RPC Parcel reads because the location of binder
1726 // objects is unknown.
1727 return OK;
1728 }
1729
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001730#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001731 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001732 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1733 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001734 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001735 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1736 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001737 // For some reason the current read position is greater than the next object
1738 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001739 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001740 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001741 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001742 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001743 if (!mServiceFuzzing) {
1744 ALOGE("Attempt to read from protected data in Parcel %p", this);
1745 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001746 return PERMISSION_DENIED;
1747 }
1748 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001749 } while (nextObject < kernelFields->mObjectsSize &&
1750 upperBound > kernelFields->mObjects[nextObject]);
1751 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001752 }
1753 return NO_ERROR;
1754 }
1755 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001756 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001757 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001758 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001759 prevObj--;
1760 if(*prevObj > *currObj) {
1761 goto data_unsorted;
1762 }
1763 currObj--;
1764 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001765 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001766 goto data_sorted;
1767
1768data_unsorted:
1769 // Insertion Sort mObjects
1770 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1771 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001772 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1773 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001774 binder_size_t temp = *iter0;
1775 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001776 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001777 *(iter1 + 1) = *iter1;
1778 iter1--;
1779 }
1780 *(iter1 + 1) = temp;
1781 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001782 kernelFields->mNextObjectHint = 0;
1783 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001784 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001785#else // BINDER_WITH_KERNEL_IPC
1786 (void)upperBound;
1787 return NO_ERROR;
1788#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001789}
1790
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791status_t Parcel::read(void* outData, size_t len) const
1792{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001793 if (len > INT32_MAX) {
1794 // don't accept size_t values which may have come from an
1795 // inadvertent conversion from a negative int.
1796 return BAD_VALUE;
1797 }
1798
1799 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1800 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001801 const auto* kernelFields = maybeKernelFields();
1802 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001803 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001804 if(err != NO_ERROR) {
1805 // Still increment the data position by the expected length
1806 mDataPos += pad_size(len);
1807 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1808 return err;
1809 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001810 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001811 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001812 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001813 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001814 return NO_ERROR;
1815 }
1816 return NOT_ENOUGH_DATA;
1817}
1818
1819const void* Parcel::readInplace(size_t len) const
1820{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001821 if (len > INT32_MAX) {
1822 // don't accept size_t values which may have come from an
1823 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001824 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001825 }
1826
1827 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1828 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001829 const auto* kernelFields = maybeKernelFields();
1830 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001831 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001832 if(err != NO_ERROR) {
1833 // Still increment the data position by the expected length
1834 mDataPos += pad_size(len);
1835 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001836 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001837 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001838 }
1839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001841 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001842 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001843 return data;
1844 }
Yi Kong91635562018-06-07 14:38:36 -07001845 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001846}
1847
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001848status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1849 if (status_t status = readInt32(size); status != OK) return status;
1850 if (*size < 0) return OK; // may be null, client to handle
1851
1852 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1853
1854 // approximation, can't know max element size (e.g. if it makes heap
1855 // allocations)
1856 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1857 int32_t allocationSize;
1858 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1859
1860 // High limit of 1MB since something this big could never be returned. Could
1861 // probably scope this down, but might impact very specific usecases.
1862 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1863
1864 if (allocationSize >= kMaxAllocationSize) {
1865 return NO_MEMORY;
1866 }
1867
1868 return OK;
1869}
1870
Andreas Huber84a6d042009-08-17 13:33:27 -07001871template<class T>
1872status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001873 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001874 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001875
1876 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001877 const auto* kernelFields = maybeKernelFields();
1878 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001879 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001880 if(err != NO_ERROR) {
1881 // Still increment the data position by the expected length
1882 mDataPos += sizeof(T);
1883 return err;
1884 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001885 }
1886
Andrei Homescue33238e2022-03-29 22:50:00 +00001887 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001888 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889 return NO_ERROR;
1890 } else {
1891 return NOT_ENOUGH_DATA;
1892 }
1893}
1894
Andreas Huber84a6d042009-08-17 13:33:27 -07001895template<class T>
1896T Parcel::readAligned() const {
1897 T result;
1898 if (readAligned(&result) != NO_ERROR) {
1899 result = 0;
1900 }
1901
1902 return result;
1903}
1904
1905template<class T>
1906status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001907 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001908 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001909
1910 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1911restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001912 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001913 return finishWrite(sizeof(val));
1914 }
1915
1916 status_t err = growData(sizeof(val));
1917 if (err == NO_ERROR) goto restart_write;
1918 return err;
1919}
1920
1921status_t Parcel::readInt32(int32_t *pArg) const
1922{
1923 return readAligned(pArg);
1924}
1925
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001926int32_t Parcel::readInt32() const
1927{
Andreas Huber84a6d042009-08-17 13:33:27 -07001928 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001929}
1930
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001931status_t Parcel::readUint32(uint32_t *pArg) const
1932{
1933 return readAligned(pArg);
1934}
1935
1936uint32_t Parcel::readUint32() const
1937{
1938 return readAligned<uint32_t>();
1939}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001940
1941status_t Parcel::readInt64(int64_t *pArg) const
1942{
Andreas Huber84a6d042009-08-17 13:33:27 -07001943 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001944}
1945
1946
1947int64_t Parcel::readInt64() const
1948{
Andreas Huber84a6d042009-08-17 13:33:27 -07001949 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950}
1951
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001952status_t Parcel::readUint64(uint64_t *pArg) const
1953{
1954 return readAligned(pArg);
1955}
1956
1957uint64_t Parcel::readUint64() const
1958{
1959 return readAligned<uint64_t>();
1960}
1961
Serban Constantinescuf683e012013-11-05 16:53:55 +00001962status_t Parcel::readPointer(uintptr_t *pArg) const
1963{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001964 status_t ret;
1965 binder_uintptr_t ptr;
1966 ret = readAligned(&ptr);
1967 if (!ret)
1968 *pArg = ptr;
1969 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001970}
1971
1972uintptr_t Parcel::readPointer() const
1973{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001974 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001975}
1976
1977
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978status_t Parcel::readFloat(float *pArg) const
1979{
Andreas Huber84a6d042009-08-17 13:33:27 -07001980 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001981}
1982
1983
1984float Parcel::readFloat() const
1985{
Andreas Huber84a6d042009-08-17 13:33:27 -07001986 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001987}
1988
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001989#if defined(__mips__) && defined(__mips_hard_float)
1990
1991status_t Parcel::readDouble(double *pArg) const
1992{
1993 union {
1994 double d;
1995 unsigned long long ll;
1996 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001997 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001998 status_t status;
1999 status = readAligned(&u.ll);
2000 *pArg = u.d;
2001 return status;
2002}
2003
2004double Parcel::readDouble() const
2005{
2006 union {
2007 double d;
2008 unsigned long long ll;
2009 } u;
2010 u.ll = readAligned<unsigned long long>();
2011 return u.d;
2012}
2013
2014#else
2015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016status_t Parcel::readDouble(double *pArg) const
2017{
Andreas Huber84a6d042009-08-17 13:33:27 -07002018 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019}
2020
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002021double Parcel::readDouble() const
2022{
Andreas Huber84a6d042009-08-17 13:33:27 -07002023 return readAligned<double>();
2024}
2025
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002026#endif
2027
Casey Dahlind6848f52015-10-15 15:44:59 -07002028status_t Parcel::readBool(bool *pArg) const
2029{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002030 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002031 status_t ret = readInt32(&tmp);
2032 *pArg = (tmp != 0);
2033 return ret;
2034}
2035
2036bool Parcel::readBool() const
2037{
2038 return readInt32() != 0;
2039}
2040
2041status_t Parcel::readChar(char16_t *pArg) const
2042{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002043 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002044 status_t ret = readInt32(&tmp);
2045 *pArg = char16_t(tmp);
2046 return ret;
2047}
2048
2049char16_t Parcel::readChar() const
2050{
2051 return char16_t(readInt32());
2052}
2053
2054status_t Parcel::readByte(int8_t *pArg) const
2055{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002056 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002057 status_t ret = readInt32(&tmp);
2058 *pArg = int8_t(tmp);
2059 return ret;
2060}
2061
2062int8_t Parcel::readByte() const
2063{
2064 return int8_t(readInt32());
2065}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002067status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2068 size_t utf16Size = 0;
2069 const char16_t* src = readString16Inplace(&utf16Size);
2070 if (!src) {
2071 return UNEXPECTED_NULL;
2072 }
2073
2074 // Save ourselves the trouble, we're done.
2075 if (utf16Size == 0u) {
2076 str->clear();
2077 return NO_ERROR;
2078 }
2079
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002080 // Allow for closing '\0'
2081 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2082 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002083 return BAD_VALUE;
2084 }
2085 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002086 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002087 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002088 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2089 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002090 return NO_ERROR;
2091}
2092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093const char* Parcel::readCString() const
2094{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002095 if (mDataPos < mDataSize) {
2096 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2098 // is the string's trailing NUL within the parcel's valid bounds?
2099 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2100 if (eos) {
2101 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002102 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002103 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002104 return str;
2105 }
2106 }
Yi Kong91635562018-06-07 14:38:36 -07002107 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108}
2109
2110String8 Parcel::readString8() const
2111{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002112 size_t len;
2113 const char* str = readString8Inplace(&len);
2114 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002115
2116 if (!mServiceFuzzing) {
2117 ALOGE("Reading a NULL string not supported here.");
2118 }
2119
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002120 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002121}
2122
2123status_t Parcel::readString8(String8* pArg) const
2124{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002125 size_t len;
2126 const char* str = readString8Inplace(&len);
2127 if (str) {
2128 pArg->setTo(str, len);
2129 return 0;
2130 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002131 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002132 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002133 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002134}
2135
2136const char* Parcel::readString8Inplace(size_t* outLen) const
2137{
2138 int32_t size = readInt32();
2139 // watch for potential int overflow from size+1
2140 if (size >= 0 && size < INT32_MAX) {
2141 *outLen = size;
2142 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002143 if (str != nullptr) {
2144 if (str[size] == '\0') {
2145 return str;
2146 }
2147 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002148 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002149 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002150 *outLen = 0;
2151 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152}
2153
2154String16 Parcel::readString16() const
2155{
2156 size_t len;
2157 const char16_t* str = readString16Inplace(&len);
2158 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002159
2160 if (!mServiceFuzzing) {
2161 ALOGE("Reading a NULL string not supported here.");
2162 }
2163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 return String16();
2165}
2166
Casey Dahlinb9872622015-11-25 15:09:45 -08002167
Casey Dahlin451ff582015-10-19 18:12:18 -07002168status_t Parcel::readString16(String16* pArg) const
2169{
2170 size_t len;
2171 const char16_t* str = readString16Inplace(&len);
2172 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002173 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002174 return 0;
2175 } else {
2176 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002177 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002178 }
2179}
2180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2182{
2183 int32_t size = readInt32();
2184 // watch for potential int overflow from size+1
2185 if (size >= 0 && size < INT32_MAX) {
2186 *outLen = size;
2187 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002188 if (str != nullptr) {
2189 if (str[size] == u'\0') {
2190 return str;
2191 }
2192 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002193 }
2194 }
2195 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002196 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002197}
2198
Casey Dahlinf0c13772015-10-27 18:33:56 -07002199status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2200{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002201 status_t status = readNullableStrongBinder(val);
2202 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002203 if (!mServiceFuzzing) {
2204 ALOGW("Expecting binder but got null!");
2205 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002206 status = UNEXPECTED_NULL;
2207 }
2208 return status;
2209}
2210
2211status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2212{
Steven Morelanda86a3562019-08-01 23:28:34 +00002213 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002214}
2215
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002216sp<IBinder> Parcel::readStrongBinder() const
2217{
2218 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002219 // Note that a lot of code in Android reads binders by hand with this
2220 // method, and that code has historically been ok with getting nullptr
2221 // back (while ignoring error codes).
2222 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223 return val;
2224}
2225
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002226int32_t Parcel::readExceptionCode() const
2227{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002228 binder::Status status;
2229 status.readFromParcel(*this);
2230 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002231}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002232
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002233native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002234{
2235 int numFds, numInts;
2236 status_t err;
2237 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002238 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002239 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002240 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002241
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002242 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002243 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002244 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002245 }
2246
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002247 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002248 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002249 if (h->data[i] < 0) {
2250 for (int j = 0; j < i; j++) {
2251 close(h->data[j]);
2252 }
2253 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002254 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002255 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002256 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002257 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002258 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002259 native_handle_close(h);
2260 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002261 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002262 }
2263 return h;
2264}
2265
Frederick Mayle69a0c992022-05-26 20:38:39 +00002266int Parcel::readFileDescriptor() const {
2267 if (const auto* rpcFields = maybeRpcFields()) {
2268 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2269 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002270 if (!mServiceFuzzing) {
2271 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2272 "the object list",
2273 this, mDataPos);
2274 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002275 return BAD_TYPE;
2276 }
2277
2278 int32_t objectType = readInt32();
2279 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2280 return BAD_TYPE;
2281 }
2282
2283 int32_t fdIndex = readInt32();
2284 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2285 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2286 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2287 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2288 return BAD_VALUE;
2289 }
2290 return toRawFd(rpcFields->mFds->at(fdIndex));
2291 }
2292
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002293#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002294 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002295
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002296 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002297 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002301#else // BINDER_WITH_KERNEL_IPC
2302 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2303 return INVALID_OPERATION;
2304#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002305}
2306
Frederick Mayle69a0c992022-05-26 20:38:39 +00002307int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002308 int32_t hasComm = readInt32();
2309 int fd = readFileDescriptor();
2310 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002311 // detach (owned by the binder driver)
2312 int comm = readFileDescriptor();
2313
2314 // warning: this must be kept in sync with:
2315 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2316 enum ParcelFileDescriptorStatus {
2317 DETACHED = 2,
2318 };
2319
2320#if BYTE_ORDER == BIG_ENDIAN
2321 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2322#endif
2323#if BYTE_ORDER == LITTLE_ENDIAN
2324 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2325#endif
2326
2327 ssize_t written = TEMP_FAILURE_RETRY(
2328 ::write(comm, &message, sizeof(message)));
2329
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002330 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002331 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2332 written, strerror(errno));
2333 return BAD_TYPE;
2334 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002335 }
2336 return fd;
2337}
2338
Christopher Wiley2cf19952016-04-11 11:09:37 -07002339status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002340{
2341 int got = readFileDescriptor();
2342
2343 if (got == BAD_TYPE) {
2344 return BAD_TYPE;
2345 }
2346
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002347 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002348 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002349 return BAD_VALUE;
2350 }
2351
2352 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002353
2354 if (val->get() < 0) {
2355 return BAD_VALUE;
2356 }
2357
2358 return OK;
2359}
2360
Ryo Hashimotobf551892018-05-31 16:58:35 +09002361status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2362{
2363 int got = readParcelFileDescriptor();
2364
2365 if (got == BAD_TYPE) {
2366 return BAD_TYPE;
2367 }
2368
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002369 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002370 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002371 return BAD_VALUE;
2372 }
2373
2374 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002375
2376 if (val->get() < 0) {
2377 return BAD_VALUE;
2378 }
2379
2380 return OK;
2381}
Casey Dahlin06673e32015-11-23 13:24:23 -08002382
Jeff Brown5707dbf2011-09-23 21:17:56 -07002383status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2384{
Jeff Brown13b16042014-11-11 16:44:25 -08002385 int32_t blobType;
2386 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002387 if (status) return status;
2388
Jeff Brown13b16042014-11-11 16:44:25 -08002389 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002390 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002391 const void* ptr = readInplace(len);
2392 if (!ptr) return BAD_VALUE;
2393
Jeff Brown13b16042014-11-11 16:44:25 -08002394 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002395 return NO_ERROR;
2396 }
2397
Steve Block6807e592011-10-20 11:56:00 +01002398 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002399 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002400 int fd = readFileDescriptor();
2401 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2402
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002403 if (!ashmem_valid(fd)) {
2404 ALOGE("invalid fd");
2405 return BAD_VALUE;
2406 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002407 int size = ashmem_get_size_region(fd);
2408 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002409 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002410 return BAD_VALUE;
2411 }
Yi Kong91635562018-06-07 14:38:36 -07002412 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002413 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002414 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002415
Jeff Brown13b16042014-11-11 16:44:25 -08002416 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002417 return NO_ERROR;
2418}
2419
Mathias Agopiane1424282013-07-29 21:24:40 -07002420status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002421{
2422 // size
2423 const size_t len = this->readInt32();
2424 const size_t fd_count = this->readInt32();
2425
Andrei Homescu1519b982022-06-09 02:04:44 +00002426 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002427 // don't accept size_t values which may have come from an
2428 // inadvertent conversion from a negative int.
2429 return BAD_VALUE;
2430 }
2431
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002432 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002433 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002434 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002435 return BAD_VALUE;
2436
Yi Kong91635562018-06-07 14:38:36 -07002437 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002438 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002439 fds = new (std::nothrow) int[fd_count];
2440 if (fds == nullptr) {
2441 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2442 return BAD_VALUE;
2443 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002444 }
2445
2446 status_t err = NO_ERROR;
2447 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002448 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002449 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002450 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002451 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 -07002452 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2453 // Close all the file descriptors that were dup-ed.
2454 for (size_t j=0; j<i ;j++) {
2455 close(fds[j]);
2456 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002457 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002458 }
2459
2460 if (err == NO_ERROR) {
2461 err = val.unflatten(buf, len, fds, fd_count);
2462 }
2463
2464 if (fd_count) {
2465 delete [] fds;
2466 }
2467
2468 return err;
2469}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002470
2471#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2473{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002474 const auto* kernelFields = maybeKernelFields();
2475 if (kernelFields == nullptr) {
2476 return nullptr;
2477 }
2478
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 const size_t DPOS = mDataPos;
2480 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2481 const flat_binder_object* obj
2482 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2483 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002484 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002485 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 // the object list, so we don't want to check for it when
2487 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002488 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 return obj;
2490 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002493 binder_size_t* const OBJS = kernelFields->mObjects;
2494 const size_t N = kernelFields->mObjectsSize;
2495 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002498 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002499 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 // Start at the current hint position, looking for an object at
2502 // the current data position.
2503 if (opos < N) {
2504 while (opos < (N-1) && OBJS[opos] < DPOS) {
2505 opos++;
2506 }
2507 } else {
2508 opos = N-1;
2509 }
2510 if (OBJS[opos] == DPOS) {
2511 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002512 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002514 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516 return obj;
2517 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002518
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 // Look backwards for it...
2520 while (opos > 0 && OBJS[opos] > DPOS) {
2521 opos--;
2522 }
2523 if (OBJS[opos] == DPOS) {
2524 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002525 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002526 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002527 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002528 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002529 return obj;
2530 }
2531 }
Steven Moreland3a667492023-06-02 21:41:39 +00002532 if (!mServiceFuzzing) {
2533 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2534 "list",
2535 this, DPOS);
2536 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002537 }
Yi Kong91635562018-06-07 14:38:36 -07002538 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002539}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002540#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541
Frederick Mayled3c595a2022-05-09 23:02:54 +00002542void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002543 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002544#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002545 size_t i = kernelFields->mObjectsSize;
2546 if (i > 0) {
2547 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002548 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002549 while (i > 0) {
2550 i--;
2551 const flat_binder_object* flat =
2552 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2553 if (flat->hdr.type == BINDER_TYPE_FD) {
2554 // ALOGI("Closing fd: %ld", flat->handle);
2555 close(flat->handle);
2556 }
2557 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002558#else // BINDER_WITH_KERNEL_IPC
2559 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2560#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002561 } else if (auto* rpcFields = maybeRpcFields()) {
2562 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 }
2564}
2565
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002566uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002568 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569}
2570
2571size_t Parcel::ipcDataSize() const
2572{
2573 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2574}
2575
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002576uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002578 if (const auto* kernelFields = maybeKernelFields()) {
2579 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2580 }
2581 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582}
2583
2584size_t Parcel::ipcObjectsCount() const
2585{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002586 if (const auto* kernelFields = maybeKernelFields()) {
2587 return kernelFields->mObjectsSize;
2588 }
2589 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590}
2591
Frederick Mayled3c595a2022-05-09 23:02:54 +00002592void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2593 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002594 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2595 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2596
Steven Morelandceed9bb2020-12-17 01:01:06 +00002597 freeData();
2598
Frederick Mayled3c595a2022-05-09 23:02:54 +00002599 auto* kernelFields = maybeKernelFields();
2600 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2601
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602 mData = const_cast<uint8_t*>(data);
2603 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002604 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2605 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002606 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002607
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002608#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002609 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002610 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2611 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002612 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002613 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002614 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002615 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002616 break;
2617 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002618 const flat_binder_object* flat
2619 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2620 uint32_t type = flat->hdr.type;
2621 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2622 type == BINDER_TYPE_FD)) {
2623 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2624 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002625 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002626 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002627 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002628 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2629 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002630
2631 // WARNING: callers of ipcSetDataReference need to make sure they
2632 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002633 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002634 break;
2635 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002636 minOffset = offset + sizeof(flat_binder_object);
2637 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002639#else // BINDER_WITH_KERNEL_IPC
2640 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2641 "Non-zero objects count passed to Parcel with kernel driver disabled");
2642#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002643}
2644
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002645status_t Parcel::rpcSetDataReference(
2646 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2647 const uint32_t* objectTable, size_t objectTableSize,
2648 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds,
2649 release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002650 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2651 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2652
2653 LOG_ALWAYS_FATAL_IF(session == nullptr);
2654
Frederick Mayle694e9732022-07-15 00:24:58 +00002655 if (objectTableSize != ancillaryFds.size()) {
2656 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2657 relFunc(data, dataSize, nullptr, 0);
2658 return BAD_VALUE;
2659 }
2660 for (size_t i = 0; i < objectTableSize; i++) {
2661 uint32_t minObjectEnd;
2662 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2663 minObjectEnd >= dataSize) {
2664 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2665 objectTable[i], dataSize);
2666 relFunc(data, dataSize, nullptr, 0);
2667 return BAD_VALUE;
2668 }
2669 }
2670
Frederick Mayled3c595a2022-05-09 23:02:54 +00002671 freeData();
2672 markForRpc(session);
2673
Frederick Mayle69a0c992022-05-26 20:38:39 +00002674 auto* rpcFields = maybeRpcFields();
2675 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2676
Frederick Mayled3c595a2022-05-09 23:02:54 +00002677 mData = const_cast<uint8_t*>(data);
2678 mDataSize = mDataCapacity = dataSize;
2679 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002680
Frederick Mayle69a0c992022-05-26 20:38:39 +00002681 rpcFields->mObjectPositions.reserve(objectTableSize);
2682 for (size_t i = 0; i < objectTableSize; i++) {
2683 rpcFields->mObjectPositions.push_back(objectTable[i]);
2684 }
2685 if (!ancillaryFds.empty()) {
2686 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002687 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002688 }
2689
2690 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002691}
2692
Pawan Wagh7063b522022-09-28 18:52:26 +00002693void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002694 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002695
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002696 if (errorCheck() != NO_ERROR) {
2697 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002698 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699 } else if (dataSize() > 0) {
2700 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002701 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002702#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002703 if (const auto* kernelFields = maybeKernelFields()) {
2704 const binder_size_t* OBJS = kernelFields->mObjects;
2705 const size_t N = objectsCount();
2706 for (size_t i = 0; i < N; i++) {
2707 const flat_binder_object* flat =
2708 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002709 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002710 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2711 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002712 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002713#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002714 } else {
2715 to << "NULL";
2716 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002718 to << ")";
2719}
2720
2721void Parcel::releaseObjects()
2722{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002723 auto* kernelFields = maybeKernelFields();
2724 if (kernelFields == nullptr) {
2725 return;
2726 }
2727
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002728#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002729 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002730 if (i == 0) {
2731 return;
2732 }
2733 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002735 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002736 while (i > 0) {
2737 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002738 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002739 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002741#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742}
2743
2744void Parcel::acquireObjects()
2745{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002746 auto* kernelFields = maybeKernelFields();
2747 if (kernelFields == nullptr) {
2748 return;
2749 }
2750
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002751#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002752 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002753 if (i == 0) {
2754 return;
2755 }
2756 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002757 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002758 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002759 while (i > 0) {
2760 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002761 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002762 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002763 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002764#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765}
2766
2767void Parcel::freeData()
2768{
2769 freeDataNoInit();
2770 initState();
2771}
2772
2773void Parcel::freeDataNoInit()
2774{
2775 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002776 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002777 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002778 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002779 // Close FDs before freeing, otherwise they will leak for kernel binder.
2780 closeFileDescriptors();
2781 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002782 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002784 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002786 if (mData) {
2787 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002788 gParcelGlobalAllocSize -= mDataCapacity;
2789 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002790 if (mDeallocZero) {
2791 zeroMemory(mData, mDataSize);
2792 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002793 free(mData);
2794 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002795 auto* kernelFields = maybeKernelFields();
2796 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002797 }
2798}
2799
2800status_t Parcel::growData(size_t len)
2801{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002802 if (len > INT32_MAX) {
2803 // don't accept size_t values which may have come from an
2804 // inadvertent conversion from a negative int.
2805 return BAD_VALUE;
2806 }
2807
Martijn Coenen93fe5182020-01-22 10:46:25 +01002808 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2809 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002810 size_t newSize = ((mDataSize+len)*3)/2;
2811 return (newSize <= mDataSize)
2812 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002813 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002814}
2815
Steven Morelandf183fdd2020-10-27 00:12:12 +00002816static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2817 if (!zero) {
2818 return (uint8_t*)realloc(data, newCapacity);
2819 }
2820 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2821 if (!newData) {
2822 return nullptr;
2823 }
2824
2825 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2826 zeroMemory(data, oldCapacity);
2827 free(data);
2828 return newData;
2829}
2830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831status_t Parcel::restartWrite(size_t desired)
2832{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002833 if (desired > INT32_MAX) {
2834 // don't accept size_t values which may have come from an
2835 // inadvertent conversion from a negative int.
2836 return BAD_VALUE;
2837 }
2838
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002839 if (mOwner) {
2840 freeData();
2841 return continueWrite(desired);
2842 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002843
Steven Morelandf183fdd2020-10-27 00:12:12 +00002844 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002845 if (!data && desired > mDataCapacity) {
2846 mError = NO_MEMORY;
2847 return NO_MEMORY;
2848 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002849
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002851
Devin Moore4a0a55e2020-06-04 13:23:10 -07002852 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002853 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002854 if (mDataCapacity > desired) {
2855 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2856 } else {
2857 gParcelGlobalAllocSize += (desired - mDataCapacity);
2858 }
2859
Colin Cross83ec65e2015-12-08 17:15:50 -08002860 if (!mData) {
2861 gParcelGlobalAllocCount++;
2862 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 mData = data;
2864 mDataCapacity = desired;
2865 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002867 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002868 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2869 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2870
Frederick Mayled3c595a2022-05-09 23:02:54 +00002871 if (auto* kernelFields = maybeKernelFields()) {
2872 free(kernelFields->mObjects);
2873 kernelFields->mObjects = nullptr;
2874 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2875 kernelFields->mNextObjectHint = 0;
2876 kernelFields->mObjectsSorted = false;
2877 kernelFields->mHasFds = false;
2878 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002879 } else if (auto* rpcFields = maybeRpcFields()) {
2880 rpcFields->mObjectPositions.clear();
2881 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002882 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002883 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002884
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002885 return NO_ERROR;
2886}
2887
2888status_t Parcel::continueWrite(size_t desired)
2889{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002890 if (desired > INT32_MAX) {
2891 // don't accept size_t values which may have come from an
2892 // inadvertent conversion from a negative int.
2893 return BAD_VALUE;
2894 }
2895
Frederick Mayled3c595a2022-05-09 23:02:54 +00002896 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002897 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002899 // If shrinking, first adjust for any objects that appear
2900 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002901 size_t objectsSize =
2902 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2903 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002904 if (desired == 0) {
2905 objectsSize = 0;
2906 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002907 if (kernelFields) {
2908 while (objectsSize > 0) {
2909 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2910 objectsSize--;
2911 }
2912 } else {
2913 while (objectsSize > 0) {
2914 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2915 objectsSize--;
2916 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002917 }
2918 }
2919 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921 if (mOwner) {
2922 // If the size is going to zero, just release the owner's data.
2923 if (desired == 0) {
2924 freeData();
2925 return NO_ERROR;
2926 }
2927
2928 // If there is a different owner, we need to take
2929 // posession.
2930 uint8_t* data = (uint8_t*)malloc(desired);
2931 if (!data) {
2932 mError = NO_MEMORY;
2933 return NO_MEMORY;
2934 }
Yi Kong91635562018-06-07 14:38:36 -07002935 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002936
Frederick Mayle69a0c992022-05-26 20:38:39 +00002937 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002938 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002939 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002940 free(data);
2941
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002942 mError = NO_MEMORY;
2943 return NO_MEMORY;
2944 }
2945
2946 // Little hack to only acquire references on objects
2947 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002948 size_t oldObjectsSize = kernelFields->mObjectsSize;
2949 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002950 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002951 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002952 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002953 if (rpcFields) {
2954 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2955 free(data);
2956 return status;
2957 }
2958 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002959
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002960 if (mData) {
2961 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2962 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002963 if (objects && kernelFields && kernelFields->mObjects) {
2964 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002965 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002966 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2967 if (kernelFields) {
2968 // TODO(b/239222407): This seems wrong. We should only free FDs when
2969 // they are in a truncated section of the parcel.
2970 closeFileDescriptors();
2971 }
2972 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002973 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002974 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002975
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002976 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002977 gParcelGlobalAllocSize += desired;
2978 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002979
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002980 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002981 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002982 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002983 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002984 if (kernelFields) {
2985 kernelFields->mObjects = objects;
2986 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2987 kernelFields->mNextObjectHint = 0;
2988 kernelFields->mObjectsSorted = false;
2989 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002990
2991 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002992 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002993#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002994 // Need to release refs on any objects we are dropping.
2995 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002996 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2997 const flat_binder_object* flat =
2998 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002999 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003000 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003001 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003002 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003003 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003004 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003005
3006 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003007 free(kernelFields->mObjects);
3008 kernelFields->mObjects = nullptr;
3009 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003010 } else {
3011 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003012 (binder_size_t*)realloc(kernelFields->mObjects,
3013 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003014 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003015 kernelFields->mObjects = objects;
3016 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003017 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003018 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003019 kernelFields->mObjectsSize = objectsSize;
3020 kernelFields->mNextObjectHint = 0;
3021 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003022#else // BINDER_WITH_KERNEL_IPC
3023 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3024#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003025 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003026 if (rpcFields) {
3027 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3028 return status;
3029 }
3030 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003031
3032 // We own the data, so we can just do a realloc().
3033 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003034 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003035 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003036 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3037 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003038 gParcelGlobalAllocSize += desired;
3039 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003040 mData = data;
3041 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003042 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003043 mError = NO_MEMORY;
3044 return NO_MEMORY;
3045 }
3046 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003047 if (mDataSize > desired) {
3048 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003049 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003050 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003051 if (mDataPos > desired) {
3052 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003053 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003054 }
3055 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003057 } else {
3058 // This is the first data. Easy!
3059 uint8_t* data = (uint8_t*)malloc(desired);
3060 if (!data) {
3061 mError = NO_MEMORY;
3062 return NO_MEMORY;
3063 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003064
Frederick Mayled3c595a2022-05-09 23:02:54 +00003065 if (!(mDataCapacity == 0 &&
3066 (kernelFields == nullptr ||
3067 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3068 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3069 kernelFields ? kernelFields->mObjects : nullptr,
3070 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003071 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003072
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003073 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003074 gParcelGlobalAllocSize += desired;
3075 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003077 mData = data;
3078 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003079 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3080 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003081 mDataCapacity = desired;
3082 }
3083
3084 return NO_ERROR;
3085}
3086
Frederick Mayle69a0c992022-05-26 20:38:39 +00003087status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3088 auto* rpcFields = maybeRpcFields();
3089 if (newObjectsSize == 0) {
3090 rpcFields->mObjectPositions.clear();
3091 if (rpcFields->mFds) {
3092 rpcFields->mFds->clear();
3093 }
3094 return OK;
3095 }
3096 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3097 uint32_t pos = rpcFields->mObjectPositions.back();
3098 rpcFields->mObjectPositions.pop_back();
3099 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3100 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3101 const auto fdIndex =
3102 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3103 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3104 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3105 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3106 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3107 return BAD_VALUE;
3108 }
3109 // In practice, this always removes the last element.
3110 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3111 }
3112 }
3113 return OK;
3114}
3115
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003116void Parcel::initState()
3117{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003118 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003119 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003120 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003121 mDataSize = 0;
3122 mDataCapacity = 0;
3123 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003124 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3125 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003126 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003127 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003128 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003129 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003130 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003131 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003132}
3133
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003134void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003135 auto* kernelFields = maybeKernelFields();
3136 if (kernelFields == nullptr) {
3137 return;
3138 }
3139 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003140 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003141 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003142}
3143
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003144#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003145size_t Parcel::getBlobAshmemSize() const
3146{
Adrian Roos6bb31142015-10-22 16:46:12 -07003147 // This used to return the size of all blobs that were written to ashmem, now we're returning
3148 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003149 // TODO(b/202029388): Remove method once ABI can be changed.
3150 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003151}
3152
Adrian Rooscbf37262015-10-22 16:12:53 -07003153size_t Parcel::getOpenAshmemSize() const
3154{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003155 auto* kernelFields = maybeKernelFields();
3156 if (kernelFields == nullptr) {
3157 return 0;
3158 }
3159
Steven Morelandc673f1f2021-10-07 18:23:35 -07003160 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003161 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003162 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003163 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003164
3165 // cookie is compared against zero for historical reasons
3166 // > obj.cookie = takeOwnership ? 1 : 0;
3167 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3168 int size = ashmem_get_size_region(flat->handle);
3169 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3170 ALOGE("Overflow when computing ashmem size.");
3171 return SIZE_MAX;
3172 }
3173 }
3174 }
3175 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003176}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003177#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003178
3179// --- Parcel::Blob ---
3180
3181Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003182 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003183}
3184
3185Parcel::Blob::~Blob() {
3186 release();
3187}
3188
3189void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003190 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003191 ::munmap(mData, mSize);
3192 }
3193 clear();
3194}
3195
Jeff Brown13b16042014-11-11 16:44:25 -08003196void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3197 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003198 mData = data;
3199 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003200 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003201}
3202
3203void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003204 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003205 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003206 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003207 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003208}
3209
Steven Moreland61ff8492019-09-26 16:05:45 -07003210} // namespace android