blob: 2dd310e9cac65d4fd7db325e208403029419b3ff [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) {
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000259 status_t status = writeInt32(RpcFields::TYPE_BINDER); // non-null
Steven Moreland5553ac42020-11-11 02:14:45 +0000260 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 {
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000269 status_t status = writeInt32(RpcFields::TYPE_BINDER_NULL); // null
Steven Moreland5553ac42020-11-11 02:14:45 +0000270 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
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000743status_t Parcel::hasBinders(bool* result) const {
744 status_t status = hasBindersInRange(0, dataSize(), result);
745 ALOGE_IF(status != NO_ERROR, "Error %d calling hasBindersInRange()", status);
746 return status;
747}
748
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000749std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
750 std::vector<sp<IBinder>> ret;
751
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000752#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000753 const auto* kernelFields = maybeKernelFields();
754 if (kernelFields == nullptr) {
755 return ret;
756 }
757
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000758 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000759 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
760 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000761 const flat_binder_object* flat =
762 reinterpret_cast<const flat_binder_object*>(mData + offset);
763 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
764
765 setDataPosition(offset);
766
767 sp<IBinder> binder = readStrongBinder();
768 if (binder != nullptr) ret.push_back(binder);
769 }
770
771 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000772#endif // BINDER_WITH_KERNEL_IPC
773
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000774 return ret;
775}
776
777std::vector<int> Parcel::debugReadAllFileDescriptors() const {
778 std::vector<int> ret;
779
Frederick Mayle69a0c992022-05-26 20:38:39 +0000780 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000781#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000782 size_t initPosition = dataPosition();
783 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
784 binder_size_t offset = kernelFields->mObjects[i];
785 const flat_binder_object* flat =
786 reinterpret_cast<const flat_binder_object*>(mData + offset);
787 if (flat->hdr.type != BINDER_TYPE_FD) continue;
788
789 setDataPosition(offset);
790
791 int fd = readFileDescriptor();
792 LOG_ALWAYS_FATAL_IF(fd == -1);
793 ret.push_back(fd);
794 }
795 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000796#else
797 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
798#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000799 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
800 for (const auto& fd : *rpcFields->mFds) {
801 ret.push_back(toRawFd(fd));
802 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000803 }
804
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000805 return ret;
806}
807
sandeepbandaruaa9d3a32024-02-22 17:16:53 +0000808status_t Parcel::hasBindersInRange(size_t offset, size_t len, bool* result) const {
809 if (len > INT32_MAX || offset > INT32_MAX) {
810 // Don't accept size_t values which may have come from an inadvertent conversion from a
811 // negative int.
812 return BAD_VALUE;
813 }
814 size_t limit;
815 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
816 return BAD_VALUE;
817 }
818 *result = false;
819 if (const auto* kernelFields = maybeKernelFields()) {
820#ifdef BINDER_WITH_KERNEL_IPC
821 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
822 size_t pos = kernelFields->mObjects[i];
823 if (pos < offset) continue;
824 if (pos + sizeof(flat_binder_object) > offset + len) {
825 if (kernelFields->mObjectsSorted) {
826 break;
827 } else {
828 continue;
829 }
830 }
831 const flat_binder_object* flat =
832 reinterpret_cast<const flat_binder_object*>(mData + pos);
833 if (flat->hdr.type == BINDER_TYPE_BINDER || flat->hdr.type == BINDER_TYPE_HANDLE) {
834 *result = true;
835 break;
836 }
837 }
838#else
839 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
840 return INVALID_OPERATION;
841#endif // BINDER_WITH_KERNEL_IPC
842 } else if (const auto* rpcFields = maybeRpcFields()) {
843 return INVALID_OPERATION;
844 }
845 return NO_ERROR;
846}
847
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100848status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100849 if (len > INT32_MAX || offset > INT32_MAX) {
850 // Don't accept size_t values which may have come from an inadvertent conversion from a
851 // negative int.
852 return BAD_VALUE;
853 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100854 size_t limit;
855 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100856 return BAD_VALUE;
857 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100858 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000859 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000860#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000861 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
862 size_t pos = kernelFields->mObjects[i];
863 if (pos < offset) continue;
864 if (pos + sizeof(flat_binder_object) > offset + len) {
865 if (kernelFields->mObjectsSorted) {
866 break;
867 } else {
868 continue;
869 }
870 }
871 const flat_binder_object* flat =
872 reinterpret_cast<const flat_binder_object*>(mData + pos);
873 if (flat->hdr.type == BINDER_TYPE_FD) {
874 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000875 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000876 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100877 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000878#else
879 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
880 return INVALID_OPERATION;
881#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000882 } else if (const auto* rpcFields = maybeRpcFields()) {
883 for (uint32_t pos : rpcFields->mObjectPositions) {
884 if (offset <= pos && pos < limit) {
885 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
886 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
887 *result = true;
888 break;
889 }
890 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100891 }
892 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100893 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100894}
895
Steven Morelandf183fdd2020-10-27 00:12:12 +0000896void Parcel::markSensitive() const
897{
898 mDeallocZero = true;
899}
900
Steven Moreland5553ac42020-11-11 02:14:45 +0000901void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000902 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
903
Steven Moreland5553ac42020-11-11 02:14:45 +0000904 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700905 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000906 }
907}
908
Steven Morelandc9939062021-05-05 17:57:41 +0000909void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000910 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
911 "format must be set before data is written OR on IPC data");
912
Frederick Mayled3c595a2022-05-09 23:02:54 +0000913 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000914}
915
916bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000917 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000918}
919
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000920void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000921 auto* kernelFields = maybeKernelFields();
922 if (kernelFields == nullptr) {
923 return;
924 }
925
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000926 // Only update the request headers once. We only want to point
927 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000928 if (!kernelFields->mRequestHeaderPresent) {
929 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
930 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000931 }
932}
933
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000934#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland94e63e12023-12-05 20:29:50 +0000935
936#if defined(__ANDROID__)
937
Steven Morelandb6c7e222021-02-18 19:20:14 +0000938#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700939constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700940#elif defined(__ANDROID_RECOVERY__)
941constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700942#else
943constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
944#endif
Steven Moreland94e63e12023-12-05 20:29:50 +0000945
946#else // ANDROID not defined
947
948// If kernel binder is used in new environments, we need to make sure it's separated
949// out and has a separate header.
950constexpr int32_t kHeader = B_PACK_CHARS('U', 'N', 'K', 'N');
951#endif
952
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000953#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700954
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700955// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700956status_t Parcel::writeInterfaceToken(const String16& interface)
957{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000958 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000959}
960
961status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000962 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000963#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000964 const IPCThreadState* threadState = IPCThreadState::self();
965 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
966 updateWorkSourceRequestHeaderPosition();
967 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
968 : IPCThreadState::kUnsetWorkSource);
969 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000970#else // BINDER_WITH_KERNEL_IPC
971 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
972 return INVALID_OPERATION;
973#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000974 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000975
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000977 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700978}
979
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000980bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
981{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000982 auto* kernelFields = maybeKernelFields();
983 if (kernelFields == nullptr) {
984 return false;
985 }
986 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000987 return false;
988 }
989
990 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000991 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000992 status_t err = writeInt32(uid);
993 setDataPosition(initialPosition);
994 return err == NO_ERROR;
995}
996
Steven Morelandf1b1e492019-05-06 15:05:13 -0700997uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000998{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000999 auto* kernelFields = maybeKernelFields();
1000 if (kernelFields == nullptr) {
1001 return false;
1002 }
1003 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001004 return IPCThreadState::kUnsetWorkSource;
1005 }
1006
1007 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +00001008 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +00001009 uid_t uid = readInt32();
1010 setDataPosition(initialPosition);
1011 return uid;
1012}
1013
Mathias Agopian83c04462009-05-22 19:00:22 -07001014bool Parcel::checkInterface(IBinder* binder) const
1015{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001016 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -07001017}
1018
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001019bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -07001020 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001021{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001022 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001023}
1024
1025bool Parcel::enforceInterface(const char16_t* interface,
1026 size_t len,
1027 IPCThreadState* threadState) const
1028{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001029 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001030#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +00001031 // StrictModePolicy.
1032 int32_t strictPolicy = readInt32();
1033 if (threadState == nullptr) {
1034 threadState = IPCThreadState::self();
1035 }
1036 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
1037 // For one-way calls, the callee is running entirely
1038 // disconnected from the caller, so disable StrictMode entirely.
1039 // Not only does disk/network usage not impact the caller, but
1040 // there's no way to communicate back violations anyway.
1041 threadState->setStrictModePolicy(0);
1042 } else {
1043 threadState->setStrictModePolicy(strictPolicy);
1044 }
1045 // WorkSource.
1046 updateWorkSourceRequestHeaderPosition();
1047 int32_t workSource = readInt32();
1048 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
1049 // vendor header
1050 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +00001051
1052 // fuzzers skip this check, because it is for protecting the underlying ABI, but
1053 // we don't want it to reduce our coverage
1054 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +00001055 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
1056 header);
1057 return false;
1058 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001059#else // BINDER_WITH_KERNEL_IPC
1060 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1061 (void)threadState;
1062 return false;
1063#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001064 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001065
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001066 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001067 size_t parcel_interface_len;
1068 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1069 if (len == parcel_interface_len &&
1070 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001071 return true;
1072 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001073 if (mServiceFuzzing) {
1074 // ignore. Theoretically, this could cause a few false positives, because
1075 // people could assume things about getInterfaceDescriptor if they pass
1076 // this point, but it would be extremely fragile. It's more important that
1077 // we fuzz with the above things read from the Parcel.
1078 return true;
1079 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001080 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001081 String8(interface, len).c_str(),
1082 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001083 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001084 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001085 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001086}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087
Pawan Wagh104654a2022-11-15 21:18:42 +00001088void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1089 mEnforceNoDataAvail = enforceNoDataAvail;
1090}
1091
Steven Moreland3a667492023-06-02 21:41:39 +00001092void Parcel::setServiceFuzzing() {
1093 mServiceFuzzing = true;
1094}
1095
Steven Moreland418914a2023-06-28 21:47:14 +00001096bool Parcel::isServiceFuzzing() const {
1097 return mServiceFuzzing;
1098}
1099
Jooyung Hand23f9502021-12-23 14:39:57 +09001100binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001101 if (!mEnforceNoDataAvail) {
1102 return binder::Status::ok();
1103 }
1104
Jooyung Hand23f9502021-12-23 14:39:57 +09001105 const auto n = dataAvail();
1106 if (n == 0) {
1107 return binder::Status::ok();
1108 }
1109 return binder::Status::
1110 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1111 String8::format("Parcel data not fully consumed, unread size: %zu",
1112 n));
1113}
1114
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115size_t Parcel::objectsCount() const
1116{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001117 if (const auto* kernelFields = maybeKernelFields()) {
1118 return kernelFields->mObjectsSize;
1119 }
1120 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001121}
1122
1123status_t Parcel::errorCheck() const
1124{
1125 return mError;
1126}
1127
1128void Parcel::setError(status_t err)
1129{
1130 mError = err;
1131}
1132
1133status_t Parcel::finishWrite(size_t len)
1134{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001135 if (len > INT32_MAX) {
1136 // don't accept size_t values which may have come from an
1137 // inadvertent conversion from a negative int.
1138 return BAD_VALUE;
1139 }
1140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001141 //printf("Finish write of %d\n", len);
1142 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001143 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001144 if (mDataPos > mDataSize) {
1145 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001146 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001147 }
1148 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1149 return NO_ERROR;
1150}
1151
1152status_t Parcel::writeUnpadded(const void* data, size_t len)
1153{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001154 if (len > INT32_MAX) {
1155 // don't accept size_t values which may have come from an
1156 // inadvertent conversion from a negative int.
1157 return BAD_VALUE;
1158 }
1159
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001160 size_t end = mDataPos + len;
1161 if (end < mDataPos) {
1162 // integer overflow
1163 return BAD_VALUE;
1164 }
1165
1166 if (end <= mDataCapacity) {
1167restart_write:
1168 memcpy(mData+mDataPos, data, len);
1169 return finishWrite(len);
1170 }
1171
1172 status_t err = growData(len);
1173 if (err == NO_ERROR) goto restart_write;
1174 return err;
1175}
1176
1177status_t Parcel::write(const void* data, size_t len)
1178{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001179 if (len > INT32_MAX) {
1180 // don't accept size_t values which may have come from an
1181 // inadvertent conversion from a negative int.
1182 return BAD_VALUE;
1183 }
1184
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185 void* const d = writeInplace(len);
1186 if (d) {
1187 memcpy(d, data, len);
1188 return NO_ERROR;
1189 }
1190 return mError;
1191}
1192
1193void* Parcel::writeInplace(size_t len)
1194{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001195 if (len > INT32_MAX) {
1196 // don't accept size_t values which may have come from an
1197 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001198 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001199 }
1200
1201 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001202
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001203 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001205 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206 }
1207
1208 if ((mDataPos+padded) <= mDataCapacity) {
1209restart_write:
1210 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1211 uint8_t* const data = mData+mDataPos;
1212
1213 // Need to pad at end?
1214 if (padded != len) {
1215#if BYTE_ORDER == BIG_ENDIAN
1216 static const uint32_t mask[4] = {
1217 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1218 };
1219#endif
1220#if BYTE_ORDER == LITTLE_ENDIAN
1221 static const uint32_t mask[4] = {
1222 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1223 };
1224#endif
1225 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1226 // *reinterpret_cast<void**>(data+padded-4));
1227 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1228 }
1229
1230 finishWrite(padded);
1231 return data;
1232 }
1233
1234 status_t err = growData(padded);
1235 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001236 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001237}
1238
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001239status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1240 const uint8_t* strData = (uint8_t*)str.data();
1241 const size_t strLen= str.length();
1242 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001243 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001244 return BAD_VALUE;
1245 }
1246
1247 status_t err = writeInt32(utf16Len);
1248 if (err) {
1249 return err;
1250 }
1251
1252 // Allocate enough bytes to hold our converted string and its terminating NULL.
1253 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1254 if (!dst) {
1255 return NO_MEMORY;
1256 }
1257
Sergio Girof4607432016-07-21 14:46:35 +01001258 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001259
1260 return NO_ERROR;
1261}
1262
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001263
Andy Hung49198cf2020-11-18 11:02:39 -08001264status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1265status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001266
Andy Hung49198cf2020-11-18 11:02:39 -08001267status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1268status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001269
Andy Hung49198cf2020-11-18 11:02:39 -08001270status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1271status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1272status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1273status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1274status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1275status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1276status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1277status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1278status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1279status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1280status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1281status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1282status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1283status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1284status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1285status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1286status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1287status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1288status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1289status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1290status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1291status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1292status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1293status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1294status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1295status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1296status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001297
Andy Hung49198cf2020-11-18 11:02:39 -08001298status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001299status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001300 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001301status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001302 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001303status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001304 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001305status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001306 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1307status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001308
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001309status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1310 return writeData(val);
1311}
1312status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1313 return writeData(val);
1314}
1315status_t Parcel::writeUniqueFileDescriptorVector(
1316 const std::unique_ptr<std::vector<unique_fd>>& val) {
1317 return writeData(val);
1318}
Andy Hung49198cf2020-11-18 11:02:39 -08001319
1320status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1321status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1322status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1323
1324status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1325
1326status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1327status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1328
1329status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1330status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1331
1332status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1333status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1334status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1335status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1336status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1337status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1338status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1339status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1340status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1341status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1342status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1343status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1344status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1345status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1346status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1347status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1348status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1349status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1350status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1351status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1352status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1353status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1354status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1355status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1356status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1357status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1358status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1359
1360status_t Parcel::readString16Vector(
1361 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1362status_t Parcel::readString16Vector(
1363 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1364status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1365status_t Parcel::readUtf8VectorFromUtf16Vector(
1366 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1367status_t Parcel::readUtf8VectorFromUtf16Vector(
1368 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1369status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1370
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001371status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1372 return readData(val);
1373}
1374status_t Parcel::readUniqueFileDescriptorVector(
1375 std::unique_ptr<std::vector<unique_fd>>* val) const {
1376 return readData(val);
1377}
1378status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1379 return readData(val);
1380}
Andy Hung49198cf2020-11-18 11:02:39 -08001381
1382status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1383status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1384status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1385
1386status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388status_t Parcel::writeInt32(int32_t val)
1389{
Andreas Huber84a6d042009-08-17 13:33:27 -07001390 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001391}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001392
1393status_t Parcel::writeUint32(uint32_t val)
1394{
1395 return writeAligned(val);
1396}
1397
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001398status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001399 if (len > INT32_MAX) {
1400 // don't accept size_t values which may have come from an
1401 // inadvertent conversion from a negative int.
1402 return BAD_VALUE;
1403 }
1404
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001405 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001406 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001407 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001408 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001409 if (ret == NO_ERROR) {
1410 ret = write(val, len * sizeof(*val));
1411 }
1412 return ret;
1413}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001414status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001415 if (len > INT32_MAX) {
1416 // don't accept size_t values which may have come from an
1417 // inadvertent conversion from a negative int.
1418 return BAD_VALUE;
1419 }
1420
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001421 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001422 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001423 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001424 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001425 if (ret == NO_ERROR) {
1426 ret = write(val, len * sizeof(*val));
1427 }
1428 return ret;
1429}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001430
Casey Dahlind6848f52015-10-15 15:44:59 -07001431status_t Parcel::writeBool(bool val)
1432{
1433 return writeInt32(int32_t(val));
1434}
1435
1436status_t Parcel::writeChar(char16_t val)
1437{
1438 return writeInt32(int32_t(val));
1439}
1440
1441status_t Parcel::writeByte(int8_t val)
1442{
1443 return writeInt32(int32_t(val));
1444}
1445
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001446status_t Parcel::writeInt64(int64_t val)
1447{
Andreas Huber84a6d042009-08-17 13:33:27 -07001448 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001449}
1450
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001451status_t Parcel::writeUint64(uint64_t val)
1452{
1453 return writeAligned(val);
1454}
1455
Serban Constantinescuf683e012013-11-05 16:53:55 +00001456status_t Parcel::writePointer(uintptr_t val)
1457{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001458 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001459}
1460
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461status_t Parcel::writeFloat(float val)
1462{
Andreas Huber84a6d042009-08-17 13:33:27 -07001463 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001464}
1465
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001466#if defined(__mips__) && defined(__mips_hard_float)
1467
1468status_t Parcel::writeDouble(double val)
1469{
1470 union {
1471 double d;
1472 unsigned long long ll;
1473 } u;
1474 u.d = val;
1475 return writeAligned(u.ll);
1476}
1477
1478#else
1479
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480status_t Parcel::writeDouble(double val)
1481{
Andreas Huber84a6d042009-08-17 13:33:27 -07001482 return writeAligned(val);
1483}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001485#endif
1486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001487status_t Parcel::writeCString(const char* str)
1488{
1489 return write(str, strlen(str)+1);
1490}
1491
1492status_t Parcel::writeString8(const String8& str)
1493{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001494 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001495}
1496
1497status_t Parcel::writeString8(const char* str, size_t len)
1498{
1499 if (str == nullptr) return writeInt32(-1);
1500
Jeff Sharkey18220902020-11-05 08:36:20 -07001501 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001502 status_t err = writeInt32(len);
1503 if (err == NO_ERROR) {
1504 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1505 if (data) {
1506 memcpy(data, str, len);
1507 *reinterpret_cast<char*>(data+len) = 0;
1508 return NO_ERROR;
1509 }
1510 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001511 }
1512 return err;
1513}
1514
1515status_t Parcel::writeString16(const String16& str)
1516{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001517 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518}
1519
1520status_t Parcel::writeString16(const char16_t* str, size_t len)
1521{
Yi Kong91635562018-06-07 14:38:36 -07001522 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001523
Jeff Sharkey18220902020-11-05 08:36:20 -07001524 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525 status_t err = writeInt32(len);
1526 if (err == NO_ERROR) {
1527 len *= sizeof(char16_t);
1528 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1529 if (data) {
1530 memcpy(data, str, len);
1531 *reinterpret_cast<char16_t*>(data+len) = 0;
1532 return NO_ERROR;
1533 }
1534 err = mError;
1535 }
1536 return err;
1537}
1538
1539status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1540{
Steven Morelanda86a3562019-08-01 23:28:34 +00001541 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542}
1543
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001544
Casey Dahlinb9872622015-11-25 15:09:45 -08001545status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1546 if (!parcelable) {
1547 return writeInt32(0);
1548 }
1549
1550 return writeParcelable(*parcelable);
1551}
1552
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001553#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001554status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001555{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001556 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001557 return BAD_TYPE;
1558
1559 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001560 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001561 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001563 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001564 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001566 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1567 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568
1569 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001570 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 return err;
1572 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001573 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001574 return err;
1575}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001576#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001577
Frederick Mayle69a0c992022-05-26 20:38:39 +00001578status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1579 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001580 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001581 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001582 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001583 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001584 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001585 }
1586 if (!mAllowFds) {
1587 return FDS_NOT_ALLOWED;
1588 }
1589 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1590 case RpcSession::FileDescriptorTransportMode::NONE: {
1591 return FDS_NOT_ALLOWED;
1592 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001593 case RpcSession::FileDescriptorTransportMode::UNIX:
1594 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001595 if (rpcFields->mFds == nullptr) {
1596 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1597 }
1598 size_t dataPos = mDataPos;
1599 if (dataPos > UINT32_MAX) {
1600 return NO_MEMORY;
1601 }
1602 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1603 return err;
1604 }
1605 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1606 return err;
1607 }
1608 rpcFields->mObjectPositions.push_back(dataPos);
1609 rpcFields->mFds->push_back(std::move(fdVariant));
1610 return OK;
1611 }
1612 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001613 }
1614
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001615#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001617 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001618 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001619 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001620 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001621 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001623#else // BINDER_WITH_KERNEL_IPC
1624 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1625 (void)fd;
1626 (void)takeOwnership;
1627 return INVALID_OPERATION;
1628#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001629}
1630
1631status_t Parcel::writeDupFileDescriptor(int fd)
1632{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001633 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001634 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001635 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001636 }
1637 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001638 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001639 close(dupFd);
1640 }
1641 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642}
1643
Dianne Hackborn1941a402016-08-29 12:30:43 -07001644status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1645{
1646 writeInt32(0);
1647 return writeFileDescriptor(fd, takeOwnership);
1648}
1649
Ryo Hashimotobf551892018-05-31 16:58:35 +09001650status_t Parcel::writeDupParcelFileDescriptor(int fd)
1651{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001652 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001653 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001654 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001655 }
1656 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1657 if (err != OK) {
1658 close(dupFd);
1659 }
1660 return err;
1661}
1662
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001663status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001664 return writeDupFileDescriptor(fd.get());
1665}
1666
Jeff Brown13b16042014-11-11 16:44:25 -08001667status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001668{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001669#ifdef BINDER_DISABLE_BLOB
1670 (void)len;
1671 (void)mutableCopy;
1672 (void)outBlob;
1673 return INVALID_OPERATION;
1674#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001675 if (len > INT32_MAX) {
1676 // don't accept size_t values which may have come from an
1677 // inadvertent conversion from a negative int.
1678 return BAD_VALUE;
1679 }
1680
Jeff Brown13b16042014-11-11 16:44:25 -08001681 status_t status;
1682 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001683 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001684 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001685 if (status) return status;
1686
1687 void* ptr = writeInplace(len);
1688 if (!ptr) return NO_MEMORY;
1689
Jeff Brown13b16042014-11-11 16:44:25 -08001690 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001691 return NO_ERROR;
1692 }
1693
Steve Block6807e592011-10-20 11:56:00 +01001694 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001695 int fd = ashmem_create_region("Parcel Blob", len);
1696 if (fd < 0) return NO_MEMORY;
1697
1698 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1699 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001700 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001701 } else {
Yi Kong91635562018-06-07 14:38:36 -07001702 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001703 if (ptr == MAP_FAILED) {
1704 status = -errno;
1705 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001706 if (!mutableCopy) {
1707 result = ashmem_set_prot_region(fd, PROT_READ);
1708 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001709 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001710 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001711 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001712 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001713 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001714 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001715 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001716 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001717 return NO_ERROR;
1718 }
1719 }
1720 }
1721 }
1722 ::munmap(ptr, len);
1723 }
1724 ::close(fd);
1725 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001726#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001727}
1728
Jeff Brown13b16042014-11-11 16:44:25 -08001729status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1730{
1731 // Must match up with what's done in writeBlob.
1732 if (!mAllowFds) return FDS_NOT_ALLOWED;
1733 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1734 if (status) return status;
1735 return writeDupFileDescriptor(fd);
1736}
1737
Mathias Agopiane1424282013-07-29 21:24:40 -07001738status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001739{
1740 status_t err;
1741
1742 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001743 const size_t len = val.getFlattenedSize();
1744 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001745
Andrei Homescu1519b982022-06-09 02:04:44 +00001746 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001747 // don't accept size_t values which may have come from an
1748 // inadvertent conversion from a negative int.
1749 return BAD_VALUE;
1750 }
1751
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001752 err = this->writeInt32(len);
1753 if (err) return err;
1754
1755 err = this->writeInt32(fd_count);
1756 if (err) return err;
1757
1758 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001759 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001760 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001761 return BAD_VALUE;
1762
Yi Kong91635562018-06-07 14:38:36 -07001763 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001764 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001765 fds = new (std::nothrow) int[fd_count];
1766 if (fds == nullptr) {
1767 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1768 return BAD_VALUE;
1769 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001770 }
1771
1772 err = val.flatten(buf, len, fds, fd_count);
1773 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1774 err = this->writeDupFileDescriptor( fds[i] );
1775 }
1776
1777 if (fd_count) {
1778 delete [] fds;
1779 }
1780
1781 return err;
1782}
1783
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1785{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001786 auto* kernelFields = maybeKernelFields();
1787 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1788
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001789#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001790 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001791 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792 if (enoughData && enoughObjects) {
1793restart_write:
1794 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001795
Christopher Tate98e67d32015-06-03 18:44:15 -07001796 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001797 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001798 if (!mAllowFds) {
1799 // fail before modifying our object index
1800 return FDS_NOT_ALLOWED;
1801 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001802 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001803 }
1804
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001805 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001806 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001807 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001808 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001809 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001810 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001811
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812 return finishWrite(sizeof(flat_binder_object));
1813 }
1814
1815 if (!enoughData) {
1816 const status_t err = growData(sizeof(val));
1817 if (err != NO_ERROR) return err;
1818 }
1819 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001820 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1821 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1822 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001823 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001824 binder_size_t* objects =
1825 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001826 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001827 kernelFields->mObjects = objects;
1828 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001831 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001832#else // BINDER_WITH_KERNEL_IPC
1833 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1834 (void)val;
1835 (void)nullMetaData;
1836 return INVALID_OPERATION;
1837#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838}
1839
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001840status_t Parcel::writeNoException()
1841{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001842 binder::Status status;
1843 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001844}
1845
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001846status_t Parcel::validateReadData(size_t upperBound) const
1847{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001848 const auto* kernelFields = maybeKernelFields();
1849 if (kernelFields == nullptr) {
1850 // Can't validate RPC Parcel reads because the location of binder
1851 // objects is unknown.
1852 return OK;
1853 }
1854
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001855#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001856 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001857 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1858 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001859 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001860 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1861 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001862 // For some reason the current read position is greater than the next object
1863 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001864 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001865 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001866 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001867 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001868 if (!mServiceFuzzing) {
1869 ALOGE("Attempt to read from protected data in Parcel %p", this);
1870 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001871 return PERMISSION_DENIED;
1872 }
1873 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001874 } while (nextObject < kernelFields->mObjectsSize &&
1875 upperBound > kernelFields->mObjects[nextObject]);
1876 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001877 }
1878 return NO_ERROR;
1879 }
1880 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001881 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001882 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001883 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001884 prevObj--;
1885 if(*prevObj > *currObj) {
1886 goto data_unsorted;
1887 }
1888 currObj--;
1889 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001890 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001891 goto data_sorted;
1892
1893data_unsorted:
1894 // Insertion Sort mObjects
1895 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1896 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001897 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1898 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001899 binder_size_t temp = *iter0;
1900 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001901 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001902 *(iter1 + 1) = *iter1;
1903 iter1--;
1904 }
1905 *(iter1 + 1) = temp;
1906 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001907 kernelFields->mNextObjectHint = 0;
1908 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001909 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001910#else // BINDER_WITH_KERNEL_IPC
1911 (void)upperBound;
1912 return NO_ERROR;
1913#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001914}
1915
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916status_t Parcel::read(void* outData, size_t len) const
1917{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001918 if (len > INT32_MAX) {
1919 // don't accept size_t values which may have come from an
1920 // inadvertent conversion from a negative int.
1921 return BAD_VALUE;
1922 }
1923
1924 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1925 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001926 const auto* kernelFields = maybeKernelFields();
1927 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001928 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001929 if(err != NO_ERROR) {
1930 // Still increment the data position by the expected length
1931 mDataPos += pad_size(len);
1932 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1933 return err;
1934 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001935 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001936 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001937 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001938 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001939 return NO_ERROR;
1940 }
1941 return NOT_ENOUGH_DATA;
1942}
1943
1944const void* Parcel::readInplace(size_t len) const
1945{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001946 if (len > INT32_MAX) {
1947 // don't accept size_t values which may have come from an
1948 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001949 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001950 }
1951
1952 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1953 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001954 const auto* kernelFields = maybeKernelFields();
1955 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001956 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001957 if(err != NO_ERROR) {
1958 // Still increment the data position by the expected length
1959 mDataPos += pad_size(len);
1960 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001961 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001962 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001963 }
1964
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001965 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001966 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001967 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001968 return data;
1969 }
Yi Kong91635562018-06-07 14:38:36 -07001970 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971}
1972
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001973status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1974 if (status_t status = readInt32(size); status != OK) return status;
1975 if (*size < 0) return OK; // may be null, client to handle
1976
1977 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1978
1979 // approximation, can't know max element size (e.g. if it makes heap
1980 // allocations)
1981 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1982 int32_t allocationSize;
1983 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1984
1985 // High limit of 1MB since something this big could never be returned. Could
1986 // probably scope this down, but might impact very specific usecases.
1987 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1988
1989 if (allocationSize >= kMaxAllocationSize) {
1990 return NO_MEMORY;
1991 }
1992
1993 return OK;
1994}
1995
Andreas Huber84a6d042009-08-17 13:33:27 -07001996template<class T>
1997status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001998 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001999 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002000
2001 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002002 const auto* kernelFields = maybeKernelFields();
2003 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002004 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07002005 if(err != NO_ERROR) {
2006 // Still increment the data position by the expected length
2007 mDataPos += sizeof(T);
2008 return err;
2009 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002010 }
2011
Andrei Homescue33238e2022-03-29 22:50:00 +00002012 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07002013 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014 return NO_ERROR;
2015 } else {
2016 return NOT_ENOUGH_DATA;
2017 }
2018}
2019
Andreas Huber84a6d042009-08-17 13:33:27 -07002020template<class T>
2021T Parcel::readAligned() const {
2022 T result;
2023 if (readAligned(&result) != NO_ERROR) {
2024 result = 0;
2025 }
2026
2027 return result;
2028}
2029
2030template<class T>
2031status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07002032 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00002033 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07002034
2035 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
2036restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00002037 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07002038 return finishWrite(sizeof(val));
2039 }
2040
2041 status_t err = growData(sizeof(val));
2042 if (err == NO_ERROR) goto restart_write;
2043 return err;
2044}
2045
2046status_t Parcel::readInt32(int32_t *pArg) const
2047{
2048 return readAligned(pArg);
2049}
2050
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051int32_t Parcel::readInt32() const
2052{
Andreas Huber84a6d042009-08-17 13:33:27 -07002053 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054}
2055
Dan Stoza41a0f2f2014-12-01 10:01:10 -08002056status_t Parcel::readUint32(uint32_t *pArg) const
2057{
2058 return readAligned(pArg);
2059}
2060
2061uint32_t Parcel::readUint32() const
2062{
2063 return readAligned<uint32_t>();
2064}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065
2066status_t Parcel::readInt64(int64_t *pArg) const
2067{
Andreas Huber84a6d042009-08-17 13:33:27 -07002068 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002069}
2070
2071
2072int64_t Parcel::readInt64() const
2073{
Andreas Huber84a6d042009-08-17 13:33:27 -07002074 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075}
2076
Ronghua Wu2d13afd2015-03-16 11:11:07 -07002077status_t Parcel::readUint64(uint64_t *pArg) const
2078{
2079 return readAligned(pArg);
2080}
2081
2082uint64_t Parcel::readUint64() const
2083{
2084 return readAligned<uint64_t>();
2085}
2086
Serban Constantinescuf683e012013-11-05 16:53:55 +00002087status_t Parcel::readPointer(uintptr_t *pArg) const
2088{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002089 status_t ret;
2090 binder_uintptr_t ptr;
2091 ret = readAligned(&ptr);
2092 if (!ret)
2093 *pArg = ptr;
2094 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002095}
2096
2097uintptr_t Parcel::readPointer() const
2098{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002099 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002100}
2101
2102
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103status_t Parcel::readFloat(float *pArg) const
2104{
Andreas Huber84a6d042009-08-17 13:33:27 -07002105 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106}
2107
2108
2109float Parcel::readFloat() const
2110{
Andreas Huber84a6d042009-08-17 13:33:27 -07002111 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002112}
2113
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002114#if defined(__mips__) && defined(__mips_hard_float)
2115
2116status_t Parcel::readDouble(double *pArg) const
2117{
2118 union {
2119 double d;
2120 unsigned long long ll;
2121 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002122 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002123 status_t status;
2124 status = readAligned(&u.ll);
2125 *pArg = u.d;
2126 return status;
2127}
2128
2129double Parcel::readDouble() const
2130{
2131 union {
2132 double d;
2133 unsigned long long ll;
2134 } u;
2135 u.ll = readAligned<unsigned long long>();
2136 return u.d;
2137}
2138
2139#else
2140
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141status_t Parcel::readDouble(double *pArg) const
2142{
Andreas Huber84a6d042009-08-17 13:33:27 -07002143 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144}
2145
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146double Parcel::readDouble() const
2147{
Andreas Huber84a6d042009-08-17 13:33:27 -07002148 return readAligned<double>();
2149}
2150
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002151#endif
2152
Casey Dahlind6848f52015-10-15 15:44:59 -07002153status_t Parcel::readBool(bool *pArg) const
2154{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002155 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002156 status_t ret = readInt32(&tmp);
2157 *pArg = (tmp != 0);
2158 return ret;
2159}
2160
2161bool Parcel::readBool() const
2162{
2163 return readInt32() != 0;
2164}
2165
2166status_t Parcel::readChar(char16_t *pArg) const
2167{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002168 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002169 status_t ret = readInt32(&tmp);
2170 *pArg = char16_t(tmp);
2171 return ret;
2172}
2173
2174char16_t Parcel::readChar() const
2175{
2176 return char16_t(readInt32());
2177}
2178
2179status_t Parcel::readByte(int8_t *pArg) const
2180{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002181 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002182 status_t ret = readInt32(&tmp);
2183 *pArg = int8_t(tmp);
2184 return ret;
2185}
2186
2187int8_t Parcel::readByte() const
2188{
2189 return int8_t(readInt32());
2190}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002192status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2193 size_t utf16Size = 0;
2194 const char16_t* src = readString16Inplace(&utf16Size);
2195 if (!src) {
2196 return UNEXPECTED_NULL;
2197 }
2198
2199 // Save ourselves the trouble, we're done.
2200 if (utf16Size == 0u) {
2201 str->clear();
2202 return NO_ERROR;
2203 }
2204
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002205 // Allow for closing '\0'
2206 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2207 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002208 return BAD_VALUE;
2209 }
2210 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002211 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002212 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002213 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2214 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002215 return NO_ERROR;
2216}
2217
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218const char* Parcel::readCString() const
2219{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002220 if (mDataPos < mDataSize) {
2221 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002222 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2223 // is the string's trailing NUL within the parcel's valid bounds?
2224 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2225 if (eos) {
2226 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002227 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002228 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002229 return str;
2230 }
2231 }
Yi Kong91635562018-06-07 14:38:36 -07002232 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233}
2234
2235String8 Parcel::readString8() const
2236{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002237 size_t len;
2238 const char* str = readString8Inplace(&len);
2239 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002240
2241 if (!mServiceFuzzing) {
2242 ALOGE("Reading a NULL string not supported here.");
2243 }
2244
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002245 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002246}
2247
2248status_t Parcel::readString8(String8* pArg) const
2249{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002250 size_t len;
2251 const char* str = readString8Inplace(&len);
2252 if (str) {
2253 pArg->setTo(str, len);
2254 return 0;
2255 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002256 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002257 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002258 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002259}
2260
2261const char* Parcel::readString8Inplace(size_t* outLen) const
2262{
2263 int32_t size = readInt32();
2264 // watch for potential int overflow from size+1
2265 if (size >= 0 && size < INT32_MAX) {
2266 *outLen = size;
2267 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002268 if (str != nullptr) {
2269 if (str[size] == '\0') {
2270 return str;
2271 }
2272 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002273 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002274 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002275 *outLen = 0;
2276 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277}
2278
2279String16 Parcel::readString16() const
2280{
2281 size_t len;
2282 const char16_t* str = readString16Inplace(&len);
2283 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002284
2285 if (!mServiceFuzzing) {
2286 ALOGE("Reading a NULL string not supported here.");
2287 }
2288
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002289 return String16();
2290}
2291
Casey Dahlinb9872622015-11-25 15:09:45 -08002292
Casey Dahlin451ff582015-10-19 18:12:18 -07002293status_t Parcel::readString16(String16* pArg) const
2294{
2295 size_t len;
2296 const char16_t* str = readString16Inplace(&len);
2297 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002298 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002299 return 0;
2300 } else {
2301 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002302 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002303 }
2304}
2305
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002306const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2307{
2308 int32_t size = readInt32();
2309 // watch for potential int overflow from size+1
2310 if (size >= 0 && size < INT32_MAX) {
2311 *outLen = size;
2312 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002313 if (str != nullptr) {
2314 if (str[size] == u'\0') {
2315 return str;
2316 }
2317 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002318 }
2319 }
2320 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002321 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002322}
2323
Casey Dahlinf0c13772015-10-27 18:33:56 -07002324status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2325{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002326 status_t status = readNullableStrongBinder(val);
2327 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002328 if (!mServiceFuzzing) {
2329 ALOGW("Expecting binder but got null!");
2330 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002331 status = UNEXPECTED_NULL;
2332 }
2333 return status;
2334}
2335
2336status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2337{
Steven Morelanda86a3562019-08-01 23:28:34 +00002338 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002339}
2340
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002341sp<IBinder> Parcel::readStrongBinder() const
2342{
2343 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002344 // Note that a lot of code in Android reads binders by hand with this
2345 // method, and that code has historically been ok with getting nullptr
2346 // back (while ignoring error codes).
2347 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 return val;
2349}
2350
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002351int32_t Parcel::readExceptionCode() const
2352{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002353 binder::Status status;
2354 status.readFromParcel(*this);
2355 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002356}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002357
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002358#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002359native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002360{
2361 int numFds, numInts;
2362 status_t err;
2363 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002364 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002365 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002366 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002367
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002368 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002369 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002370 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002371 }
2372
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002373 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002374 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002375 if (h->data[i] < 0) {
2376 for (int j = 0; j < i; j++) {
2377 close(h->data[j]);
2378 }
2379 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002380 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002381 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002382 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002383 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002384 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002385 native_handle_close(h);
2386 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002387 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002388 }
2389 return h;
2390}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002391#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002392
Frederick Mayle69a0c992022-05-26 20:38:39 +00002393int Parcel::readFileDescriptor() const {
2394 if (const auto* rpcFields = maybeRpcFields()) {
2395 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2396 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002397 if (!mServiceFuzzing) {
2398 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2399 "the object list",
2400 this, mDataPos);
2401 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002402 return BAD_TYPE;
2403 }
2404
2405 int32_t objectType = readInt32();
2406 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2407 return BAD_TYPE;
2408 }
2409
2410 int32_t fdIndex = readInt32();
2411 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2412 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2413 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2414 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2415 return BAD_VALUE;
2416 }
2417 return toRawFd(rpcFields->mFds->at(fdIndex));
2418 }
2419
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002420#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002421 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002422
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002423 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002424 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002428#else // BINDER_WITH_KERNEL_IPC
2429 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2430 return INVALID_OPERATION;
2431#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432}
2433
Frederick Mayle69a0c992022-05-26 20:38:39 +00002434int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002435 int32_t hasComm = readInt32();
2436 int fd = readFileDescriptor();
2437 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002438 // detach (owned by the binder driver)
2439 int comm = readFileDescriptor();
2440
2441 // warning: this must be kept in sync with:
2442 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2443 enum ParcelFileDescriptorStatus {
2444 DETACHED = 2,
2445 };
2446
2447#if BYTE_ORDER == BIG_ENDIAN
2448 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2449#endif
2450#if BYTE_ORDER == LITTLE_ENDIAN
2451 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2452#endif
2453
2454 ssize_t written = TEMP_FAILURE_RETRY(
2455 ::write(comm, &message, sizeof(message)));
2456
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002457 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002458 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2459 written, strerror(errno));
2460 return BAD_TYPE;
2461 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002462 }
2463 return fd;
2464}
2465
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002466status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002467 int got = readFileDescriptor();
2468
2469 if (got == BAD_TYPE) {
2470 return BAD_TYPE;
2471 }
2472
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002473 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002474 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002475 return BAD_VALUE;
2476 }
2477
2478 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002479
2480 if (val->get() < 0) {
2481 return BAD_VALUE;
2482 }
2483
2484 return OK;
2485}
2486
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002487status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002488 int got = readParcelFileDescriptor();
2489
2490 if (got == BAD_TYPE) {
2491 return BAD_TYPE;
2492 }
2493
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002494 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002495 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002496 return BAD_VALUE;
2497 }
2498
2499 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002500
2501 if (val->get() < 0) {
2502 return BAD_VALUE;
2503 }
2504
2505 return OK;
2506}
Casey Dahlin06673e32015-11-23 13:24:23 -08002507
Jeff Brown5707dbf2011-09-23 21:17:56 -07002508status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2509{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002510#ifdef BINDER_DISABLE_BLOB
2511 (void)len;
2512 (void)outBlob;
2513 return INVALID_OPERATION;
2514#else
Jeff Brown13b16042014-11-11 16:44:25 -08002515 int32_t blobType;
2516 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002517 if (status) return status;
2518
Jeff Brown13b16042014-11-11 16:44:25 -08002519 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002520 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002521 const void* ptr = readInplace(len);
2522 if (!ptr) return BAD_VALUE;
2523
Jeff Brown13b16042014-11-11 16:44:25 -08002524 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002525 return NO_ERROR;
2526 }
2527
Steve Block6807e592011-10-20 11:56:00 +01002528 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002529 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002530 int fd = readFileDescriptor();
2531 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2532
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002533 if (!ashmem_valid(fd)) {
2534 ALOGE("invalid fd");
2535 return BAD_VALUE;
2536 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002537 int size = ashmem_get_size_region(fd);
2538 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002539 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002540 return BAD_VALUE;
2541 }
Yi Kong91635562018-06-07 14:38:36 -07002542 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002543 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002544 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002545
Jeff Brown13b16042014-11-11 16:44:25 -08002546 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002547 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002548#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002549}
2550
Mathias Agopiane1424282013-07-29 21:24:40 -07002551status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002552{
2553 // size
2554 const size_t len = this->readInt32();
2555 const size_t fd_count = this->readInt32();
2556
Andrei Homescu1519b982022-06-09 02:04:44 +00002557 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002558 // don't accept size_t values which may have come from an
2559 // inadvertent conversion from a negative int.
2560 return BAD_VALUE;
2561 }
2562
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002563 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002564 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002565 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002566 return BAD_VALUE;
2567
Yi Kong91635562018-06-07 14:38:36 -07002568 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002569 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002570 fds = new (std::nothrow) int[fd_count];
2571 if (fds == nullptr) {
2572 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2573 return BAD_VALUE;
2574 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002575 }
2576
2577 status_t err = NO_ERROR;
2578 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002579 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002580 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002581 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002582 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 -07002583 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2584 // Close all the file descriptors that were dup-ed.
2585 for (size_t j=0; j<i ;j++) {
2586 close(fds[j]);
2587 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002588 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002589 }
2590
2591 if (err == NO_ERROR) {
2592 err = val.unflatten(buf, len, fds, fd_count);
2593 }
2594
2595 if (fd_count) {
2596 delete [] fds;
2597 }
2598
2599 return err;
2600}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002601
2602#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2604{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002605 const auto* kernelFields = maybeKernelFields();
2606 if (kernelFields == nullptr) {
2607 return nullptr;
2608 }
2609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610 const size_t DPOS = mDataPos;
2611 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2612 const flat_binder_object* obj
2613 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2614 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002615 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002616 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002617 // the object list, so we don't want to check for it when
2618 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002619 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002620 return obj;
2621 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002622
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002624 binder_size_t* const OBJS = kernelFields->mObjects;
2625 const size_t N = kernelFields->mObjectsSize;
2626 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002627
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002628 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002629 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002630 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002631
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002632 // Start at the current hint position, looking for an object at
2633 // the current data position.
2634 if (opos < N) {
2635 while (opos < (N-1) && OBJS[opos] < DPOS) {
2636 opos++;
2637 }
2638 } else {
2639 opos = N-1;
2640 }
2641 if (OBJS[opos] == DPOS) {
2642 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002643 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002645 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002646 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002647 return obj;
2648 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002649
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 // Look backwards for it...
2651 while (opos > 0 && OBJS[opos] > DPOS) {
2652 opos--;
2653 }
2654 if (OBJS[opos] == DPOS) {
2655 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002656 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002658 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002659 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002660 return obj;
2661 }
2662 }
Steven Moreland3a667492023-06-02 21:41:39 +00002663 if (!mServiceFuzzing) {
2664 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2665 "list",
2666 this, DPOS);
2667 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002668 }
Yi Kong91635562018-06-07 14:38:36 -07002669 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002670}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002671#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002672
Frederick Mayled3c595a2022-05-09 23:02:54 +00002673void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002674 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002675#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002676 size_t i = kernelFields->mObjectsSize;
2677 if (i > 0) {
2678 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002679 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002680 while (i > 0) {
2681 i--;
2682 const flat_binder_object* flat =
2683 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2684 if (flat->hdr.type == BINDER_TYPE_FD) {
2685 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002686 // FDs from the kernel are always owned
2687 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002688 }
2689 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002690#else // BINDER_WITH_KERNEL_IPC
2691 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2692#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002693 } else if (auto* rpcFields = maybeRpcFields()) {
2694 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 }
2696}
2697
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002698uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002699{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002700 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002701}
2702
2703size_t Parcel::ipcDataSize() const
2704{
2705 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2706}
2707
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002708uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002709{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002710 if (const auto* kernelFields = maybeKernelFields()) {
2711 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2712 }
2713 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002714}
2715
2716size_t Parcel::ipcObjectsCount() const
2717{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002718 if (const auto* kernelFields = maybeKernelFields()) {
2719 return kernelFields->mObjectsSize;
2720 }
2721 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722}
2723
Frederick Mayled3c595a2022-05-09 23:02:54 +00002724void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2725 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002726 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2727 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2728
Steven Morelandceed9bb2020-12-17 01:01:06 +00002729 freeData();
2730
Frederick Mayled3c595a2022-05-09 23:02:54 +00002731 auto* kernelFields = maybeKernelFields();
2732 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2733
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002734 mData = const_cast<uint8_t*>(data);
2735 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002736 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2737 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002738 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002739
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002740#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002741 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002742 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2743 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002744 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002745 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002746 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002747 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002748 break;
2749 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002750 const flat_binder_object* flat
2751 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2752 uint32_t type = flat->hdr.type;
2753 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2754 type == BINDER_TYPE_FD)) {
2755 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2756 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002757 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002758 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002759 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002760 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2761 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002762
2763 // WARNING: callers of ipcSetDataReference need to make sure they
2764 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002765 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002766 break;
2767 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002768 if (type == BINDER_TYPE_FD) {
2769 // FDs from the kernel are always owned
2770 FdTag(flat->handle, 0, this);
2771 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002772 minOffset = offset + sizeof(flat_binder_object);
2773 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002775#else // BINDER_WITH_KERNEL_IPC
2776 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2777 "Non-zero objects count passed to Parcel with kernel driver disabled");
2778#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002779}
2780
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002781status_t Parcel::rpcSetDataReference(
2782 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2783 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002784 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002785 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2786 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2787
2788 LOG_ALWAYS_FATAL_IF(session == nullptr);
2789
Frederick Mayle694e9732022-07-15 00:24:58 +00002790 if (objectTableSize != ancillaryFds.size()) {
2791 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2792 relFunc(data, dataSize, nullptr, 0);
2793 return BAD_VALUE;
2794 }
2795 for (size_t i = 0; i < objectTableSize; i++) {
2796 uint32_t minObjectEnd;
2797 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2798 minObjectEnd >= dataSize) {
2799 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2800 objectTable[i], dataSize);
2801 relFunc(data, dataSize, nullptr, 0);
2802 return BAD_VALUE;
2803 }
2804 }
2805
Frederick Mayled3c595a2022-05-09 23:02:54 +00002806 freeData();
2807 markForRpc(session);
2808
Frederick Mayle69a0c992022-05-26 20:38:39 +00002809 auto* rpcFields = maybeRpcFields();
2810 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2811
Frederick Mayled3c595a2022-05-09 23:02:54 +00002812 mData = const_cast<uint8_t*>(data);
2813 mDataSize = mDataCapacity = dataSize;
2814 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002815
Frederick Mayle69a0c992022-05-26 20:38:39 +00002816 rpcFields->mObjectPositions.reserve(objectTableSize);
2817 for (size_t i = 0; i < objectTableSize; i++) {
2818 rpcFields->mObjectPositions.push_back(objectTable[i]);
2819 }
2820 if (!ancillaryFds.empty()) {
2821 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002822 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002823 }
2824
2825 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002826}
2827
Pawan Wagh7063b522022-09-28 18:52:26 +00002828void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002830
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831 if (errorCheck() != NO_ERROR) {
2832 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002833 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002834 } else if (dataSize() > 0) {
2835 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002836 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002837#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002838 if (const auto* kernelFields = maybeKernelFields()) {
2839 const binder_size_t* OBJS = kernelFields->mObjects;
2840 const size_t N = objectsCount();
2841 for (size_t i = 0; i < N; i++) {
2842 const flat_binder_object* flat =
2843 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002844 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002845 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2846 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002847 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002848#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002849 } else {
2850 to << "NULL";
2851 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002852
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002853 to << ")";
2854}
2855
2856void Parcel::releaseObjects()
2857{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002858 auto* kernelFields = maybeKernelFields();
2859 if (kernelFields == nullptr) {
2860 return;
2861 }
2862
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002863#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002864 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002865 if (i == 0) {
2866 return;
2867 }
2868 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002869 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002870 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002871 while (i > 0) {
2872 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002873 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002874 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002876#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877}
2878
2879void Parcel::acquireObjects()
2880{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002881 auto* kernelFields = maybeKernelFields();
2882 if (kernelFields == nullptr) {
2883 return;
2884 }
2885
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002886#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002887 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002888 if (i == 0) {
2889 return;
2890 }
2891 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002892 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002893 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002894 while (i > 0) {
2895 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002896 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002897 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002898 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002899#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900}
2901
2902void Parcel::freeData()
2903{
2904 freeDataNoInit();
2905 initState();
2906}
2907
2908void Parcel::freeDataNoInit()
2909{
2910 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002911 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002912 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002913 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002914 // Close FDs before freeing, otherwise they will leak for kernel binder.
2915 closeFileDescriptors();
2916 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002917 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002918 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002919 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002920 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002921 if (mData) {
2922 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002923 gParcelGlobalAllocSize -= mDataCapacity;
2924 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002925 if (mDeallocZero) {
2926 zeroMemory(mData, mDataSize);
2927 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002928 free(mData);
2929 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002930 auto* kernelFields = maybeKernelFields();
2931 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002932 }
2933}
2934
2935status_t Parcel::growData(size_t len)
2936{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002937 if (len > INT32_MAX) {
2938 // don't accept size_t values which may have come from an
2939 // inadvertent conversion from a negative int.
2940 return BAD_VALUE;
2941 }
2942
Martijn Coenen93fe5182020-01-22 10:46:25 +01002943 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2944 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002945 size_t newSize = ((mDataSize+len)*3)/2;
2946 return (newSize <= mDataSize)
2947 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002948 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002949}
2950
Steven Morelandf183fdd2020-10-27 00:12:12 +00002951static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2952 if (!zero) {
2953 return (uint8_t*)realloc(data, newCapacity);
2954 }
2955 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2956 if (!newData) {
2957 return nullptr;
2958 }
2959
2960 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2961 zeroMemory(data, oldCapacity);
2962 free(data);
2963 return newData;
2964}
2965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002966status_t Parcel::restartWrite(size_t desired)
2967{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002968 if (desired > INT32_MAX) {
2969 // don't accept size_t values which may have come from an
2970 // inadvertent conversion from a negative int.
2971 return BAD_VALUE;
2972 }
2973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002974 if (mOwner) {
2975 freeData();
2976 return continueWrite(desired);
2977 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002978
Steven Morelandf183fdd2020-10-27 00:12:12 +00002979 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002980 if (!data && desired > mDataCapacity) {
2981 mError = NO_MEMORY;
2982 return NO_MEMORY;
2983 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002984
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002985 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002986
Devin Moore4a0a55e2020-06-04 13:23:10 -07002987 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002988 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002989 if (mDataCapacity > desired) {
2990 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2991 } else {
2992 gParcelGlobalAllocSize += (desired - mDataCapacity);
2993 }
2994
Colin Cross83ec65e2015-12-08 17:15:50 -08002995 if (!mData) {
2996 gParcelGlobalAllocCount++;
2997 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002998 mData = data;
2999 mDataCapacity = desired;
3000 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003001
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003002 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003003 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
3004 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
3005
Frederick Mayled3c595a2022-05-09 23:02:54 +00003006 if (auto* kernelFields = maybeKernelFields()) {
3007 free(kernelFields->mObjects);
3008 kernelFields->mObjects = nullptr;
3009 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
3010 kernelFields->mNextObjectHint = 0;
3011 kernelFields->mObjectsSorted = false;
3012 kernelFields->mHasFds = false;
3013 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00003014 } else if (auto* rpcFields = maybeRpcFields()) {
3015 rpcFields->mObjectPositions.clear();
3016 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003017 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04003018 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003020 return NO_ERROR;
3021}
3022
3023status_t Parcel::continueWrite(size_t desired)
3024{
Nick Kralevichb6b14232015-04-02 09:36:02 -07003025 if (desired > INT32_MAX) {
3026 // don't accept size_t values which may have come from an
3027 // inadvertent conversion from a negative int.
3028 return BAD_VALUE;
3029 }
3030
Frederick Mayled3c595a2022-05-09 23:02:54 +00003031 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00003032 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003034 // If shrinking, first adjust for any objects that appear
3035 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00003036 size_t objectsSize =
3037 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
3038 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003039 if (desired == 0) {
3040 objectsSize = 0;
3041 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00003042 if (kernelFields) {
3043 while (objectsSize > 0) {
3044 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
3045 objectsSize--;
3046 }
3047 } else {
3048 while (objectsSize > 0) {
3049 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
3050 objectsSize--;
3051 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003052 }
3053 }
3054 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003056 if (mOwner) {
3057 // If the size is going to zero, just release the owner's data.
3058 if (desired == 0) {
3059 freeData();
3060 return NO_ERROR;
3061 }
3062
3063 // If there is a different owner, we need to take
3064 // posession.
3065 uint8_t* data = (uint8_t*)malloc(desired);
3066 if (!data) {
3067 mError = NO_MEMORY;
3068 return NO_MEMORY;
3069 }
Yi Kong91635562018-06-07 14:38:36 -07003070 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003071
Frederick Mayle69a0c992022-05-26 20:38:39 +00003072 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07003073 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003074 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09003075 free(data);
3076
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003077 mError = NO_MEMORY;
3078 return NO_MEMORY;
3079 }
3080
3081 // Little hack to only acquire references on objects
3082 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00003083 size_t oldObjectsSize = kernelFields->mObjectsSize;
3084 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003085 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003086 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003087 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003088 if (rpcFields) {
3089 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3090 free(data);
3091 return status;
3092 }
3093 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003095 if (mData) {
3096 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3097 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003098 if (objects && kernelFields && kernelFields->mObjects) {
3099 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003100 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003101 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3102 if (kernelFields) {
3103 // TODO(b/239222407): This seems wrong. We should only free FDs when
3104 // they are in a truncated section of the parcel.
3105 closeFileDescriptors();
3106 }
3107 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003108 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003109 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003110
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003111 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003112 gParcelGlobalAllocSize += desired;
3113 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003114
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003115 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003116 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003117 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003118 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003119 if (kernelFields) {
3120 kernelFields->mObjects = objects;
3121 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3122 kernelFields->mNextObjectHint = 0;
3123 kernelFields->mObjectsSorted = false;
3124 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003125
3126 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003127 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003128#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003129 // Need to release refs on any objects we are dropping.
3130 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003131 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3132 const flat_binder_object* flat =
3133 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003134 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003135 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003136 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003137 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003138 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003139 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003140
3141 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003142 free(kernelFields->mObjects);
3143 kernelFields->mObjects = nullptr;
3144 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003145 } else {
3146 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003147 (binder_size_t*)realloc(kernelFields->mObjects,
3148 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003149 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003150 kernelFields->mObjects = objects;
3151 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003152 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003153 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003154 kernelFields->mObjectsSize = objectsSize;
3155 kernelFields->mNextObjectHint = 0;
3156 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003157#else // BINDER_WITH_KERNEL_IPC
3158 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3159#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003160 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003161 if (rpcFields) {
3162 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3163 return status;
3164 }
3165 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003166
3167 // We own the data, so we can just do a realloc().
3168 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003169 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003170 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003171 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3172 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003173 gParcelGlobalAllocSize += desired;
3174 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003175 mData = data;
3176 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003177 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003178 mError = NO_MEMORY;
3179 return NO_MEMORY;
3180 }
3181 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003182 if (mDataSize > desired) {
3183 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003184 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003185 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003186 if (mDataPos > desired) {
3187 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003188 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003189 }
3190 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003191
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003192 } else {
3193 // This is the first data. Easy!
3194 uint8_t* data = (uint8_t*)malloc(desired);
3195 if (!data) {
3196 mError = NO_MEMORY;
3197 return NO_MEMORY;
3198 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003199
Frederick Mayled3c595a2022-05-09 23:02:54 +00003200 if (!(mDataCapacity == 0 &&
3201 (kernelFields == nullptr ||
3202 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3203 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3204 kernelFields ? kernelFields->mObjects : nullptr,
3205 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003206 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003207
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003208 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003209 gParcelGlobalAllocSize += desired;
3210 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003211
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003212 mData = data;
3213 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003214 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3215 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003216 mDataCapacity = desired;
3217 }
3218
3219 return NO_ERROR;
3220}
3221
Frederick Mayle69a0c992022-05-26 20:38:39 +00003222status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3223 auto* rpcFields = maybeRpcFields();
3224 if (newObjectsSize == 0) {
3225 rpcFields->mObjectPositions.clear();
3226 if (rpcFields->mFds) {
3227 rpcFields->mFds->clear();
3228 }
3229 return OK;
3230 }
3231 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3232 uint32_t pos = rpcFields->mObjectPositions.back();
3233 rpcFields->mObjectPositions.pop_back();
3234 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3235 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3236 const auto fdIndex =
3237 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3238 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3239 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3240 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3241 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3242 return BAD_VALUE;
3243 }
3244 // In practice, this always removes the last element.
3245 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3246 }
3247 }
3248 return OK;
3249}
3250
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003251void Parcel::initState()
3252{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003253 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003254 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003255 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003256 mDataSize = 0;
3257 mDataCapacity = 0;
3258 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003259 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3260 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003261 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003262 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003263 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003264 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003265 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003266 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003267}
3268
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003269void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003270 auto* kernelFields = maybeKernelFields();
3271 if (kernelFields == nullptr) {
3272 return;
3273 }
3274 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003275 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003276 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003277}
3278
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003279#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003280size_t Parcel::getBlobAshmemSize() const
3281{
Adrian Roos6bb31142015-10-22 16:46:12 -07003282 // This used to return the size of all blobs that were written to ashmem, now we're returning
3283 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003284 // TODO(b/202029388): Remove method once ABI can be changed.
3285 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003286}
3287
Adrian Rooscbf37262015-10-22 16:12:53 -07003288size_t Parcel::getOpenAshmemSize() const
3289{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003290 auto* kernelFields = maybeKernelFields();
3291 if (kernelFields == nullptr) {
3292 return 0;
3293 }
3294
Steven Morelandc673f1f2021-10-07 18:23:35 -07003295 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003296#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003297 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003298 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003299 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003300
3301 // cookie is compared against zero for historical reasons
3302 // > obj.cookie = takeOwnership ? 1 : 0;
3303 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3304 int size = ashmem_get_size_region(flat->handle);
3305 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3306 ALOGE("Overflow when computing ashmem size.");
3307 return SIZE_MAX;
3308 }
3309 }
3310 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003311#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003312 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003313}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003314#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003315
3316// --- Parcel::Blob ---
3317
3318Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003319 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003320}
3321
3322Parcel::Blob::~Blob() {
3323 release();
3324}
3325
3326void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003327 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003328 ::munmap(mData, mSize);
3329 }
3330 clear();
3331}
3332
Jeff Brown13b16042014-11-11 16:44:25 -08003333void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3334 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003335 mData = data;
3336 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003337 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003338}
3339
3340void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003341 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003342 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003343 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003344 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003345}
3346
Steven Moreland61ff8492019-09-26 16:05:45 -07003347} // namespace android