blob: 72c1a1a820c0f85d3b1d6f8b38fda043537d74aa [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
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000054
55// A lot of code in this file uses definitions from the
56// Linux kernel header for Binder <linux/android/binder.h>
57// which is included indirectly via "binder_module.h".
58// Non-Linux OSes do not have that header, so libbinder should be
59// built for those targets without kernel binder support, i.e.,
60// without BINDER_WITH_KERNEL_IPC. For this reason, all code in this
61// file that depends on kernel binder, including the header itself,
62// is conditional on BINDER_WITH_KERNEL_IPC.
63#ifdef BINDER_WITH_KERNEL_IPC
64#include <linux/sched.h>
Steven Moreland6ba5a252021-05-04 22:49:00 +000065#include "binder_module.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000066#else // BINDER_WITH_KERNEL_IPC
67// Needed by {read,write}Pointer
68typedef uintptr_t binder_uintptr_t;
69#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000072// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080073#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000074// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
76// ---------------------------------------------------------------------------
77
Nick Kralevichb6b14232015-04-02 09:36:02 -070078// This macro should never be used at runtime, as a too large value
79// of s could cause an integer overflow. Instead, you should always
80// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000081#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070082
83static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070084 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070085 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070086 }
87 return PAD_SIZE_UNSAFE(s);
88}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070089
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070090// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060091#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070093namespace android {
94
Steven Moreland7b102262019-08-01 15:48:43 -070095// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000096#ifdef __LP64__
97static_assert(sizeof(Parcel) == 120);
98#else
99static_assert(sizeof(Parcel) == 60);
100#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700101
Jeff Sharkey8994c182020-09-11 12:07:10 -0600102static std::atomic<size_t> gParcelGlobalAllocCount;
103static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800104
Andrei Homescu1519b982022-06-09 02:04:44 +0000105// Maximum number of file descriptors per Parcel.
106constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700107
Jeff Brown13b16042014-11-11 16:44:25 -0800108// Maximum size of a blob to transfer in-place.
109static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
110
111enum {
112 BLOB_INPLACE = 0,
113 BLOB_ASHMEM_IMMUTABLE = 1,
114 BLOB_ASHMEM_MUTABLE = 2,
115};
116
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000117#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700118static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
119 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700120 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 case BINDER_TYPE_BINDER:
122 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000123 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800124 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 }
126 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700127 case BINDER_TYPE_HANDLE: {
128 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700129 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
131 b->incStrong(who);
132 }
133 return;
134 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 return;
137 }
138 }
139
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700140 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700141}
142
Steven Morelandc673f1f2021-10-07 18:23:35 -0700143static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
144 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700145 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700146 case BINDER_TYPE_BINDER:
147 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000148 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800149 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 }
151 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152 case BINDER_TYPE_HANDLE: {
153 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700154 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
156 b->decStrong(who);
157 }
158 return;
159 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800161 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800162 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700163 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 return;
165 }
166 }
167
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700168 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000170#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700171
Frederick Mayle69a0c992022-05-26 20:38:39 +0000172static int toRawFd(const std::variant<base::unique_fd, base::borrowed_fd>& v) {
173 return std::visit([](const auto& fd) { return fd.get(); }, v);
174}
175
Frederick Mayled3c595a2022-05-09 23:02:54 +0000176Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
177 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
178}
179
Steven Moreland34b48cb2020-12-01 22:45:38 +0000180status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700181{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700182 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700183 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000184 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185}
186
Steven Morelanda86a3562019-08-01 23:28:34 +0000187status_t Parcel::finishUnflattenBinder(
188 const sp<IBinder>& binder, sp<IBinder>* out) const
189{
190 int32_t stability;
191 status_t status = readInt32(&stability);
192 if (status != OK) return status;
193
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000194 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
195 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000196 if (status != OK) return status;
197
198 *out = binder;
199 return OK;
200}
201
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000202#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000203static constexpr inline int schedPolicyMask(int policy, int priority) {
204 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
205}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000206#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000207
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500208status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
209 BBinder* local = nullptr;
210 if (binder) local = binder->localBinder();
211 if (local) local->setParceled();
212
Frederick Mayled3c595a2022-05-09 23:02:54 +0000213 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000214 if (binder) {
215 status_t status = writeInt32(1); // non-null
216 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700217 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000218 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000219 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
220 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000221 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700222 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000223 if (status != OK) return status;
224 } else {
225 status_t status = writeInt32(0); // null
226 if (status != OK) return status;
227 }
228 return finishFlattenBinder(binder);
229 }
230
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000231#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700232 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700233
Steven Morelandbf1915b2020-07-16 22:43:02 +0000234 int schedBits = 0;
235 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
236 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700237 }
238
Yi Kong91635562018-06-07 14:38:36 -0700239 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700240 if (!local) {
241 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700242 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000243 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000244 } else {
245 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700246 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000247 return INVALID_OPERATION;
248 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700249 }
Steven Moreland99157622021-09-13 16:27:34 -0700250 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700251 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800252 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000253 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700254 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800255 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000257 int policy = local->getMinSchedulerPolicy();
258 int priority = local->getMinSchedulerPriority();
259
260 if (policy != 0 || priority != 0) {
261 // override value, since it is set explicitly
262 schedBits = schedPolicyMask(policy, priority);
263 }
Steven Moreland49c27532022-03-01 10:21:07 +0000264 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800265 if (local->isRequestingSid()) {
266 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
267 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000268 if (local->isInheritRt()) {
269 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
270 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700271 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800272 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
273 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700274 }
275 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700276 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000277 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800278 obj.binder = 0;
279 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700280 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700281
Steven Morelandbf1915b2020-07-16 22:43:02 +0000282 obj.flags |= schedBits;
283
Steven Moreland34b48cb2020-12-01 22:45:38 +0000284 status_t status = writeObject(obj, false);
285 if (status != OK) return status;
286
287 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000288#else // BINDER_WITH_KERNEL_IPC
289 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
290 return INVALID_OPERATION;
291#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700292}
293
Steven Morelanda86a3562019-08-01 23:28:34 +0000294status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700295{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000296 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700297 int32_t isPresent;
298 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000299 if (status != OK) return status;
300
301 sp<IBinder> binder;
302
Steven Moreland5623d1a2021-09-10 15:45:34 -0700303 if (isPresent & 1) {
304 uint64_t addr;
305 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000306 if (status_t status =
307 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
308 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000309 status != OK)
310 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000311 if (status_t status =
312 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
313 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700314 status != OK)
315 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000316 }
317
318 return finishUnflattenBinder(binder, out);
319 }
320
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000321#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000322 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700325 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000326 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000327 sp<IBinder> binder =
328 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000329 return finishUnflattenBinder(binder, out);
330 }
331 case BINDER_TYPE_HANDLE: {
332 sp<IBinder> binder =
333 ProcessState::self()->getStrongProxyForHandle(flat->handle);
334 return finishUnflattenBinder(binder, out);
335 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700336 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337 }
338 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000339#else // BINDER_WITH_KERNEL_IPC
340 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
341 return INVALID_OPERATION;
342#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343}
344
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345// ---------------------------------------------------------------------------
346
347Parcel::Parcel()
348{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800349 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700350 initState();
351}
352
353Parcel::~Parcel()
354{
355 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800356 LOG_ALLOC("Parcel %p: destroyed", this);
357}
358
359size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600360 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800361}
362
363size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600364 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700365}
366
367const uint8_t* Parcel::data() const
368{
369 return mData;
370}
371
372size_t Parcel::dataSize() const
373{
374 return (mDataSize > mDataPos ? mDataSize : mDataPos);
375}
376
377size_t Parcel::dataAvail() const
378{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700379 size_t result = dataSize() - dataPosition();
380 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700381 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700382 }
383 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700384}
385
386size_t Parcel::dataPosition() const
387{
388 return mDataPos;
389}
390
391size_t Parcel::dataCapacity() const
392{
393 return mDataCapacity;
394}
395
396status_t Parcel::setDataSize(size_t size)
397{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700398 if (size > INT32_MAX) {
399 // don't accept size_t values which may have come from an
400 // inadvertent conversion from a negative int.
401 return BAD_VALUE;
402 }
403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700404 status_t err;
405 err = continueWrite(size);
406 if (err == NO_ERROR) {
407 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700408 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 }
410 return err;
411}
412
413void Parcel::setDataPosition(size_t pos) const
414{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700415 if (pos > INT32_MAX) {
416 // don't accept size_t values which may have come from an
417 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700418 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700419 }
420
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700421 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000422 if (const auto* kernelFields = maybeKernelFields()) {
423 kernelFields->mNextObjectHint = 0;
424 kernelFields->mObjectsSorted = false;
425 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700426}
427
428status_t Parcel::setDataCapacity(size_t size)
429{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700430 if (size > INT32_MAX) {
431 // don't accept size_t values which may have come from an
432 // inadvertent conversion from a negative int.
433 return BAD_VALUE;
434 }
435
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700436 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 return NO_ERROR;
438}
439
440status_t Parcel::setData(const uint8_t* buffer, size_t len)
441{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700442 if (len > INT32_MAX) {
443 // don't accept size_t values which may have come from an
444 // inadvertent conversion from a negative int.
445 return BAD_VALUE;
446 }
447
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 status_t err = restartWrite(len);
449 if (err == NO_ERROR) {
450 memcpy(const_cast<uint8_t*>(data()), buffer, len);
451 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000452 if (auto* kernelFields = maybeKernelFields()) {
453 kernelFields->mFdsKnown = false;
454 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700455 }
456 return err;
457}
458
Frederick Mayled3c595a2022-05-09 23:02:54 +0000459status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
460 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700461 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
462 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000463 return BAD_TYPE;
464 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000465 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
466 ALOGE("Cannot append Parcels from different sessions");
467 return BAD_TYPE;
468 }
Steven Moreland67753c32021-04-02 18:45:19 +0000469
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000471 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473
474 if (len == 0) {
475 return NO_ERROR;
476 }
477
Nick Kralevichb6b14232015-04-02 09:36:02 -0700478 if (len > INT32_MAX) {
479 // don't accept size_t values which may have come from an
480 // inadvertent conversion from a negative int.
481 return BAD_VALUE;
482 }
483
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 // range checks against the source parcel size
485 if ((offset > parcel->mDataSize)
486 || (len > parcel->mDataSize)
487 || (offset + len > parcel->mDataSize)) {
488 return BAD_VALUE;
489 }
490
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700491 if ((mDataSize+len) > mDataCapacity) {
492 // grow data
493 err = growData(len);
494 if (err != NO_ERROR) {
495 return err;
496 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497 }
498
499 // append data
500 memcpy(mData + mDataPos, data + offset, len);
501 mDataPos += len;
502 mDataSize += len;
503
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400504 err = NO_ERROR;
505
Frederick Mayled3c595a2022-05-09 23:02:54 +0000506 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000507#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000508 auto* otherKernelFields = parcel->maybeKernelFields();
509 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
510
511 const binder_size_t* objects = otherKernelFields->mObjects;
512 size_t size = otherKernelFields->mObjectsSize;
513 // Count objects in range
514 int firstIndex = -1, lastIndex = -2;
515 for (int i = 0; i < (int)size; i++) {
516 size_t off = objects[i];
517 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
518 if (firstIndex == -1) {
519 firstIndex = i;
520 }
521 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700522 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700523 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000524 int numObjects = lastIndex - firstIndex + 1;
525 if (numObjects > 0) {
526 const sp<ProcessState> proc(ProcessState::self());
527 // grow objects
528 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
529 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
530 return NO_MEMORY; // overflow
531 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
532 return NO_MEMORY; // overflow
533 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
534 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
535 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
536 newSize * sizeof(binder_size_t));
537 if (objects == (binder_size_t*)nullptr) {
538 return NO_MEMORY;
539 }
540 kernelFields->mObjects = objects;
541 kernelFields->mObjectsCapacity = newSize;
542 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700543
Frederick Mayled3c595a2022-05-09 23:02:54 +0000544 // append and acquire objects
545 int idx = kernelFields->mObjectsSize;
546 for (int i = firstIndex; i <= lastIndex; i++) {
547 size_t off = objects[i] - offset + startPos;
548 kernelFields->mObjects[idx++] = off;
549 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700550
Frederick Mayled3c595a2022-05-09 23:02:54 +0000551 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
552 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700553
Frederick Mayled3c595a2022-05-09 23:02:54 +0000554 if (flat->hdr.type == BINDER_TYPE_FD) {
555 // If this is a file descriptor, we need to dup it so the
556 // new Parcel now owns its own fd, and can declare that we
557 // officially know we have fds.
558 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
559 flat->cookie = 1;
560 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
561 if (!mAllowFds) {
562 err = FDS_NOT_ALLOWED;
563 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400564 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700565 }
566 }
Andrei Homescu88012462022-07-07 04:30:05 +0000567#else
568 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
569 return INVALID_OPERATION;
570#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000571 } else {
572 auto* rpcFields = maybeRpcFields();
573 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
574 auto* otherRpcFields = parcel->maybeRpcFields();
575 if (otherRpcFields == nullptr) {
576 return BAD_TYPE;
577 }
578 if (rpcFields->mSession != otherRpcFields->mSession) {
579 return BAD_TYPE;
580 }
581
582 const size_t savedDataPos = mDataPos;
583 base::ScopeGuard scopeGuard = [&]() { mDataPos = savedDataPos; };
584
585 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
586 if (otherRpcFields->mFds != nullptr) {
587 if (rpcFields->mFds == nullptr) {
588 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
589 }
590 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
591 }
592 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
593 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
594 if (offset <= objPos && objPos < offset + len) {
595 size_t newDataPos = objPos - offset + startPos;
596 rpcFields->mObjectPositions.push_back(newDataPos);
597
598 mDataPos = newDataPos;
599 int32_t objectType;
600 if (status_t status = readInt32(&objectType); status != OK) {
601 return status;
602 }
603 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
604 continue;
605 }
606
607 if (!mAllowFds) {
608 return FDS_NOT_ALLOWED;
609 }
610
611 // Read FD, duplicate, and add to list.
612 int32_t fdIndex;
613 if (status_t status = readInt32(&fdIndex); status != OK) {
614 return status;
615 }
616 const auto& oldFd = otherRpcFields->mFds->at(fdIndex);
617 // To match kernel binder behavior, we always dup, even if the
618 // FD was unowned in the source parcel.
619 rpcFields->mFds->emplace_back(
620 base::unique_fd(fcntl(toRawFd(oldFd), F_DUPFD_CLOEXEC, 0)));
621 // Fixup the index in the data.
622 mDataPos = newDataPos + 4;
623 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
624 return status;
625 }
626 }
627 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700628 }
629
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400630 return err;
631}
632
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700633int Parcel::compareData(const Parcel& other) {
634 size_t size = dataSize();
635 if (size != other.dataSize()) {
636 return size < other.dataSize() ? -1 : 1;
637 }
638 return memcmp(data(), other.data(), size);
639}
640
Bernardo Rufino897b1652021-10-08 10:30:20 +0100641status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
642 size_t len, int* result) const {
643 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
644 // Don't accept size_t values which may have come from an inadvertent conversion from a
645 // negative int.
646 return BAD_VALUE;
647 }
648 size_t thisLimit;
649 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
650 return BAD_VALUE;
651 }
652 size_t otherLimit;
653 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
654 return BAD_VALUE;
655 }
656 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
657 return NO_ERROR;
658}
659
Jeff Brown13b16042014-11-11 16:44:25 -0800660bool Parcel::allowFds() const
661{
662 return mAllowFds;
663}
664
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700665bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400666{
667 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700668 if (!allowFds) {
669 mAllowFds = false;
670 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400671 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700672}
673
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700674void Parcel::restoreAllowFds(bool lastValue)
675{
676 mAllowFds = lastValue;
677}
678
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700679bool Parcel::hasFileDescriptors() const
680{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000681 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000682 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000683 }
684 auto* kernelFields = maybeKernelFields();
685 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700686 scanForFds();
687 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000688 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700689}
690
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000691std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
692 std::vector<sp<IBinder>> ret;
693
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000694#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000695 const auto* kernelFields = maybeKernelFields();
696 if (kernelFields == nullptr) {
697 return ret;
698 }
699
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000700 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000701 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
702 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000703 const flat_binder_object* flat =
704 reinterpret_cast<const flat_binder_object*>(mData + offset);
705 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
706
707 setDataPosition(offset);
708
709 sp<IBinder> binder = readStrongBinder();
710 if (binder != nullptr) ret.push_back(binder);
711 }
712
713 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000714#endif // BINDER_WITH_KERNEL_IPC
715
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000716 return ret;
717}
718
719std::vector<int> Parcel::debugReadAllFileDescriptors() const {
720 std::vector<int> ret;
721
Frederick Mayle69a0c992022-05-26 20:38:39 +0000722 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000723#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000724 size_t initPosition = dataPosition();
725 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
726 binder_size_t offset = kernelFields->mObjects[i];
727 const flat_binder_object* flat =
728 reinterpret_cast<const flat_binder_object*>(mData + offset);
729 if (flat->hdr.type != BINDER_TYPE_FD) continue;
730
731 setDataPosition(offset);
732
733 int fd = readFileDescriptor();
734 LOG_ALWAYS_FATAL_IF(fd == -1);
735 ret.push_back(fd);
736 }
737 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000738#else
739 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
740#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000741 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
742 for (const auto& fd : *rpcFields->mFds) {
743 ret.push_back(toRawFd(fd));
744 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000745 }
746
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000747 return ret;
748}
749
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100750status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100751 if (len > INT32_MAX || offset > INT32_MAX) {
752 // Don't accept size_t values which may have come from an inadvertent conversion from a
753 // negative int.
754 return BAD_VALUE;
755 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100756 size_t limit;
757 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100758 return BAD_VALUE;
759 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100760 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000761 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000762#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000763 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
764 size_t pos = kernelFields->mObjects[i];
765 if (pos < offset) continue;
766 if (pos + sizeof(flat_binder_object) > offset + len) {
767 if (kernelFields->mObjectsSorted) {
768 break;
769 } else {
770 continue;
771 }
772 }
773 const flat_binder_object* flat =
774 reinterpret_cast<const flat_binder_object*>(mData + pos);
775 if (flat->hdr.type == BINDER_TYPE_FD) {
776 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000777 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000778 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100779 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000780#else
781 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
782 return INVALID_OPERATION;
783#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000784 } else if (const auto* rpcFields = maybeRpcFields()) {
785 for (uint32_t pos : rpcFields->mObjectPositions) {
786 if (offset <= pos && pos < limit) {
787 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
788 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
789 *result = true;
790 break;
791 }
792 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100793 }
794 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100795 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100796}
797
Steven Morelandf183fdd2020-10-27 00:12:12 +0000798void Parcel::markSensitive() const
799{
800 mDeallocZero = true;
801}
802
Steven Moreland5553ac42020-11-11 02:14:45 +0000803void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000804 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
805
Steven Moreland5553ac42020-11-11 02:14:45 +0000806 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700807 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000808 }
809}
810
Steven Morelandc9939062021-05-05 17:57:41 +0000811void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000812 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
813 "format must be set before data is written OR on IPC data");
814
Frederick Mayled3c595a2022-05-09 23:02:54 +0000815 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000816}
817
818bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000819 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000820}
821
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000822void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000823 auto* kernelFields = maybeKernelFields();
824 if (kernelFields == nullptr) {
825 return;
826 }
827
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000828 // Only update the request headers once. We only want to point
829 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 if (!kernelFields->mRequestHeaderPresent) {
831 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
832 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000833 }
834}
835
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000836#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandb6c7e222021-02-18 19:20:14 +0000837#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700838constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700839#elif defined(__ANDROID_RECOVERY__)
840constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700841#else
842constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
843#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000844#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700845
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700846// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700847status_t Parcel::writeInterfaceToken(const String16& interface)
848{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000849 return writeInterfaceToken(interface.string(), interface.size());
850}
851
852status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000853 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000854#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000855 const IPCThreadState* threadState = IPCThreadState::self();
856 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
857 updateWorkSourceRequestHeaderPosition();
858 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
859 : IPCThreadState::kUnsetWorkSource);
860 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000861#else // BINDER_WITH_KERNEL_IPC
862 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
863 return INVALID_OPERATION;
864#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000865 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700867 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000868 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700869}
870
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000871bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
872{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000873 auto* kernelFields = maybeKernelFields();
874 if (kernelFields == nullptr) {
875 return false;
876 }
877 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000878 return false;
879 }
880
881 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000882 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000883 status_t err = writeInt32(uid);
884 setDataPosition(initialPosition);
885 return err == NO_ERROR;
886}
887
Steven Morelandf1b1e492019-05-06 15:05:13 -0700888uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000889{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000890 auto* kernelFields = maybeKernelFields();
891 if (kernelFields == nullptr) {
892 return false;
893 }
894 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000895 return IPCThreadState::kUnsetWorkSource;
896 }
897
898 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000899 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000900 uid_t uid = readInt32();
901 setDataPosition(initialPosition);
902 return uid;
903}
904
Mathias Agopian83c04462009-05-22 19:00:22 -0700905bool Parcel::checkInterface(IBinder* binder) const
906{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700907 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700908}
909
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700910bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700911 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700912{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700913 return enforceInterface(interface.string(), interface.size(), threadState);
914}
915
916bool Parcel::enforceInterface(const char16_t* interface,
917 size_t len,
918 IPCThreadState* threadState) const
919{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000920 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000921#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000922 // StrictModePolicy.
923 int32_t strictPolicy = readInt32();
924 if (threadState == nullptr) {
925 threadState = IPCThreadState::self();
926 }
927 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
928 // For one-way calls, the callee is running entirely
929 // disconnected from the caller, so disable StrictMode entirely.
930 // Not only does disk/network usage not impact the caller, but
931 // there's no way to communicate back violations anyway.
932 threadState->setStrictModePolicy(0);
933 } else {
934 threadState->setStrictModePolicy(strictPolicy);
935 }
936 // WorkSource.
937 updateWorkSourceRequestHeaderPosition();
938 int32_t workSource = readInt32();
939 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
940 // vendor header
941 int32_t header = readInt32();
942 if (header != kHeader) {
943 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
944 header);
945 return false;
946 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000947#else // BINDER_WITH_KERNEL_IPC
948 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
949 (void)threadState;
950 return false;
951#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700952 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000953
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100954 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700955 size_t parcel_interface_len;
956 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
957 if (len == parcel_interface_len &&
958 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700959 return true;
960 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700961 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700962 String8(interface, len).string(),
963 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964 return false;
965 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700966}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700967
Jooyung Hand23f9502021-12-23 14:39:57 +0900968binder::Status Parcel::enforceNoDataAvail() const {
969 const auto n = dataAvail();
970 if (n == 0) {
971 return binder::Status::ok();
972 }
973 return binder::Status::
974 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
975 String8::format("Parcel data not fully consumed, unread size: %zu",
976 n));
977}
978
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979size_t Parcel::objectsCount() const
980{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000981 if (const auto* kernelFields = maybeKernelFields()) {
982 return kernelFields->mObjectsSize;
983 }
984 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700985}
986
987status_t Parcel::errorCheck() const
988{
989 return mError;
990}
991
992void Parcel::setError(status_t err)
993{
994 mError = err;
995}
996
997status_t Parcel::finishWrite(size_t len)
998{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700999 if (len > INT32_MAX) {
1000 // don't accept size_t values which may have come from an
1001 // inadvertent conversion from a negative int.
1002 return BAD_VALUE;
1003 }
1004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001005 //printf("Finish write of %d\n", len);
1006 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001007 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008 if (mDataPos > mDataSize) {
1009 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001010 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001011 }
1012 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1013 return NO_ERROR;
1014}
1015
1016status_t Parcel::writeUnpadded(const void* data, size_t len)
1017{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001018 if (len > INT32_MAX) {
1019 // don't accept size_t values which may have come from an
1020 // inadvertent conversion from a negative int.
1021 return BAD_VALUE;
1022 }
1023
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001024 size_t end = mDataPos + len;
1025 if (end < mDataPos) {
1026 // integer overflow
1027 return BAD_VALUE;
1028 }
1029
1030 if (end <= mDataCapacity) {
1031restart_write:
1032 memcpy(mData+mDataPos, data, len);
1033 return finishWrite(len);
1034 }
1035
1036 status_t err = growData(len);
1037 if (err == NO_ERROR) goto restart_write;
1038 return err;
1039}
1040
1041status_t Parcel::write(const void* data, size_t len)
1042{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001043 if (len > INT32_MAX) {
1044 // don't accept size_t values which may have come from an
1045 // inadvertent conversion from a negative int.
1046 return BAD_VALUE;
1047 }
1048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049 void* const d = writeInplace(len);
1050 if (d) {
1051 memcpy(d, data, len);
1052 return NO_ERROR;
1053 }
1054 return mError;
1055}
1056
1057void* Parcel::writeInplace(size_t len)
1058{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001059 if (len > INT32_MAX) {
1060 // don't accept size_t values which may have come from an
1061 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001062 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001063 }
1064
1065 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001066
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001067 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001069 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001070 }
1071
1072 if ((mDataPos+padded) <= mDataCapacity) {
1073restart_write:
1074 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1075 uint8_t* const data = mData+mDataPos;
1076
1077 // Need to pad at end?
1078 if (padded != len) {
1079#if BYTE_ORDER == BIG_ENDIAN
1080 static const uint32_t mask[4] = {
1081 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1082 };
1083#endif
1084#if BYTE_ORDER == LITTLE_ENDIAN
1085 static const uint32_t mask[4] = {
1086 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1087 };
1088#endif
1089 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1090 // *reinterpret_cast<void**>(data+padded-4));
1091 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1092 }
1093
1094 finishWrite(padded);
1095 return data;
1096 }
1097
1098 status_t err = growData(padded);
1099 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001100 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101}
1102
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001103status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1104 const uint8_t* strData = (uint8_t*)str.data();
1105 const size_t strLen= str.length();
1106 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001107 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001108 return BAD_VALUE;
1109 }
1110
1111 status_t err = writeInt32(utf16Len);
1112 if (err) {
1113 return err;
1114 }
1115
1116 // Allocate enough bytes to hold our converted string and its terminating NULL.
1117 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1118 if (!dst) {
1119 return NO_MEMORY;
1120 }
1121
Sergio Girof4607432016-07-21 14:46:35 +01001122 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001123
1124 return NO_ERROR;
1125}
1126
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001127
Andy Hung49198cf2020-11-18 11:02:39 -08001128status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1129status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001130
Andy Hung49198cf2020-11-18 11:02:39 -08001131status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1132status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001133
Andy Hung49198cf2020-11-18 11:02:39 -08001134status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1135status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1136status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1137status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1138status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1139status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1140status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1141status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1142status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1143status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1144status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1145status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1146status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1147status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1148status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1149status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1150status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1151status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1152status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1153status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1154status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1155status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1156status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1157status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1158status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1159status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1160status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001161
Andy Hung49198cf2020-11-18 11:02:39 -08001162status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001163status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001164 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001165status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001166 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001167status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001168 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001169status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001170 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1171status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001172
Andy Hung49198cf2020-11-18 11:02:39 -08001173status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1174status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1175status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1176
1177status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1178status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1179status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1180
1181status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1182
1183status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1184status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1185
1186status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1187status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1188
1189status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1190status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1191status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1192status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1193status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1194status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1195status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1196status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1197status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1198status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1199status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1200status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1201status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1202status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1203status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1204status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1205status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1206status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1207status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1208status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1209status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1210status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1211status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1212status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1213status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1214status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1215status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1216
1217status_t Parcel::readString16Vector(
1218 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1219status_t Parcel::readString16Vector(
1220 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1221status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1222status_t Parcel::readUtf8VectorFromUtf16Vector(
1223 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1224status_t Parcel::readUtf8VectorFromUtf16Vector(
1225 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1226status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1227
1228status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1229status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1230status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1231
1232status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1233status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1234status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1235
1236status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238status_t Parcel::writeInt32(int32_t val)
1239{
Andreas Huber84a6d042009-08-17 13:33:27 -07001240 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001241}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001242
1243status_t Parcel::writeUint32(uint32_t val)
1244{
1245 return writeAligned(val);
1246}
1247
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001248status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001249 if (len > INT32_MAX) {
1250 // don't accept size_t values which may have come from an
1251 // inadvertent conversion from a negative int.
1252 return BAD_VALUE;
1253 }
1254
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001255 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001256 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001257 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001258 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001259 if (ret == NO_ERROR) {
1260 ret = write(val, len * sizeof(*val));
1261 }
1262 return ret;
1263}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001264status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001265 if (len > INT32_MAX) {
1266 // don't accept size_t values which may have come from an
1267 // inadvertent conversion from a negative int.
1268 return BAD_VALUE;
1269 }
1270
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001271 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001272 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001273 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001274 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001275 if (ret == NO_ERROR) {
1276 ret = write(val, len * sizeof(*val));
1277 }
1278 return ret;
1279}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001280
Casey Dahlind6848f52015-10-15 15:44:59 -07001281status_t Parcel::writeBool(bool val)
1282{
1283 return writeInt32(int32_t(val));
1284}
1285
1286status_t Parcel::writeChar(char16_t val)
1287{
1288 return writeInt32(int32_t(val));
1289}
1290
1291status_t Parcel::writeByte(int8_t val)
1292{
1293 return writeInt32(int32_t(val));
1294}
1295
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001296status_t Parcel::writeInt64(int64_t val)
1297{
Andreas Huber84a6d042009-08-17 13:33:27 -07001298 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001299}
1300
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001301status_t Parcel::writeUint64(uint64_t val)
1302{
1303 return writeAligned(val);
1304}
1305
Serban Constantinescuf683e012013-11-05 16:53:55 +00001306status_t Parcel::writePointer(uintptr_t val)
1307{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001308 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001309}
1310
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001311status_t Parcel::writeFloat(float val)
1312{
Andreas Huber84a6d042009-08-17 13:33:27 -07001313 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001314}
1315
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001316#if defined(__mips__) && defined(__mips_hard_float)
1317
1318status_t Parcel::writeDouble(double val)
1319{
1320 union {
1321 double d;
1322 unsigned long long ll;
1323 } u;
1324 u.d = val;
1325 return writeAligned(u.ll);
1326}
1327
1328#else
1329
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001330status_t Parcel::writeDouble(double val)
1331{
Andreas Huber84a6d042009-08-17 13:33:27 -07001332 return writeAligned(val);
1333}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001335#endif
1336
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001337status_t Parcel::writeCString(const char* str)
1338{
1339 return write(str, strlen(str)+1);
1340}
1341
1342status_t Parcel::writeString8(const String8& str)
1343{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001344 return writeString8(str.string(), str.size());
1345}
1346
1347status_t Parcel::writeString8(const char* str, size_t len)
1348{
1349 if (str == nullptr) return writeInt32(-1);
1350
Jeff Sharkey18220902020-11-05 08:36:20 -07001351 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001352 status_t err = writeInt32(len);
1353 if (err == NO_ERROR) {
1354 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1355 if (data) {
1356 memcpy(data, str, len);
1357 *reinterpret_cast<char*>(data+len) = 0;
1358 return NO_ERROR;
1359 }
1360 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 }
1362 return err;
1363}
1364
1365status_t Parcel::writeString16(const String16& str)
1366{
1367 return writeString16(str.string(), str.size());
1368}
1369
1370status_t Parcel::writeString16(const char16_t* str, size_t len)
1371{
Yi Kong91635562018-06-07 14:38:36 -07001372 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001373
Jeff Sharkey18220902020-11-05 08:36:20 -07001374 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001375 status_t err = writeInt32(len);
1376 if (err == NO_ERROR) {
1377 len *= sizeof(char16_t);
1378 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1379 if (data) {
1380 memcpy(data, str, len);
1381 *reinterpret_cast<char16_t*>(data+len) = 0;
1382 return NO_ERROR;
1383 }
1384 err = mError;
1385 }
1386 return err;
1387}
1388
1389status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1390{
Steven Morelanda86a3562019-08-01 23:28:34 +00001391 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392}
1393
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001394
Casey Dahlinb9872622015-11-25 15:09:45 -08001395status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1396 if (!parcelable) {
1397 return writeInt32(0);
1398 }
1399
1400 return writeParcelable(*parcelable);
1401}
1402
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001403status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001404{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001405 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001406 return BAD_TYPE;
1407
1408 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001409 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001410 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001412 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001413 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001415 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1416 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417
1418 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001419 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 return err;
1421 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001422 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001423 return err;
1424}
1425
Frederick Mayle69a0c992022-05-26 20:38:39 +00001426status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1427 if (auto* rpcFields = maybeRpcFields()) {
1428 std::variant<base::unique_fd, base::borrowed_fd> fdVariant;
1429 if (takeOwnership) {
1430 fdVariant = base::unique_fd(fd);
1431 } else {
1432 fdVariant = base::borrowed_fd(fd);
1433 }
1434 if (!mAllowFds) {
1435 return FDS_NOT_ALLOWED;
1436 }
1437 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1438 case RpcSession::FileDescriptorTransportMode::NONE: {
1439 return FDS_NOT_ALLOWED;
1440 }
1441 case RpcSession::FileDescriptorTransportMode::UNIX: {
1442 if (rpcFields->mFds == nullptr) {
1443 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1444 }
1445 size_t dataPos = mDataPos;
1446 if (dataPos > UINT32_MAX) {
1447 return NO_MEMORY;
1448 }
1449 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1450 return err;
1451 }
1452 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1453 return err;
1454 }
1455 rpcFields->mObjectPositions.push_back(dataPos);
1456 rpcFields->mFds->push_back(std::move(fdVariant));
1457 return OK;
1458 }
1459 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001460 }
1461
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001462#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001463 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001464 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001466 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001467 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001468 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001470#else // BINDER_WITH_KERNEL_IPC
1471 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1472 (void)fd;
1473 (void)takeOwnership;
1474 return INVALID_OPERATION;
1475#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001476}
1477
1478status_t Parcel::writeDupFileDescriptor(int fd)
1479{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001480 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001481 if (dupFd < 0) {
1482 return -errno;
1483 }
1484 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001485 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001486 close(dupFd);
1487 }
1488 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001489}
1490
Dianne Hackborn1941a402016-08-29 12:30:43 -07001491status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1492{
1493 writeInt32(0);
1494 return writeFileDescriptor(fd, takeOwnership);
1495}
1496
Ryo Hashimotobf551892018-05-31 16:58:35 +09001497status_t Parcel::writeDupParcelFileDescriptor(int fd)
1498{
1499 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1500 if (dupFd < 0) {
1501 return -errno;
1502 }
1503 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1504 if (err != OK) {
1505 close(dupFd);
1506 }
1507 return err;
1508}
1509
Christopher Wiley2cf19952016-04-11 11:09:37 -07001510status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001511 return writeDupFileDescriptor(fd.get());
1512}
1513
Jeff Brown13b16042014-11-11 16:44:25 -08001514status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001515{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001516 if (len > INT32_MAX) {
1517 // don't accept size_t values which may have come from an
1518 // inadvertent conversion from a negative int.
1519 return BAD_VALUE;
1520 }
1521
Jeff Brown13b16042014-11-11 16:44:25 -08001522 status_t status;
1523 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001524 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001525 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001526 if (status) return status;
1527
1528 void* ptr = writeInplace(len);
1529 if (!ptr) return NO_MEMORY;
1530
Jeff Brown13b16042014-11-11 16:44:25 -08001531 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001532 return NO_ERROR;
1533 }
1534
Steve Block6807e592011-10-20 11:56:00 +01001535 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001536 int fd = ashmem_create_region("Parcel Blob", len);
1537 if (fd < 0) return NO_MEMORY;
1538
1539 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1540 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001541 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001542 } else {
Yi Kong91635562018-06-07 14:38:36 -07001543 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001544 if (ptr == MAP_FAILED) {
1545 status = -errno;
1546 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001547 if (!mutableCopy) {
1548 result = ashmem_set_prot_region(fd, PROT_READ);
1549 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001550 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001551 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001552 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001553 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001554 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001555 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001556 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001557 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001558 return NO_ERROR;
1559 }
1560 }
1561 }
1562 }
1563 ::munmap(ptr, len);
1564 }
1565 ::close(fd);
1566 return status;
1567}
1568
Jeff Brown13b16042014-11-11 16:44:25 -08001569status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1570{
1571 // Must match up with what's done in writeBlob.
1572 if (!mAllowFds) return FDS_NOT_ALLOWED;
1573 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1574 if (status) return status;
1575 return writeDupFileDescriptor(fd);
1576}
1577
Mathias Agopiane1424282013-07-29 21:24:40 -07001578status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001579{
1580 status_t err;
1581
1582 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001583 const size_t len = val.getFlattenedSize();
1584 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001585
Andrei Homescu1519b982022-06-09 02:04:44 +00001586 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001587 // don't accept size_t values which may have come from an
1588 // inadvertent conversion from a negative int.
1589 return BAD_VALUE;
1590 }
1591
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001592 err = this->writeInt32(len);
1593 if (err) return err;
1594
1595 err = this->writeInt32(fd_count);
1596 if (err) return err;
1597
1598 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001599 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001600 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001601 return BAD_VALUE;
1602
Yi Kong91635562018-06-07 14:38:36 -07001603 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001604 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001605 fds = new (std::nothrow) int[fd_count];
1606 if (fds == nullptr) {
1607 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1608 return BAD_VALUE;
1609 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001610 }
1611
1612 err = val.flatten(buf, len, fds, fd_count);
1613 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1614 err = this->writeDupFileDescriptor( fds[i] );
1615 }
1616
1617 if (fd_count) {
1618 delete [] fds;
1619 }
1620
1621 return err;
1622}
1623
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001624status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1625{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001626 auto* kernelFields = maybeKernelFields();
1627 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1628
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001629#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001630 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001631 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001632 if (enoughData && enoughObjects) {
1633restart_write:
1634 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001635
Christopher Tate98e67d32015-06-03 18:44:15 -07001636 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001637 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001638 if (!mAllowFds) {
1639 // fail before modifying our object index
1640 return FDS_NOT_ALLOWED;
1641 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001642 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001643 }
1644
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001645 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001646 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001647 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001648 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001649 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001650 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001651
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652 return finishWrite(sizeof(flat_binder_object));
1653 }
1654
1655 if (!enoughData) {
1656 const status_t err = growData(sizeof(val));
1657 if (err != NO_ERROR) return err;
1658 }
1659 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001660 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1661 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1662 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001663 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001664 binder_size_t* objects =
1665 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001666 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001667 kernelFields->mObjects = objects;
1668 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001669 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001672#else // BINDER_WITH_KERNEL_IPC
1673 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1674 (void)val;
1675 (void)nullMetaData;
1676 return INVALID_OPERATION;
1677#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001678}
1679
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001680status_t Parcel::writeNoException()
1681{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001682 binder::Status status;
1683 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001684}
1685
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001686status_t Parcel::validateReadData(size_t upperBound) const
1687{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001688 const auto* kernelFields = maybeKernelFields();
1689 if (kernelFields == nullptr) {
1690 // Can't validate RPC Parcel reads because the location of binder
1691 // objects is unknown.
1692 return OK;
1693 }
1694
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001695#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001696 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001697 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1698 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001699 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001700 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1701 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001702 // For some reason the current read position is greater than the next object
1703 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001704 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001705 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001706 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001707 // Requested info overlaps with an object
1708 ALOGE("Attempt to read from protected data in Parcel %p", this);
1709 return PERMISSION_DENIED;
1710 }
1711 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001712 } while (nextObject < kernelFields->mObjectsSize &&
1713 upperBound > kernelFields->mObjects[nextObject]);
1714 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001715 }
1716 return NO_ERROR;
1717 }
1718 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001719 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001720 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001721 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001722 prevObj--;
1723 if(*prevObj > *currObj) {
1724 goto data_unsorted;
1725 }
1726 currObj--;
1727 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001728 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001729 goto data_sorted;
1730
1731data_unsorted:
1732 // Insertion Sort mObjects
1733 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1734 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001735 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1736 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001737 binder_size_t temp = *iter0;
1738 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001739 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001740 *(iter1 + 1) = *iter1;
1741 iter1--;
1742 }
1743 *(iter1 + 1) = temp;
1744 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001745 kernelFields->mNextObjectHint = 0;
1746 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001747 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001748#else // BINDER_WITH_KERNEL_IPC
1749 (void)upperBound;
1750 return NO_ERROR;
1751#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001752}
1753
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001754status_t Parcel::read(void* outData, size_t len) const
1755{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001756 if (len > INT32_MAX) {
1757 // don't accept size_t values which may have come from an
1758 // inadvertent conversion from a negative int.
1759 return BAD_VALUE;
1760 }
1761
1762 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1763 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001764 const auto* kernelFields = maybeKernelFields();
1765 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001766 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001767 if(err != NO_ERROR) {
1768 // Still increment the data position by the expected length
1769 mDataPos += pad_size(len);
1770 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1771 return err;
1772 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001773 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001774 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001775 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001776 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777 return NO_ERROR;
1778 }
1779 return NOT_ENOUGH_DATA;
1780}
1781
1782const void* Parcel::readInplace(size_t len) const
1783{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001784 if (len > INT32_MAX) {
1785 // don't accept size_t values which may have come from an
1786 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001787 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001788 }
1789
1790 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1791 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001792 const auto* kernelFields = maybeKernelFields();
1793 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001794 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001795 if(err != NO_ERROR) {
1796 // Still increment the data position by the expected length
1797 mDataPos += pad_size(len);
1798 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001799 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001800 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001801 }
1802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001804 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001805 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001806 return data;
1807 }
Yi Kong91635562018-06-07 14:38:36 -07001808 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809}
1810
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001811status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1812 if (status_t status = readInt32(size); status != OK) return status;
1813 if (*size < 0) return OK; // may be null, client to handle
1814
1815 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1816
1817 // approximation, can't know max element size (e.g. if it makes heap
1818 // allocations)
1819 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1820 int32_t allocationSize;
1821 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1822
1823 // High limit of 1MB since something this big could never be returned. Could
1824 // probably scope this down, but might impact very specific usecases.
1825 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1826
1827 if (allocationSize >= kMaxAllocationSize) {
1828 return NO_MEMORY;
1829 }
1830
1831 return OK;
1832}
1833
Andreas Huber84a6d042009-08-17 13:33:27 -07001834template<class T>
1835status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001836 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001837 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001838
1839 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001840 const auto* kernelFields = maybeKernelFields();
1841 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001842 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001843 if(err != NO_ERROR) {
1844 // Still increment the data position by the expected length
1845 mDataPos += sizeof(T);
1846 return err;
1847 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001848 }
1849
Andrei Homescue33238e2022-03-29 22:50:00 +00001850 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001851 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852 return NO_ERROR;
1853 } else {
1854 return NOT_ENOUGH_DATA;
1855 }
1856}
1857
Andreas Huber84a6d042009-08-17 13:33:27 -07001858template<class T>
1859T Parcel::readAligned() const {
1860 T result;
1861 if (readAligned(&result) != NO_ERROR) {
1862 result = 0;
1863 }
1864
1865 return result;
1866}
1867
1868template<class T>
1869status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001870 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001871 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001872
1873 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1874restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001875 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001876 return finishWrite(sizeof(val));
1877 }
1878
1879 status_t err = growData(sizeof(val));
1880 if (err == NO_ERROR) goto restart_write;
1881 return err;
1882}
1883
1884status_t Parcel::readInt32(int32_t *pArg) const
1885{
1886 return readAligned(pArg);
1887}
1888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001889int32_t Parcel::readInt32() const
1890{
Andreas Huber84a6d042009-08-17 13:33:27 -07001891 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001892}
1893
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001894status_t Parcel::readUint32(uint32_t *pArg) const
1895{
1896 return readAligned(pArg);
1897}
1898
1899uint32_t Parcel::readUint32() const
1900{
1901 return readAligned<uint32_t>();
1902}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001903
1904status_t Parcel::readInt64(int64_t *pArg) const
1905{
Andreas Huber84a6d042009-08-17 13:33:27 -07001906 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001907}
1908
1909
1910int64_t Parcel::readInt64() const
1911{
Andreas Huber84a6d042009-08-17 13:33:27 -07001912 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001913}
1914
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001915status_t Parcel::readUint64(uint64_t *pArg) const
1916{
1917 return readAligned(pArg);
1918}
1919
1920uint64_t Parcel::readUint64() const
1921{
1922 return readAligned<uint64_t>();
1923}
1924
Serban Constantinescuf683e012013-11-05 16:53:55 +00001925status_t Parcel::readPointer(uintptr_t *pArg) const
1926{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001927 status_t ret;
1928 binder_uintptr_t ptr;
1929 ret = readAligned(&ptr);
1930 if (!ret)
1931 *pArg = ptr;
1932 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001933}
1934
1935uintptr_t Parcel::readPointer() const
1936{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001937 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001938}
1939
1940
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001941status_t Parcel::readFloat(float *pArg) const
1942{
Andreas Huber84a6d042009-08-17 13:33:27 -07001943 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001944}
1945
1946
1947float Parcel::readFloat() const
1948{
Andreas Huber84a6d042009-08-17 13:33:27 -07001949 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001950}
1951
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001952#if defined(__mips__) && defined(__mips_hard_float)
1953
1954status_t Parcel::readDouble(double *pArg) const
1955{
1956 union {
1957 double d;
1958 unsigned long long ll;
1959 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001960 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001961 status_t status;
1962 status = readAligned(&u.ll);
1963 *pArg = u.d;
1964 return status;
1965}
1966
1967double Parcel::readDouble() const
1968{
1969 union {
1970 double d;
1971 unsigned long long ll;
1972 } u;
1973 u.ll = readAligned<unsigned long long>();
1974 return u.d;
1975}
1976
1977#else
1978
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979status_t Parcel::readDouble(double *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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001984double Parcel::readDouble() const
1985{
Andreas Huber84a6d042009-08-17 13:33:27 -07001986 return readAligned<double>();
1987}
1988
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001989#endif
1990
Casey Dahlind6848f52015-10-15 15:44:59 -07001991status_t Parcel::readBool(bool *pArg) const
1992{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001993 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001994 status_t ret = readInt32(&tmp);
1995 *pArg = (tmp != 0);
1996 return ret;
1997}
1998
1999bool Parcel::readBool() const
2000{
2001 return readInt32() != 0;
2002}
2003
2004status_t Parcel::readChar(char16_t *pArg) const
2005{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002006 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002007 status_t ret = readInt32(&tmp);
2008 *pArg = char16_t(tmp);
2009 return ret;
2010}
2011
2012char16_t Parcel::readChar() const
2013{
2014 return char16_t(readInt32());
2015}
2016
2017status_t Parcel::readByte(int8_t *pArg) const
2018{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002019 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002020 status_t ret = readInt32(&tmp);
2021 *pArg = int8_t(tmp);
2022 return ret;
2023}
2024
2025int8_t Parcel::readByte() const
2026{
2027 return int8_t(readInt32());
2028}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002030status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2031 size_t utf16Size = 0;
2032 const char16_t* src = readString16Inplace(&utf16Size);
2033 if (!src) {
2034 return UNEXPECTED_NULL;
2035 }
2036
2037 // Save ourselves the trouble, we're done.
2038 if (utf16Size == 0u) {
2039 str->clear();
2040 return NO_ERROR;
2041 }
2042
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002043 // Allow for closing '\0'
2044 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2045 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002046 return BAD_VALUE;
2047 }
2048 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002049 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002050 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002051 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2052 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002053 return NO_ERROR;
2054}
2055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056const char* Parcel::readCString() const
2057{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002058 if (mDataPos < mDataSize) {
2059 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002060 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2061 // is the string's trailing NUL within the parcel's valid bounds?
2062 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2063 if (eos) {
2064 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002065 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 return str;
2068 }
2069 }
Yi Kong91635562018-06-07 14:38:36 -07002070 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071}
2072
2073String8 Parcel::readString8() const
2074{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002075 size_t len;
2076 const char* str = readString8Inplace(&len);
2077 if (str) return String8(str, len);
2078 ALOGE("Reading a NULL string not supported here.");
2079 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002080}
2081
2082status_t Parcel::readString8(String8* pArg) const
2083{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002084 size_t len;
2085 const char* str = readString8Inplace(&len);
2086 if (str) {
2087 pArg->setTo(str, len);
2088 return 0;
2089 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002090 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002091 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002092 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002093}
2094
2095const char* Parcel::readString8Inplace(size_t* outLen) const
2096{
2097 int32_t size = readInt32();
2098 // watch for potential int overflow from size+1
2099 if (size >= 0 && size < INT32_MAX) {
2100 *outLen = size;
2101 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002102 if (str != nullptr) {
2103 if (str[size] == '\0') {
2104 return str;
2105 }
2106 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002107 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002108 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002109 *outLen = 0;
2110 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111}
2112
2113String16 Parcel::readString16() const
2114{
2115 size_t len;
2116 const char16_t* str = readString16Inplace(&len);
2117 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002118 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002119 return String16();
2120}
2121
Casey Dahlinb9872622015-11-25 15:09:45 -08002122
Casey Dahlin451ff582015-10-19 18:12:18 -07002123status_t Parcel::readString16(String16* pArg) const
2124{
2125 size_t len;
2126 const char16_t* str = readString16Inplace(&len);
2127 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002128 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002129 return 0;
2130 } else {
2131 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002132 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002133 }
2134}
2135
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2137{
2138 int32_t size = readInt32();
2139 // watch for potential int overflow from size+1
2140 if (size >= 0 && size < INT32_MAX) {
2141 *outLen = size;
2142 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002143 if (str != nullptr) {
2144 if (str[size] == u'\0') {
2145 return str;
2146 }
2147 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 }
2149 }
2150 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002151 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152}
2153
Casey Dahlinf0c13772015-10-27 18:33:56 -07002154status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2155{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002156 status_t status = readNullableStrongBinder(val);
2157 if (status == OK && !val->get()) {
Steven Morelanddce98242022-03-11 00:26:10 +00002158 ALOGW("Expecting binder but got null!");
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002159 status = UNEXPECTED_NULL;
2160 }
2161 return status;
2162}
2163
2164status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2165{
Steven Morelanda86a3562019-08-01 23:28:34 +00002166 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002167}
2168
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002169sp<IBinder> Parcel::readStrongBinder() const
2170{
2171 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002172 // Note that a lot of code in Android reads binders by hand with this
2173 // method, and that code has historically been ok with getting nullptr
2174 // back (while ignoring error codes).
2175 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 return val;
2177}
2178
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002179int32_t Parcel::readExceptionCode() const
2180{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002181 binder::Status status;
2182 status.readFromParcel(*this);
2183 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002184}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002185
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002186native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002187{
2188 int numFds, numInts;
2189 status_t err;
2190 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002191 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002192 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002193 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002194
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002195 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002196 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002197 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002198 }
2199
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002200 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002201 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002202 if (h->data[i] < 0) {
2203 for (int j = 0; j < i; j++) {
2204 close(h->data[j]);
2205 }
2206 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002207 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002208 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002209 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002210 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002211 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002212 native_handle_close(h);
2213 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002214 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002215 }
2216 return h;
2217}
2218
Frederick Mayle69a0c992022-05-26 20:38:39 +00002219int Parcel::readFileDescriptor() const {
2220 if (const auto* rpcFields = maybeRpcFields()) {
2221 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2222 rpcFields->mObjectPositions.end(), mDataPos)) {
2223 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in the "
2224 "object list",
2225 this, mDataPos);
2226 return BAD_TYPE;
2227 }
2228
2229 int32_t objectType = readInt32();
2230 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2231 return BAD_TYPE;
2232 }
2233
2234 int32_t fdIndex = readInt32();
2235 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2236 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2237 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2238 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2239 return BAD_VALUE;
2240 }
2241 return toRawFd(rpcFields->mFds->at(fdIndex));
2242 }
2243
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002244#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002246
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002247 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002248 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002251 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002252#else // BINDER_WITH_KERNEL_IPC
2253 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2254 return INVALID_OPERATION;
2255#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256}
2257
Frederick Mayle69a0c992022-05-26 20:38:39 +00002258int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002259 int32_t hasComm = readInt32();
2260 int fd = readFileDescriptor();
2261 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002262 // detach (owned by the binder driver)
2263 int comm = readFileDescriptor();
2264
2265 // warning: this must be kept in sync with:
2266 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2267 enum ParcelFileDescriptorStatus {
2268 DETACHED = 2,
2269 };
2270
2271#if BYTE_ORDER == BIG_ENDIAN
2272 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2273#endif
2274#if BYTE_ORDER == LITTLE_ENDIAN
2275 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2276#endif
2277
2278 ssize_t written = TEMP_FAILURE_RETRY(
2279 ::write(comm, &message, sizeof(message)));
2280
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002281 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002282 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2283 written, strerror(errno));
2284 return BAD_TYPE;
2285 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002286 }
2287 return fd;
2288}
2289
Christopher Wiley2cf19952016-04-11 11:09:37 -07002290status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002291{
2292 int got = readFileDescriptor();
2293
2294 if (got == BAD_TYPE) {
2295 return BAD_TYPE;
2296 }
2297
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002298 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002299
2300 if (val->get() < 0) {
2301 return BAD_VALUE;
2302 }
2303
2304 return OK;
2305}
2306
Ryo Hashimotobf551892018-05-31 16:58:35 +09002307status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2308{
2309 int got = readParcelFileDescriptor();
2310
2311 if (got == BAD_TYPE) {
2312 return BAD_TYPE;
2313 }
2314
2315 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2316
2317 if (val->get() < 0) {
2318 return BAD_VALUE;
2319 }
2320
2321 return OK;
2322}
Casey Dahlin06673e32015-11-23 13:24:23 -08002323
Jeff Brown5707dbf2011-09-23 21:17:56 -07002324status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2325{
Jeff Brown13b16042014-11-11 16:44:25 -08002326 int32_t blobType;
2327 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002328 if (status) return status;
2329
Jeff Brown13b16042014-11-11 16:44:25 -08002330 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002331 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002332 const void* ptr = readInplace(len);
2333 if (!ptr) return BAD_VALUE;
2334
Jeff Brown13b16042014-11-11 16:44:25 -08002335 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002336 return NO_ERROR;
2337 }
2338
Steve Block6807e592011-10-20 11:56:00 +01002339 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002340 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002341 int fd = readFileDescriptor();
2342 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2343
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002344 if (!ashmem_valid(fd)) {
2345 ALOGE("invalid fd");
2346 return BAD_VALUE;
2347 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002348 int size = ashmem_get_size_region(fd);
2349 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002350 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002351 return BAD_VALUE;
2352 }
Yi Kong91635562018-06-07 14:38:36 -07002353 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002354 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002355 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002356
Jeff Brown13b16042014-11-11 16:44:25 -08002357 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002358 return NO_ERROR;
2359}
2360
Mathias Agopiane1424282013-07-29 21:24:40 -07002361status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002362{
2363 // size
2364 const size_t len = this->readInt32();
2365 const size_t fd_count = this->readInt32();
2366
Andrei Homescu1519b982022-06-09 02:04:44 +00002367 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002368 // don't accept size_t values which may have come from an
2369 // inadvertent conversion from a negative int.
2370 return BAD_VALUE;
2371 }
2372
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002373 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002374 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002375 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002376 return BAD_VALUE;
2377
Yi Kong91635562018-06-07 14:38:36 -07002378 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002379 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002380 fds = new (std::nothrow) int[fd_count];
2381 if (fds == nullptr) {
2382 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2383 return BAD_VALUE;
2384 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002385 }
2386
2387 status_t err = NO_ERROR;
2388 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002389 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002390 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002391 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002392 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 -07002393 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2394 // Close all the file descriptors that were dup-ed.
2395 for (size_t j=0; j<i ;j++) {
2396 close(fds[j]);
2397 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002398 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002399 }
2400
2401 if (err == NO_ERROR) {
2402 err = val.unflatten(buf, len, fds, fd_count);
2403 }
2404
2405 if (fd_count) {
2406 delete [] fds;
2407 }
2408
2409 return err;
2410}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002411
2412#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2414{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002415 const auto* kernelFields = maybeKernelFields();
2416 if (kernelFields == nullptr) {
2417 return nullptr;
2418 }
2419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002420 const size_t DPOS = mDataPos;
2421 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2422 const flat_binder_object* obj
2423 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2424 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002425 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002426 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 // the object list, so we don't want to check for it when
2428 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002429 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 return obj;
2431 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002432
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002434 binder_size_t* const OBJS = kernelFields->mObjects;
2435 const size_t N = kernelFields->mObjectsSize;
2436 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002439 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 // Start at the current hint position, looking for an object at
2443 // the current data position.
2444 if (opos < N) {
2445 while (opos < (N-1) && OBJS[opos] < DPOS) {
2446 opos++;
2447 }
2448 } else {
2449 opos = N-1;
2450 }
2451 if (OBJS[opos] == DPOS) {
2452 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002453 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002454 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002455 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002456 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002457 return obj;
2458 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002459
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 // Look backwards for it...
2461 while (opos > 0 && OBJS[opos] > DPOS) {
2462 opos--;
2463 }
2464 if (OBJS[opos] == DPOS) {
2465 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002466 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002467 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002468 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002469 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002470 return obj;
2471 }
2472 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002473 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002474 this, DPOS);
2475 }
Yi Kong91635562018-06-07 14:38:36 -07002476 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002478#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479
Frederick Mayled3c595a2022-05-09 23:02:54 +00002480void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002481 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002482#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002483 size_t i = kernelFields->mObjectsSize;
2484 if (i > 0) {
2485 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002487 while (i > 0) {
2488 i--;
2489 const flat_binder_object* flat =
2490 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2491 if (flat->hdr.type == BINDER_TYPE_FD) {
2492 // ALOGI("Closing fd: %ld", flat->handle);
2493 close(flat->handle);
2494 }
2495 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002496#else // BINDER_WITH_KERNEL_IPC
2497 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2498#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002499 } else if (auto* rpcFields = maybeRpcFields()) {
2500 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 }
2502}
2503
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002504uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002506 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002507}
2508
2509size_t Parcel::ipcDataSize() const
2510{
2511 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2512}
2513
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002514uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002516 if (const auto* kernelFields = maybeKernelFields()) {
2517 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2518 }
2519 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520}
2521
2522size_t Parcel::ipcObjectsCount() const
2523{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002524 if (const auto* kernelFields = maybeKernelFields()) {
2525 return kernelFields->mObjectsSize;
2526 }
2527 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528}
2529
Frederick Mayled3c595a2022-05-09 23:02:54 +00002530void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2531 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002532 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2533 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2534
Steven Morelandceed9bb2020-12-17 01:01:06 +00002535 freeData();
2536
Frederick Mayled3c595a2022-05-09 23:02:54 +00002537 auto* kernelFields = maybeKernelFields();
2538 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540 mData = const_cast<uint8_t*>(data);
2541 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002542 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2543 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002545
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002546#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002547 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002548 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2549 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002550 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002551 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002552 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002553 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002554 break;
2555 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002556 const flat_binder_object* flat
2557 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2558 uint32_t type = flat->hdr.type;
2559 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2560 type == BINDER_TYPE_FD)) {
2561 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2562 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002563 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002564 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002565 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002566 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2567 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002568
2569 // WARNING: callers of ipcSetDataReference need to make sure they
2570 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002571 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002572 break;
2573 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002574 minOffset = offset + sizeof(flat_binder_object);
2575 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002577#else // BINDER_WITH_KERNEL_IPC
2578 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2579 "Non-zero objects count passed to Parcel with kernel driver disabled");
2580#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581}
2582
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002583status_t Parcel::rpcSetDataReference(
2584 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2585 const uint32_t* objectTable, size_t objectTableSize,
2586 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds,
2587 release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002588 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2589 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2590
2591 LOG_ALWAYS_FATAL_IF(session == nullptr);
2592
2593 freeData();
2594 markForRpc(session);
2595
Frederick Mayle69a0c992022-05-26 20:38:39 +00002596 auto* rpcFields = maybeRpcFields();
2597 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2598
Frederick Mayled3c595a2022-05-09 23:02:54 +00002599 mData = const_cast<uint8_t*>(data);
2600 mDataSize = mDataCapacity = dataSize;
2601 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002602
2603 if (objectTableSize != ancillaryFds.size()) {
2604 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2605 freeData(); // don't leak mData
2606 return BAD_VALUE;
2607 }
2608
2609 rpcFields->mObjectPositions.reserve(objectTableSize);
2610 for (size_t i = 0; i < objectTableSize; i++) {
2611 rpcFields->mObjectPositions.push_back(objectTable[i]);
2612 }
2613 if (!ancillaryFds.empty()) {
2614 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002615 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002616 }
2617
2618 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002619}
2620
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002621void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622{
2623 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002624
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 if (errorCheck() != NO_ERROR) {
2626 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002627 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 } else if (dataSize() > 0) {
2629 const uint8_t* DATA = data();
2630 to << indent << HexDump(DATA, dataSize()) << dedent;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002631#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002632 if (const auto* kernelFields = maybeKernelFields()) {
2633 const binder_size_t* OBJS = kernelFields->mObjects;
2634 const size_t N = objectsCount();
2635 for (size_t i = 0; i < N; i++) {
2636 const flat_binder_object* flat =
2637 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
2638 to << endl
2639 << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2640 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2641 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002643#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 } else {
2645 to << "NULL";
2646 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002647
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002648 to << ")";
2649}
2650
2651void Parcel::releaseObjects()
2652{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002653 auto* kernelFields = maybeKernelFields();
2654 if (kernelFields == nullptr) {
2655 return;
2656 }
2657
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002658#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002659 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002660 if (i == 0) {
2661 return;
2662 }
2663 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002664 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002665 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002666 while (i > 0) {
2667 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002668 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002669 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002671#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672}
2673
2674void Parcel::acquireObjects()
2675{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002676 auto* kernelFields = maybeKernelFields();
2677 if (kernelFields == nullptr) {
2678 return;
2679 }
2680
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002681#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002682 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002683 if (i == 0) {
2684 return;
2685 }
2686 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002688 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002689 while (i > 0) {
2690 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002691 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002692 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002693 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002694#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695}
2696
2697void Parcel::freeData()
2698{
2699 freeDataNoInit();
2700 initState();
2701}
2702
2703void Parcel::freeDataNoInit()
2704{
2705 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002706 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002707 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002708 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002709 // Close FDs before freeing, otherwise they will leak for kernel binder.
2710 closeFileDescriptors();
2711 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002712 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002714 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002715 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002716 if (mData) {
2717 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002718 gParcelGlobalAllocSize -= mDataCapacity;
2719 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002720 if (mDeallocZero) {
2721 zeroMemory(mData, mDataSize);
2722 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002723 free(mData);
2724 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002725 auto* kernelFields = maybeKernelFields();
2726 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002727 }
2728}
2729
2730status_t Parcel::growData(size_t len)
2731{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002732 if (len > INT32_MAX) {
2733 // don't accept size_t values which may have come from an
2734 // inadvertent conversion from a negative int.
2735 return BAD_VALUE;
2736 }
2737
Martijn Coenen93fe5182020-01-22 10:46:25 +01002738 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2739 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002740 size_t newSize = ((mDataSize+len)*3)/2;
2741 return (newSize <= mDataSize)
2742 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002743 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002744}
2745
Steven Morelandf183fdd2020-10-27 00:12:12 +00002746static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2747 if (!zero) {
2748 return (uint8_t*)realloc(data, newCapacity);
2749 }
2750 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2751 if (!newData) {
2752 return nullptr;
2753 }
2754
2755 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2756 zeroMemory(data, oldCapacity);
2757 free(data);
2758 return newData;
2759}
2760
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761status_t Parcel::restartWrite(size_t desired)
2762{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002763 if (desired > INT32_MAX) {
2764 // don't accept size_t values which may have come from an
2765 // inadvertent conversion from a negative int.
2766 return BAD_VALUE;
2767 }
2768
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002769 if (mOwner) {
2770 freeData();
2771 return continueWrite(desired);
2772 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002773
Steven Morelandf183fdd2020-10-27 00:12:12 +00002774 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002775 if (!data && desired > mDataCapacity) {
2776 mError = NO_MEMORY;
2777 return NO_MEMORY;
2778 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002779
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002781
Devin Moore4a0a55e2020-06-04 13:23:10 -07002782 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002783 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002784 if (mDataCapacity > desired) {
2785 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2786 } else {
2787 gParcelGlobalAllocSize += (desired - mDataCapacity);
2788 }
2789
Colin Cross83ec65e2015-12-08 17:15:50 -08002790 if (!mData) {
2791 gParcelGlobalAllocCount++;
2792 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 mData = data;
2794 mDataCapacity = desired;
2795 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002796
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002797 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002798 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2799 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2800
Frederick Mayled3c595a2022-05-09 23:02:54 +00002801 if (auto* kernelFields = maybeKernelFields()) {
2802 free(kernelFields->mObjects);
2803 kernelFields->mObjects = nullptr;
2804 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2805 kernelFields->mNextObjectHint = 0;
2806 kernelFields->mObjectsSorted = false;
2807 kernelFields->mHasFds = false;
2808 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002809 } else if (auto* rpcFields = maybeRpcFields()) {
2810 rpcFields->mObjectPositions.clear();
2811 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002812 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002813 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 return NO_ERROR;
2816}
2817
2818status_t Parcel::continueWrite(size_t desired)
2819{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002820 if (desired > INT32_MAX) {
2821 // don't accept size_t values which may have come from an
2822 // inadvertent conversion from a negative int.
2823 return BAD_VALUE;
2824 }
2825
Frederick Mayled3c595a2022-05-09 23:02:54 +00002826 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002827 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002828
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 // If shrinking, first adjust for any objects that appear
2830 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002831 size_t objectsSize =
2832 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2833 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002834 if (desired == 0) {
2835 objectsSize = 0;
2836 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002837 if (kernelFields) {
2838 while (objectsSize > 0) {
2839 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2840 objectsSize--;
2841 }
2842 } else {
2843 while (objectsSize > 0) {
2844 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2845 objectsSize--;
2846 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002847 }
2848 }
2849 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002850
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002851 if (mOwner) {
2852 // If the size is going to zero, just release the owner's data.
2853 if (desired == 0) {
2854 freeData();
2855 return NO_ERROR;
2856 }
2857
2858 // If there is a different owner, we need to take
2859 // posession.
2860 uint8_t* data = (uint8_t*)malloc(desired);
2861 if (!data) {
2862 mError = NO_MEMORY;
2863 return NO_MEMORY;
2864 }
Yi Kong91635562018-06-07 14:38:36 -07002865 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002866
Frederick Mayle69a0c992022-05-26 20:38:39 +00002867 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002868 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002869 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002870 free(data);
2871
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 mError = NO_MEMORY;
2873 return NO_MEMORY;
2874 }
2875
2876 // Little hack to only acquire references on objects
2877 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002878 size_t oldObjectsSize = kernelFields->mObjectsSize;
2879 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002880 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002881 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002883 if (rpcFields) {
2884 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2885 free(data);
2886 return status;
2887 }
2888 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002890 if (mData) {
2891 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2892 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002893 if (objects && kernelFields && kernelFields->mObjects) {
2894 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002895 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002896 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2897 if (kernelFields) {
2898 // TODO(b/239222407): This seems wrong. We should only free FDs when
2899 // they are in a truncated section of the parcel.
2900 closeFileDescriptors();
2901 }
2902 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002903 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002904 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002905
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002906 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002907 gParcelGlobalAllocSize += desired;
2908 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002910 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002911 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002912 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002913 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002914 if (kernelFields) {
2915 kernelFields->mObjects = objects;
2916 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2917 kernelFields->mNextObjectHint = 0;
2918 kernelFields->mObjectsSorted = false;
2919 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002920
2921 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002922 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002923#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002924 // Need to release refs on any objects we are dropping.
2925 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002926 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2927 const flat_binder_object* flat =
2928 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002929 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002930 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00002931 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002932 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002933 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002934 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002935
2936 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002937 free(kernelFields->mObjects);
2938 kernelFields->mObjects = nullptr;
2939 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002940 } else {
2941 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002942 (binder_size_t*)realloc(kernelFields->mObjects,
2943 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002944 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002945 kernelFields->mObjects = objects;
2946 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002947 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002948 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002949 kernelFields->mObjectsSize = objectsSize;
2950 kernelFields->mNextObjectHint = 0;
2951 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002952#else // BINDER_WITH_KERNEL_IPC
2953 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
2954#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002955 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002956 if (rpcFields) {
2957 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2958 return status;
2959 }
2960 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002961
2962 // We own the data, so we can just do a realloc().
2963 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002964 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002965 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002966 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2967 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002968 gParcelGlobalAllocSize += desired;
2969 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002970 mData = data;
2971 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002972 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002973 mError = NO_MEMORY;
2974 return NO_MEMORY;
2975 }
2976 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002977 if (mDataSize > desired) {
2978 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002979 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002980 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002981 if (mDataPos > desired) {
2982 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002983 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002984 }
2985 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002986
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002987 } else {
2988 // This is the first data. Easy!
2989 uint8_t* data = (uint8_t*)malloc(desired);
2990 if (!data) {
2991 mError = NO_MEMORY;
2992 return NO_MEMORY;
2993 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002994
Frederick Mayled3c595a2022-05-09 23:02:54 +00002995 if (!(mDataCapacity == 0 &&
2996 (kernelFields == nullptr ||
2997 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
2998 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
2999 kernelFields ? kernelFields->mObjects : nullptr,
3000 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003001 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003002
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003003 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003004 gParcelGlobalAllocSize += desired;
3005 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003006
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003007 mData = data;
3008 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003009 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3010 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003011 mDataCapacity = desired;
3012 }
3013
3014 return NO_ERROR;
3015}
3016
Frederick Mayle69a0c992022-05-26 20:38:39 +00003017status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3018 auto* rpcFields = maybeRpcFields();
3019 if (newObjectsSize == 0) {
3020 rpcFields->mObjectPositions.clear();
3021 if (rpcFields->mFds) {
3022 rpcFields->mFds->clear();
3023 }
3024 return OK;
3025 }
3026 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3027 uint32_t pos = rpcFields->mObjectPositions.back();
3028 rpcFields->mObjectPositions.pop_back();
3029 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3030 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3031 const auto fdIndex =
3032 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3033 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3034 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3035 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3036 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3037 return BAD_VALUE;
3038 }
3039 // In practice, this always removes the last element.
3040 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3041 }
3042 }
3043 return OK;
3044}
3045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003046void Parcel::initState()
3047{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003048 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003049 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003050 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003051 mDataSize = 0;
3052 mDataCapacity = 0;
3053 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003054 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3055 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003056 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003057 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003058 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003059 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003060}
3061
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003062void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003063 auto* kernelFields = maybeKernelFields();
3064 if (kernelFields == nullptr) {
3065 return;
3066 }
3067 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003068 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003069 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003070}
3071
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003072#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003073size_t Parcel::getBlobAshmemSize() const
3074{
Adrian Roos6bb31142015-10-22 16:46:12 -07003075 // This used to return the size of all blobs that were written to ashmem, now we're returning
3076 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003077 // TODO(b/202029388): Remove method once ABI can be changed.
3078 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003079}
3080
Adrian Rooscbf37262015-10-22 16:12:53 -07003081size_t Parcel::getOpenAshmemSize() const
3082{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003083 auto* kernelFields = maybeKernelFields();
3084 if (kernelFields == nullptr) {
3085 return 0;
3086 }
3087
Steven Morelandc673f1f2021-10-07 18:23:35 -07003088 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003089 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003090 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003091 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003092
3093 // cookie is compared against zero for historical reasons
3094 // > obj.cookie = takeOwnership ? 1 : 0;
3095 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3096 int size = ashmem_get_size_region(flat->handle);
3097 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3098 ALOGE("Overflow when computing ashmem size.");
3099 return SIZE_MAX;
3100 }
3101 }
3102 }
3103 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003104}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003105#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003106
3107// --- Parcel::Blob ---
3108
3109Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003110 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003111}
3112
3113Parcel::Blob::~Blob() {
3114 release();
3115}
3116
3117void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003118 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003119 ::munmap(mData, mSize);
3120 }
3121 clear();
3122}
3123
Jeff Brown13b16042014-11-11 16:44:25 -08003124void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3125 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003126 mData = data;
3127 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003128 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003129}
3130
3131void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003132 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003133 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003134 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003135 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003136}
3137
Steven Moreland61ff8492019-09-26 16:05:45 -07003138} // namespace android