blob: 4d1463ca09339313bcac3e2efa3c92c477955167 [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
Tomasz Wasilczyk1d1e9ff2023-10-25 16:17:54 -070020#include <endian.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080021#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080022#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080023#include <inttypes.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080024#include <pthread.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <sys/mman.h>
Andrei Homescua9cca9c2022-05-03 04:48:01 +000029#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080030#include <sys/stat.h>
31#include <sys/types.h>
32#include <unistd.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070033
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/Binder.h>
35#include <binder/BpBinder.h>
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000036#include <binder/Functional.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080037#include <binder/IPCThreadState.h>
38#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070039#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070040#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080041#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070042#include <binder/TextOutput.h>
43
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +000044#ifndef BINDER_DISABLE_BLOB
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <cutils/ashmem.h>
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +000046#endif
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>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070049
Andrei Homescu24ad36e2022-08-04 01:33:33 +000050#include "OS.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000054
55// A lot of code in this file uses definitions from the
56// Linux kernel header for Binder <linux/android/binder.h>
57// which is included indirectly via "binder_module.h".
58// Non-Linux OSes do not have that header, so libbinder should be
59// built for those targets without kernel binder support, i.e.,
60// without BINDER_WITH_KERNEL_IPC. For this reason, all code in this
61// file that depends on kernel binder, including the header itself,
62// is conditional on BINDER_WITH_KERNEL_IPC.
63#ifdef BINDER_WITH_KERNEL_IPC
64#include <linux/sched.h>
Steven Moreland6ba5a252021-05-04 22:49:00 +000065#include "binder_module.h"
Andrei Homescua9cca9c2022-05-03 04:48:01 +000066#else // BINDER_WITH_KERNEL_IPC
67// Needed by {read,write}Pointer
68typedef uintptr_t binder_uintptr_t;
69#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070070
Steven Moreland02f736f2023-06-22 23:03:57 +000071#ifdef __BIONIC__
72#include <android/fdsan.h>
73#endif
74
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000076// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080077#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000078// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079
80// ---------------------------------------------------------------------------
81
Nick Kralevichb6b14232015-04-02 09:36:02 -070082// This macro should never be used at runtime, as a too large value
83// of s could cause an integer overflow. Instead, you should always
84// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000085#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070086
87static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070088 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070089 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070090 }
91 return PAD_SIZE_UNSAFE(s);
92}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070093
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070094// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060095#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070097namespace android {
98
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000099using namespace android::binder::impl;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700100using binder::borrowed_fd;
101using binder::unique_fd;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000102
Steven Moreland7b102262019-08-01 15:48:43 -0700103// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +0000104#ifdef __LP64__
105static_assert(sizeof(Parcel) == 120);
106#else
107static_assert(sizeof(Parcel) == 60);
108#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700109
Jeff Sharkey8994c182020-09-11 12:07:10 -0600110static std::atomic<size_t> gParcelGlobalAllocCount;
111static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800112
Andrei Homescu1519b982022-06-09 02:04:44 +0000113// Maximum number of file descriptors per Parcel.
114constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700115
Jeff Brown13b16042014-11-11 16:44:25 -0800116// Maximum size of a blob to transfer in-place.
Tomasz Wasilczyk83780132023-11-29 21:39:29 -0800117[[maybe_unused]] static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
Jeff Brown13b16042014-11-11 16:44:25 -0800118
Steven Moreland02f736f2023-06-22 23:03:57 +0000119#if defined(__BIONIC__)
120static void FdTag(int fd, const void* old_addr, const void* new_addr) {
121 if (android_fdsan_exchange_owner_tag) {
122 uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
123 reinterpret_cast<uint64_t>(old_addr));
124 uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
125 reinterpret_cast<uint64_t>(new_addr));
126 android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
127 }
128}
129static void FdTagClose(int fd, const void* addr) {
130 if (android_fdsan_close_with_tag) {
131 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
132 reinterpret_cast<uint64_t>(addr));
133 android_fdsan_close_with_tag(fd, tag);
134 } else {
135 close(fd);
136 }
137}
138#else
139static void FdTag(int fd, const void* old_addr, const void* new_addr) {
140 (void)fd;
141 (void)old_addr;
142 (void)new_addr;
143}
144static void FdTagClose(int fd, const void* addr) {
145 (void)addr;
146 close(fd);
147}
148#endif
149
Jeff Brown13b16042014-11-11 16:44:25 -0800150enum {
151 BLOB_INPLACE = 0,
152 BLOB_ASHMEM_IMMUTABLE = 1,
153 BLOB_ASHMEM_MUTABLE = 2,
154};
155
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000156#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700157static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
158 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700159 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 case BINDER_TYPE_BINDER:
161 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000162 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800163 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 }
165 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
170 b->incStrong(who);
171 }
172 return;
173 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000175 if (obj.cookie != 0) { // owned
176 FdTag(obj.handle, nullptr, who);
177 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 return;
179 }
180 }
181
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700182 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183}
184
Steven Morelandc673f1f2021-10-07 18:23:35 -0700185static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
186 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700187 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188 case BINDER_TYPE_BINDER:
189 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000190 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800191 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 }
193 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 case BINDER_TYPE_HANDLE: {
195 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700196 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
198 b->decStrong(who);
199 }
200 return;
201 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000203 // note: this path is not used when mOwner, so the tag is also released
204 // in 'closeFileDescriptors'
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800205 if (obj.cookie != 0) { // owned
Steven Moreland02f736f2023-06-22 23:03:57 +0000206 FdTagClose(obj.handle, who);
Adrian Rooscbf37262015-10-22 16:12:53 -0700207 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 return;
209 }
210 }
211
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700212 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000214#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700216static int toRawFd(const std::variant<unique_fd, borrowed_fd>& v) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000217 return std::visit([](const auto& fd) { return fd.get(); }, v);
218}
219
Frederick Mayled3c595a2022-05-09 23:02:54 +0000220Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
221 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
222}
223
Steven Moreland34b48cb2020-12-01 22:45:38 +0000224status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700226 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700227 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000228 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229}
230
Steven Morelanda86a3562019-08-01 23:28:34 +0000231status_t Parcel::finishUnflattenBinder(
232 const sp<IBinder>& binder, sp<IBinder>* out) const
233{
234 int32_t stability;
235 status_t status = readInt32(&stability);
236 if (status != OK) return status;
237
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000238 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
239 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000240 if (status != OK) return status;
241
242 *out = binder;
243 return OK;
244}
245
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000246#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000247static constexpr inline int schedPolicyMask(int policy, int priority) {
248 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
249}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000250#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000251
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500252status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
253 BBinder* local = nullptr;
254 if (binder) local = binder->localBinder();
255 if (local) local->setParceled();
256
Frederick Mayled3c595a2022-05-09 23:02:54 +0000257 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000258 if (binder) {
259 status_t status = writeInt32(1); // non-null
260 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700261 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000262 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000263 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
264 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000265 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700266 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000267 if (status != OK) return status;
268 } else {
269 status_t status = writeInt32(0); // null
270 if (status != OK) return status;
271 }
272 return finishFlattenBinder(binder);
273 }
274
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000275#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277
Steven Morelandbf1915b2020-07-16 22:43:02 +0000278 int schedBits = 0;
279 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
280 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700281 }
282
Yi Kong91635562018-06-07 14:38:36 -0700283 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 if (!local) {
285 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700286 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000287 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 } else {
289 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700290 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000291 return INVALID_OPERATION;
292 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 }
Steven Moreland99157622021-09-13 16:27:34 -0700294 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700295 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800296 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000297 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800299 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000301 int policy = local->getMinSchedulerPolicy();
302 int priority = local->getMinSchedulerPriority();
303
304 if (policy != 0 || priority != 0) {
305 // override value, since it is set explicitly
306 schedBits = schedPolicyMask(policy, priority);
307 }
Steven Moreland49c27532022-03-01 10:21:07 +0000308 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800309 if (local->isRequestingSid()) {
310 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
311 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000312 if (local->isInheritRt()) {
313 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
314 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700315 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800316 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
317 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 }
319 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700320 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000321 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 obj.binder = 0;
323 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325
Steven Morelandbf1915b2020-07-16 22:43:02 +0000326 obj.flags |= schedBits;
327
Steven Moreland34b48cb2020-12-01 22:45:38 +0000328 status_t status = writeObject(obj, false);
329 if (status != OK) return status;
330
331 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000332#else // BINDER_WITH_KERNEL_IPC
333 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
334 return INVALID_OPERATION;
335#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336}
337
Steven Morelanda86a3562019-08-01 23:28:34 +0000338status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000340 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700341 int32_t isPresent;
342 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000343 if (status != OK) return status;
344
345 sp<IBinder> binder;
346
Steven Moreland5623d1a2021-09-10 15:45:34 -0700347 if (isPresent & 1) {
348 uint64_t addr;
349 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000350 if (status_t status =
351 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
352 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000353 status != OK)
354 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000355 if (status_t status =
356 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
357 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700358 status != OK)
359 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000360 }
361
362 return finishUnflattenBinder(binder, out);
363 }
364
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000365#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000366 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700369 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000370 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000371 sp<IBinder> binder =
372 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000373 return finishUnflattenBinder(binder, out);
374 }
375 case BINDER_TYPE_HANDLE: {
376 sp<IBinder> binder =
377 ProcessState::self()->getStrongProxyForHandle(flat->handle);
378 return finishUnflattenBinder(binder, out);
379 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700380 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 }
382 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000383#else // BINDER_WITH_KERNEL_IPC
384 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
385 return INVALID_OPERATION;
386#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387}
388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700389// ---------------------------------------------------------------------------
390
391Parcel::Parcel()
392{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800393 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394 initState();
395}
396
397Parcel::~Parcel()
398{
399 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800400 LOG_ALLOC("Parcel %p: destroyed", this);
401}
402
403size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600404 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800405}
406
407size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600408 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409}
410
411const uint8_t* Parcel::data() const
412{
413 return mData;
414}
415
416size_t Parcel::dataSize() const
417{
418 return (mDataSize > mDataPos ? mDataSize : mDataPos);
419}
420
Pawan Waghd886a442023-01-21 02:16:38 +0000421size_t Parcel::dataBufferSize() const {
422 return mDataSize;
423}
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425size_t Parcel::dataAvail() const
426{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700427 size_t result = dataSize() - dataPosition();
428 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700429 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700430 }
431 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432}
433
434size_t Parcel::dataPosition() const
435{
436 return mDataPos;
437}
438
439size_t Parcel::dataCapacity() const
440{
441 return mDataCapacity;
442}
443
444status_t Parcel::setDataSize(size_t size)
445{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700446 if (size > INT32_MAX) {
447 // don't accept size_t values which may have come from an
448 // inadvertent conversion from a negative int.
449 return BAD_VALUE;
450 }
451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 status_t err;
453 err = continueWrite(size);
454 if (err == NO_ERROR) {
455 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700456 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 }
458 return err;
459}
460
461void Parcel::setDataPosition(size_t pos) const
462{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700463 if (pos > INT32_MAX) {
464 // don't accept size_t values which may have come from an
465 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700466 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700467 }
468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000470 if (const auto* kernelFields = maybeKernelFields()) {
471 kernelFields->mNextObjectHint = 0;
472 kernelFields->mObjectsSorted = false;
473 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474}
475
476status_t Parcel::setDataCapacity(size_t size)
477{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700478 if (size > 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
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700484 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 return NO_ERROR;
486}
487
488status_t Parcel::setData(const uint8_t* buffer, size_t len)
489{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700490 if (len > INT32_MAX) {
491 // don't accept size_t values which may have come from an
492 // inadvertent conversion from a negative int.
493 return BAD_VALUE;
494 }
495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 status_t err = restartWrite(len);
497 if (err == NO_ERROR) {
498 memcpy(const_cast<uint8_t*>(data()), buffer, len);
499 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000500 if (auto* kernelFields = maybeKernelFields()) {
501 kernelFields->mFdsKnown = false;
502 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700503 }
504 return err;
505}
506
Frederick Mayled3c595a2022-05-09 23:02:54 +0000507status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
508 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700509 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
510 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000511 return BAD_TYPE;
512 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000513 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
514 ALOGE("Cannot append Parcels from different sessions");
515 return BAD_TYPE;
516 }
Steven Moreland67753c32021-04-02 18:45:19 +0000517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000519 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521
522 if (len == 0) {
523 return NO_ERROR;
524 }
525
Nick Kralevichb6b14232015-04-02 09:36:02 -0700526 if (len > INT32_MAX) {
527 // don't accept size_t values which may have come from an
528 // inadvertent conversion from a negative int.
529 return BAD_VALUE;
530 }
531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532 // range checks against the source parcel size
533 if ((offset > parcel->mDataSize)
534 || (len > parcel->mDataSize)
535 || (offset + len > parcel->mDataSize)) {
536 return BAD_VALUE;
537 }
538
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700539 if ((mDataSize+len) > mDataCapacity) {
540 // grow data
541 err = growData(len);
542 if (err != NO_ERROR) {
543 return err;
544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 }
546
547 // append data
548 memcpy(mData + mDataPos, data + offset, len);
549 mDataPos += len;
550 mDataSize += len;
551
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 err = NO_ERROR;
553
Frederick Mayled3c595a2022-05-09 23:02:54 +0000554 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000555#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000556 auto* otherKernelFields = parcel->maybeKernelFields();
557 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
558
559 const binder_size_t* objects = otherKernelFields->mObjects;
560 size_t size = otherKernelFields->mObjectsSize;
561 // Count objects in range
562 int firstIndex = -1, lastIndex = -2;
563 for (int i = 0; i < (int)size; i++) {
564 size_t off = objects[i];
565 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
566 if (firstIndex == -1) {
567 firstIndex = i;
568 }
569 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000572 int numObjects = lastIndex - firstIndex + 1;
573 if (numObjects > 0) {
574 const sp<ProcessState> proc(ProcessState::self());
575 // grow objects
576 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
577 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
578 return NO_MEMORY; // overflow
579 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
580 return NO_MEMORY; // overflow
581 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
582 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
583 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
584 newSize * sizeof(binder_size_t));
585 if (objects == (binder_size_t*)nullptr) {
586 return NO_MEMORY;
587 }
588 kernelFields->mObjects = objects;
589 kernelFields->mObjectsCapacity = newSize;
590 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700591
Frederick Mayled3c595a2022-05-09 23:02:54 +0000592 // append and acquire objects
593 int idx = kernelFields->mObjectsSize;
594 for (int i = firstIndex; i <= lastIndex; i++) {
595 size_t off = objects[i] - offset + startPos;
596 kernelFields->mObjects[idx++] = off;
597 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598
Frederick Mayled3c595a2022-05-09 23:02:54 +0000599 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600
Frederick Mayled3c595a2022-05-09 23:02:54 +0000601 if (flat->hdr.type == BINDER_TYPE_FD) {
602 // If this is a file descriptor, we need to dup it so the
603 // new Parcel now owns its own fd, and can declare that we
604 // officially know we have fds.
605 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
606 flat->cookie = 1;
607 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
608 if (!mAllowFds) {
609 err = FDS_NOT_ALLOWED;
610 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400611 }
Steven Moreland02f736f2023-06-22 23:03:57 +0000612
613 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 }
615 }
Andrei Homescu88012462022-07-07 04:30:05 +0000616#else
617 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
618 return INVALID_OPERATION;
619#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000620 } else {
621 auto* rpcFields = maybeRpcFields();
622 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
623 auto* otherRpcFields = parcel->maybeRpcFields();
624 if (otherRpcFields == nullptr) {
625 return BAD_TYPE;
626 }
627 if (rpcFields->mSession != otherRpcFields->mSession) {
628 return BAD_TYPE;
629 }
630
631 const size_t savedDataPos = mDataPos;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000632 auto scopeGuard = make_scope_guard([&]() { mDataPos = savedDataPos; });
Frederick Mayle69a0c992022-05-26 20:38:39 +0000633
634 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
635 if (otherRpcFields->mFds != nullptr) {
636 if (rpcFields->mFds == nullptr) {
637 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
638 }
639 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
640 }
641 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
642 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
643 if (offset <= objPos && objPos < offset + len) {
644 size_t newDataPos = objPos - offset + startPos;
645 rpcFields->mObjectPositions.push_back(newDataPos);
646
647 mDataPos = newDataPos;
648 int32_t objectType;
649 if (status_t status = readInt32(&objectType); status != OK) {
650 return status;
651 }
652 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
653 continue;
654 }
655
656 if (!mAllowFds) {
657 return FDS_NOT_ALLOWED;
658 }
659
660 // Read FD, duplicate, and add to list.
661 int32_t fdIndex;
662 if (status_t status = readInt32(&fdIndex); status != OK) {
663 return status;
664 }
Andrei Homescufc221502022-10-08 03:51:17 +0000665 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000666 // To match kernel binder behavior, we always dup, even if the
667 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000668 int newFd = -1;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000669 if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) {
Andrei Homescufc221502022-10-08 03:51:17 +0000670 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
671 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700672 rpcFields->mFds->emplace_back(unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000673 // Fixup the index in the data.
674 mDataPos = newDataPos + 4;
675 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
676 return status;
677 }
678 }
679 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 }
681
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400682 return err;
683}
684
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700685int Parcel::compareData(const Parcel& other) {
686 size_t size = dataSize();
687 if (size != other.dataSize()) {
688 return size < other.dataSize() ? -1 : 1;
689 }
690 return memcmp(data(), other.data(), size);
691}
692
Bernardo Rufino897b1652021-10-08 10:30:20 +0100693status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
694 size_t len, int* result) const {
695 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
696 // Don't accept size_t values which may have come from an inadvertent conversion from a
697 // negative int.
698 return BAD_VALUE;
699 }
700 size_t thisLimit;
701 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
702 return BAD_VALUE;
703 }
704 size_t otherLimit;
705 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
706 return BAD_VALUE;
707 }
708 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
709 return NO_ERROR;
710}
711
Jeff Brown13b16042014-11-11 16:44:25 -0800712bool Parcel::allowFds() const
713{
714 return mAllowFds;
715}
716
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700717bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400718{
719 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700720 if (!allowFds) {
721 mAllowFds = false;
722 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400723 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724}
725
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700726void Parcel::restoreAllowFds(bool lastValue)
727{
728 mAllowFds = lastValue;
729}
730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731bool Parcel::hasFileDescriptors() const
732{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000733 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000734 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000735 }
736 auto* kernelFields = maybeKernelFields();
737 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700738 scanForFds();
739 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000740 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700741}
742
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000743std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
744 std::vector<sp<IBinder>> ret;
745
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000746#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000747 const auto* kernelFields = maybeKernelFields();
748 if (kernelFields == nullptr) {
749 return ret;
750 }
751
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000752 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000753 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
754 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000755 const flat_binder_object* flat =
756 reinterpret_cast<const flat_binder_object*>(mData + offset);
757 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
758
759 setDataPosition(offset);
760
761 sp<IBinder> binder = readStrongBinder();
762 if (binder != nullptr) ret.push_back(binder);
763 }
764
765 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000766#endif // BINDER_WITH_KERNEL_IPC
767
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000768 return ret;
769}
770
771std::vector<int> Parcel::debugReadAllFileDescriptors() const {
772 std::vector<int> ret;
773
Frederick Mayle69a0c992022-05-26 20:38:39 +0000774 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000775#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000776 size_t initPosition = dataPosition();
777 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
778 binder_size_t offset = kernelFields->mObjects[i];
779 const flat_binder_object* flat =
780 reinterpret_cast<const flat_binder_object*>(mData + offset);
781 if (flat->hdr.type != BINDER_TYPE_FD) continue;
782
783 setDataPosition(offset);
784
785 int fd = readFileDescriptor();
786 LOG_ALWAYS_FATAL_IF(fd == -1);
787 ret.push_back(fd);
788 }
789 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000790#else
791 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
792#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000793 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
794 for (const auto& fd : *rpcFields->mFds) {
795 ret.push_back(toRawFd(fd));
796 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000797 }
798
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000799 return ret;
800}
801
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100802status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100803 if (len > INT32_MAX || offset > INT32_MAX) {
804 // Don't accept size_t values which may have come from an inadvertent conversion from a
805 // negative int.
806 return BAD_VALUE;
807 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100808 size_t limit;
809 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100810 return BAD_VALUE;
811 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100812 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000813 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000814#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000815 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
816 size_t pos = kernelFields->mObjects[i];
817 if (pos < offset) continue;
818 if (pos + sizeof(flat_binder_object) > offset + len) {
819 if (kernelFields->mObjectsSorted) {
820 break;
821 } else {
822 continue;
823 }
824 }
825 const flat_binder_object* flat =
826 reinterpret_cast<const flat_binder_object*>(mData + pos);
827 if (flat->hdr.type == BINDER_TYPE_FD) {
828 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000829 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100831 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000832#else
833 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
834 return INVALID_OPERATION;
835#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000836 } else if (const auto* rpcFields = maybeRpcFields()) {
837 for (uint32_t pos : rpcFields->mObjectPositions) {
838 if (offset <= pos && pos < limit) {
839 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
840 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
841 *result = true;
842 break;
843 }
844 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100845 }
846 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100847 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100848}
849
Steven Morelandf183fdd2020-10-27 00:12:12 +0000850void Parcel::markSensitive() const
851{
852 mDeallocZero = true;
853}
854
Steven Moreland5553ac42020-11-11 02:14:45 +0000855void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000856 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
857
Steven Moreland5553ac42020-11-11 02:14:45 +0000858 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700859 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000860 }
861}
862
Steven Morelandc9939062021-05-05 17:57:41 +0000863void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000864 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
865 "format must be set before data is written OR on IPC data");
866
Frederick Mayled3c595a2022-05-09 23:02:54 +0000867 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000868}
869
870bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000871 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000872}
873
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000874void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000875 auto* kernelFields = maybeKernelFields();
876 if (kernelFields == nullptr) {
877 return;
878 }
879
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000880 // Only update the request headers once. We only want to point
881 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000882 if (!kernelFields->mRequestHeaderPresent) {
883 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
884 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000885 }
886}
887
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000888#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland94e63e12023-12-05 20:29:50 +0000889
890#if defined(__ANDROID__)
891
Steven Morelandb6c7e222021-02-18 19:20:14 +0000892#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700893constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700894#elif defined(__ANDROID_RECOVERY__)
895constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700896#else
897constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
898#endif
Steven Moreland94e63e12023-12-05 20:29:50 +0000899
900#else // ANDROID not defined
901
902// If kernel binder is used in new environments, we need to make sure it's separated
903// out and has a separate header.
904constexpr int32_t kHeader = B_PACK_CHARS('U', 'N', 'K', 'N');
905#endif
906
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000907#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700908
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700909// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700910status_t Parcel::writeInterfaceToken(const String16& interface)
911{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000912 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000913}
914
915status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000916 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000917#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000918 const IPCThreadState* threadState = IPCThreadState::self();
919 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
920 updateWorkSourceRequestHeaderPosition();
921 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
922 : IPCThreadState::kUnsetWorkSource);
923 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000924#else // BINDER_WITH_KERNEL_IPC
925 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
926 return INVALID_OPERATION;
927#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000928 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000929
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700930 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000931 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700932}
933
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000934bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
935{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000936 auto* kernelFields = maybeKernelFields();
937 if (kernelFields == nullptr) {
938 return false;
939 }
940 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000941 return false;
942 }
943
944 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000945 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000946 status_t err = writeInt32(uid);
947 setDataPosition(initialPosition);
948 return err == NO_ERROR;
949}
950
Steven Morelandf1b1e492019-05-06 15:05:13 -0700951uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000952{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000953 auto* kernelFields = maybeKernelFields();
954 if (kernelFields == nullptr) {
955 return false;
956 }
957 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000958 return IPCThreadState::kUnsetWorkSource;
959 }
960
961 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000962 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000963 uid_t uid = readInt32();
964 setDataPosition(initialPosition);
965 return uid;
966}
967
Mathias Agopian83c04462009-05-22 19:00:22 -0700968bool Parcel::checkInterface(IBinder* binder) const
969{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700970 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700971}
972
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700973bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700974 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700975{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000976 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700977}
978
979bool Parcel::enforceInterface(const char16_t* interface,
980 size_t len,
981 IPCThreadState* threadState) const
982{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000983 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000984#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000985 // StrictModePolicy.
986 int32_t strictPolicy = readInt32();
987 if (threadState == nullptr) {
988 threadState = IPCThreadState::self();
989 }
990 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
991 // For one-way calls, the callee is running entirely
992 // disconnected from the caller, so disable StrictMode entirely.
993 // Not only does disk/network usage not impact the caller, but
994 // there's no way to communicate back violations anyway.
995 threadState->setStrictModePolicy(0);
996 } else {
997 threadState->setStrictModePolicy(strictPolicy);
998 }
999 // WorkSource.
1000 updateWorkSourceRequestHeaderPosition();
1001 int32_t workSource = readInt32();
1002 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
1003 // vendor header
1004 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +00001005
1006 // fuzzers skip this check, because it is for protecting the underlying ABI, but
1007 // we don't want it to reduce our coverage
1008 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +00001009 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
1010 header);
1011 return false;
1012 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001013#else // BINDER_WITH_KERNEL_IPC
1014 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1015 (void)threadState;
1016 return false;
1017#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001018 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001019
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001020 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001021 size_t parcel_interface_len;
1022 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1023 if (len == parcel_interface_len &&
1024 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001025 return true;
1026 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001027 if (mServiceFuzzing) {
1028 // ignore. Theoretically, this could cause a few false positives, because
1029 // people could assume things about getInterfaceDescriptor if they pass
1030 // this point, but it would be extremely fragile. It's more important that
1031 // we fuzz with the above things read from the Parcel.
1032 return true;
1033 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001034 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001035 String8(interface, len).c_str(),
1036 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001037 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001038 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001039 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001040}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001041
Pawan Wagh104654a2022-11-15 21:18:42 +00001042void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1043 mEnforceNoDataAvail = enforceNoDataAvail;
1044}
1045
Steven Moreland3a667492023-06-02 21:41:39 +00001046void Parcel::setServiceFuzzing() {
1047 mServiceFuzzing = true;
1048}
1049
Steven Moreland418914a2023-06-28 21:47:14 +00001050bool Parcel::isServiceFuzzing() const {
1051 return mServiceFuzzing;
1052}
1053
Jooyung Hand23f9502021-12-23 14:39:57 +09001054binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001055 if (!mEnforceNoDataAvail) {
1056 return binder::Status::ok();
1057 }
1058
Jooyung Hand23f9502021-12-23 14:39:57 +09001059 const auto n = dataAvail();
1060 if (n == 0) {
1061 return binder::Status::ok();
1062 }
1063 return binder::Status::
1064 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1065 String8::format("Parcel data not fully consumed, unread size: %zu",
1066 n));
1067}
1068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069size_t Parcel::objectsCount() const
1070{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001071 if (const auto* kernelFields = maybeKernelFields()) {
1072 return kernelFields->mObjectsSize;
1073 }
1074 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075}
1076
1077status_t Parcel::errorCheck() const
1078{
1079 return mError;
1080}
1081
1082void Parcel::setError(status_t err)
1083{
1084 mError = err;
1085}
1086
1087status_t Parcel::finishWrite(size_t len)
1088{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001089 if (len > INT32_MAX) {
1090 // don't accept size_t values which may have come from an
1091 // inadvertent conversion from a negative int.
1092 return BAD_VALUE;
1093 }
1094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001095 //printf("Finish write of %d\n", len);
1096 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001097 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098 if (mDataPos > mDataSize) {
1099 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001100 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001101 }
1102 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1103 return NO_ERROR;
1104}
1105
1106status_t Parcel::writeUnpadded(const void* data, size_t len)
1107{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001108 if (len > INT32_MAX) {
1109 // don't accept size_t values which may have come from an
1110 // inadvertent conversion from a negative int.
1111 return BAD_VALUE;
1112 }
1113
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 size_t end = mDataPos + len;
1115 if (end < mDataPos) {
1116 // integer overflow
1117 return BAD_VALUE;
1118 }
1119
1120 if (end <= mDataCapacity) {
1121restart_write:
1122 memcpy(mData+mDataPos, data, len);
1123 return finishWrite(len);
1124 }
1125
1126 status_t err = growData(len);
1127 if (err == NO_ERROR) goto restart_write;
1128 return err;
1129}
1130
1131status_t Parcel::write(const void* data, size_t len)
1132{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001133 if (len > INT32_MAX) {
1134 // don't accept size_t values which may have come from an
1135 // inadvertent conversion from a negative int.
1136 return BAD_VALUE;
1137 }
1138
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139 void* const d = writeInplace(len);
1140 if (d) {
1141 memcpy(d, data, len);
1142 return NO_ERROR;
1143 }
1144 return mError;
1145}
1146
1147void* Parcel::writeInplace(size_t len)
1148{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001149 if (len > INT32_MAX) {
1150 // don't accept size_t values which may have come from an
1151 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001152 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001153 }
1154
1155 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001156
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001157 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001158 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001159 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 }
1161
1162 if ((mDataPos+padded) <= mDataCapacity) {
1163restart_write:
1164 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1165 uint8_t* const data = mData+mDataPos;
1166
1167 // Need to pad at end?
1168 if (padded != len) {
1169#if BYTE_ORDER == BIG_ENDIAN
1170 static const uint32_t mask[4] = {
1171 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1172 };
1173#endif
1174#if BYTE_ORDER == LITTLE_ENDIAN
1175 static const uint32_t mask[4] = {
1176 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1177 };
1178#endif
1179 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1180 // *reinterpret_cast<void**>(data+padded-4));
1181 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1182 }
1183
1184 finishWrite(padded);
1185 return data;
1186 }
1187
1188 status_t err = growData(padded);
1189 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001190 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191}
1192
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001193status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1194 const uint8_t* strData = (uint8_t*)str.data();
1195 const size_t strLen= str.length();
1196 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001197 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001198 return BAD_VALUE;
1199 }
1200
1201 status_t err = writeInt32(utf16Len);
1202 if (err) {
1203 return err;
1204 }
1205
1206 // Allocate enough bytes to hold our converted string and its terminating NULL.
1207 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1208 if (!dst) {
1209 return NO_MEMORY;
1210 }
1211
Sergio Girof4607432016-07-21 14:46:35 +01001212 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001213
1214 return NO_ERROR;
1215}
1216
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001217
Andy Hung49198cf2020-11-18 11:02:39 -08001218status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1219status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001220
Andy Hung49198cf2020-11-18 11:02:39 -08001221status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1222status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001223
Andy Hung49198cf2020-11-18 11:02:39 -08001224status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1225status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1226status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1227status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1228status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1229status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1230status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1231status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1232status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1233status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1234status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1235status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1236status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1237status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1238status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1239status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1240status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1241status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1242status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1243status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1244status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1245status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1246status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1247status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1248status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1249status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1250status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001251
Andy Hung49198cf2020-11-18 11:02:39 -08001252status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001253status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001254 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001255status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001256 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001257status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001258 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001259status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001260 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1261status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001262
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001263status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1264 return writeData(val);
1265}
1266status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1267 return writeData(val);
1268}
1269status_t Parcel::writeUniqueFileDescriptorVector(
1270 const std::unique_ptr<std::vector<unique_fd>>& val) {
1271 return writeData(val);
1272}
Andy Hung49198cf2020-11-18 11:02:39 -08001273
1274status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1275status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1276status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1277
1278status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1279
1280status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1281status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1282
1283status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1284status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1285
1286status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1287status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1288status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1289status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1290status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1291status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1292status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1293status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1294status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1295status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1296status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1297status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1298status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1299status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1300status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1301status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1302status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1303status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1304status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1305status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1306status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1307status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1308status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1309status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1310status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1311status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1312status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1313
1314status_t Parcel::readString16Vector(
1315 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1316status_t Parcel::readString16Vector(
1317 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1318status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1319status_t Parcel::readUtf8VectorFromUtf16Vector(
1320 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1321status_t Parcel::readUtf8VectorFromUtf16Vector(
1322 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1323status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1324
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001325status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1326 return readData(val);
1327}
1328status_t Parcel::readUniqueFileDescriptorVector(
1329 std::unique_ptr<std::vector<unique_fd>>* val) const {
1330 return readData(val);
1331}
1332status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1333 return readData(val);
1334}
Andy Hung49198cf2020-11-18 11:02:39 -08001335
1336status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1337status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1338status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1339
1340status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001341
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001342status_t Parcel::writeInt32(int32_t val)
1343{
Andreas Huber84a6d042009-08-17 13:33:27 -07001344 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001346
1347status_t Parcel::writeUint32(uint32_t val)
1348{
1349 return writeAligned(val);
1350}
1351
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001352status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001353 if (len > INT32_MAX) {
1354 // don't accept size_t values which may have come from an
1355 // inadvertent conversion from a negative int.
1356 return BAD_VALUE;
1357 }
1358
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001359 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001360 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001361 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001362 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001363 if (ret == NO_ERROR) {
1364 ret = write(val, len * sizeof(*val));
1365 }
1366 return ret;
1367}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001368status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001369 if (len > INT32_MAX) {
1370 // don't accept size_t values which may have come from an
1371 // inadvertent conversion from a negative int.
1372 return BAD_VALUE;
1373 }
1374
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001375 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001376 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001377 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001378 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001379 if (ret == NO_ERROR) {
1380 ret = write(val, len * sizeof(*val));
1381 }
1382 return ret;
1383}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384
Casey Dahlind6848f52015-10-15 15:44:59 -07001385status_t Parcel::writeBool(bool val)
1386{
1387 return writeInt32(int32_t(val));
1388}
1389
1390status_t Parcel::writeChar(char16_t val)
1391{
1392 return writeInt32(int32_t(val));
1393}
1394
1395status_t Parcel::writeByte(int8_t val)
1396{
1397 return writeInt32(int32_t(val));
1398}
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400status_t Parcel::writeInt64(int64_t val)
1401{
Andreas Huber84a6d042009-08-17 13:33:27 -07001402 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001403}
1404
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001405status_t Parcel::writeUint64(uint64_t val)
1406{
1407 return writeAligned(val);
1408}
1409
Serban Constantinescuf683e012013-11-05 16:53:55 +00001410status_t Parcel::writePointer(uintptr_t val)
1411{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001412 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001413}
1414
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001415status_t Parcel::writeFloat(float val)
1416{
Andreas Huber84a6d042009-08-17 13:33:27 -07001417 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001418}
1419
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001420#if defined(__mips__) && defined(__mips_hard_float)
1421
1422status_t Parcel::writeDouble(double val)
1423{
1424 union {
1425 double d;
1426 unsigned long long ll;
1427 } u;
1428 u.d = val;
1429 return writeAligned(u.ll);
1430}
1431
1432#else
1433
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001434status_t Parcel::writeDouble(double val)
1435{
Andreas Huber84a6d042009-08-17 13:33:27 -07001436 return writeAligned(val);
1437}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001439#endif
1440
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001441status_t Parcel::writeCString(const char* str)
1442{
1443 return write(str, strlen(str)+1);
1444}
1445
1446status_t Parcel::writeString8(const String8& str)
1447{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001448 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001449}
1450
1451status_t Parcel::writeString8(const char* str, size_t len)
1452{
1453 if (str == nullptr) return writeInt32(-1);
1454
Jeff Sharkey18220902020-11-05 08:36:20 -07001455 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001456 status_t err = writeInt32(len);
1457 if (err == NO_ERROR) {
1458 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1459 if (data) {
1460 memcpy(data, str, len);
1461 *reinterpret_cast<char*>(data+len) = 0;
1462 return NO_ERROR;
1463 }
1464 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001465 }
1466 return err;
1467}
1468
1469status_t Parcel::writeString16(const String16& str)
1470{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001471 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472}
1473
1474status_t Parcel::writeString16(const char16_t* str, size_t len)
1475{
Yi Kong91635562018-06-07 14:38:36 -07001476 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001477
Jeff Sharkey18220902020-11-05 08:36:20 -07001478 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001479 status_t err = writeInt32(len);
1480 if (err == NO_ERROR) {
1481 len *= sizeof(char16_t);
1482 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1483 if (data) {
1484 memcpy(data, str, len);
1485 *reinterpret_cast<char16_t*>(data+len) = 0;
1486 return NO_ERROR;
1487 }
1488 err = mError;
1489 }
1490 return err;
1491}
1492
1493status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1494{
Steven Morelanda86a3562019-08-01 23:28:34 +00001495 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001496}
1497
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001498
Casey Dahlinb9872622015-11-25 15:09:45 -08001499status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1500 if (!parcelable) {
1501 return writeInt32(0);
1502 }
1503
1504 return writeParcelable(*parcelable);
1505}
1506
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001507#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001508status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001509{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001510 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001511 return BAD_TYPE;
1512
1513 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001514 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001515 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001517 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001518 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001520 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1521 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522
1523 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001524 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 return err;
1526 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001527 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001528 return err;
1529}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001530#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001531
Frederick Mayle69a0c992022-05-26 20:38:39 +00001532status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1533 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001534 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001535 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001536 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001537 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001538 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001539 }
1540 if (!mAllowFds) {
1541 return FDS_NOT_ALLOWED;
1542 }
1543 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1544 case RpcSession::FileDescriptorTransportMode::NONE: {
1545 return FDS_NOT_ALLOWED;
1546 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001547 case RpcSession::FileDescriptorTransportMode::UNIX:
1548 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001549 if (rpcFields->mFds == nullptr) {
1550 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1551 }
1552 size_t dataPos = mDataPos;
1553 if (dataPos > UINT32_MAX) {
1554 return NO_MEMORY;
1555 }
1556 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1557 return err;
1558 }
1559 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1560 return err;
1561 }
1562 rpcFields->mObjectPositions.push_back(dataPos);
1563 rpcFields->mFds->push_back(std::move(fdVariant));
1564 return OK;
1565 }
1566 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001567 }
1568
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001569#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001570 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001571 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001572 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001573 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001574 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001575 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001577#else // BINDER_WITH_KERNEL_IPC
1578 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1579 (void)fd;
1580 (void)takeOwnership;
1581 return INVALID_OPERATION;
1582#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001583}
1584
1585status_t Parcel::writeDupFileDescriptor(int fd)
1586{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001587 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001588 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001589 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001590 }
1591 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001592 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001593 close(dupFd);
1594 }
1595 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001596}
1597
Dianne Hackborn1941a402016-08-29 12:30:43 -07001598status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1599{
1600 writeInt32(0);
1601 return writeFileDescriptor(fd, takeOwnership);
1602}
1603
Ryo Hashimotobf551892018-05-31 16:58:35 +09001604status_t Parcel::writeDupParcelFileDescriptor(int fd)
1605{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001606 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001607 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001608 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001609 }
1610 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1611 if (err != OK) {
1612 close(dupFd);
1613 }
1614 return err;
1615}
1616
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001617status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001618 return writeDupFileDescriptor(fd.get());
1619}
1620
Jeff Brown13b16042014-11-11 16:44:25 -08001621status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001622{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001623#ifdef BINDER_DISABLE_BLOB
1624 (void)len;
1625 (void)mutableCopy;
1626 (void)outBlob;
1627 return INVALID_OPERATION;
1628#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001629 if (len > INT32_MAX) {
1630 // don't accept size_t values which may have come from an
1631 // inadvertent conversion from a negative int.
1632 return BAD_VALUE;
1633 }
1634
Jeff Brown13b16042014-11-11 16:44:25 -08001635 status_t status;
1636 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001637 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001638 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001639 if (status) return status;
1640
1641 void* ptr = writeInplace(len);
1642 if (!ptr) return NO_MEMORY;
1643
Jeff Brown13b16042014-11-11 16:44:25 -08001644 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001645 return NO_ERROR;
1646 }
1647
Steve Block6807e592011-10-20 11:56:00 +01001648 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001649 int fd = ashmem_create_region("Parcel Blob", len);
1650 if (fd < 0) return NO_MEMORY;
1651
1652 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1653 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001654 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001655 } else {
Yi Kong91635562018-06-07 14:38:36 -07001656 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001657 if (ptr == MAP_FAILED) {
1658 status = -errno;
1659 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001660 if (!mutableCopy) {
1661 result = ashmem_set_prot_region(fd, PROT_READ);
1662 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001663 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001664 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001665 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001666 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001667 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001668 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001669 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001670 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001671 return NO_ERROR;
1672 }
1673 }
1674 }
1675 }
1676 ::munmap(ptr, len);
1677 }
1678 ::close(fd);
1679 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001680#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001681}
1682
Jeff Brown13b16042014-11-11 16:44:25 -08001683status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1684{
1685 // Must match up with what's done in writeBlob.
1686 if (!mAllowFds) return FDS_NOT_ALLOWED;
1687 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1688 if (status) return status;
1689 return writeDupFileDescriptor(fd);
1690}
1691
Mathias Agopiane1424282013-07-29 21:24:40 -07001692status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001693{
1694 status_t err;
1695
1696 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001697 const size_t len = val.getFlattenedSize();
1698 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001699
Andrei Homescu1519b982022-06-09 02:04:44 +00001700 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001701 // don't accept size_t values which may have come from an
1702 // inadvertent conversion from a negative int.
1703 return BAD_VALUE;
1704 }
1705
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001706 err = this->writeInt32(len);
1707 if (err) return err;
1708
1709 err = this->writeInt32(fd_count);
1710 if (err) return err;
1711
1712 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001713 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001714 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001715 return BAD_VALUE;
1716
Yi Kong91635562018-06-07 14:38:36 -07001717 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001718 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001719 fds = new (std::nothrow) int[fd_count];
1720 if (fds == nullptr) {
1721 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1722 return BAD_VALUE;
1723 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001724 }
1725
1726 err = val.flatten(buf, len, fds, fd_count);
1727 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1728 err = this->writeDupFileDescriptor( fds[i] );
1729 }
1730
1731 if (fd_count) {
1732 delete [] fds;
1733 }
1734
1735 return err;
1736}
1737
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001738status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1739{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001740 auto* kernelFields = maybeKernelFields();
1741 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1742
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001743#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001745 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001746 if (enoughData && enoughObjects) {
1747restart_write:
1748 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001749
Christopher Tate98e67d32015-06-03 18:44:15 -07001750 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001751 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001752 if (!mAllowFds) {
1753 // fail before modifying our object index
1754 return FDS_NOT_ALLOWED;
1755 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001756 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001757 }
1758
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001759 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001760 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001761 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001762 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001763 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001764 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001765
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001766 return finishWrite(sizeof(flat_binder_object));
1767 }
1768
1769 if (!enoughData) {
1770 const status_t err = growData(sizeof(val));
1771 if (err != NO_ERROR) return err;
1772 }
1773 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001774 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1775 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1776 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001777 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001778 binder_size_t* objects =
1779 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001780 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001781 kernelFields->mObjects = objects;
1782 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001784
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001785 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001786#else // BINDER_WITH_KERNEL_IPC
1787 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1788 (void)val;
1789 (void)nullMetaData;
1790 return INVALID_OPERATION;
1791#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792}
1793
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001794status_t Parcel::writeNoException()
1795{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001796 binder::Status status;
1797 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001798}
1799
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001800status_t Parcel::validateReadData(size_t upperBound) const
1801{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001802 const auto* kernelFields = maybeKernelFields();
1803 if (kernelFields == nullptr) {
1804 // Can't validate RPC Parcel reads because the location of binder
1805 // objects is unknown.
1806 return OK;
1807 }
1808
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001809#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001810 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001811 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1812 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001813 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001814 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1815 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001816 // For some reason the current read position is greater than the next object
1817 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001818 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001819 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001820 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001821 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001822 if (!mServiceFuzzing) {
1823 ALOGE("Attempt to read from protected data in Parcel %p", this);
1824 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001825 return PERMISSION_DENIED;
1826 }
1827 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001828 } while (nextObject < kernelFields->mObjectsSize &&
1829 upperBound > kernelFields->mObjects[nextObject]);
1830 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001831 }
1832 return NO_ERROR;
1833 }
1834 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001835 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001836 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001837 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001838 prevObj--;
1839 if(*prevObj > *currObj) {
1840 goto data_unsorted;
1841 }
1842 currObj--;
1843 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001844 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001845 goto data_sorted;
1846
1847data_unsorted:
1848 // Insertion Sort mObjects
1849 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1850 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001851 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1852 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001853 binder_size_t temp = *iter0;
1854 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001855 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001856 *(iter1 + 1) = *iter1;
1857 iter1--;
1858 }
1859 *(iter1 + 1) = temp;
1860 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001861 kernelFields->mNextObjectHint = 0;
1862 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001863 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001864#else // BINDER_WITH_KERNEL_IPC
1865 (void)upperBound;
1866 return NO_ERROR;
1867#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001868}
1869
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870status_t Parcel::read(void* outData, size_t len) const
1871{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001872 if (len > INT32_MAX) {
1873 // don't accept size_t values which may have come from an
1874 // inadvertent conversion from a negative int.
1875 return BAD_VALUE;
1876 }
1877
1878 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1879 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001880 const auto* kernelFields = maybeKernelFields();
1881 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001882 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001883 if(err != NO_ERROR) {
1884 // Still increment the data position by the expected length
1885 mDataPos += pad_size(len);
1886 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1887 return err;
1888 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001889 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001891 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001892 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001893 return NO_ERROR;
1894 }
1895 return NOT_ENOUGH_DATA;
1896}
1897
1898const void* Parcel::readInplace(size_t len) const
1899{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001900 if (len > INT32_MAX) {
1901 // don't accept size_t values which may have come from an
1902 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001903 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001904 }
1905
1906 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1907 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001908 const auto* kernelFields = maybeKernelFields();
1909 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001910 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001911 if(err != NO_ERROR) {
1912 // Still increment the data position by the expected length
1913 mDataPos += pad_size(len);
1914 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001915 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001916 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001917 }
1918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001919 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001920 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001921 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001922 return data;
1923 }
Yi Kong91635562018-06-07 14:38:36 -07001924 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001925}
1926
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001927status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1928 if (status_t status = readInt32(size); status != OK) return status;
1929 if (*size < 0) return OK; // may be null, client to handle
1930
1931 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1932
1933 // approximation, can't know max element size (e.g. if it makes heap
1934 // allocations)
1935 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1936 int32_t allocationSize;
1937 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1938
1939 // High limit of 1MB since something this big could never be returned. Could
1940 // probably scope this down, but might impact very specific usecases.
1941 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1942
1943 if (allocationSize >= kMaxAllocationSize) {
1944 return NO_MEMORY;
1945 }
1946
1947 return OK;
1948}
1949
Andreas Huber84a6d042009-08-17 13:33:27 -07001950template<class T>
1951status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001952 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001953 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001954
1955 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001956 const auto* kernelFields = maybeKernelFields();
1957 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001958 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001959 if(err != NO_ERROR) {
1960 // Still increment the data position by the expected length
1961 mDataPos += sizeof(T);
1962 return err;
1963 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001964 }
1965
Andrei Homescue33238e2022-03-29 22:50:00 +00001966 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001967 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001968 return NO_ERROR;
1969 } else {
1970 return NOT_ENOUGH_DATA;
1971 }
1972}
1973
Andreas Huber84a6d042009-08-17 13:33:27 -07001974template<class T>
1975T Parcel::readAligned() const {
1976 T result;
1977 if (readAligned(&result) != NO_ERROR) {
1978 result = 0;
1979 }
1980
1981 return result;
1982}
1983
1984template<class T>
1985status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001986 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001987 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001988
1989 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1990restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001991 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001992 return finishWrite(sizeof(val));
1993 }
1994
1995 status_t err = growData(sizeof(val));
1996 if (err == NO_ERROR) goto restart_write;
1997 return err;
1998}
1999
2000status_t Parcel::readInt32(int32_t *pArg) const
2001{
2002 return readAligned(pArg);
2003}
2004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005int32_t Parcel::readInt32() const
2006{
Andreas Huber84a6d042009-08-17 13:33:27 -07002007 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008}
2009
Dan Stoza41a0f2f2014-12-01 10:01:10 -08002010status_t Parcel::readUint32(uint32_t *pArg) const
2011{
2012 return readAligned(pArg);
2013}
2014
2015uint32_t Parcel::readUint32() const
2016{
2017 return readAligned<uint32_t>();
2018}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019
2020status_t Parcel::readInt64(int64_t *pArg) const
2021{
Andreas Huber84a6d042009-08-17 13:33:27 -07002022 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002023}
2024
2025
2026int64_t Parcel::readInt64() const
2027{
Andreas Huber84a6d042009-08-17 13:33:27 -07002028 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002029}
2030
Ronghua Wu2d13afd2015-03-16 11:11:07 -07002031status_t Parcel::readUint64(uint64_t *pArg) const
2032{
2033 return readAligned(pArg);
2034}
2035
2036uint64_t Parcel::readUint64() const
2037{
2038 return readAligned<uint64_t>();
2039}
2040
Serban Constantinescuf683e012013-11-05 16:53:55 +00002041status_t Parcel::readPointer(uintptr_t *pArg) const
2042{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002043 status_t ret;
2044 binder_uintptr_t ptr;
2045 ret = readAligned(&ptr);
2046 if (!ret)
2047 *pArg = ptr;
2048 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002049}
2050
2051uintptr_t Parcel::readPointer() const
2052{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002053 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002054}
2055
2056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002057status_t Parcel::readFloat(float *pArg) const
2058{
Andreas Huber84a6d042009-08-17 13:33:27 -07002059 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002060}
2061
2062
2063float Parcel::readFloat() const
2064{
Andreas Huber84a6d042009-08-17 13:33:27 -07002065 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002066}
2067
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002068#if defined(__mips__) && defined(__mips_hard_float)
2069
2070status_t Parcel::readDouble(double *pArg) const
2071{
2072 union {
2073 double d;
2074 unsigned long long ll;
2075 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002076 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002077 status_t status;
2078 status = readAligned(&u.ll);
2079 *pArg = u.d;
2080 return status;
2081}
2082
2083double Parcel::readDouble() const
2084{
2085 union {
2086 double d;
2087 unsigned long long ll;
2088 } u;
2089 u.ll = readAligned<unsigned long long>();
2090 return u.d;
2091}
2092
2093#else
2094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095status_t Parcel::readDouble(double *pArg) const
2096{
Andreas Huber84a6d042009-08-17 13:33:27 -07002097 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098}
2099
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002100double Parcel::readDouble() const
2101{
Andreas Huber84a6d042009-08-17 13:33:27 -07002102 return readAligned<double>();
2103}
2104
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002105#endif
2106
Casey Dahlind6848f52015-10-15 15:44:59 -07002107status_t Parcel::readBool(bool *pArg) const
2108{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002109 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002110 status_t ret = readInt32(&tmp);
2111 *pArg = (tmp != 0);
2112 return ret;
2113}
2114
2115bool Parcel::readBool() const
2116{
2117 return readInt32() != 0;
2118}
2119
2120status_t Parcel::readChar(char16_t *pArg) const
2121{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002122 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002123 status_t ret = readInt32(&tmp);
2124 *pArg = char16_t(tmp);
2125 return ret;
2126}
2127
2128char16_t Parcel::readChar() const
2129{
2130 return char16_t(readInt32());
2131}
2132
2133status_t Parcel::readByte(int8_t *pArg) const
2134{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002135 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002136 status_t ret = readInt32(&tmp);
2137 *pArg = int8_t(tmp);
2138 return ret;
2139}
2140
2141int8_t Parcel::readByte() const
2142{
2143 return int8_t(readInt32());
2144}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002146status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2147 size_t utf16Size = 0;
2148 const char16_t* src = readString16Inplace(&utf16Size);
2149 if (!src) {
2150 return UNEXPECTED_NULL;
2151 }
2152
2153 // Save ourselves the trouble, we're done.
2154 if (utf16Size == 0u) {
2155 str->clear();
2156 return NO_ERROR;
2157 }
2158
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002159 // Allow for closing '\0'
2160 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2161 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002162 return BAD_VALUE;
2163 }
2164 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002165 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002166 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002167 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2168 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002169 return NO_ERROR;
2170}
2171
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172const char* Parcel::readCString() const
2173{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002174 if (mDataPos < mDataSize) {
2175 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2177 // is the string's trailing NUL within the parcel's valid bounds?
2178 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2179 if (eos) {
2180 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002181 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002182 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002183 return str;
2184 }
2185 }
Yi Kong91635562018-06-07 14:38:36 -07002186 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187}
2188
2189String8 Parcel::readString8() const
2190{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002191 size_t len;
2192 const char* str = readString8Inplace(&len);
2193 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002194
2195 if (!mServiceFuzzing) {
2196 ALOGE("Reading a NULL string not supported here.");
2197 }
2198
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002199 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002200}
2201
2202status_t Parcel::readString8(String8* pArg) const
2203{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002204 size_t len;
2205 const char* str = readString8Inplace(&len);
2206 if (str) {
2207 pArg->setTo(str, len);
2208 return 0;
2209 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002210 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002211 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002212 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002213}
2214
2215const char* Parcel::readString8Inplace(size_t* outLen) const
2216{
2217 int32_t size = readInt32();
2218 // watch for potential int overflow from size+1
2219 if (size >= 0 && size < INT32_MAX) {
2220 *outLen = size;
2221 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002222 if (str != nullptr) {
2223 if (str[size] == '\0') {
2224 return str;
2225 }
2226 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002227 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002228 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002229 *outLen = 0;
2230 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231}
2232
2233String16 Parcel::readString16() const
2234{
2235 size_t len;
2236 const char16_t* str = readString16Inplace(&len);
2237 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002238
2239 if (!mServiceFuzzing) {
2240 ALOGE("Reading a NULL string not supported here.");
2241 }
2242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 return String16();
2244}
2245
Casey Dahlinb9872622015-11-25 15:09:45 -08002246
Casey Dahlin451ff582015-10-19 18:12:18 -07002247status_t Parcel::readString16(String16* pArg) const
2248{
2249 size_t len;
2250 const char16_t* str = readString16Inplace(&len);
2251 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002252 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002253 return 0;
2254 } else {
2255 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002256 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002257 }
2258}
2259
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002260const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2261{
2262 int32_t size = readInt32();
2263 // watch for potential int overflow from size+1
2264 if (size >= 0 && size < INT32_MAX) {
2265 *outLen = size;
2266 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002267 if (str != nullptr) {
2268 if (str[size] == u'\0') {
2269 return str;
2270 }
2271 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 }
2273 }
2274 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002275 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002276}
2277
Casey Dahlinf0c13772015-10-27 18:33:56 -07002278status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2279{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002280 status_t status = readNullableStrongBinder(val);
2281 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002282 if (!mServiceFuzzing) {
2283 ALOGW("Expecting binder but got null!");
2284 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002285 status = UNEXPECTED_NULL;
2286 }
2287 return status;
2288}
2289
2290status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2291{
Steven Morelanda86a3562019-08-01 23:28:34 +00002292 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002293}
2294
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295sp<IBinder> Parcel::readStrongBinder() const
2296{
2297 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002298 // Note that a lot of code in Android reads binders by hand with this
2299 // method, and that code has historically been ok with getting nullptr
2300 // back (while ignoring error codes).
2301 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002302 return val;
2303}
2304
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002305int32_t Parcel::readExceptionCode() const
2306{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002307 binder::Status status;
2308 status.readFromParcel(*this);
2309 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002310}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002311
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002312#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002313native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002314{
2315 int numFds, numInts;
2316 status_t err;
2317 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002318 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002319 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002320 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002321
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002322 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002323 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002324 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002325 }
2326
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002327 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002328 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002329 if (h->data[i] < 0) {
2330 for (int j = 0; j < i; j++) {
2331 close(h->data[j]);
2332 }
2333 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002334 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002335 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002336 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002337 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002338 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002339 native_handle_close(h);
2340 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002341 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002342 }
2343 return h;
2344}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002345#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002346
Frederick Mayle69a0c992022-05-26 20:38:39 +00002347int Parcel::readFileDescriptor() const {
2348 if (const auto* rpcFields = maybeRpcFields()) {
2349 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2350 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002351 if (!mServiceFuzzing) {
2352 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2353 "the object list",
2354 this, mDataPos);
2355 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002356 return BAD_TYPE;
2357 }
2358
2359 int32_t objectType = readInt32();
2360 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2361 return BAD_TYPE;
2362 }
2363
2364 int32_t fdIndex = readInt32();
2365 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2366 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2367 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2368 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2369 return BAD_VALUE;
2370 }
2371 return toRawFd(rpcFields->mFds->at(fdIndex));
2372 }
2373
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002374#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002376
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002377 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002378 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002379 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002380
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002382#else // BINDER_WITH_KERNEL_IPC
2383 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2384 return INVALID_OPERATION;
2385#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386}
2387
Frederick Mayle69a0c992022-05-26 20:38:39 +00002388int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002389 int32_t hasComm = readInt32();
2390 int fd = readFileDescriptor();
2391 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002392 // detach (owned by the binder driver)
2393 int comm = readFileDescriptor();
2394
2395 // warning: this must be kept in sync with:
2396 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2397 enum ParcelFileDescriptorStatus {
2398 DETACHED = 2,
2399 };
2400
2401#if BYTE_ORDER == BIG_ENDIAN
2402 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2403#endif
2404#if BYTE_ORDER == LITTLE_ENDIAN
2405 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2406#endif
2407
2408 ssize_t written = TEMP_FAILURE_RETRY(
2409 ::write(comm, &message, sizeof(message)));
2410
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002411 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002412 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2413 written, strerror(errno));
2414 return BAD_TYPE;
2415 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002416 }
2417 return fd;
2418}
2419
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002420status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002421 int got = readFileDescriptor();
2422
2423 if (got == BAD_TYPE) {
2424 return BAD_TYPE;
2425 }
2426
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002427 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002428 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002429 return BAD_VALUE;
2430 }
2431
2432 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002433
2434 if (val->get() < 0) {
2435 return BAD_VALUE;
2436 }
2437
2438 return OK;
2439}
2440
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002441status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002442 int got = readParcelFileDescriptor();
2443
2444 if (got == BAD_TYPE) {
2445 return BAD_TYPE;
2446 }
2447
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002448 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002449 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002450 return BAD_VALUE;
2451 }
2452
2453 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002454
2455 if (val->get() < 0) {
2456 return BAD_VALUE;
2457 }
2458
2459 return OK;
2460}
Casey Dahlin06673e32015-11-23 13:24:23 -08002461
Jeff Brown5707dbf2011-09-23 21:17:56 -07002462status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2463{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002464#ifdef BINDER_DISABLE_BLOB
2465 (void)len;
2466 (void)outBlob;
2467 return INVALID_OPERATION;
2468#else
Jeff Brown13b16042014-11-11 16:44:25 -08002469 int32_t blobType;
2470 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002471 if (status) return status;
2472
Jeff Brown13b16042014-11-11 16:44:25 -08002473 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002474 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002475 const void* ptr = readInplace(len);
2476 if (!ptr) return BAD_VALUE;
2477
Jeff Brown13b16042014-11-11 16:44:25 -08002478 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002479 return NO_ERROR;
2480 }
2481
Steve Block6807e592011-10-20 11:56:00 +01002482 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002483 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002484 int fd = readFileDescriptor();
2485 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2486
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002487 if (!ashmem_valid(fd)) {
2488 ALOGE("invalid fd");
2489 return BAD_VALUE;
2490 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002491 int size = ashmem_get_size_region(fd);
2492 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002493 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002494 return BAD_VALUE;
2495 }
Yi Kong91635562018-06-07 14:38:36 -07002496 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002497 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002498 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002499
Jeff Brown13b16042014-11-11 16:44:25 -08002500 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002501 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002502#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002503}
2504
Mathias Agopiane1424282013-07-29 21:24:40 -07002505status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002506{
2507 // size
2508 const size_t len = this->readInt32();
2509 const size_t fd_count = this->readInt32();
2510
Andrei Homescu1519b982022-06-09 02:04:44 +00002511 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002512 // don't accept size_t values which may have come from an
2513 // inadvertent conversion from a negative int.
2514 return BAD_VALUE;
2515 }
2516
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002517 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002518 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002519 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002520 return BAD_VALUE;
2521
Yi Kong91635562018-06-07 14:38:36 -07002522 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002523 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002524 fds = new (std::nothrow) int[fd_count];
2525 if (fds == nullptr) {
2526 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2527 return BAD_VALUE;
2528 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002529 }
2530
2531 status_t err = NO_ERROR;
2532 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002533 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002534 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002535 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002536 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 -07002537 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2538 // Close all the file descriptors that were dup-ed.
2539 for (size_t j=0; j<i ;j++) {
2540 close(fds[j]);
2541 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002542 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002543 }
2544
2545 if (err == NO_ERROR) {
2546 err = val.unflatten(buf, len, fds, fd_count);
2547 }
2548
2549 if (fd_count) {
2550 delete [] fds;
2551 }
2552
2553 return err;
2554}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002555
2556#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2558{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002559 const auto* kernelFields = maybeKernelFields();
2560 if (kernelFields == nullptr) {
2561 return nullptr;
2562 }
2563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002564 const size_t DPOS = mDataPos;
2565 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2566 const flat_binder_object* obj
2567 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2568 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002569 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002570 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 // the object list, so we don't want to check for it when
2572 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002573 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574 return obj;
2575 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002576
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002577 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002578 binder_size_t* const OBJS = kernelFields->mObjects;
2579 const size_t N = kernelFields->mObjectsSize;
2580 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002581
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002583 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002584 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002586 // Start at the current hint position, looking for an object at
2587 // the current data position.
2588 if (opos < N) {
2589 while (opos < (N-1) && OBJS[opos] < DPOS) {
2590 opos++;
2591 }
2592 } else {
2593 opos = N-1;
2594 }
2595 if (OBJS[opos] == DPOS) {
2596 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002597 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002598 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002599 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002600 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002601 return obj;
2602 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002603
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002604 // Look backwards for it...
2605 while (opos > 0 && OBJS[opos] > DPOS) {
2606 opos--;
2607 }
2608 if (OBJS[opos] == DPOS) {
2609 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002610 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002612 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002613 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 return obj;
2615 }
2616 }
Steven Moreland3a667492023-06-02 21:41:39 +00002617 if (!mServiceFuzzing) {
2618 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2619 "list",
2620 this, DPOS);
2621 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 }
Yi Kong91635562018-06-07 14:38:36 -07002623 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002624}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002625#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626
Frederick Mayled3c595a2022-05-09 23:02:54 +00002627void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002628 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002629#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002630 size_t i = kernelFields->mObjectsSize;
2631 if (i > 0) {
2632 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002633 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002634 while (i > 0) {
2635 i--;
2636 const flat_binder_object* flat =
2637 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2638 if (flat->hdr.type == BINDER_TYPE_FD) {
2639 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002640 // FDs from the kernel are always owned
2641 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002642 }
2643 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002644#else // BINDER_WITH_KERNEL_IPC
2645 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2646#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002647 } else if (auto* rpcFields = maybeRpcFields()) {
2648 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 }
2650}
2651
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002652uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002653{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002654 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002655}
2656
2657size_t Parcel::ipcDataSize() const
2658{
2659 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2660}
2661
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002662uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002663{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002664 if (const auto* kernelFields = maybeKernelFields()) {
2665 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2666 }
2667 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668}
2669
2670size_t Parcel::ipcObjectsCount() const
2671{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002672 if (const auto* kernelFields = maybeKernelFields()) {
2673 return kernelFields->mObjectsSize;
2674 }
2675 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676}
2677
Frederick Mayled3c595a2022-05-09 23:02:54 +00002678void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2679 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002680 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2681 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2682
Steven Morelandceed9bb2020-12-17 01:01:06 +00002683 freeData();
2684
Frederick Mayled3c595a2022-05-09 23:02:54 +00002685 auto* kernelFields = maybeKernelFields();
2686 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2687
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002688 mData = const_cast<uint8_t*>(data);
2689 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002690 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2691 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002692 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002693
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002694#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002695 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002696 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2697 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002698 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002699 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002700 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002701 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002702 break;
2703 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002704 const flat_binder_object* flat
2705 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2706 uint32_t type = flat->hdr.type;
2707 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2708 type == BINDER_TYPE_FD)) {
2709 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2710 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002711 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002712 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002713 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002714 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2715 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002716
2717 // WARNING: callers of ipcSetDataReference need to make sure they
2718 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002719 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002720 break;
2721 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002722 if (type == BINDER_TYPE_FD) {
2723 // FDs from the kernel are always owned
2724 FdTag(flat->handle, 0, this);
2725 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002726 minOffset = offset + sizeof(flat_binder_object);
2727 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002729#else // BINDER_WITH_KERNEL_IPC
2730 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2731 "Non-zero objects count passed to Parcel with kernel driver disabled");
2732#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002733}
2734
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002735status_t Parcel::rpcSetDataReference(
2736 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2737 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002738 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002739 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2740 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2741
2742 LOG_ALWAYS_FATAL_IF(session == nullptr);
2743
Frederick Mayle694e9732022-07-15 00:24:58 +00002744 if (objectTableSize != ancillaryFds.size()) {
2745 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2746 relFunc(data, dataSize, nullptr, 0);
2747 return BAD_VALUE;
2748 }
2749 for (size_t i = 0; i < objectTableSize; i++) {
2750 uint32_t minObjectEnd;
2751 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2752 minObjectEnd >= dataSize) {
2753 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2754 objectTable[i], dataSize);
2755 relFunc(data, dataSize, nullptr, 0);
2756 return BAD_VALUE;
2757 }
2758 }
2759
Frederick Mayled3c595a2022-05-09 23:02:54 +00002760 freeData();
2761 markForRpc(session);
2762
Frederick Mayle69a0c992022-05-26 20:38:39 +00002763 auto* rpcFields = maybeRpcFields();
2764 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2765
Frederick Mayled3c595a2022-05-09 23:02:54 +00002766 mData = const_cast<uint8_t*>(data);
2767 mDataSize = mDataCapacity = dataSize;
2768 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002769
Frederick Mayle69a0c992022-05-26 20:38:39 +00002770 rpcFields->mObjectPositions.reserve(objectTableSize);
2771 for (size_t i = 0; i < objectTableSize; i++) {
2772 rpcFields->mObjectPositions.push_back(objectTable[i]);
2773 }
2774 if (!ancillaryFds.empty()) {
2775 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002776 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002777 }
2778
2779 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002780}
2781
Pawan Wagh7063b522022-09-28 18:52:26 +00002782void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002784
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785 if (errorCheck() != NO_ERROR) {
2786 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002787 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 } else if (dataSize() > 0) {
2789 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002790 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002791#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002792 if (const auto* kernelFields = maybeKernelFields()) {
2793 const binder_size_t* OBJS = kernelFields->mObjects;
2794 const size_t N = objectsCount();
2795 for (size_t i = 0; i < N; i++) {
2796 const flat_binder_object* flat =
2797 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002798 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002799 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2800 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002801 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002802#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002803 } else {
2804 to << "NULL";
2805 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002807 to << ")";
2808}
2809
2810void Parcel::releaseObjects()
2811{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002812 auto* kernelFields = maybeKernelFields();
2813 if (kernelFields == nullptr) {
2814 return;
2815 }
2816
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002817#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002818 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002819 if (i == 0) {
2820 return;
2821 }
2822 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002824 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 while (i > 0) {
2826 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002827 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002828 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002830#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831}
2832
2833void Parcel::acquireObjects()
2834{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002835 auto* kernelFields = maybeKernelFields();
2836 if (kernelFields == nullptr) {
2837 return;
2838 }
2839
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002840#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002841 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002842 if (i == 0) {
2843 return;
2844 }
2845 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002847 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002848 while (i > 0) {
2849 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002850 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002851 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002852 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002853#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854}
2855
2856void Parcel::freeData()
2857{
2858 freeDataNoInit();
2859 initState();
2860}
2861
2862void Parcel::freeDataNoInit()
2863{
2864 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002865 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002866 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002867 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002868 // Close FDs before freeing, otherwise they will leak for kernel binder.
2869 closeFileDescriptors();
2870 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002871 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002872 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002873 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002875 if (mData) {
2876 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002877 gParcelGlobalAllocSize -= mDataCapacity;
2878 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002879 if (mDeallocZero) {
2880 zeroMemory(mData, mDataSize);
2881 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002882 free(mData);
2883 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002884 auto* kernelFields = maybeKernelFields();
2885 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002886 }
2887}
2888
2889status_t Parcel::growData(size_t len)
2890{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002891 if (len > INT32_MAX) {
2892 // don't accept size_t values which may have come from an
2893 // inadvertent conversion from a negative int.
2894 return BAD_VALUE;
2895 }
2896
Martijn Coenen93fe5182020-01-22 10:46:25 +01002897 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2898 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002899 size_t newSize = ((mDataSize+len)*3)/2;
2900 return (newSize <= mDataSize)
2901 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002902 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002903}
2904
Steven Morelandf183fdd2020-10-27 00:12:12 +00002905static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2906 if (!zero) {
2907 return (uint8_t*)realloc(data, newCapacity);
2908 }
2909 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2910 if (!newData) {
2911 return nullptr;
2912 }
2913
2914 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2915 zeroMemory(data, oldCapacity);
2916 free(data);
2917 return newData;
2918}
2919
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002920status_t Parcel::restartWrite(size_t desired)
2921{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002922 if (desired > INT32_MAX) {
2923 // don't accept size_t values which may have come from an
2924 // inadvertent conversion from a negative int.
2925 return BAD_VALUE;
2926 }
2927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002928 if (mOwner) {
2929 freeData();
2930 return continueWrite(desired);
2931 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002932
Steven Morelandc6b0dfa2024-03-05 09:10:02 +00002933 releaseObjects();
2934
Steven Morelandf183fdd2020-10-27 00:12:12 +00002935 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002936 if (!data && desired > mDataCapacity) {
2937 mError = NO_MEMORY;
2938 return NO_MEMORY;
2939 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002940
Devin Moore4a0a55e2020-06-04 13:23:10 -07002941 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002942 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002943 if (mDataCapacity > desired) {
2944 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2945 } else {
2946 gParcelGlobalAllocSize += (desired - mDataCapacity);
2947 }
2948
Colin Cross83ec65e2015-12-08 17:15:50 -08002949 if (!mData) {
2950 gParcelGlobalAllocCount++;
2951 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002952 mData = data;
2953 mDataCapacity = desired;
2954 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002955
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002956 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002957 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2958 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2959
Frederick Mayled3c595a2022-05-09 23:02:54 +00002960 if (auto* kernelFields = maybeKernelFields()) {
2961 free(kernelFields->mObjects);
2962 kernelFields->mObjects = nullptr;
2963 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2964 kernelFields->mNextObjectHint = 0;
2965 kernelFields->mObjectsSorted = false;
2966 kernelFields->mHasFds = false;
2967 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002968 } else if (auto* rpcFields = maybeRpcFields()) {
2969 rpcFields->mObjectPositions.clear();
2970 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002971 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002972 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002974 return NO_ERROR;
2975}
2976
2977status_t Parcel::continueWrite(size_t desired)
2978{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002979 if (desired > INT32_MAX) {
2980 // don't accept size_t values which may have come from an
2981 // inadvertent conversion from a negative int.
2982 return BAD_VALUE;
2983 }
2984
Frederick Mayled3c595a2022-05-09 23:02:54 +00002985 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002986 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002987
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002988 // If shrinking, first adjust for any objects that appear
2989 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002990 size_t objectsSize =
2991 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2992 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002993 if (desired == 0) {
2994 objectsSize = 0;
2995 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002996 if (kernelFields) {
2997 while (objectsSize > 0) {
2998 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2999 objectsSize--;
3000 }
3001 } else {
3002 while (objectsSize > 0) {
3003 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
3004 objectsSize--;
3005 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003006 }
3007 }
3008 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003009
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003010 if (mOwner) {
3011 // If the size is going to zero, just release the owner's data.
3012 if (desired == 0) {
3013 freeData();
3014 return NO_ERROR;
3015 }
3016
3017 // If there is a different owner, we need to take
3018 // posession.
3019 uint8_t* data = (uint8_t*)malloc(desired);
3020 if (!data) {
3021 mError = NO_MEMORY;
3022 return NO_MEMORY;
3023 }
Yi Kong91635562018-06-07 14:38:36 -07003024 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003025
Frederick Mayle69a0c992022-05-26 20:38:39 +00003026 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07003027 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003028 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09003029 free(data);
3030
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003031 mError = NO_MEMORY;
3032 return NO_MEMORY;
3033 }
3034
3035 // Little hack to only acquire references on objects
3036 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00003037 size_t oldObjectsSize = kernelFields->mObjectsSize;
3038 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003039 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003040 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003041 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003042 if (rpcFields) {
3043 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3044 free(data);
3045 return status;
3046 }
3047 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003049 if (mData) {
3050 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3051 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003052 if (objects && kernelFields && kernelFields->mObjects) {
3053 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003054 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003055 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3056 if (kernelFields) {
3057 // TODO(b/239222407): This seems wrong. We should only free FDs when
3058 // they are in a truncated section of the parcel.
3059 closeFileDescriptors();
3060 }
3061 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003062 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003063 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003064
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003065 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003066 gParcelGlobalAllocSize += desired;
3067 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003069 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003070 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003071 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003072 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003073 if (kernelFields) {
3074 kernelFields->mObjects = objects;
3075 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3076 kernelFields->mNextObjectHint = 0;
3077 kernelFields->mObjectsSorted = false;
3078 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003079
3080 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003081 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003082#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003083 // Need to release refs on any objects we are dropping.
3084 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003085 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3086 const flat_binder_object* flat =
3087 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003088 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003089 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003090 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003091 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003092 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003093 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003094
3095 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003096 free(kernelFields->mObjects);
3097 kernelFields->mObjects = nullptr;
3098 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003099 } else {
3100 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003101 (binder_size_t*)realloc(kernelFields->mObjects,
3102 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003103 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003104 kernelFields->mObjects = objects;
3105 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003106 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003107 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003108 kernelFields->mObjectsSize = objectsSize;
3109 kernelFields->mNextObjectHint = 0;
3110 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003111#else // BINDER_WITH_KERNEL_IPC
3112 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3113#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003114 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003115 if (rpcFields) {
3116 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3117 return status;
3118 }
3119 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003120
3121 // We own the data, so we can just do a realloc().
3122 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003123 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003124 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003125 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3126 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003127 gParcelGlobalAllocSize += desired;
3128 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003129 mData = data;
3130 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003131 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003132 mError = NO_MEMORY;
3133 return NO_MEMORY;
3134 }
3135 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003136 if (mDataSize > desired) {
3137 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003138 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003139 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003140 if (mDataPos > desired) {
3141 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003142 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003143 }
3144 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003145
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003146 } else {
3147 // This is the first data. Easy!
3148 uint8_t* data = (uint8_t*)malloc(desired);
3149 if (!data) {
3150 mError = NO_MEMORY;
3151 return NO_MEMORY;
3152 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003153
Frederick Mayled3c595a2022-05-09 23:02:54 +00003154 if (!(mDataCapacity == 0 &&
3155 (kernelFields == nullptr ||
3156 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3157 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3158 kernelFields ? kernelFields->mObjects : nullptr,
3159 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003160 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003161
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003162 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003163 gParcelGlobalAllocSize += desired;
3164 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003165
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003166 mData = data;
3167 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003168 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3169 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003170 mDataCapacity = desired;
3171 }
3172
3173 return NO_ERROR;
3174}
3175
Frederick Mayle69a0c992022-05-26 20:38:39 +00003176status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3177 auto* rpcFields = maybeRpcFields();
3178 if (newObjectsSize == 0) {
3179 rpcFields->mObjectPositions.clear();
3180 if (rpcFields->mFds) {
3181 rpcFields->mFds->clear();
3182 }
3183 return OK;
3184 }
3185 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3186 uint32_t pos = rpcFields->mObjectPositions.back();
3187 rpcFields->mObjectPositions.pop_back();
3188 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3189 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3190 const auto fdIndex =
3191 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3192 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3193 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3194 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3195 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3196 return BAD_VALUE;
3197 }
3198 // In practice, this always removes the last element.
3199 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3200 }
3201 }
3202 return OK;
3203}
3204
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003205void Parcel::initState()
3206{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003207 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003208 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003209 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003210 mDataSize = 0;
3211 mDataCapacity = 0;
3212 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003213 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3214 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003215 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003216 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003217 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003218 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003219 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003220 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003221}
3222
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003223void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003224 auto* kernelFields = maybeKernelFields();
3225 if (kernelFields == nullptr) {
3226 return;
3227 }
3228 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003229 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003230 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003231}
3232
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003233#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003234size_t Parcel::getBlobAshmemSize() const
3235{
Adrian Roos6bb31142015-10-22 16:46:12 -07003236 // This used to return the size of all blobs that were written to ashmem, now we're returning
3237 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003238 // TODO(b/202029388): Remove method once ABI can be changed.
3239 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003240}
3241
Adrian Rooscbf37262015-10-22 16:12:53 -07003242size_t Parcel::getOpenAshmemSize() const
3243{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003244 auto* kernelFields = maybeKernelFields();
3245 if (kernelFields == nullptr) {
3246 return 0;
3247 }
3248
Steven Morelandc673f1f2021-10-07 18:23:35 -07003249 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003250#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003251 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003252 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003253 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003254
3255 // cookie is compared against zero for historical reasons
3256 // > obj.cookie = takeOwnership ? 1 : 0;
3257 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3258 int size = ashmem_get_size_region(flat->handle);
3259 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3260 ALOGE("Overflow when computing ashmem size.");
3261 return SIZE_MAX;
3262 }
3263 }
3264 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003265#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003266 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003267}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003268#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003269
3270// --- Parcel::Blob ---
3271
3272Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003273 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003274}
3275
3276Parcel::Blob::~Blob() {
3277 release();
3278}
3279
3280void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003281 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003282 ::munmap(mData, mSize);
3283 }
3284 clear();
3285}
3286
Jeff Brown13b16042014-11-11 16:44:25 -08003287void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3288 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003289 mData = data;
3290 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003291 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003292}
3293
3294void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003295 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003296 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003297 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003298 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003299}
3300
Steven Moreland61ff8492019-09-26 16:05:45 -07003301} // namespace android