blob: 5b34efc25741d63232fea6abcc8f4e0ed9dd5dc3 [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>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Andrei Homescu24ad36e2022-08-04 01:33:33 +000051#include "OS.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000052#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070053#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000054#include "Utils.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000055
56// A lot of code in this file uses definitions from the
57// Linux kernel header for Binder <linux/android/binder.h>
58// which is included indirectly via "binder_module.h".
59// Non-Linux OSes do not have that header, so libbinder should be
60// built for those targets without kernel binder support, i.e.,
61// without BINDER_WITH_KERNEL_IPC. For this reason, all code in this
62// file that depends on kernel binder, including the header itself,
63// is conditional on BINDER_WITH_KERNEL_IPC.
64#ifdef BINDER_WITH_KERNEL_IPC
65#include <linux/sched.h>
Steven Moreland6ba5a252021-05-04 22:49:00 +000066#include "binder_module.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000067#else // BINDER_WITH_KERNEL_IPC
68// Needed by {read,write}Pointer
69typedef uintptr_t binder_uintptr_t;
70#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070072#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000073// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080074#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000075// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070076
77// ---------------------------------------------------------------------------
78
Nick Kralevichb6b14232015-04-02 09:36:02 -070079// This macro should never be used at runtime, as a too large value
80// of s could cause an integer overflow. Instead, you should always
81// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000082#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070083
84static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070085 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070086 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070087 }
88 return PAD_SIZE_UNSAFE(s);
89}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070090
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070091// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060092#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070093
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070094namespace android {
95
Steven Moreland7b102262019-08-01 15:48:43 -070096// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000097#ifdef __LP64__
98static_assert(sizeof(Parcel) == 120);
99#else
100static_assert(sizeof(Parcel) == 60);
101#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700102
Jeff Sharkey8994c182020-09-11 12:07:10 -0600103static std::atomic<size_t> gParcelGlobalAllocCount;
104static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800105
Andrei Homescu1519b982022-06-09 02:04:44 +0000106// Maximum number of file descriptors per Parcel.
107constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700108
Jeff Brown13b16042014-11-11 16:44:25 -0800109// Maximum size of a blob to transfer in-place.
110static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
111
112enum {
113 BLOB_INPLACE = 0,
114 BLOB_ASHMEM_IMMUTABLE = 1,
115 BLOB_ASHMEM_MUTABLE = 2,
116};
117
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000118#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700119static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
120 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700121 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700122 case BINDER_TYPE_BINDER:
123 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000124 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800125 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126 }
127 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700128 case BINDER_TYPE_HANDLE: {
129 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700130 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
132 b->incStrong(who);
133 }
134 return;
135 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 return;
138 }
139 }
140
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700141 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700142}
143
Steven Morelandc673f1f2021-10-07 18:23:35 -0700144static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
145 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700146 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 case BINDER_TYPE_BINDER:
148 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000149 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800150 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700151 }
152 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700153 case BINDER_TYPE_HANDLE: {
154 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700155 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
157 b->decStrong(who);
158 }
159 return;
160 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700161 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800162 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800163 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700164 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165 return;
166 }
167 }
168
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700169 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700170}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000171#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700172
Frederick Mayle69a0c992022-05-26 20:38:39 +0000173static int toRawFd(const std::variant<base::unique_fd, base::borrowed_fd>& v) {
174 return std::visit([](const auto& fd) { return fd.get(); }, v);
175}
176
Frederick Mayled3c595a2022-05-09 23:02:54 +0000177Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
178 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
179}
180
Steven Moreland34b48cb2020-12-01 22:45:38 +0000181status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700182{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700183 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700184 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000185 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186}
187
Steven Morelanda86a3562019-08-01 23:28:34 +0000188status_t Parcel::finishUnflattenBinder(
189 const sp<IBinder>& binder, sp<IBinder>* out) const
190{
191 int32_t stability;
192 status_t status = readInt32(&stability);
193 if (status != OK) return status;
194
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000195 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
196 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000197 if (status != OK) return status;
198
199 *out = binder;
200 return OK;
201}
202
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000203#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000204static constexpr inline int schedPolicyMask(int policy, int priority) {
205 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
206}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000207#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000208
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500209status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
210 BBinder* local = nullptr;
211 if (binder) local = binder->localBinder();
212 if (local) local->setParceled();
213
Frederick Mayled3c595a2022-05-09 23:02:54 +0000214 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000215 if (binder) {
216 status_t status = writeInt32(1); // non-null
217 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700218 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000219 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000220 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
221 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000222 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700223 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000224 if (status != OK) return status;
225 } else {
226 status_t status = writeInt32(0); // null
227 if (status != OK) return status;
228 }
229 return finishFlattenBinder(binder);
230 }
231
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000232#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700233 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700234
Steven Morelandbf1915b2020-07-16 22:43:02 +0000235 int schedBits = 0;
236 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
237 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700238 }
239
Yi Kong91635562018-06-07 14:38:36 -0700240 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 if (!local) {
242 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700243 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000244 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000245 } else {
246 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700247 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000248 return INVALID_OPERATION;
249 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 }
Steven Moreland99157622021-09-13 16:27:34 -0700251 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700252 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800253 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000254 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800256 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000258 int policy = local->getMinSchedulerPolicy();
259 int priority = local->getMinSchedulerPriority();
260
261 if (policy != 0 || priority != 0) {
262 // override value, since it is set explicitly
263 schedBits = schedPolicyMask(policy, priority);
264 }
Steven Moreland49c27532022-03-01 10:21:07 +0000265 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800266 if (local->isRequestingSid()) {
267 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
268 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000269 if (local->isInheritRt()) {
270 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
271 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700272 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800273 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
274 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700275 }
276 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700277 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000278 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800279 obj.binder = 0;
280 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700281 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700282
Steven Morelandbf1915b2020-07-16 22:43:02 +0000283 obj.flags |= schedBits;
284
Steven Moreland34b48cb2020-12-01 22:45:38 +0000285 status_t status = writeObject(obj, false);
286 if (status != OK) return status;
287
288 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000289#else // BINDER_WITH_KERNEL_IPC
290 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
291 return INVALID_OPERATION;
292#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293}
294
Steven Morelanda86a3562019-08-01 23:28:34 +0000295status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000297 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700298 int32_t isPresent;
299 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000300 if (status != OK) return status;
301
302 sp<IBinder> binder;
303
Steven Moreland5623d1a2021-09-10 15:45:34 -0700304 if (isPresent & 1) {
305 uint64_t addr;
306 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000307 if (status_t status =
308 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
309 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000310 status != OK)
311 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000312 if (status_t status =
313 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
314 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700315 status != OK)
316 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000317 }
318
319 return finishUnflattenBinder(binder, out);
320 }
321
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000322#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000323 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700325 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700326 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000327 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000328 sp<IBinder> binder =
329 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000330 return finishUnflattenBinder(binder, out);
331 }
332 case BINDER_TYPE_HANDLE: {
333 sp<IBinder> binder =
334 ProcessState::self()->getStrongProxyForHandle(flat->handle);
335 return finishUnflattenBinder(binder, out);
336 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700337 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700338 }
339 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000340#else // BINDER_WITH_KERNEL_IPC
341 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
342 return INVALID_OPERATION;
343#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700344}
345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700346// ---------------------------------------------------------------------------
347
348Parcel::Parcel()
349{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800350 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700351 initState();
352}
353
354Parcel::~Parcel()
355{
356 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800357 LOG_ALLOC("Parcel %p: destroyed", this);
358}
359
360size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600361 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800362}
363
364size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600365 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700366}
367
368const uint8_t* Parcel::data() const
369{
370 return mData;
371}
372
373size_t Parcel::dataSize() const
374{
375 return (mDataSize > mDataPos ? mDataSize : mDataPos);
376}
377
Pawan Waghd886a442023-01-21 02:16:38 +0000378size_t Parcel::dataBufferSize() const {
379 return mDataSize;
380}
381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700382size_t Parcel::dataAvail() const
383{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700384 size_t result = dataSize() - dataPosition();
385 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700386 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700387 }
388 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700389}
390
391size_t Parcel::dataPosition() const
392{
393 return mDataPos;
394}
395
396size_t Parcel::dataCapacity() const
397{
398 return mDataCapacity;
399}
400
401status_t Parcel::setDataSize(size_t size)
402{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700403 if (size > INT32_MAX) {
404 // don't accept size_t values which may have come from an
405 // inadvertent conversion from a negative int.
406 return BAD_VALUE;
407 }
408
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 status_t err;
410 err = continueWrite(size);
411 if (err == NO_ERROR) {
412 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700413 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700414 }
415 return err;
416}
417
418void Parcel::setDataPosition(size_t pos) const
419{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700420 if (pos > INT32_MAX) {
421 // don't accept size_t values which may have come from an
422 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700423 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700424 }
425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000427 if (const auto* kernelFields = maybeKernelFields()) {
428 kernelFields->mNextObjectHint = 0;
429 kernelFields->mObjectsSorted = false;
430 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700431}
432
433status_t Parcel::setDataCapacity(size_t size)
434{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700435 if (size > INT32_MAX) {
436 // don't accept size_t values which may have come from an
437 // inadvertent conversion from a negative int.
438 return BAD_VALUE;
439 }
440
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700441 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 return NO_ERROR;
443}
444
445status_t Parcel::setData(const uint8_t* buffer, size_t len)
446{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700447 if (len > INT32_MAX) {
448 // don't accept size_t values which may have come from an
449 // inadvertent conversion from a negative int.
450 return BAD_VALUE;
451 }
452
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700453 status_t err = restartWrite(len);
454 if (err == NO_ERROR) {
455 memcpy(const_cast<uint8_t*>(data()), buffer, len);
456 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000457 if (auto* kernelFields = maybeKernelFields()) {
458 kernelFields->mFdsKnown = false;
459 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460 }
461 return err;
462}
463
Frederick Mayled3c595a2022-05-09 23:02:54 +0000464status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
465 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700466 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
467 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000468 return BAD_TYPE;
469 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000470 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
471 ALOGE("Cannot append Parcels from different sessions");
472 return BAD_TYPE;
473 }
Steven Moreland67753c32021-04-02 18:45:19 +0000474
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000476 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700477 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478
479 if (len == 0) {
480 return NO_ERROR;
481 }
482
Nick Kralevichb6b14232015-04-02 09:36:02 -0700483 if (len > INT32_MAX) {
484 // don't accept size_t values which may have come from an
485 // inadvertent conversion from a negative int.
486 return BAD_VALUE;
487 }
488
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700489 // range checks against the source parcel size
490 if ((offset > parcel->mDataSize)
491 || (len > parcel->mDataSize)
492 || (offset + len > parcel->mDataSize)) {
493 return BAD_VALUE;
494 }
495
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700496 if ((mDataSize+len) > mDataCapacity) {
497 // grow data
498 err = growData(len);
499 if (err != NO_ERROR) {
500 return err;
501 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700502 }
503
504 // append data
505 memcpy(mData + mDataPos, data + offset, len);
506 mDataPos += len;
507 mDataSize += len;
508
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400509 err = NO_ERROR;
510
Frederick Mayled3c595a2022-05-09 23:02:54 +0000511 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000512#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000513 auto* otherKernelFields = parcel->maybeKernelFields();
514 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
515
516 const binder_size_t* objects = otherKernelFields->mObjects;
517 size_t size = otherKernelFields->mObjectsSize;
518 // Count objects in range
519 int firstIndex = -1, lastIndex = -2;
520 for (int i = 0; i < (int)size; i++) {
521 size_t off = objects[i];
522 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
523 if (firstIndex == -1) {
524 firstIndex = i;
525 }
526 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700527 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700528 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000529 int numObjects = lastIndex - firstIndex + 1;
530 if (numObjects > 0) {
531 const sp<ProcessState> proc(ProcessState::self());
532 // grow objects
533 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
534 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
535 return NO_MEMORY; // overflow
536 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
537 return NO_MEMORY; // overflow
538 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
539 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
540 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
541 newSize * sizeof(binder_size_t));
542 if (objects == (binder_size_t*)nullptr) {
543 return NO_MEMORY;
544 }
545 kernelFields->mObjects = objects;
546 kernelFields->mObjectsCapacity = newSize;
547 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700548
Frederick Mayled3c595a2022-05-09 23:02:54 +0000549 // append and acquire objects
550 int idx = kernelFields->mObjectsSize;
551 for (int i = firstIndex; i <= lastIndex; i++) {
552 size_t off = objects[i] - offset + startPos;
553 kernelFields->mObjects[idx++] = off;
554 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555
Frederick Mayled3c595a2022-05-09 23:02:54 +0000556 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
557 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558
Frederick Mayled3c595a2022-05-09 23:02:54 +0000559 if (flat->hdr.type == BINDER_TYPE_FD) {
560 // If this is a file descriptor, we need to dup it so the
561 // new Parcel now owns its own fd, and can declare that we
562 // officially know we have fds.
563 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
564 flat->cookie = 1;
565 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
566 if (!mAllowFds) {
567 err = FDS_NOT_ALLOWED;
568 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400569 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570 }
571 }
Andrei Homescu88012462022-07-07 04:30:05 +0000572#else
573 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
574 return INVALID_OPERATION;
575#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000576 } else {
577 auto* rpcFields = maybeRpcFields();
578 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
579 auto* otherRpcFields = parcel->maybeRpcFields();
580 if (otherRpcFields == nullptr) {
581 return BAD_TYPE;
582 }
583 if (rpcFields->mSession != otherRpcFields->mSession) {
584 return BAD_TYPE;
585 }
586
587 const size_t savedDataPos = mDataPos;
588 base::ScopeGuard scopeGuard = [&]() { mDataPos = savedDataPos; };
589
590 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
591 if (otherRpcFields->mFds != nullptr) {
592 if (rpcFields->mFds == nullptr) {
593 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
594 }
595 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
596 }
597 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
598 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
599 if (offset <= objPos && objPos < offset + len) {
600 size_t newDataPos = objPos - offset + startPos;
601 rpcFields->mObjectPositions.push_back(newDataPos);
602
603 mDataPos = newDataPos;
604 int32_t objectType;
605 if (status_t status = readInt32(&objectType); status != OK) {
606 return status;
607 }
608 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
609 continue;
610 }
611
612 if (!mAllowFds) {
613 return FDS_NOT_ALLOWED;
614 }
615
616 // Read FD, duplicate, and add to list.
617 int32_t fdIndex;
618 if (status_t status = readInt32(&fdIndex); status != OK) {
619 return status;
620 }
Andrei Homescufc221502022-10-08 03:51:17 +0000621 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000622 // To match kernel binder behavior, we always dup, even if the
623 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000624 int newFd = -1;
625 if (status_t status = dupFileDescriptor(oldFd, &newFd); status != OK) {
626 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
627 }
628 rpcFields->mFds->emplace_back(base::unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000629 // Fixup the index in the data.
630 mDataPos = newDataPos + 4;
631 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
632 return status;
633 }
634 }
635 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636 }
637
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400638 return err;
639}
640
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700641int Parcel::compareData(const Parcel& other) {
642 size_t size = dataSize();
643 if (size != other.dataSize()) {
644 return size < other.dataSize() ? -1 : 1;
645 }
646 return memcmp(data(), other.data(), size);
647}
648
Bernardo Rufino897b1652021-10-08 10:30:20 +0100649status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
650 size_t len, int* result) const {
651 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
652 // Don't accept size_t values which may have come from an inadvertent conversion from a
653 // negative int.
654 return BAD_VALUE;
655 }
656 size_t thisLimit;
657 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
658 return BAD_VALUE;
659 }
660 size_t otherLimit;
661 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
662 return BAD_VALUE;
663 }
664 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
665 return NO_ERROR;
666}
667
Jeff Brown13b16042014-11-11 16:44:25 -0800668bool Parcel::allowFds() const
669{
670 return mAllowFds;
671}
672
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700673bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400674{
675 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700676 if (!allowFds) {
677 mAllowFds = false;
678 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400679 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680}
681
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700682void Parcel::restoreAllowFds(bool lastValue)
683{
684 mAllowFds = lastValue;
685}
686
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700687bool Parcel::hasFileDescriptors() const
688{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000689 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000690 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000691 }
692 auto* kernelFields = maybeKernelFields();
693 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700694 scanForFds();
695 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000696 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697}
698
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000699std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
700 std::vector<sp<IBinder>> ret;
701
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000702#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000703 const auto* kernelFields = maybeKernelFields();
704 if (kernelFields == nullptr) {
705 return ret;
706 }
707
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000708 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000709 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
710 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000711 const flat_binder_object* flat =
712 reinterpret_cast<const flat_binder_object*>(mData + offset);
713 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
714
715 setDataPosition(offset);
716
717 sp<IBinder> binder = readStrongBinder();
718 if (binder != nullptr) ret.push_back(binder);
719 }
720
721 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000722#endif // BINDER_WITH_KERNEL_IPC
723
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000724 return ret;
725}
726
727std::vector<int> Parcel::debugReadAllFileDescriptors() const {
728 std::vector<int> ret;
729
Frederick Mayle69a0c992022-05-26 20:38:39 +0000730 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000731#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000732 size_t initPosition = dataPosition();
733 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
734 binder_size_t offset = kernelFields->mObjects[i];
735 const flat_binder_object* flat =
736 reinterpret_cast<const flat_binder_object*>(mData + offset);
737 if (flat->hdr.type != BINDER_TYPE_FD) continue;
738
739 setDataPosition(offset);
740
741 int fd = readFileDescriptor();
742 LOG_ALWAYS_FATAL_IF(fd == -1);
743 ret.push_back(fd);
744 }
745 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000746#else
747 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
748#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000749 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
750 for (const auto& fd : *rpcFields->mFds) {
751 ret.push_back(toRawFd(fd));
752 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000753 }
754
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000755 return ret;
756}
757
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100758status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100759 if (len > INT32_MAX || offset > INT32_MAX) {
760 // Don't accept size_t values which may have come from an inadvertent conversion from a
761 // negative int.
762 return BAD_VALUE;
763 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100764 size_t limit;
765 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100766 return BAD_VALUE;
767 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100768 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000769 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000770#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000771 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
772 size_t pos = kernelFields->mObjects[i];
773 if (pos < offset) continue;
774 if (pos + sizeof(flat_binder_object) > offset + len) {
775 if (kernelFields->mObjectsSorted) {
776 break;
777 } else {
778 continue;
779 }
780 }
781 const flat_binder_object* flat =
782 reinterpret_cast<const flat_binder_object*>(mData + pos);
783 if (flat->hdr.type == BINDER_TYPE_FD) {
784 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000785 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000786 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100787 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000788#else
789 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
790 return INVALID_OPERATION;
791#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000792 } else if (const auto* rpcFields = maybeRpcFields()) {
793 for (uint32_t pos : rpcFields->mObjectPositions) {
794 if (offset <= pos && pos < limit) {
795 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
796 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
797 *result = true;
798 break;
799 }
800 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100801 }
802 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100803 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100804}
805
Steven Morelandf183fdd2020-10-27 00:12:12 +0000806void Parcel::markSensitive() const
807{
808 mDeallocZero = true;
809}
810
Steven Moreland5553ac42020-11-11 02:14:45 +0000811void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000812 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
813
Steven Moreland5553ac42020-11-11 02:14:45 +0000814 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700815 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000816 }
817}
818
Steven Morelandc9939062021-05-05 17:57:41 +0000819void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000820 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
821 "format must be set before data is written OR on IPC data");
822
Frederick Mayled3c595a2022-05-09 23:02:54 +0000823 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000824}
825
826bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000827 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000828}
829
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000830void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000831 auto* kernelFields = maybeKernelFields();
832 if (kernelFields == nullptr) {
833 return;
834 }
835
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000836 // Only update the request headers once. We only want to point
837 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000838 if (!kernelFields->mRequestHeaderPresent) {
839 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
840 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000841 }
842}
843
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000844#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandb6c7e222021-02-18 19:20:14 +0000845#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700846constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700847#elif defined(__ANDROID_RECOVERY__)
848constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700849#else
850constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
851#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000852#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700853
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700854// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700855status_t Parcel::writeInterfaceToken(const String16& interface)
856{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000857 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000858}
859
860status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000861 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000862#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000863 const IPCThreadState* threadState = IPCThreadState::self();
864 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
865 updateWorkSourceRequestHeaderPosition();
866 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
867 : IPCThreadState::kUnsetWorkSource);
868 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000869#else // BINDER_WITH_KERNEL_IPC
870 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
871 return INVALID_OPERATION;
872#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000873 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000874
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700875 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000876 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700877}
878
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000879bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
880{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000881 auto* kernelFields = maybeKernelFields();
882 if (kernelFields == nullptr) {
883 return false;
884 }
885 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000886 return false;
887 }
888
889 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000890 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000891 status_t err = writeInt32(uid);
892 setDataPosition(initialPosition);
893 return err == NO_ERROR;
894}
895
Steven Morelandf1b1e492019-05-06 15:05:13 -0700896uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000897{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000898 auto* kernelFields = maybeKernelFields();
899 if (kernelFields == nullptr) {
900 return false;
901 }
902 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000903 return IPCThreadState::kUnsetWorkSource;
904 }
905
906 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000907 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000908 uid_t uid = readInt32();
909 setDataPosition(initialPosition);
910 return uid;
911}
912
Mathias Agopian83c04462009-05-22 19:00:22 -0700913bool Parcel::checkInterface(IBinder* binder) const
914{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700915 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700916}
917
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700918bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700919 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700920{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000921 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700922}
923
924bool Parcel::enforceInterface(const char16_t* interface,
925 size_t len,
926 IPCThreadState* threadState) const
927{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000928 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000929#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000930 // StrictModePolicy.
931 int32_t strictPolicy = readInt32();
932 if (threadState == nullptr) {
933 threadState = IPCThreadState::self();
934 }
935 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
936 // For one-way calls, the callee is running entirely
937 // disconnected from the caller, so disable StrictMode entirely.
938 // Not only does disk/network usage not impact the caller, but
939 // there's no way to communicate back violations anyway.
940 threadState->setStrictModePolicy(0);
941 } else {
942 threadState->setStrictModePolicy(strictPolicy);
943 }
944 // WorkSource.
945 updateWorkSourceRequestHeaderPosition();
946 int32_t workSource = readInt32();
947 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
948 // vendor header
949 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000950
951 // fuzzers skip this check, because it is for protecting the underlying ABI, but
952 // we don't want it to reduce our coverage
953 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000954 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
955 header);
956 return false;
957 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000958#else // BINDER_WITH_KERNEL_IPC
959 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
960 (void)threadState;
961 return false;
962#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700963 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000964
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100965 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700966 size_t parcel_interface_len;
967 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
968 if (len == parcel_interface_len &&
969 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700970 return true;
971 } else {
Steven Morelande99d8822023-06-02 21:56:39 +0000972 if (mServiceFuzzing) {
973 // ignore. Theoretically, this could cause a few false positives, because
974 // people could assume things about getInterfaceDescriptor if they pass
975 // this point, but it would be extremely fragile. It's more important that
976 // we fuzz with the above things read from the Parcel.
977 return true;
978 } else {
Steven Moreland3a667492023-06-02 21:41:39 +0000979 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000980 String8(interface, len).c_str(),
981 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +0000982 return false;
Steven Moreland3a667492023-06-02 21:41:39 +0000983 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700985}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700986
Pawan Wagh104654a2022-11-15 21:18:42 +0000987void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
988 mEnforceNoDataAvail = enforceNoDataAvail;
989}
990
Steven Moreland3a667492023-06-02 21:41:39 +0000991void Parcel::setServiceFuzzing() {
992 mServiceFuzzing = true;
993}
994
Steven Moreland418914a2023-06-28 21:47:14 +0000995bool Parcel::isServiceFuzzing() const {
996 return mServiceFuzzing;
997}
998
Jooyung Hand23f9502021-12-23 14:39:57 +0900999binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001000 if (!mEnforceNoDataAvail) {
1001 return binder::Status::ok();
1002 }
1003
Jooyung Hand23f9502021-12-23 14:39:57 +09001004 const auto n = dataAvail();
1005 if (n == 0) {
1006 return binder::Status::ok();
1007 }
1008 return binder::Status::
1009 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1010 String8::format("Parcel data not fully consumed, unread size: %zu",
1011 n));
1012}
1013
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014size_t Parcel::objectsCount() const
1015{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001016 if (const auto* kernelFields = maybeKernelFields()) {
1017 return kernelFields->mObjectsSize;
1018 }
1019 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001020}
1021
1022status_t Parcel::errorCheck() const
1023{
1024 return mError;
1025}
1026
1027void Parcel::setError(status_t err)
1028{
1029 mError = err;
1030}
1031
1032status_t Parcel::finishWrite(size_t len)
1033{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001034 if (len > INT32_MAX) {
1035 // don't accept size_t values which may have come from an
1036 // inadvertent conversion from a negative int.
1037 return BAD_VALUE;
1038 }
1039
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001040 //printf("Finish write of %d\n", len);
1041 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001042 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043 if (mDataPos > mDataSize) {
1044 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001045 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046 }
1047 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1048 return NO_ERROR;
1049}
1050
1051status_t Parcel::writeUnpadded(const void* data, size_t len)
1052{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001053 if (len > INT32_MAX) {
1054 // don't accept size_t values which may have come from an
1055 // inadvertent conversion from a negative int.
1056 return BAD_VALUE;
1057 }
1058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001059 size_t end = mDataPos + len;
1060 if (end < mDataPos) {
1061 // integer overflow
1062 return BAD_VALUE;
1063 }
1064
1065 if (end <= mDataCapacity) {
1066restart_write:
1067 memcpy(mData+mDataPos, data, len);
1068 return finishWrite(len);
1069 }
1070
1071 status_t err = growData(len);
1072 if (err == NO_ERROR) goto restart_write;
1073 return err;
1074}
1075
1076status_t Parcel::write(const void* data, size_t len)
1077{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001078 if (len > INT32_MAX) {
1079 // don't accept size_t values which may have come from an
1080 // inadvertent conversion from a negative int.
1081 return BAD_VALUE;
1082 }
1083
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 void* const d = writeInplace(len);
1085 if (d) {
1086 memcpy(d, data, len);
1087 return NO_ERROR;
1088 }
1089 return mError;
1090}
1091
1092void* Parcel::writeInplace(size_t len)
1093{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001094 if (len > INT32_MAX) {
1095 // don't accept size_t values which may have come from an
1096 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001097 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001098 }
1099
1100 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001102 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001104 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001105 }
1106
1107 if ((mDataPos+padded) <= mDataCapacity) {
1108restart_write:
1109 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1110 uint8_t* const data = mData+mDataPos;
1111
1112 // Need to pad at end?
1113 if (padded != len) {
1114#if BYTE_ORDER == BIG_ENDIAN
1115 static const uint32_t mask[4] = {
1116 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1117 };
1118#endif
1119#if BYTE_ORDER == LITTLE_ENDIAN
1120 static const uint32_t mask[4] = {
1121 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1122 };
1123#endif
1124 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1125 // *reinterpret_cast<void**>(data+padded-4));
1126 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1127 }
1128
1129 finishWrite(padded);
1130 return data;
1131 }
1132
1133 status_t err = growData(padded);
1134 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001135 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001136}
1137
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001138status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1139 const uint8_t* strData = (uint8_t*)str.data();
1140 const size_t strLen= str.length();
1141 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001142 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001143 return BAD_VALUE;
1144 }
1145
1146 status_t err = writeInt32(utf16Len);
1147 if (err) {
1148 return err;
1149 }
1150
1151 // Allocate enough bytes to hold our converted string and its terminating NULL.
1152 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1153 if (!dst) {
1154 return NO_MEMORY;
1155 }
1156
Sergio Girof4607432016-07-21 14:46:35 +01001157 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001158
1159 return NO_ERROR;
1160}
1161
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001162
Andy Hung49198cf2020-11-18 11:02:39 -08001163status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1164status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001165
Andy Hung49198cf2020-11-18 11:02:39 -08001166status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1167status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001168
Andy Hung49198cf2020-11-18 11:02:39 -08001169status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1170status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1171status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1172status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1173status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1174status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1175status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1176status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1177status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1178status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1179status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1180status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1181status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1182status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1183status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1184status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1185status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1186status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1187status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1188status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1189status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1190status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1191status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1192status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1193status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1194status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1195status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001196
Andy Hung49198cf2020-11-18 11:02:39 -08001197status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001198status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001199 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001200status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001201 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001202status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001203 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001204status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001205 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1206status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001207
Andy Hung49198cf2020-11-18 11:02:39 -08001208status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1209status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1210status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1211
1212status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1213status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1214status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1215
1216status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1217
1218status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1219status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1220
1221status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1222status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1223
1224status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1225status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1226status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1227status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1228status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1229status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1230status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1231status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1232status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1233status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1234status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1235status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1236status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1237status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1238status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1239status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1240status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1241status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1242status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1243status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1244status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1245status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1246status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1247status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1248status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1249status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1250status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1251
1252status_t Parcel::readString16Vector(
1253 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1254status_t Parcel::readString16Vector(
1255 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1256status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1257status_t Parcel::readUtf8VectorFromUtf16Vector(
1258 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1259status_t Parcel::readUtf8VectorFromUtf16Vector(
1260 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1261status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1262
1263status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1264status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1265status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1266
1267status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1268status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1269status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1270
1271status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001272
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001273status_t Parcel::writeInt32(int32_t val)
1274{
Andreas Huber84a6d042009-08-17 13:33:27 -07001275 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001277
1278status_t Parcel::writeUint32(uint32_t val)
1279{
1280 return writeAligned(val);
1281}
1282
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001283status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001284 if (len > INT32_MAX) {
1285 // don't accept size_t values which may have come from an
1286 // inadvertent conversion from a negative int.
1287 return BAD_VALUE;
1288 }
1289
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001290 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001291 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001292 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001293 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001294 if (ret == NO_ERROR) {
1295 ret = write(val, len * sizeof(*val));
1296 }
1297 return ret;
1298}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001299status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001300 if (len > INT32_MAX) {
1301 // don't accept size_t values which may have come from an
1302 // inadvertent conversion from a negative int.
1303 return BAD_VALUE;
1304 }
1305
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001306 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001307 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001308 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001309 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001310 if (ret == NO_ERROR) {
1311 ret = write(val, len * sizeof(*val));
1312 }
1313 return ret;
1314}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001315
Casey Dahlind6848f52015-10-15 15:44:59 -07001316status_t Parcel::writeBool(bool val)
1317{
1318 return writeInt32(int32_t(val));
1319}
1320
1321status_t Parcel::writeChar(char16_t val)
1322{
1323 return writeInt32(int32_t(val));
1324}
1325
1326status_t Parcel::writeByte(int8_t val)
1327{
1328 return writeInt32(int32_t(val));
1329}
1330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331status_t Parcel::writeInt64(int64_t val)
1332{
Andreas Huber84a6d042009-08-17 13:33:27 -07001333 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334}
1335
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001336status_t Parcel::writeUint64(uint64_t val)
1337{
1338 return writeAligned(val);
1339}
1340
Serban Constantinescuf683e012013-11-05 16:53:55 +00001341status_t Parcel::writePointer(uintptr_t val)
1342{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001343 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001344}
1345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346status_t Parcel::writeFloat(float val)
1347{
Andreas Huber84a6d042009-08-17 13:33:27 -07001348 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001349}
1350
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001351#if defined(__mips__) && defined(__mips_hard_float)
1352
1353status_t Parcel::writeDouble(double val)
1354{
1355 union {
1356 double d;
1357 unsigned long long ll;
1358 } u;
1359 u.d = val;
1360 return writeAligned(u.ll);
1361}
1362
1363#else
1364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365status_t Parcel::writeDouble(double val)
1366{
Andreas Huber84a6d042009-08-17 13:33:27 -07001367 return writeAligned(val);
1368}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001370#endif
1371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001372status_t Parcel::writeCString(const char* str)
1373{
1374 return write(str, strlen(str)+1);
1375}
1376
1377status_t Parcel::writeString8(const String8& str)
1378{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001379 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001380}
1381
1382status_t Parcel::writeString8(const char* str, size_t len)
1383{
1384 if (str == nullptr) return writeInt32(-1);
1385
Jeff Sharkey18220902020-11-05 08:36:20 -07001386 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001387 status_t err = writeInt32(len);
1388 if (err == NO_ERROR) {
1389 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1390 if (data) {
1391 memcpy(data, str, len);
1392 *reinterpret_cast<char*>(data+len) = 0;
1393 return NO_ERROR;
1394 }
1395 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001396 }
1397 return err;
1398}
1399
1400status_t Parcel::writeString16(const String16& str)
1401{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001402 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403}
1404
1405status_t Parcel::writeString16(const char16_t* str, size_t len)
1406{
Yi Kong91635562018-06-07 14:38:36 -07001407 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001408
Jeff Sharkey18220902020-11-05 08:36:20 -07001409 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 status_t err = writeInt32(len);
1411 if (err == NO_ERROR) {
1412 len *= sizeof(char16_t);
1413 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1414 if (data) {
1415 memcpy(data, str, len);
1416 *reinterpret_cast<char16_t*>(data+len) = 0;
1417 return NO_ERROR;
1418 }
1419 err = mError;
1420 }
1421 return err;
1422}
1423
1424status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1425{
Steven Morelanda86a3562019-08-01 23:28:34 +00001426 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427}
1428
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001429
Casey Dahlinb9872622015-11-25 15:09:45 -08001430status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1431 if (!parcelable) {
1432 return writeInt32(0);
1433 }
1434
1435 return writeParcelable(*parcelable);
1436}
1437
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001438status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001439{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001440 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001441 return BAD_TYPE;
1442
1443 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001444 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001445 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001447 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001448 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001450 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1451 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452
1453 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001454 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 return err;
1456 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001457 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001458 return err;
1459}
1460
Frederick Mayle69a0c992022-05-26 20:38:39 +00001461status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1462 if (auto* rpcFields = maybeRpcFields()) {
1463 std::variant<base::unique_fd, base::borrowed_fd> fdVariant;
1464 if (takeOwnership) {
1465 fdVariant = base::unique_fd(fd);
1466 } else {
1467 fdVariant = base::borrowed_fd(fd);
1468 }
1469 if (!mAllowFds) {
1470 return FDS_NOT_ALLOWED;
1471 }
1472 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1473 case RpcSession::FileDescriptorTransportMode::NONE: {
1474 return FDS_NOT_ALLOWED;
1475 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001476 case RpcSession::FileDescriptorTransportMode::UNIX:
1477 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001478 if (rpcFields->mFds == nullptr) {
1479 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1480 }
1481 size_t dataPos = mDataPos;
1482 if (dataPos > UINT32_MAX) {
1483 return NO_MEMORY;
1484 }
1485 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1486 return err;
1487 }
1488 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1489 return err;
1490 }
1491 rpcFields->mObjectPositions.push_back(dataPos);
1492 rpcFields->mFds->push_back(std::move(fdVariant));
1493 return OK;
1494 }
1495 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001496 }
1497
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001498#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001499 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001500 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001501 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001502 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001504 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001505 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001506#else // BINDER_WITH_KERNEL_IPC
1507 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1508 (void)fd;
1509 (void)takeOwnership;
1510 return INVALID_OPERATION;
1511#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001512}
1513
1514status_t Parcel::writeDupFileDescriptor(int fd)
1515{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001516 int dupFd;
1517 if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) {
1518 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001519 }
1520 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001521 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001522 close(dupFd);
1523 }
1524 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525}
1526
Dianne Hackborn1941a402016-08-29 12:30:43 -07001527status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1528{
1529 writeInt32(0);
1530 return writeFileDescriptor(fd, takeOwnership);
1531}
1532
Ryo Hashimotobf551892018-05-31 16:58:35 +09001533status_t Parcel::writeDupParcelFileDescriptor(int fd)
1534{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001535 int dupFd;
1536 if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) {
1537 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001538 }
1539 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1540 if (err != OK) {
1541 close(dupFd);
1542 }
1543 return err;
1544}
1545
Christopher Wiley2cf19952016-04-11 11:09:37 -07001546status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001547 return writeDupFileDescriptor(fd.get());
1548}
1549
Jeff Brown13b16042014-11-11 16:44:25 -08001550status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001551{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001552 if (len > INT32_MAX) {
1553 // don't accept size_t values which may have come from an
1554 // inadvertent conversion from a negative int.
1555 return BAD_VALUE;
1556 }
1557
Jeff Brown13b16042014-11-11 16:44:25 -08001558 status_t status;
1559 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001560 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001561 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001562 if (status) return status;
1563
1564 void* ptr = writeInplace(len);
1565 if (!ptr) return NO_MEMORY;
1566
Jeff Brown13b16042014-11-11 16:44:25 -08001567 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001568 return NO_ERROR;
1569 }
1570
Steve Block6807e592011-10-20 11:56:00 +01001571 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001572 int fd = ashmem_create_region("Parcel Blob", len);
1573 if (fd < 0) return NO_MEMORY;
1574
1575 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1576 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001577 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001578 } else {
Yi Kong91635562018-06-07 14:38:36 -07001579 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001580 if (ptr == MAP_FAILED) {
1581 status = -errno;
1582 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001583 if (!mutableCopy) {
1584 result = ashmem_set_prot_region(fd, PROT_READ);
1585 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001586 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001587 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001588 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001589 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001590 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001591 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001592 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001593 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001594 return NO_ERROR;
1595 }
1596 }
1597 }
1598 }
1599 ::munmap(ptr, len);
1600 }
1601 ::close(fd);
1602 return status;
1603}
1604
Jeff Brown13b16042014-11-11 16:44:25 -08001605status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1606{
1607 // Must match up with what's done in writeBlob.
1608 if (!mAllowFds) return FDS_NOT_ALLOWED;
1609 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1610 if (status) return status;
1611 return writeDupFileDescriptor(fd);
1612}
1613
Mathias Agopiane1424282013-07-29 21:24:40 -07001614status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001615{
1616 status_t err;
1617
1618 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001619 const size_t len = val.getFlattenedSize();
1620 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001621
Andrei Homescu1519b982022-06-09 02:04:44 +00001622 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001623 // don't accept size_t values which may have come from an
1624 // inadvertent conversion from a negative int.
1625 return BAD_VALUE;
1626 }
1627
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001628 err = this->writeInt32(len);
1629 if (err) return err;
1630
1631 err = this->writeInt32(fd_count);
1632 if (err) return err;
1633
1634 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001635 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001636 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001637 return BAD_VALUE;
1638
Yi Kong91635562018-06-07 14:38:36 -07001639 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001640 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001641 fds = new (std::nothrow) int[fd_count];
1642 if (fds == nullptr) {
1643 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1644 return BAD_VALUE;
1645 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001646 }
1647
1648 err = val.flatten(buf, len, fds, fd_count);
1649 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1650 err = this->writeDupFileDescriptor( fds[i] );
1651 }
1652
1653 if (fd_count) {
1654 delete [] fds;
1655 }
1656
1657 return err;
1658}
1659
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001660status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1661{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001662 auto* kernelFields = maybeKernelFields();
1663 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1664
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001665#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001666 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001667 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001668 if (enoughData && enoughObjects) {
1669restart_write:
1670 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001671
Christopher Tate98e67d32015-06-03 18:44:15 -07001672 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001673 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001674 if (!mAllowFds) {
1675 // fail before modifying our object index
1676 return FDS_NOT_ALLOWED;
1677 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001678 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001679 }
1680
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001681 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001682 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001683 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001684 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001685 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001688 return finishWrite(sizeof(flat_binder_object));
1689 }
1690
1691 if (!enoughData) {
1692 const status_t err = growData(sizeof(val));
1693 if (err != NO_ERROR) return err;
1694 }
1695 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001696 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1697 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1698 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001699 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001700 binder_size_t* objects =
1701 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001702 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001703 kernelFields->mObjects = objects;
1704 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001707 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001708#else // BINDER_WITH_KERNEL_IPC
1709 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1710 (void)val;
1711 (void)nullMetaData;
1712 return INVALID_OPERATION;
1713#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714}
1715
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001716status_t Parcel::writeNoException()
1717{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001718 binder::Status status;
1719 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001720}
1721
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001722status_t Parcel::validateReadData(size_t upperBound) const
1723{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001724 const auto* kernelFields = maybeKernelFields();
1725 if (kernelFields == nullptr) {
1726 // Can't validate RPC Parcel reads because the location of binder
1727 // objects is unknown.
1728 return OK;
1729 }
1730
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001731#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001732 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001733 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1734 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001735 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001736 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1737 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001738 // For some reason the current read position is greater than the next object
1739 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001740 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001741 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001742 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001743 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001744 if (!mServiceFuzzing) {
1745 ALOGE("Attempt to read from protected data in Parcel %p", this);
1746 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001747 return PERMISSION_DENIED;
1748 }
1749 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001750 } while (nextObject < kernelFields->mObjectsSize &&
1751 upperBound > kernelFields->mObjects[nextObject]);
1752 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001753 }
1754 return NO_ERROR;
1755 }
1756 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001757 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001758 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001759 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001760 prevObj--;
1761 if(*prevObj > *currObj) {
1762 goto data_unsorted;
1763 }
1764 currObj--;
1765 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001766 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001767 goto data_sorted;
1768
1769data_unsorted:
1770 // Insertion Sort mObjects
1771 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1772 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001773 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1774 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001775 binder_size_t temp = *iter0;
1776 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001777 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001778 *(iter1 + 1) = *iter1;
1779 iter1--;
1780 }
1781 *(iter1 + 1) = temp;
1782 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001783 kernelFields->mNextObjectHint = 0;
1784 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001785 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001786#else // BINDER_WITH_KERNEL_IPC
1787 (void)upperBound;
1788 return NO_ERROR;
1789#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001790}
1791
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792status_t Parcel::read(void* outData, size_t len) const
1793{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001794 if (len > INT32_MAX) {
1795 // don't accept size_t values which may have come from an
1796 // inadvertent conversion from a negative int.
1797 return BAD_VALUE;
1798 }
1799
1800 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1801 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001802 const auto* kernelFields = maybeKernelFields();
1803 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001804 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001805 if(err != NO_ERROR) {
1806 // Still increment the data position by the expected length
1807 mDataPos += pad_size(len);
1808 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1809 return err;
1810 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001811 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001813 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001814 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815 return NO_ERROR;
1816 }
1817 return NOT_ENOUGH_DATA;
1818}
1819
1820const void* Parcel::readInplace(size_t len) const
1821{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001822 if (len > INT32_MAX) {
1823 // don't accept size_t values which may have come from an
1824 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001825 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001826 }
1827
1828 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1829 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001830 const auto* kernelFields = maybeKernelFields();
1831 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001832 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001833 if(err != NO_ERROR) {
1834 // Still increment the data position by the expected length
1835 mDataPos += pad_size(len);
1836 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001837 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001838 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001839 }
1840
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001842 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001843 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844 return data;
1845 }
Yi Kong91635562018-06-07 14:38:36 -07001846 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847}
1848
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001849status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1850 if (status_t status = readInt32(size); status != OK) return status;
1851 if (*size < 0) return OK; // may be null, client to handle
1852
1853 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1854
1855 // approximation, can't know max element size (e.g. if it makes heap
1856 // allocations)
1857 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1858 int32_t allocationSize;
1859 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1860
1861 // High limit of 1MB since something this big could never be returned. Could
1862 // probably scope this down, but might impact very specific usecases.
1863 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1864
1865 if (allocationSize >= kMaxAllocationSize) {
1866 return NO_MEMORY;
1867 }
1868
1869 return OK;
1870}
1871
Andreas Huber84a6d042009-08-17 13:33:27 -07001872template<class T>
1873status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001874 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001875 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001876
1877 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001878 const auto* kernelFields = maybeKernelFields();
1879 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001880 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001881 if(err != NO_ERROR) {
1882 // Still increment the data position by the expected length
1883 mDataPos += sizeof(T);
1884 return err;
1885 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001886 }
1887
Andrei Homescue33238e2022-03-29 22:50:00 +00001888 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001889 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 return NO_ERROR;
1891 } else {
1892 return NOT_ENOUGH_DATA;
1893 }
1894}
1895
Andreas Huber84a6d042009-08-17 13:33:27 -07001896template<class T>
1897T Parcel::readAligned() const {
1898 T result;
1899 if (readAligned(&result) != NO_ERROR) {
1900 result = 0;
1901 }
1902
1903 return result;
1904}
1905
1906template<class T>
1907status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001908 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001909 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001910
1911 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1912restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001913 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001914 return finishWrite(sizeof(val));
1915 }
1916
1917 status_t err = growData(sizeof(val));
1918 if (err == NO_ERROR) goto restart_write;
1919 return err;
1920}
1921
1922status_t Parcel::readInt32(int32_t *pArg) const
1923{
1924 return readAligned(pArg);
1925}
1926
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927int32_t Parcel::readInt32() const
1928{
Andreas Huber84a6d042009-08-17 13:33:27 -07001929 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001930}
1931
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001932status_t Parcel::readUint32(uint32_t *pArg) const
1933{
1934 return readAligned(pArg);
1935}
1936
1937uint32_t Parcel::readUint32() const
1938{
1939 return readAligned<uint32_t>();
1940}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941
1942status_t Parcel::readInt64(int64_t *pArg) const
1943{
Andreas Huber84a6d042009-08-17 13:33:27 -07001944 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001945}
1946
1947
1948int64_t Parcel::readInt64() const
1949{
Andreas Huber84a6d042009-08-17 13:33:27 -07001950 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001951}
1952
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001953status_t Parcel::readUint64(uint64_t *pArg) const
1954{
1955 return readAligned(pArg);
1956}
1957
1958uint64_t Parcel::readUint64() const
1959{
1960 return readAligned<uint64_t>();
1961}
1962
Serban Constantinescuf683e012013-11-05 16:53:55 +00001963status_t Parcel::readPointer(uintptr_t *pArg) const
1964{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001965 status_t ret;
1966 binder_uintptr_t ptr;
1967 ret = readAligned(&ptr);
1968 if (!ret)
1969 *pArg = ptr;
1970 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001971}
1972
1973uintptr_t Parcel::readPointer() const
1974{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001975 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001976}
1977
1978
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979status_t Parcel::readFloat(float *pArg) const
1980{
Andreas Huber84a6d042009-08-17 13:33:27 -07001981 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982}
1983
1984
1985float Parcel::readFloat() const
1986{
Andreas Huber84a6d042009-08-17 13:33:27 -07001987 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988}
1989
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001990#if defined(__mips__) && defined(__mips_hard_float)
1991
1992status_t Parcel::readDouble(double *pArg) const
1993{
1994 union {
1995 double d;
1996 unsigned long long ll;
1997 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001998 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001999 status_t status;
2000 status = readAligned(&u.ll);
2001 *pArg = u.d;
2002 return status;
2003}
2004
2005double Parcel::readDouble() const
2006{
2007 union {
2008 double d;
2009 unsigned long long ll;
2010 } u;
2011 u.ll = readAligned<unsigned long long>();
2012 return u.d;
2013}
2014
2015#else
2016
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002017status_t Parcel::readDouble(double *pArg) const
2018{
Andreas Huber84a6d042009-08-17 13:33:27 -07002019 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002020}
2021
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002022double Parcel::readDouble() const
2023{
Andreas Huber84a6d042009-08-17 13:33:27 -07002024 return readAligned<double>();
2025}
2026
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002027#endif
2028
Casey Dahlind6848f52015-10-15 15:44:59 -07002029status_t Parcel::readBool(bool *pArg) const
2030{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002031 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002032 status_t ret = readInt32(&tmp);
2033 *pArg = (tmp != 0);
2034 return ret;
2035}
2036
2037bool Parcel::readBool() const
2038{
2039 return readInt32() != 0;
2040}
2041
2042status_t Parcel::readChar(char16_t *pArg) const
2043{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002044 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002045 status_t ret = readInt32(&tmp);
2046 *pArg = char16_t(tmp);
2047 return ret;
2048}
2049
2050char16_t Parcel::readChar() const
2051{
2052 return char16_t(readInt32());
2053}
2054
2055status_t Parcel::readByte(int8_t *pArg) const
2056{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002057 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002058 status_t ret = readInt32(&tmp);
2059 *pArg = int8_t(tmp);
2060 return ret;
2061}
2062
2063int8_t Parcel::readByte() const
2064{
2065 return int8_t(readInt32());
2066}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002068status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2069 size_t utf16Size = 0;
2070 const char16_t* src = readString16Inplace(&utf16Size);
2071 if (!src) {
2072 return UNEXPECTED_NULL;
2073 }
2074
2075 // Save ourselves the trouble, we're done.
2076 if (utf16Size == 0u) {
2077 str->clear();
2078 return NO_ERROR;
2079 }
2080
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002081 // Allow for closing '\0'
2082 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2083 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002084 return BAD_VALUE;
2085 }
2086 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002087 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002088 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002089 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2090 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002091 return NO_ERROR;
2092}
2093
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002094const char* Parcel::readCString() const
2095{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002096 if (mDataPos < mDataSize) {
2097 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2099 // is the string's trailing NUL within the parcel's valid bounds?
2100 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2101 if (eos) {
2102 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002103 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002104 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 return str;
2106 }
2107 }
Yi Kong91635562018-06-07 14:38:36 -07002108 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002109}
2110
2111String8 Parcel::readString8() const
2112{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002113 size_t len;
2114 const char* str = readString8Inplace(&len);
2115 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002116
2117 if (!mServiceFuzzing) {
2118 ALOGE("Reading a NULL string not supported here.");
2119 }
2120
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002121 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002122}
2123
2124status_t Parcel::readString8(String8* pArg) const
2125{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002126 size_t len;
2127 const char* str = readString8Inplace(&len);
2128 if (str) {
2129 pArg->setTo(str, len);
2130 return 0;
2131 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002132 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002133 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002134 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002135}
2136
2137const char* Parcel::readString8Inplace(size_t* outLen) const
2138{
2139 int32_t size = readInt32();
2140 // watch for potential int overflow from size+1
2141 if (size >= 0 && size < INT32_MAX) {
2142 *outLen = size;
2143 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002144 if (str != nullptr) {
2145 if (str[size] == '\0') {
2146 return str;
2147 }
2148 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002149 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002150 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002151 *outLen = 0;
2152 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002153}
2154
2155String16 Parcel::readString16() const
2156{
2157 size_t len;
2158 const char16_t* str = readString16Inplace(&len);
2159 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002160
2161 if (!mServiceFuzzing) {
2162 ALOGE("Reading a NULL string not supported here.");
2163 }
2164
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 return String16();
2166}
2167
Casey Dahlinb9872622015-11-25 15:09:45 -08002168
Casey Dahlin451ff582015-10-19 18:12:18 -07002169status_t Parcel::readString16(String16* pArg) const
2170{
2171 size_t len;
2172 const char16_t* str = readString16Inplace(&len);
2173 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002174 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002175 return 0;
2176 } else {
2177 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002178 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002179 }
2180}
2181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2183{
2184 int32_t size = readInt32();
2185 // watch for potential int overflow from size+1
2186 if (size >= 0 && size < INT32_MAX) {
2187 *outLen = size;
2188 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002189 if (str != nullptr) {
2190 if (str[size] == u'\0') {
2191 return str;
2192 }
2193 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 }
2195 }
2196 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002197 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198}
2199
Casey Dahlinf0c13772015-10-27 18:33:56 -07002200status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2201{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002202 status_t status = readNullableStrongBinder(val);
2203 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002204 if (!mServiceFuzzing) {
2205 ALOGW("Expecting binder but got null!");
2206 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002207 status = UNEXPECTED_NULL;
2208 }
2209 return status;
2210}
2211
2212status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2213{
Steven Morelanda86a3562019-08-01 23:28:34 +00002214 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002215}
2216
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217sp<IBinder> Parcel::readStrongBinder() const
2218{
2219 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002220 // Note that a lot of code in Android reads binders by hand with this
2221 // method, and that code has historically been ok with getting nullptr
2222 // back (while ignoring error codes).
2223 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 return val;
2225}
2226
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002227int32_t Parcel::readExceptionCode() const
2228{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002229 binder::Status status;
2230 status.readFromParcel(*this);
2231 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002232}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002233
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002234native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002235{
2236 int numFds, numInts;
2237 status_t err;
2238 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002239 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002240 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002241 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002242
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002243 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002244 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002245 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002246 }
2247
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002248 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002249 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002250 if (h->data[i] < 0) {
2251 for (int j = 0; j < i; j++) {
2252 close(h->data[j]);
2253 }
2254 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002255 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002256 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002257 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002258 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002259 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002260 native_handle_close(h);
2261 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002262 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002263 }
2264 return h;
2265}
2266
Frederick Mayle69a0c992022-05-26 20:38:39 +00002267int Parcel::readFileDescriptor() const {
2268 if (const auto* rpcFields = maybeRpcFields()) {
2269 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2270 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002271 if (!mServiceFuzzing) {
2272 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2273 "the object list",
2274 this, mDataPos);
2275 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002276 return BAD_TYPE;
2277 }
2278
2279 int32_t objectType = readInt32();
2280 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2281 return BAD_TYPE;
2282 }
2283
2284 int32_t fdIndex = readInt32();
2285 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2286 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2287 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2288 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2289 return BAD_VALUE;
2290 }
2291 return toRawFd(rpcFields->mFds->at(fdIndex));
2292 }
2293
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002294#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002296
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002297 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002298 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002302#else // BINDER_WITH_KERNEL_IPC
2303 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2304 return INVALID_OPERATION;
2305#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306}
2307
Frederick Mayle69a0c992022-05-26 20:38:39 +00002308int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002309 int32_t hasComm = readInt32();
2310 int fd = readFileDescriptor();
2311 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002312 // detach (owned by the binder driver)
2313 int comm = readFileDescriptor();
2314
2315 // warning: this must be kept in sync with:
2316 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2317 enum ParcelFileDescriptorStatus {
2318 DETACHED = 2,
2319 };
2320
2321#if BYTE_ORDER == BIG_ENDIAN
2322 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2323#endif
2324#if BYTE_ORDER == LITTLE_ENDIAN
2325 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2326#endif
2327
2328 ssize_t written = TEMP_FAILURE_RETRY(
2329 ::write(comm, &message, sizeof(message)));
2330
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002331 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002332 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2333 written, strerror(errno));
2334 return BAD_TYPE;
2335 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002336 }
2337 return fd;
2338}
2339
Christopher Wiley2cf19952016-04-11 11:09:37 -07002340status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002341{
2342 int got = readFileDescriptor();
2343
2344 if (got == BAD_TYPE) {
2345 return BAD_TYPE;
2346 }
2347
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002348 int dupFd;
2349 if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) {
2350 return BAD_VALUE;
2351 }
2352
2353 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002354
2355 if (val->get() < 0) {
2356 return BAD_VALUE;
2357 }
2358
2359 return OK;
2360}
2361
Ryo Hashimotobf551892018-05-31 16:58:35 +09002362status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2363{
2364 int got = readParcelFileDescriptor();
2365
2366 if (got == BAD_TYPE) {
2367 return BAD_TYPE;
2368 }
2369
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002370 int dupFd;
2371 if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) {
2372 return BAD_VALUE;
2373 }
2374
2375 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002376
2377 if (val->get() < 0) {
2378 return BAD_VALUE;
2379 }
2380
2381 return OK;
2382}
Casey Dahlin06673e32015-11-23 13:24:23 -08002383
Jeff Brown5707dbf2011-09-23 21:17:56 -07002384status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2385{
Jeff Brown13b16042014-11-11 16:44:25 -08002386 int32_t blobType;
2387 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002388 if (status) return status;
2389
Jeff Brown13b16042014-11-11 16:44:25 -08002390 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002391 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002392 const void* ptr = readInplace(len);
2393 if (!ptr) return BAD_VALUE;
2394
Jeff Brown13b16042014-11-11 16:44:25 -08002395 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002396 return NO_ERROR;
2397 }
2398
Steve Block6807e592011-10-20 11:56:00 +01002399 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002400 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002401 int fd = readFileDescriptor();
2402 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2403
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002404 if (!ashmem_valid(fd)) {
2405 ALOGE("invalid fd");
2406 return BAD_VALUE;
2407 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002408 int size = ashmem_get_size_region(fd);
2409 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002410 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002411 return BAD_VALUE;
2412 }
Yi Kong91635562018-06-07 14:38:36 -07002413 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002414 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002415 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002416
Jeff Brown13b16042014-11-11 16:44:25 -08002417 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002418 return NO_ERROR;
2419}
2420
Mathias Agopiane1424282013-07-29 21:24:40 -07002421status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002422{
2423 // size
2424 const size_t len = this->readInt32();
2425 const size_t fd_count = this->readInt32();
2426
Andrei Homescu1519b982022-06-09 02:04:44 +00002427 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002428 // don't accept size_t values which may have come from an
2429 // inadvertent conversion from a negative int.
2430 return BAD_VALUE;
2431 }
2432
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002433 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002434 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002435 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002436 return BAD_VALUE;
2437
Yi Kong91635562018-06-07 14:38:36 -07002438 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002439 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002440 fds = new (std::nothrow) int[fd_count];
2441 if (fds == nullptr) {
2442 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2443 return BAD_VALUE;
2444 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002445 }
2446
2447 status_t err = NO_ERROR;
2448 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002449 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002450 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002451 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002452 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 -07002453 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2454 // Close all the file descriptors that were dup-ed.
2455 for (size_t j=0; j<i ;j++) {
2456 close(fds[j]);
2457 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002458 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002459 }
2460
2461 if (err == NO_ERROR) {
2462 err = val.unflatten(buf, len, fds, fd_count);
2463 }
2464
2465 if (fd_count) {
2466 delete [] fds;
2467 }
2468
2469 return err;
2470}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002471
2472#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2474{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002475 const auto* kernelFields = maybeKernelFields();
2476 if (kernelFields == nullptr) {
2477 return nullptr;
2478 }
2479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002480 const size_t DPOS = mDataPos;
2481 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2482 const flat_binder_object* obj
2483 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2484 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002485 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002486 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 // the object list, so we don't want to check for it when
2488 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002489 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 return obj;
2491 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002492
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002493 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002494 binder_size_t* const OBJS = kernelFields->mObjects;
2495 const size_t N = kernelFields->mObjectsSize;
2496 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002497
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002499 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002501
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002502 // Start at the current hint position, looking for an object at
2503 // the current data position.
2504 if (opos < N) {
2505 while (opos < (N-1) && OBJS[opos] < DPOS) {
2506 opos++;
2507 }
2508 } else {
2509 opos = N-1;
2510 }
2511 if (OBJS[opos] == DPOS) {
2512 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002513 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002515 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002516 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 return obj;
2518 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 // Look backwards for it...
2521 while (opos > 0 && OBJS[opos] > DPOS) {
2522 opos--;
2523 }
2524 if (OBJS[opos] == DPOS) {
2525 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002526 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002527 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002528 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002529 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 return obj;
2531 }
2532 }
Steven Moreland3a667492023-06-02 21:41:39 +00002533 if (!mServiceFuzzing) {
2534 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2535 "list",
2536 this, DPOS);
2537 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 }
Yi Kong91635562018-06-07 14:38:36 -07002539 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002541#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542
Frederick Mayled3c595a2022-05-09 23:02:54 +00002543void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002544 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002545#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002546 size_t i = kernelFields->mObjectsSize;
2547 if (i > 0) {
2548 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002550 while (i > 0) {
2551 i--;
2552 const flat_binder_object* flat =
2553 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2554 if (flat->hdr.type == BINDER_TYPE_FD) {
2555 // ALOGI("Closing fd: %ld", flat->handle);
2556 close(flat->handle);
2557 }
2558 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002559#else // BINDER_WITH_KERNEL_IPC
2560 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2561#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002562 } else if (auto* rpcFields = maybeRpcFields()) {
2563 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 }
2565}
2566
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002567uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002569 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570}
2571
2572size_t Parcel::ipcDataSize() const
2573{
2574 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2575}
2576
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002577uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002578{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002579 if (const auto* kernelFields = maybeKernelFields()) {
2580 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2581 }
2582 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583}
2584
2585size_t Parcel::ipcObjectsCount() const
2586{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002587 if (const auto* kernelFields = maybeKernelFields()) {
2588 return kernelFields->mObjectsSize;
2589 }
2590 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591}
2592
Frederick Mayled3c595a2022-05-09 23:02:54 +00002593void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2594 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002595 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2596 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2597
Steven Morelandceed9bb2020-12-17 01:01:06 +00002598 freeData();
2599
Frederick Mayled3c595a2022-05-09 23:02:54 +00002600 auto* kernelFields = maybeKernelFields();
2601 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2602
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 mData = const_cast<uint8_t*>(data);
2604 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002605 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2606 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002608
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002609#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002610 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002611 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2612 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002613 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002614 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002615 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002616 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002617 break;
2618 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002619 const flat_binder_object* flat
2620 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2621 uint32_t type = flat->hdr.type;
2622 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2623 type == BINDER_TYPE_FD)) {
2624 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2625 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002626 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002627 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002628 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002629 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2630 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002631
2632 // WARNING: callers of ipcSetDataReference need to make sure they
2633 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002634 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002635 break;
2636 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002637 minOffset = offset + sizeof(flat_binder_object);
2638 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002640#else // BINDER_WITH_KERNEL_IPC
2641 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2642 "Non-zero objects count passed to Parcel with kernel driver disabled");
2643#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644}
2645
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002646status_t Parcel::rpcSetDataReference(
2647 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2648 const uint32_t* objectTable, size_t objectTableSize,
2649 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds,
2650 release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002651 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2652 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2653
2654 LOG_ALWAYS_FATAL_IF(session == nullptr);
2655
Frederick Mayle694e9732022-07-15 00:24:58 +00002656 if (objectTableSize != ancillaryFds.size()) {
2657 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2658 relFunc(data, dataSize, nullptr, 0);
2659 return BAD_VALUE;
2660 }
2661 for (size_t i = 0; i < objectTableSize; i++) {
2662 uint32_t minObjectEnd;
2663 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2664 minObjectEnd >= dataSize) {
2665 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2666 objectTable[i], dataSize);
2667 relFunc(data, dataSize, nullptr, 0);
2668 return BAD_VALUE;
2669 }
2670 }
2671
Frederick Mayled3c595a2022-05-09 23:02:54 +00002672 freeData();
2673 markForRpc(session);
2674
Frederick Mayle69a0c992022-05-26 20:38:39 +00002675 auto* rpcFields = maybeRpcFields();
2676 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2677
Frederick Mayled3c595a2022-05-09 23:02:54 +00002678 mData = const_cast<uint8_t*>(data);
2679 mDataSize = mDataCapacity = dataSize;
2680 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002681
Frederick Mayle69a0c992022-05-26 20:38:39 +00002682 rpcFields->mObjectPositions.reserve(objectTableSize);
2683 for (size_t i = 0; i < objectTableSize; i++) {
2684 rpcFields->mObjectPositions.push_back(objectTable[i]);
2685 }
2686 if (!ancillaryFds.empty()) {
2687 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002688 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002689 }
2690
2691 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002692}
2693
Pawan Wagh7063b522022-09-28 18:52:26 +00002694void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002697 if (errorCheck() != NO_ERROR) {
2698 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002699 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002700 } else if (dataSize() > 0) {
2701 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002702 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002703#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002704 if (const auto* kernelFields = maybeKernelFields()) {
2705 const binder_size_t* OBJS = kernelFields->mObjects;
2706 const size_t N = objectsCount();
2707 for (size_t i = 0; i < N; i++) {
2708 const flat_binder_object* flat =
2709 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002710 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002711 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2712 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002714#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 } else {
2716 to << "NULL";
2717 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002718
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002719 to << ")";
2720}
2721
2722void Parcel::releaseObjects()
2723{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002724 auto* kernelFields = maybeKernelFields();
2725 if (kernelFields == nullptr) {
2726 return;
2727 }
2728
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002729#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002730 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002731 if (i == 0) {
2732 return;
2733 }
2734 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002735 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002736 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 while (i > 0) {
2738 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002739 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002740 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002741 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002742#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002743}
2744
2745void Parcel::acquireObjects()
2746{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002747 auto* kernelFields = maybeKernelFields();
2748 if (kernelFields == nullptr) {
2749 return;
2750 }
2751
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002752#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002753 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002754 if (i == 0) {
2755 return;
2756 }
2757 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002758 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002759 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002760 while (i > 0) {
2761 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002762 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002763 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002764 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002765#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766}
2767
2768void Parcel::freeData()
2769{
2770 freeDataNoInit();
2771 initState();
2772}
2773
2774void Parcel::freeDataNoInit()
2775{
2776 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002777 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002778 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002779 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002780 // Close FDs before freeing, otherwise they will leak for kernel binder.
2781 closeFileDescriptors();
2782 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002783 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002784 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002785 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002786 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002787 if (mData) {
2788 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002789 gParcelGlobalAllocSize -= mDataCapacity;
2790 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002791 if (mDeallocZero) {
2792 zeroMemory(mData, mDataSize);
2793 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002794 free(mData);
2795 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002796 auto* kernelFields = maybeKernelFields();
2797 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798 }
2799}
2800
2801status_t Parcel::growData(size_t len)
2802{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002803 if (len > INT32_MAX) {
2804 // don't accept size_t values which may have come from an
2805 // inadvertent conversion from a negative int.
2806 return BAD_VALUE;
2807 }
2808
Martijn Coenen93fe5182020-01-22 10:46:25 +01002809 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2810 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002811 size_t newSize = ((mDataSize+len)*3)/2;
2812 return (newSize <= mDataSize)
2813 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002814 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815}
2816
Steven Morelandf183fdd2020-10-27 00:12:12 +00002817static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2818 if (!zero) {
2819 return (uint8_t*)realloc(data, newCapacity);
2820 }
2821 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2822 if (!newData) {
2823 return nullptr;
2824 }
2825
2826 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2827 zeroMemory(data, oldCapacity);
2828 free(data);
2829 return newData;
2830}
2831
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002832status_t Parcel::restartWrite(size_t desired)
2833{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002834 if (desired > INT32_MAX) {
2835 // don't accept size_t values which may have come from an
2836 // inadvertent conversion from a negative int.
2837 return BAD_VALUE;
2838 }
2839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 if (mOwner) {
2841 freeData();
2842 return continueWrite(desired);
2843 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002844
Steven Morelandf183fdd2020-10-27 00:12:12 +00002845 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 if (!data && desired > mDataCapacity) {
2847 mError = NO_MEMORY;
2848 return NO_MEMORY;
2849 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002850
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002851 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002852
Devin Moore4a0a55e2020-06-04 13:23:10 -07002853 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002854 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002855 if (mDataCapacity > desired) {
2856 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2857 } else {
2858 gParcelGlobalAllocSize += (desired - mDataCapacity);
2859 }
2860
Colin Cross83ec65e2015-12-08 17:15:50 -08002861 if (!mData) {
2862 gParcelGlobalAllocCount++;
2863 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002864 mData = data;
2865 mDataCapacity = desired;
2866 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002867
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002868 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002869 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2870 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2871
Frederick Mayled3c595a2022-05-09 23:02:54 +00002872 if (auto* kernelFields = maybeKernelFields()) {
2873 free(kernelFields->mObjects);
2874 kernelFields->mObjects = nullptr;
2875 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2876 kernelFields->mNextObjectHint = 0;
2877 kernelFields->mObjectsSorted = false;
2878 kernelFields->mHasFds = false;
2879 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002880 } else if (auto* rpcFields = maybeRpcFields()) {
2881 rpcFields->mObjectPositions.clear();
2882 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002883 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002884 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002885
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 return NO_ERROR;
2887}
2888
2889status_t Parcel::continueWrite(size_t desired)
2890{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002891 if (desired > INT32_MAX) {
2892 // don't accept size_t values which may have come from an
2893 // inadvertent conversion from a negative int.
2894 return BAD_VALUE;
2895 }
2896
Frederick Mayled3c595a2022-05-09 23:02:54 +00002897 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002898 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002899
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900 // If shrinking, first adjust for any objects that appear
2901 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002902 size_t objectsSize =
2903 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2904 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002905 if (desired == 0) {
2906 objectsSize = 0;
2907 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002908 if (kernelFields) {
2909 while (objectsSize > 0) {
2910 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2911 objectsSize--;
2912 }
2913 } else {
2914 while (objectsSize > 0) {
2915 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2916 objectsSize--;
2917 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002918 }
2919 }
2920 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002921
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002922 if (mOwner) {
2923 // If the size is going to zero, just release the owner's data.
2924 if (desired == 0) {
2925 freeData();
2926 return NO_ERROR;
2927 }
2928
2929 // If there is a different owner, we need to take
2930 // posession.
2931 uint8_t* data = (uint8_t*)malloc(desired);
2932 if (!data) {
2933 mError = NO_MEMORY;
2934 return NO_MEMORY;
2935 }
Yi Kong91635562018-06-07 14:38:36 -07002936 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002937
Frederick Mayle69a0c992022-05-26 20:38:39 +00002938 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002939 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002940 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002941 free(data);
2942
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002943 mError = NO_MEMORY;
2944 return NO_MEMORY;
2945 }
2946
2947 // Little hack to only acquire references on objects
2948 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002949 size_t oldObjectsSize = kernelFields->mObjectsSize;
2950 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002951 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002952 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002953 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002954 if (rpcFields) {
2955 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2956 free(data);
2957 return status;
2958 }
2959 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002960
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002961 if (mData) {
2962 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2963 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002964 if (objects && kernelFields && kernelFields->mObjects) {
2965 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002966 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002967 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2968 if (kernelFields) {
2969 // TODO(b/239222407): This seems wrong. We should only free FDs when
2970 // they are in a truncated section of the parcel.
2971 closeFileDescriptors();
2972 }
2973 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002974 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002975 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002976
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002977 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002978 gParcelGlobalAllocSize += desired;
2979 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002980
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002981 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002982 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002983 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002984 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002985 if (kernelFields) {
2986 kernelFields->mObjects = objects;
2987 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2988 kernelFields->mNextObjectHint = 0;
2989 kernelFields->mObjectsSorted = false;
2990 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002991
2992 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002993 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002994#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002995 // Need to release refs on any objects we are dropping.
2996 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002997 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2998 const flat_binder_object* flat =
2999 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003000 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003001 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003002 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003003 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003004 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003005 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003006
3007 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003008 free(kernelFields->mObjects);
3009 kernelFields->mObjects = nullptr;
3010 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003011 } else {
3012 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003013 (binder_size_t*)realloc(kernelFields->mObjects,
3014 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003015 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003016 kernelFields->mObjects = objects;
3017 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003018 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003019 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003020 kernelFields->mObjectsSize = objectsSize;
3021 kernelFields->mNextObjectHint = 0;
3022 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003023#else // BINDER_WITH_KERNEL_IPC
3024 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3025#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003026 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003027 if (rpcFields) {
3028 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3029 return status;
3030 }
3031 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003032
3033 // We own the data, so we can just do a realloc().
3034 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003035 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003036 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003037 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3038 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003039 gParcelGlobalAllocSize += desired;
3040 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003041 mData = data;
3042 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003043 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003044 mError = NO_MEMORY;
3045 return NO_MEMORY;
3046 }
3047 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003048 if (mDataSize > desired) {
3049 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003050 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003051 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003052 if (mDataPos > desired) {
3053 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003054 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003055 }
3056 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003058 } else {
3059 // This is the first data. Easy!
3060 uint8_t* data = (uint8_t*)malloc(desired);
3061 if (!data) {
3062 mError = NO_MEMORY;
3063 return NO_MEMORY;
3064 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003065
Frederick Mayled3c595a2022-05-09 23:02:54 +00003066 if (!(mDataCapacity == 0 &&
3067 (kernelFields == nullptr ||
3068 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3069 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3070 kernelFields ? kernelFields->mObjects : nullptr,
3071 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003072 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003073
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003074 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003075 gParcelGlobalAllocSize += desired;
3076 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003078 mData = data;
3079 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003080 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3081 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003082 mDataCapacity = desired;
3083 }
3084
3085 return NO_ERROR;
3086}
3087
Frederick Mayle69a0c992022-05-26 20:38:39 +00003088status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3089 auto* rpcFields = maybeRpcFields();
3090 if (newObjectsSize == 0) {
3091 rpcFields->mObjectPositions.clear();
3092 if (rpcFields->mFds) {
3093 rpcFields->mFds->clear();
3094 }
3095 return OK;
3096 }
3097 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3098 uint32_t pos = rpcFields->mObjectPositions.back();
3099 rpcFields->mObjectPositions.pop_back();
3100 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3101 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3102 const auto fdIndex =
3103 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3104 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3105 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3106 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3107 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3108 return BAD_VALUE;
3109 }
3110 // In practice, this always removes the last element.
3111 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3112 }
3113 }
3114 return OK;
3115}
3116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003117void Parcel::initState()
3118{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003119 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003120 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003121 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003122 mDataSize = 0;
3123 mDataCapacity = 0;
3124 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003125 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3126 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003127 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003128 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003129 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003130 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003131 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003132 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003133}
3134
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003135void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003136 auto* kernelFields = maybeKernelFields();
3137 if (kernelFields == nullptr) {
3138 return;
3139 }
3140 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003141 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003142 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003143}
3144
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003145#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003146size_t Parcel::getBlobAshmemSize() const
3147{
Adrian Roos6bb31142015-10-22 16:46:12 -07003148 // This used to return the size of all blobs that were written to ashmem, now we're returning
3149 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003150 // TODO(b/202029388): Remove method once ABI can be changed.
3151 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003152}
3153
Adrian Rooscbf37262015-10-22 16:12:53 -07003154size_t Parcel::getOpenAshmemSize() const
3155{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003156 auto* kernelFields = maybeKernelFields();
3157 if (kernelFields == nullptr) {
3158 return 0;
3159 }
3160
Steven Morelandc673f1f2021-10-07 18:23:35 -07003161 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003162 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003163 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003164 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003165
3166 // cookie is compared against zero for historical reasons
3167 // > obj.cookie = takeOwnership ? 1 : 0;
3168 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3169 int size = ashmem_get_size_region(flat->handle);
3170 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3171 ALOGE("Overflow when computing ashmem size.");
3172 return SIZE_MAX;
3173 }
3174 }
3175 }
3176 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003177}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003178#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003179
3180// --- Parcel::Blob ---
3181
3182Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003183 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003184}
3185
3186Parcel::Blob::~Blob() {
3187 release();
3188}
3189
3190void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003191 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003192 ::munmap(mData, mSize);
3193 }
3194 clear();
3195}
3196
Jeff Brown13b16042014-11-11 16:44:25 -08003197void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3198 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003199 mData = data;
3200 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003201 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003202}
3203
3204void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003205 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003206 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003207 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003208 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003209}
3210
Steven Moreland61ff8492019-09-26 16:05:45 -07003211} // namespace android