blob: 146ddef8ba219398da871e05f4dccf1ed208190a [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.
117static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
118
Steven Moreland02f736f2023-06-22 23:03:57 +0000119#if defined(__BIONIC__)
120static void FdTag(int fd, const void* old_addr, const void* new_addr) {
121 if (android_fdsan_exchange_owner_tag) {
122 uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
123 reinterpret_cast<uint64_t>(old_addr));
124 uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
125 reinterpret_cast<uint64_t>(new_addr));
126 android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
127 }
128}
129static void FdTagClose(int fd, const void* addr) {
130 if (android_fdsan_close_with_tag) {
131 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_PARCEL,
132 reinterpret_cast<uint64_t>(addr));
133 android_fdsan_close_with_tag(fd, tag);
134 } else {
135 close(fd);
136 }
137}
138#else
139static void FdTag(int fd, const void* old_addr, const void* new_addr) {
140 (void)fd;
141 (void)old_addr;
142 (void)new_addr;
143}
144static void FdTagClose(int fd, const void* addr) {
145 (void)addr;
146 close(fd);
147}
148#endif
149
Jeff Brown13b16042014-11-11 16:44:25 -0800150enum {
151 BLOB_INPLACE = 0,
152 BLOB_ASHMEM_IMMUTABLE = 1,
153 BLOB_ASHMEM_MUTABLE = 2,
154};
155
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000156#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700157static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
158 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700159 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700160 case BINDER_TYPE_BINDER:
161 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000162 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800163 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 }
165 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700166 case BINDER_TYPE_HANDLE: {
167 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700168 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
170 b->incStrong(who);
171 }
172 return;
173 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700174 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000175 if (obj.cookie != 0) { // owned
176 FdTag(obj.handle, nullptr, who);
177 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 return;
179 }
180 }
181
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700182 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183}
184
Steven Morelandc673f1f2021-10-07 18:23:35 -0700185static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
186 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700187 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700188 case BINDER_TYPE_BINDER:
189 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000190 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800191 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700192 }
193 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700194 case BINDER_TYPE_HANDLE: {
195 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700196 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700197 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
198 b->decStrong(who);
199 }
200 return;
201 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 case BINDER_TYPE_FD: {
Steven Moreland02f736f2023-06-22 23:03:57 +0000203 // note: this path is not used when mOwner, so the tag is also released
204 // in 'closeFileDescriptors'
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800205 if (obj.cookie != 0) { // owned
Steven Moreland02f736f2023-06-22 23:03:57 +0000206 FdTagClose(obj.handle, who);
Adrian Rooscbf37262015-10-22 16:12:53 -0700207 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700208 return;
209 }
210 }
211
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700212 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000214#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700215
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700216static int toRawFd(const std::variant<unique_fd, borrowed_fd>& v) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000217 return std::visit([](const auto& fd) { return fd.get(); }, v);
218}
219
Frederick Mayled3c595a2022-05-09 23:02:54 +0000220Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
221 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
222}
223
Steven Moreland34b48cb2020-12-01 22:45:38 +0000224status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700225{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700226 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700227 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000228 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700229}
230
Steven Morelanda86a3562019-08-01 23:28:34 +0000231status_t Parcel::finishUnflattenBinder(
232 const sp<IBinder>& binder, sp<IBinder>* out) const
233{
234 int32_t stability;
235 status_t status = readInt32(&stability);
236 if (status != OK) return status;
237
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000238 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
239 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000240 if (status != OK) return status;
241
242 *out = binder;
243 return OK;
244}
245
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000246#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000247static constexpr inline int schedPolicyMask(int policy, int priority) {
248 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
249}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000250#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000251
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500252status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
253 BBinder* local = nullptr;
254 if (binder) local = binder->localBinder();
255 if (local) local->setParceled();
256
Frederick Mayled3c595a2022-05-09 23:02:54 +0000257 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000258 if (binder) {
259 status_t status = writeInt32(1); // non-null
260 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700261 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000262 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000263 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
264 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000265 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700266 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000267 if (status != OK) return status;
268 } else {
269 status_t status = writeInt32(0); // null
270 if (status != OK) return status;
271 }
272 return finishFlattenBinder(binder);
273 }
274
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000275#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700277
Steven Morelandbf1915b2020-07-16 22:43:02 +0000278 int schedBits = 0;
279 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
280 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700281 }
282
Yi Kong91635562018-06-07 14:38:36 -0700283 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 if (!local) {
285 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700286 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000287 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 } else {
289 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700290 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000291 return INVALID_OPERATION;
292 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700293 }
Steven Moreland99157622021-09-13 16:27:34 -0700294 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700295 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800296 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000297 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800299 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000301 int policy = local->getMinSchedulerPolicy();
302 int priority = local->getMinSchedulerPriority();
303
304 if (policy != 0 || priority != 0) {
305 // override value, since it is set explicitly
306 schedBits = schedPolicyMask(policy, priority);
307 }
Steven Moreland49c27532022-03-01 10:21:07 +0000308 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800309 if (local->isRequestingSid()) {
310 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
311 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000312 if (local->isInheritRt()) {
313 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
314 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700315 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800316 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
317 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700318 }
319 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700320 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000321 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800322 obj.binder = 0;
323 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700325
Steven Morelandbf1915b2020-07-16 22:43:02 +0000326 obj.flags |= schedBits;
327
Steven Moreland34b48cb2020-12-01 22:45:38 +0000328 status_t status = writeObject(obj, false);
329 if (status != OK) return status;
330
331 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000332#else // BINDER_WITH_KERNEL_IPC
333 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
334 return INVALID_OPERATION;
335#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700336}
337
Steven Morelanda86a3562019-08-01 23:28:34 +0000338status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700339{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000340 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700341 int32_t isPresent;
342 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000343 if (status != OK) return status;
344
345 sp<IBinder> binder;
346
Steven Moreland5623d1a2021-09-10 15:45:34 -0700347 if (isPresent & 1) {
348 uint64_t addr;
349 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000350 if (status_t status =
351 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
352 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000353 status != OK)
354 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000355 if (status_t status =
356 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
357 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700358 status != OK)
359 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000360 }
361
362 return finishUnflattenBinder(binder, out);
363 }
364
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000365#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000366 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700369 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000370 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000371 sp<IBinder> binder =
372 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000373 return finishUnflattenBinder(binder, out);
374 }
375 case BINDER_TYPE_HANDLE: {
376 sp<IBinder> binder =
377 ProcessState::self()->getStrongProxyForHandle(flat->handle);
378 return finishUnflattenBinder(binder, out);
379 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700380 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 }
382 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000383#else // BINDER_WITH_KERNEL_IPC
384 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
385 return INVALID_OPERATION;
386#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700387}
388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700389// ---------------------------------------------------------------------------
390
391Parcel::Parcel()
392{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800393 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394 initState();
395}
396
397Parcel::~Parcel()
398{
399 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800400 LOG_ALLOC("Parcel %p: destroyed", this);
401}
402
403size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600404 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800405}
406
407size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600408 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409}
410
411const uint8_t* Parcel::data() const
412{
413 return mData;
414}
415
416size_t Parcel::dataSize() const
417{
418 return (mDataSize > mDataPos ? mDataSize : mDataPos);
419}
420
Pawan Waghd886a442023-01-21 02:16:38 +0000421size_t Parcel::dataBufferSize() const {
422 return mDataSize;
423}
424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425size_t Parcel::dataAvail() const
426{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700427 size_t result = dataSize() - dataPosition();
428 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700429 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700430 }
431 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700432}
433
434size_t Parcel::dataPosition() const
435{
436 return mDataPos;
437}
438
439size_t Parcel::dataCapacity() const
440{
441 return mDataCapacity;
442}
443
444status_t Parcel::setDataSize(size_t size)
445{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700446 if (size > INT32_MAX) {
447 // don't accept size_t values which may have come from an
448 // inadvertent conversion from a negative int.
449 return BAD_VALUE;
450 }
451
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700452 status_t err;
453 err = continueWrite(size);
454 if (err == NO_ERROR) {
455 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700456 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700457 }
458 return err;
459}
460
461void Parcel::setDataPosition(size_t pos) const
462{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700463 if (pos > INT32_MAX) {
464 // don't accept size_t values which may have come from an
465 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700466 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700467 }
468
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000470 if (const auto* kernelFields = maybeKernelFields()) {
471 kernelFields->mNextObjectHint = 0;
472 kernelFields->mObjectsSorted = false;
473 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700474}
475
476status_t Parcel::setDataCapacity(size_t size)
477{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700478 if (size > INT32_MAX) {
479 // don't accept size_t values which may have come from an
480 // inadvertent conversion from a negative int.
481 return BAD_VALUE;
482 }
483
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700484 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700485 return NO_ERROR;
486}
487
488status_t Parcel::setData(const uint8_t* buffer, size_t len)
489{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700490 if (len > INT32_MAX) {
491 // don't accept size_t values which may have come from an
492 // inadvertent conversion from a negative int.
493 return BAD_VALUE;
494 }
495
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 status_t err = restartWrite(len);
497 if (err == NO_ERROR) {
498 memcpy(const_cast<uint8_t*>(data()), buffer, len);
499 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000500 if (auto* kernelFields = maybeKernelFields()) {
501 kernelFields->mFdsKnown = false;
502 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700503 }
504 return err;
505}
506
Frederick Mayled3c595a2022-05-09 23:02:54 +0000507status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
508 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700509 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
510 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000511 return BAD_TYPE;
512 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000513 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
514 ALOGE("Cannot append Parcels from different sessions");
515 return BAD_TYPE;
516 }
Steven Moreland67753c32021-04-02 18:45:19 +0000517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000519 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700520 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521
522 if (len == 0) {
523 return NO_ERROR;
524 }
525
Nick Kralevichb6b14232015-04-02 09:36:02 -0700526 if (len > INT32_MAX) {
527 // don't accept size_t values which may have come from an
528 // inadvertent conversion from a negative int.
529 return BAD_VALUE;
530 }
531
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700532 // range checks against the source parcel size
533 if ((offset > parcel->mDataSize)
534 || (len > parcel->mDataSize)
535 || (offset + len > parcel->mDataSize)) {
536 return BAD_VALUE;
537 }
538
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700539 if ((mDataSize+len) > mDataCapacity) {
540 // grow data
541 err = growData(len);
542 if (err != NO_ERROR) {
543 return err;
544 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700545 }
546
547 // append data
548 memcpy(mData + mDataPos, data + offset, len);
549 mDataPos += len;
550 mDataSize += len;
551
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400552 err = NO_ERROR;
553
Frederick Mayled3c595a2022-05-09 23:02:54 +0000554 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000555#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000556 auto* otherKernelFields = parcel->maybeKernelFields();
557 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
558
559 const binder_size_t* objects = otherKernelFields->mObjects;
560 size_t size = otherKernelFields->mObjectsSize;
561 // Count objects in range
562 int firstIndex = -1, lastIndex = -2;
563 for (int i = 0; i < (int)size; i++) {
564 size_t off = objects[i];
565 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
566 if (firstIndex == -1) {
567 firstIndex = i;
568 }
569 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700570 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000572 int numObjects = lastIndex - firstIndex + 1;
573 if (numObjects > 0) {
574 const sp<ProcessState> proc(ProcessState::self());
575 // grow objects
576 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
577 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
578 return NO_MEMORY; // overflow
579 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
580 return NO_MEMORY; // overflow
581 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
582 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
583 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
584 newSize * sizeof(binder_size_t));
585 if (objects == (binder_size_t*)nullptr) {
586 return NO_MEMORY;
587 }
588 kernelFields->mObjects = objects;
589 kernelFields->mObjectsCapacity = newSize;
590 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700591
Frederick Mayled3c595a2022-05-09 23:02:54 +0000592 // append and acquire objects
593 int idx = kernelFields->mObjectsSize;
594 for (int i = firstIndex; i <= lastIndex; i++) {
595 size_t off = objects[i] - offset + startPos;
596 kernelFields->mObjects[idx++] = off;
597 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700598
Frederick Mayled3c595a2022-05-09 23:02:54 +0000599 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700600
Frederick Mayled3c595a2022-05-09 23:02:54 +0000601 if (flat->hdr.type == BINDER_TYPE_FD) {
602 // If this is a file descriptor, we need to dup it so the
603 // new Parcel now owns its own fd, and can declare that we
604 // officially know we have fds.
605 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
606 flat->cookie = 1;
607 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
608 if (!mAllowFds) {
609 err = FDS_NOT_ALLOWED;
610 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400611 }
Steven Moreland02f736f2023-06-22 23:03:57 +0000612
613 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700614 }
615 }
Andrei Homescu88012462022-07-07 04:30:05 +0000616#else
617 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
618 return INVALID_OPERATION;
619#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000620 } else {
621 auto* rpcFields = maybeRpcFields();
622 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
623 auto* otherRpcFields = parcel->maybeRpcFields();
624 if (otherRpcFields == nullptr) {
625 return BAD_TYPE;
626 }
627 if (rpcFields->mSession != otherRpcFields->mSession) {
628 return BAD_TYPE;
629 }
630
631 const size_t savedDataPos = mDataPos;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000632 auto scopeGuard = make_scope_guard([&]() { mDataPos = savedDataPos; });
Frederick Mayle69a0c992022-05-26 20:38:39 +0000633
634 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
635 if (otherRpcFields->mFds != nullptr) {
636 if (rpcFields->mFds == nullptr) {
637 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
638 }
639 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
640 }
641 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
642 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
643 if (offset <= objPos && objPos < offset + len) {
644 size_t newDataPos = objPos - offset + startPos;
645 rpcFields->mObjectPositions.push_back(newDataPos);
646
647 mDataPos = newDataPos;
648 int32_t objectType;
649 if (status_t status = readInt32(&objectType); status != OK) {
650 return status;
651 }
652 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
653 continue;
654 }
655
656 if (!mAllowFds) {
657 return FDS_NOT_ALLOWED;
658 }
659
660 // Read FD, duplicate, and add to list.
661 int32_t fdIndex;
662 if (status_t status = readInt32(&fdIndex); status != OK) {
663 return status;
664 }
Andrei Homescufc221502022-10-08 03:51:17 +0000665 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000666 // To match kernel binder behavior, we always dup, even if the
667 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000668 int newFd = -1;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000669 if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) {
Andrei Homescufc221502022-10-08 03:51:17 +0000670 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
671 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700672 rpcFields->mFds->emplace_back(unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000673 // Fixup the index in the data.
674 mDataPos = newDataPos + 4;
675 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
676 return status;
677 }
678 }
679 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700680 }
681
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400682 return err;
683}
684
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700685int Parcel::compareData(const Parcel& other) {
686 size_t size = dataSize();
687 if (size != other.dataSize()) {
688 return size < other.dataSize() ? -1 : 1;
689 }
690 return memcmp(data(), other.data(), size);
691}
692
Bernardo Rufino897b1652021-10-08 10:30:20 +0100693status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
694 size_t len, int* result) const {
695 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
696 // Don't accept size_t values which may have come from an inadvertent conversion from a
697 // negative int.
698 return BAD_VALUE;
699 }
700 size_t thisLimit;
701 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
702 return BAD_VALUE;
703 }
704 size_t otherLimit;
705 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
706 return BAD_VALUE;
707 }
708 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
709 return NO_ERROR;
710}
711
Jeff Brown13b16042014-11-11 16:44:25 -0800712bool Parcel::allowFds() const
713{
714 return mAllowFds;
715}
716
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700717bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400718{
719 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700720 if (!allowFds) {
721 mAllowFds = false;
722 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400723 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700724}
725
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700726void Parcel::restoreAllowFds(bool lastValue)
727{
728 mAllowFds = lastValue;
729}
730
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731bool Parcel::hasFileDescriptors() const
732{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000733 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000734 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000735 }
736 auto* kernelFields = maybeKernelFields();
737 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700738 scanForFds();
739 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000740 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700741}
742
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000743std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
744 std::vector<sp<IBinder>> ret;
745
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000746#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000747 const auto* kernelFields = maybeKernelFields();
748 if (kernelFields == nullptr) {
749 return ret;
750 }
751
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000752 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000753 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
754 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000755 const flat_binder_object* flat =
756 reinterpret_cast<const flat_binder_object*>(mData + offset);
757 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
758
759 setDataPosition(offset);
760
761 sp<IBinder> binder = readStrongBinder();
762 if (binder != nullptr) ret.push_back(binder);
763 }
764
765 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000766#endif // BINDER_WITH_KERNEL_IPC
767
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000768 return ret;
769}
770
771std::vector<int> Parcel::debugReadAllFileDescriptors() const {
772 std::vector<int> ret;
773
Frederick Mayle69a0c992022-05-26 20:38:39 +0000774 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000775#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000776 size_t initPosition = dataPosition();
777 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
778 binder_size_t offset = kernelFields->mObjects[i];
779 const flat_binder_object* flat =
780 reinterpret_cast<const flat_binder_object*>(mData + offset);
781 if (flat->hdr.type != BINDER_TYPE_FD) continue;
782
783 setDataPosition(offset);
784
785 int fd = readFileDescriptor();
786 LOG_ALWAYS_FATAL_IF(fd == -1);
787 ret.push_back(fd);
788 }
789 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000790#else
791 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
792#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000793 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
794 for (const auto& fd : *rpcFields->mFds) {
795 ret.push_back(toRawFd(fd));
796 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000797 }
798
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000799 return ret;
800}
801
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100802status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100803 if (len > INT32_MAX || offset > INT32_MAX) {
804 // Don't accept size_t values which may have come from an inadvertent conversion from a
805 // negative int.
806 return BAD_VALUE;
807 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100808 size_t limit;
809 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100810 return BAD_VALUE;
811 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100812 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000813 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000814#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000815 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
816 size_t pos = kernelFields->mObjects[i];
817 if (pos < offset) continue;
818 if (pos + sizeof(flat_binder_object) > offset + len) {
819 if (kernelFields->mObjectsSorted) {
820 break;
821 } else {
822 continue;
823 }
824 }
825 const flat_binder_object* flat =
826 reinterpret_cast<const flat_binder_object*>(mData + pos);
827 if (flat->hdr.type == BINDER_TYPE_FD) {
828 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000829 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100831 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000832#else
833 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
834 return INVALID_OPERATION;
835#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000836 } else if (const auto* rpcFields = maybeRpcFields()) {
837 for (uint32_t pos : rpcFields->mObjectPositions) {
838 if (offset <= pos && pos < limit) {
839 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
840 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
841 *result = true;
842 break;
843 }
844 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100845 }
846 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100847 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100848}
849
Steven Morelandf183fdd2020-10-27 00:12:12 +0000850void Parcel::markSensitive() const
851{
852 mDeallocZero = true;
853}
854
Steven Moreland5553ac42020-11-11 02:14:45 +0000855void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000856 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
857
Steven Moreland5553ac42020-11-11 02:14:45 +0000858 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700859 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000860 }
861}
862
Steven Morelandc9939062021-05-05 17:57:41 +0000863void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000864 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
865 "format must be set before data is written OR on IPC data");
866
Frederick Mayled3c595a2022-05-09 23:02:54 +0000867 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000868}
869
870bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000871 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000872}
873
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000874void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000875 auto* kernelFields = maybeKernelFields();
876 if (kernelFields == nullptr) {
877 return;
878 }
879
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000880 // Only update the request headers once. We only want to point
881 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000882 if (!kernelFields->mRequestHeaderPresent) {
883 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
884 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000885 }
886}
887
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000888#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandb6c7e222021-02-18 19:20:14 +0000889#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700890constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700891#elif defined(__ANDROID_RECOVERY__)
892constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700893#else
894constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
895#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000896#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700897
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700898// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700899status_t Parcel::writeInterfaceToken(const String16& interface)
900{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000901 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000902}
903
904status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000905 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000906#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000907 const IPCThreadState* threadState = IPCThreadState::self();
908 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
909 updateWorkSourceRequestHeaderPosition();
910 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
911 : IPCThreadState::kUnsetWorkSource);
912 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000913#else // BINDER_WITH_KERNEL_IPC
914 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
915 return INVALID_OPERATION;
916#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000917 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000918
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700919 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000920 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921}
922
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000923bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
924{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000925 auto* kernelFields = maybeKernelFields();
926 if (kernelFields == nullptr) {
927 return false;
928 }
929 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000930 return false;
931 }
932
933 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000934 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000935 status_t err = writeInt32(uid);
936 setDataPosition(initialPosition);
937 return err == NO_ERROR;
938}
939
Steven Morelandf1b1e492019-05-06 15:05:13 -0700940uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000941{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000942 auto* kernelFields = maybeKernelFields();
943 if (kernelFields == nullptr) {
944 return false;
945 }
946 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000947 return IPCThreadState::kUnsetWorkSource;
948 }
949
950 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000951 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000952 uid_t uid = readInt32();
953 setDataPosition(initialPosition);
954 return uid;
955}
956
Mathias Agopian83c04462009-05-22 19:00:22 -0700957bool Parcel::checkInterface(IBinder* binder) const
958{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700959 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700960}
961
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700962bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700963 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700964{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000965 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700966}
967
968bool Parcel::enforceInterface(const char16_t* interface,
969 size_t len,
970 IPCThreadState* threadState) const
971{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000972 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000973#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000974 // StrictModePolicy.
975 int32_t strictPolicy = readInt32();
976 if (threadState == nullptr) {
977 threadState = IPCThreadState::self();
978 }
979 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
980 // For one-way calls, the callee is running entirely
981 // disconnected from the caller, so disable StrictMode entirely.
982 // Not only does disk/network usage not impact the caller, but
983 // there's no way to communicate back violations anyway.
984 threadState->setStrictModePolicy(0);
985 } else {
986 threadState->setStrictModePolicy(strictPolicy);
987 }
988 // WorkSource.
989 updateWorkSourceRequestHeaderPosition();
990 int32_t workSource = readInt32();
991 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
992 // vendor header
993 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000994
995 // fuzzers skip this check, because it is for protecting the underlying ABI, but
996 // we don't want it to reduce our coverage
997 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000998 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
999 header);
1000 return false;
1001 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001002#else // BINDER_WITH_KERNEL_IPC
1003 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1004 (void)threadState;
1005 return false;
1006#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -07001007 }
Steven Moreland3af936a2021-03-26 03:05:38 +00001008
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +01001009 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -07001010 size_t parcel_interface_len;
1011 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
1012 if (len == parcel_interface_len &&
1013 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001014 return true;
1015 } else {
Steven Morelande99d8822023-06-02 21:56:39 +00001016 if (mServiceFuzzing) {
1017 // ignore. Theoretically, this could cause a few false positives, because
1018 // people could assume things about getInterfaceDescriptor if they pass
1019 // this point, but it would be extremely fragile. It's more important that
1020 // we fuzz with the above things read from the Parcel.
1021 return true;
1022 } else {
Steven Moreland3a667492023-06-02 21:41:39 +00001023 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001024 String8(interface, len).c_str(),
1025 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +00001026 return false;
Steven Moreland3a667492023-06-02 21:41:39 +00001027 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -07001029}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001030
Pawan Wagh104654a2022-11-15 21:18:42 +00001031void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1032 mEnforceNoDataAvail = enforceNoDataAvail;
1033}
1034
Steven Moreland3a667492023-06-02 21:41:39 +00001035void Parcel::setServiceFuzzing() {
1036 mServiceFuzzing = true;
1037}
1038
Steven Moreland418914a2023-06-28 21:47:14 +00001039bool Parcel::isServiceFuzzing() const {
1040 return mServiceFuzzing;
1041}
1042
Jooyung Hand23f9502021-12-23 14:39:57 +09001043binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001044 if (!mEnforceNoDataAvail) {
1045 return binder::Status::ok();
1046 }
1047
Jooyung Hand23f9502021-12-23 14:39:57 +09001048 const auto n = dataAvail();
1049 if (n == 0) {
1050 return binder::Status::ok();
1051 }
1052 return binder::Status::
1053 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1054 String8::format("Parcel data not fully consumed, unread size: %zu",
1055 n));
1056}
1057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001058size_t Parcel::objectsCount() const
1059{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001060 if (const auto* kernelFields = maybeKernelFields()) {
1061 return kernelFields->mObjectsSize;
1062 }
1063 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001064}
1065
1066status_t Parcel::errorCheck() const
1067{
1068 return mError;
1069}
1070
1071void Parcel::setError(status_t err)
1072{
1073 mError = err;
1074}
1075
1076status_t Parcel::finishWrite(size_t len)
1077{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001078 if (len > INT32_MAX) {
1079 // don't accept size_t values which may have come from an
1080 // inadvertent conversion from a negative int.
1081 return BAD_VALUE;
1082 }
1083
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001084 //printf("Finish write of %d\n", len);
1085 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001086 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 if (mDataPos > mDataSize) {
1088 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001089 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001090 }
1091 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1092 return NO_ERROR;
1093}
1094
1095status_t Parcel::writeUnpadded(const void* data, size_t len)
1096{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001097 if (len > INT32_MAX) {
1098 // don't accept size_t values which may have come from an
1099 // inadvertent conversion from a negative int.
1100 return BAD_VALUE;
1101 }
1102
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001103 size_t end = mDataPos + len;
1104 if (end < mDataPos) {
1105 // integer overflow
1106 return BAD_VALUE;
1107 }
1108
1109 if (end <= mDataCapacity) {
1110restart_write:
1111 memcpy(mData+mDataPos, data, len);
1112 return finishWrite(len);
1113 }
1114
1115 status_t err = growData(len);
1116 if (err == NO_ERROR) goto restart_write;
1117 return err;
1118}
1119
1120status_t Parcel::write(const void* data, size_t len)
1121{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001122 if (len > INT32_MAX) {
1123 // don't accept size_t values which may have come from an
1124 // inadvertent conversion from a negative int.
1125 return BAD_VALUE;
1126 }
1127
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128 void* const d = writeInplace(len);
1129 if (d) {
1130 memcpy(d, data, len);
1131 return NO_ERROR;
1132 }
1133 return mError;
1134}
1135
1136void* Parcel::writeInplace(size_t len)
1137{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001138 if (len > INT32_MAX) {
1139 // don't accept size_t values which may have come from an
1140 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001141 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001142 }
1143
1144 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001145
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001146 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001147 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001148 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001149 }
1150
1151 if ((mDataPos+padded) <= mDataCapacity) {
1152restart_write:
1153 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1154 uint8_t* const data = mData+mDataPos;
1155
1156 // Need to pad at end?
1157 if (padded != len) {
1158#if BYTE_ORDER == BIG_ENDIAN
1159 static const uint32_t mask[4] = {
1160 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1161 };
1162#endif
1163#if BYTE_ORDER == LITTLE_ENDIAN
1164 static const uint32_t mask[4] = {
1165 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1166 };
1167#endif
1168 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1169 // *reinterpret_cast<void**>(data+padded-4));
1170 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1171 }
1172
1173 finishWrite(padded);
1174 return data;
1175 }
1176
1177 status_t err = growData(padded);
1178 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001179 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001180}
1181
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001182status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1183 const uint8_t* strData = (uint8_t*)str.data();
1184 const size_t strLen= str.length();
1185 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001186 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001187 return BAD_VALUE;
1188 }
1189
1190 status_t err = writeInt32(utf16Len);
1191 if (err) {
1192 return err;
1193 }
1194
1195 // Allocate enough bytes to hold our converted string and its terminating NULL.
1196 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1197 if (!dst) {
1198 return NO_MEMORY;
1199 }
1200
Sergio Girof4607432016-07-21 14:46:35 +01001201 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001202
1203 return NO_ERROR;
1204}
1205
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001206
Andy Hung49198cf2020-11-18 11:02:39 -08001207status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1208status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001209
Andy Hung49198cf2020-11-18 11:02:39 -08001210status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1211status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001212
Andy Hung49198cf2020-11-18 11:02:39 -08001213status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1214status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1215status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1216status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1217status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1218status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1219status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1220status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1221status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1222status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1223status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1224status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1225status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1226status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1227status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1228status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1229status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1230status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1231status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1232status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1233status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1234status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1235status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1236status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1237status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1238status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1239status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001240
Andy Hung49198cf2020-11-18 11:02:39 -08001241status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001242status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001243 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001244status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001245 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001246status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001247 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001248status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001249 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1250status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001251
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001252status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1253 return writeData(val);
1254}
1255status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1256 return writeData(val);
1257}
1258status_t Parcel::writeUniqueFileDescriptorVector(
1259 const std::unique_ptr<std::vector<unique_fd>>& val) {
1260 return writeData(val);
1261}
Andy Hung49198cf2020-11-18 11:02:39 -08001262
1263status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1264status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1265status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1266
1267status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1268
1269status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1270status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1271
1272status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1273status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1274
1275status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1276status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1277status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1278status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1279status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1280status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1281status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1282status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1283status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1284status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1285status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1286status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1287status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1288status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1289status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1290status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1291status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1292status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1293status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1294status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1295status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1296status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1297status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1298status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1299status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1300status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1301status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1302
1303status_t Parcel::readString16Vector(
1304 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1305status_t Parcel::readString16Vector(
1306 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1307status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1308status_t Parcel::readUtf8VectorFromUtf16Vector(
1309 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1310status_t Parcel::readUtf8VectorFromUtf16Vector(
1311 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1312status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1313
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001314status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1315 return readData(val);
1316}
1317status_t Parcel::readUniqueFileDescriptorVector(
1318 std::unique_ptr<std::vector<unique_fd>>* val) const {
1319 return readData(val);
1320}
1321status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1322 return readData(val);
1323}
Andy Hung49198cf2020-11-18 11:02:39 -08001324
1325status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1326status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1327status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1328
1329status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001330
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001331status_t Parcel::writeInt32(int32_t val)
1332{
Andreas Huber84a6d042009-08-17 13:33:27 -07001333 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001335
1336status_t Parcel::writeUint32(uint32_t val)
1337{
1338 return writeAligned(val);
1339}
1340
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001341status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001342 if (len > INT32_MAX) {
1343 // don't accept size_t values which may have come from an
1344 // inadvertent conversion from a negative int.
1345 return BAD_VALUE;
1346 }
1347
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001348 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001349 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001350 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001351 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001352 if (ret == NO_ERROR) {
1353 ret = write(val, len * sizeof(*val));
1354 }
1355 return ret;
1356}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001357status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001358 if (len > INT32_MAX) {
1359 // don't accept size_t values which may have come from an
1360 // inadvertent conversion from a negative int.
1361 return BAD_VALUE;
1362 }
1363
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001364 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001365 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001366 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001367 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001368 if (ret == NO_ERROR) {
1369 ret = write(val, len * sizeof(*val));
1370 }
1371 return ret;
1372}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001373
Casey Dahlind6848f52015-10-15 15:44:59 -07001374status_t Parcel::writeBool(bool val)
1375{
1376 return writeInt32(int32_t(val));
1377}
1378
1379status_t Parcel::writeChar(char16_t val)
1380{
1381 return writeInt32(int32_t(val));
1382}
1383
1384status_t Parcel::writeByte(int8_t val)
1385{
1386 return writeInt32(int32_t(val));
1387}
1388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389status_t Parcel::writeInt64(int64_t val)
1390{
Andreas Huber84a6d042009-08-17 13:33:27 -07001391 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001392}
1393
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001394status_t Parcel::writeUint64(uint64_t val)
1395{
1396 return writeAligned(val);
1397}
1398
Serban Constantinescuf683e012013-11-05 16:53:55 +00001399status_t Parcel::writePointer(uintptr_t val)
1400{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001401 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001402}
1403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404status_t Parcel::writeFloat(float val)
1405{
Andreas Huber84a6d042009-08-17 13:33:27 -07001406 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001407}
1408
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001409#if defined(__mips__) && defined(__mips_hard_float)
1410
1411status_t Parcel::writeDouble(double val)
1412{
1413 union {
1414 double d;
1415 unsigned long long ll;
1416 } u;
1417 u.d = val;
1418 return writeAligned(u.ll);
1419}
1420
1421#else
1422
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001423status_t Parcel::writeDouble(double val)
1424{
Andreas Huber84a6d042009-08-17 13:33:27 -07001425 return writeAligned(val);
1426}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001428#endif
1429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001430status_t Parcel::writeCString(const char* str)
1431{
1432 return write(str, strlen(str)+1);
1433}
1434
1435status_t Parcel::writeString8(const String8& str)
1436{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001437 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001438}
1439
1440status_t Parcel::writeString8(const char* str, size_t len)
1441{
1442 if (str == nullptr) return writeInt32(-1);
1443
Jeff Sharkey18220902020-11-05 08:36:20 -07001444 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001445 status_t err = writeInt32(len);
1446 if (err == NO_ERROR) {
1447 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1448 if (data) {
1449 memcpy(data, str, len);
1450 *reinterpret_cast<char*>(data+len) = 0;
1451 return NO_ERROR;
1452 }
1453 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001454 }
1455 return err;
1456}
1457
1458status_t Parcel::writeString16(const String16& str)
1459{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001460 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001461}
1462
1463status_t Parcel::writeString16(const char16_t* str, size_t len)
1464{
Yi Kong91635562018-06-07 14:38:36 -07001465 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001466
Jeff Sharkey18220902020-11-05 08:36:20 -07001467 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001468 status_t err = writeInt32(len);
1469 if (err == NO_ERROR) {
1470 len *= sizeof(char16_t);
1471 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1472 if (data) {
1473 memcpy(data, str, len);
1474 *reinterpret_cast<char16_t*>(data+len) = 0;
1475 return NO_ERROR;
1476 }
1477 err = mError;
1478 }
1479 return err;
1480}
1481
1482status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1483{
Steven Morelanda86a3562019-08-01 23:28:34 +00001484 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001485}
1486
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001487
Casey Dahlinb9872622015-11-25 15:09:45 -08001488status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1489 if (!parcelable) {
1490 return writeInt32(0);
1491 }
1492
1493 return writeParcelable(*parcelable);
1494}
1495
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001496#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001497status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001498{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001499 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001500 return BAD_TYPE;
1501
1502 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001503 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001504 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001506 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001507 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001509 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1510 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511
1512 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001513 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 return err;
1515 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001516 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001517 return err;
1518}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001519#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001520
Frederick Mayle69a0c992022-05-26 20:38:39 +00001521status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1522 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001523 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001524 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001525 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001526 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001527 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001528 }
1529 if (!mAllowFds) {
1530 return FDS_NOT_ALLOWED;
1531 }
1532 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1533 case RpcSession::FileDescriptorTransportMode::NONE: {
1534 return FDS_NOT_ALLOWED;
1535 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001536 case RpcSession::FileDescriptorTransportMode::UNIX:
1537 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001538 if (rpcFields->mFds == nullptr) {
1539 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1540 }
1541 size_t dataPos = mDataPos;
1542 if (dataPos > UINT32_MAX) {
1543 return NO_MEMORY;
1544 }
1545 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1546 return err;
1547 }
1548 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1549 return err;
1550 }
1551 rpcFields->mObjectPositions.push_back(dataPos);
1552 rpcFields->mFds->push_back(std::move(fdVariant));
1553 return OK;
1554 }
1555 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001556 }
1557
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001558#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001560 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001561 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001562 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001563 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001564 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001566#else // BINDER_WITH_KERNEL_IPC
1567 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1568 (void)fd;
1569 (void)takeOwnership;
1570 return INVALID_OPERATION;
1571#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001572}
1573
1574status_t Parcel::writeDupFileDescriptor(int fd)
1575{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001576 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001577 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001578 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001579 }
1580 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001581 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001582 close(dupFd);
1583 }
1584 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585}
1586
Dianne Hackborn1941a402016-08-29 12:30:43 -07001587status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1588{
1589 writeInt32(0);
1590 return writeFileDescriptor(fd, takeOwnership);
1591}
1592
Ryo Hashimotobf551892018-05-31 16:58:35 +09001593status_t Parcel::writeDupParcelFileDescriptor(int fd)
1594{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001595 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001596 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001597 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001598 }
1599 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1600 if (err != OK) {
1601 close(dupFd);
1602 }
1603 return err;
1604}
1605
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001606status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001607 return writeDupFileDescriptor(fd.get());
1608}
1609
Jeff Brown13b16042014-11-11 16:44:25 -08001610status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001611{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001612#ifdef BINDER_DISABLE_BLOB
1613 (void)len;
1614 (void)mutableCopy;
1615 (void)outBlob;
1616 return INVALID_OPERATION;
1617#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001618 if (len > INT32_MAX) {
1619 // don't accept size_t values which may have come from an
1620 // inadvertent conversion from a negative int.
1621 return BAD_VALUE;
1622 }
1623
Jeff Brown13b16042014-11-11 16:44:25 -08001624 status_t status;
1625 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001626 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001627 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001628 if (status) return status;
1629
1630 void* ptr = writeInplace(len);
1631 if (!ptr) return NO_MEMORY;
1632
Jeff Brown13b16042014-11-11 16:44:25 -08001633 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001634 return NO_ERROR;
1635 }
1636
Steve Block6807e592011-10-20 11:56:00 +01001637 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001638 int fd = ashmem_create_region("Parcel Blob", len);
1639 if (fd < 0) return NO_MEMORY;
1640
1641 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1642 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001643 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001644 } else {
Yi Kong91635562018-06-07 14:38:36 -07001645 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001646 if (ptr == MAP_FAILED) {
1647 status = -errno;
1648 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001649 if (!mutableCopy) {
1650 result = ashmem_set_prot_region(fd, PROT_READ);
1651 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001652 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001653 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001654 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001655 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001656 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001657 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001658 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001659 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001660 return NO_ERROR;
1661 }
1662 }
1663 }
1664 }
1665 ::munmap(ptr, len);
1666 }
1667 ::close(fd);
1668 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001669#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001670}
1671
Jeff Brown13b16042014-11-11 16:44:25 -08001672status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1673{
1674 // Must match up with what's done in writeBlob.
1675 if (!mAllowFds) return FDS_NOT_ALLOWED;
1676 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1677 if (status) return status;
1678 return writeDupFileDescriptor(fd);
1679}
1680
Mathias Agopiane1424282013-07-29 21:24:40 -07001681status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001682{
1683 status_t err;
1684
1685 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001686 const size_t len = val.getFlattenedSize();
1687 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001688
Andrei Homescu1519b982022-06-09 02:04:44 +00001689 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001690 // don't accept size_t values which may have come from an
1691 // inadvertent conversion from a negative int.
1692 return BAD_VALUE;
1693 }
1694
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001695 err = this->writeInt32(len);
1696 if (err) return err;
1697
1698 err = this->writeInt32(fd_count);
1699 if (err) return err;
1700
1701 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001702 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001703 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001704 return BAD_VALUE;
1705
Yi Kong91635562018-06-07 14:38:36 -07001706 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001707 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001708 fds = new (std::nothrow) int[fd_count];
1709 if (fds == nullptr) {
1710 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1711 return BAD_VALUE;
1712 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001713 }
1714
1715 err = val.flatten(buf, len, fds, fd_count);
1716 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1717 err = this->writeDupFileDescriptor( fds[i] );
1718 }
1719
1720 if (fd_count) {
1721 delete [] fds;
1722 }
1723
1724 return err;
1725}
1726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1728{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001729 auto* kernelFields = maybeKernelFields();
1730 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1731
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001732#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001733 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001734 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001735 if (enoughData && enoughObjects) {
1736restart_write:
1737 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001738
Christopher Tate98e67d32015-06-03 18:44:15 -07001739 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001740 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001741 if (!mAllowFds) {
1742 // fail before modifying our object index
1743 return FDS_NOT_ALLOWED;
1744 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001745 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001746 }
1747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001749 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001750 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001751 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001752 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001753 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001754
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001755 return finishWrite(sizeof(flat_binder_object));
1756 }
1757
1758 if (!enoughData) {
1759 const status_t err = growData(sizeof(val));
1760 if (err != NO_ERROR) return err;
1761 }
1762 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001763 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1764 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1765 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001766 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001767 binder_size_t* objects =
1768 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001769 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001770 kernelFields->mObjects = objects;
1771 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001772 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001773
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001774 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001775#else // BINDER_WITH_KERNEL_IPC
1776 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1777 (void)val;
1778 (void)nullMetaData;
1779 return INVALID_OPERATION;
1780#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001781}
1782
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001783status_t Parcel::writeNoException()
1784{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001785 binder::Status status;
1786 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001787}
1788
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001789status_t Parcel::validateReadData(size_t upperBound) const
1790{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001791 const auto* kernelFields = maybeKernelFields();
1792 if (kernelFields == nullptr) {
1793 // Can't validate RPC Parcel reads because the location of binder
1794 // objects is unknown.
1795 return OK;
1796 }
1797
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001798#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001799 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001800 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1801 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001802 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001803 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1804 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001805 // For some reason the current read position is greater than the next object
1806 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001807 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001808 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001809 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001810 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001811 if (!mServiceFuzzing) {
1812 ALOGE("Attempt to read from protected data in Parcel %p", this);
1813 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001814 return PERMISSION_DENIED;
1815 }
1816 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001817 } while (nextObject < kernelFields->mObjectsSize &&
1818 upperBound > kernelFields->mObjects[nextObject]);
1819 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001820 }
1821 return NO_ERROR;
1822 }
1823 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001824 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001825 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001826 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001827 prevObj--;
1828 if(*prevObj > *currObj) {
1829 goto data_unsorted;
1830 }
1831 currObj--;
1832 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001833 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001834 goto data_sorted;
1835
1836data_unsorted:
1837 // Insertion Sort mObjects
1838 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1839 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001840 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1841 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001842 binder_size_t temp = *iter0;
1843 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001844 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001845 *(iter1 + 1) = *iter1;
1846 iter1--;
1847 }
1848 *(iter1 + 1) = temp;
1849 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001850 kernelFields->mNextObjectHint = 0;
1851 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001852 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001853#else // BINDER_WITH_KERNEL_IPC
1854 (void)upperBound;
1855 return NO_ERROR;
1856#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001857}
1858
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859status_t Parcel::read(void* outData, size_t len) const
1860{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001861 if (len > INT32_MAX) {
1862 // don't accept size_t values which may have come from an
1863 // inadvertent conversion from a negative int.
1864 return BAD_VALUE;
1865 }
1866
1867 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1868 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001869 const auto* kernelFields = maybeKernelFields();
1870 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001871 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001872 if(err != NO_ERROR) {
1873 // Still increment the data position by the expected length
1874 mDataPos += pad_size(len);
1875 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1876 return err;
1877 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001878 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001879 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001880 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001881 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001882 return NO_ERROR;
1883 }
1884 return NOT_ENOUGH_DATA;
1885}
1886
1887const void* Parcel::readInplace(size_t len) const
1888{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001889 if (len > INT32_MAX) {
1890 // don't accept size_t values which may have come from an
1891 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001892 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001893 }
1894
1895 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1896 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001897 const auto* kernelFields = maybeKernelFields();
1898 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001899 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001900 if(err != NO_ERROR) {
1901 // Still increment the data position by the expected length
1902 mDataPos += pad_size(len);
1903 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001904 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001905 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001906 }
1907
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001908 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001909 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001910 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001911 return data;
1912 }
Yi Kong91635562018-06-07 14:38:36 -07001913 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001914}
1915
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001916status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1917 if (status_t status = readInt32(size); status != OK) return status;
1918 if (*size < 0) return OK; // may be null, client to handle
1919
1920 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1921
1922 // approximation, can't know max element size (e.g. if it makes heap
1923 // allocations)
1924 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1925 int32_t allocationSize;
1926 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1927
1928 // High limit of 1MB since something this big could never be returned. Could
1929 // probably scope this down, but might impact very specific usecases.
1930 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1931
1932 if (allocationSize >= kMaxAllocationSize) {
1933 return NO_MEMORY;
1934 }
1935
1936 return OK;
1937}
1938
Andreas Huber84a6d042009-08-17 13:33:27 -07001939template<class T>
1940status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001941 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001942 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001943
1944 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001945 const auto* kernelFields = maybeKernelFields();
1946 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001947 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001948 if(err != NO_ERROR) {
1949 // Still increment the data position by the expected length
1950 mDataPos += sizeof(T);
1951 return err;
1952 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001953 }
1954
Andrei Homescue33238e2022-03-29 22:50:00 +00001955 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001956 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001957 return NO_ERROR;
1958 } else {
1959 return NOT_ENOUGH_DATA;
1960 }
1961}
1962
Andreas Huber84a6d042009-08-17 13:33:27 -07001963template<class T>
1964T Parcel::readAligned() const {
1965 T result;
1966 if (readAligned(&result) != NO_ERROR) {
1967 result = 0;
1968 }
1969
1970 return result;
1971}
1972
1973template<class T>
1974status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001975 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001976 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001977
1978 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1979restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001980 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001981 return finishWrite(sizeof(val));
1982 }
1983
1984 status_t err = growData(sizeof(val));
1985 if (err == NO_ERROR) goto restart_write;
1986 return err;
1987}
1988
1989status_t Parcel::readInt32(int32_t *pArg) const
1990{
1991 return readAligned(pArg);
1992}
1993
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994int32_t Parcel::readInt32() const
1995{
Andreas Huber84a6d042009-08-17 13:33:27 -07001996 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001997}
1998
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001999status_t Parcel::readUint32(uint32_t *pArg) const
2000{
2001 return readAligned(pArg);
2002}
2003
2004uint32_t Parcel::readUint32() const
2005{
2006 return readAligned<uint32_t>();
2007}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008
2009status_t Parcel::readInt64(int64_t *pArg) const
2010{
Andreas Huber84a6d042009-08-17 13:33:27 -07002011 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002012}
2013
2014
2015int64_t Parcel::readInt64() const
2016{
Andreas Huber84a6d042009-08-17 13:33:27 -07002017 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002018}
2019
Ronghua Wu2d13afd2015-03-16 11:11:07 -07002020status_t Parcel::readUint64(uint64_t *pArg) const
2021{
2022 return readAligned(pArg);
2023}
2024
2025uint64_t Parcel::readUint64() const
2026{
2027 return readAligned<uint64_t>();
2028}
2029
Serban Constantinescuf683e012013-11-05 16:53:55 +00002030status_t Parcel::readPointer(uintptr_t *pArg) const
2031{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002032 status_t ret;
2033 binder_uintptr_t ptr;
2034 ret = readAligned(&ptr);
2035 if (!ret)
2036 *pArg = ptr;
2037 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002038}
2039
2040uintptr_t Parcel::readPointer() const
2041{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002042 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002043}
2044
2045
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046status_t Parcel::readFloat(float *pArg) const
2047{
Andreas Huber84a6d042009-08-17 13:33:27 -07002048 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002049}
2050
2051
2052float Parcel::readFloat() const
2053{
Andreas Huber84a6d042009-08-17 13:33:27 -07002054 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055}
2056
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002057#if defined(__mips__) && defined(__mips_hard_float)
2058
2059status_t Parcel::readDouble(double *pArg) const
2060{
2061 union {
2062 double d;
2063 unsigned long long ll;
2064 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002065 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002066 status_t status;
2067 status = readAligned(&u.ll);
2068 *pArg = u.d;
2069 return status;
2070}
2071
2072double Parcel::readDouble() const
2073{
2074 union {
2075 double d;
2076 unsigned long long ll;
2077 } u;
2078 u.ll = readAligned<unsigned long long>();
2079 return u.d;
2080}
2081
2082#else
2083
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084status_t Parcel::readDouble(double *pArg) const
2085{
Andreas Huber84a6d042009-08-17 13:33:27 -07002086 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087}
2088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002089double Parcel::readDouble() const
2090{
Andreas Huber84a6d042009-08-17 13:33:27 -07002091 return readAligned<double>();
2092}
2093
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002094#endif
2095
Casey Dahlind6848f52015-10-15 15:44:59 -07002096status_t Parcel::readBool(bool *pArg) const
2097{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002098 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002099 status_t ret = readInt32(&tmp);
2100 *pArg = (tmp != 0);
2101 return ret;
2102}
2103
2104bool Parcel::readBool() const
2105{
2106 return readInt32() != 0;
2107}
2108
2109status_t Parcel::readChar(char16_t *pArg) const
2110{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002111 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002112 status_t ret = readInt32(&tmp);
2113 *pArg = char16_t(tmp);
2114 return ret;
2115}
2116
2117char16_t Parcel::readChar() const
2118{
2119 return char16_t(readInt32());
2120}
2121
2122status_t Parcel::readByte(int8_t *pArg) const
2123{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002124 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002125 status_t ret = readInt32(&tmp);
2126 *pArg = int8_t(tmp);
2127 return ret;
2128}
2129
2130int8_t Parcel::readByte() const
2131{
2132 return int8_t(readInt32());
2133}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002135status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2136 size_t utf16Size = 0;
2137 const char16_t* src = readString16Inplace(&utf16Size);
2138 if (!src) {
2139 return UNEXPECTED_NULL;
2140 }
2141
2142 // Save ourselves the trouble, we're done.
2143 if (utf16Size == 0u) {
2144 str->clear();
2145 return NO_ERROR;
2146 }
2147
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002148 // Allow for closing '\0'
2149 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2150 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002151 return BAD_VALUE;
2152 }
2153 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002154 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002155 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002156 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2157 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002158 return NO_ERROR;
2159}
2160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161const char* Parcel::readCString() const
2162{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002163 if (mDataPos < mDataSize) {
2164 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2166 // is the string's trailing NUL within the parcel's valid bounds?
2167 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2168 if (eos) {
2169 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002170 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002171 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002172 return str;
2173 }
2174 }
Yi Kong91635562018-06-07 14:38:36 -07002175 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002176}
2177
2178String8 Parcel::readString8() const
2179{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002180 size_t len;
2181 const char* str = readString8Inplace(&len);
2182 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002183
2184 if (!mServiceFuzzing) {
2185 ALOGE("Reading a NULL string not supported here.");
2186 }
2187
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002188 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002189}
2190
2191status_t Parcel::readString8(String8* pArg) const
2192{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002193 size_t len;
2194 const char* str = readString8Inplace(&len);
2195 if (str) {
2196 pArg->setTo(str, len);
2197 return 0;
2198 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002199 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002200 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002201 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002202}
2203
2204const char* Parcel::readString8Inplace(size_t* outLen) const
2205{
2206 int32_t size = readInt32();
2207 // watch for potential int overflow from size+1
2208 if (size >= 0 && size < INT32_MAX) {
2209 *outLen = size;
2210 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002211 if (str != nullptr) {
2212 if (str[size] == '\0') {
2213 return str;
2214 }
2215 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002216 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002217 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002218 *outLen = 0;
2219 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220}
2221
2222String16 Parcel::readString16() const
2223{
2224 size_t len;
2225 const char16_t* str = readString16Inplace(&len);
2226 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002227
2228 if (!mServiceFuzzing) {
2229 ALOGE("Reading a NULL string not supported here.");
2230 }
2231
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 return String16();
2233}
2234
Casey Dahlinb9872622015-11-25 15:09:45 -08002235
Casey Dahlin451ff582015-10-19 18:12:18 -07002236status_t Parcel::readString16(String16* pArg) const
2237{
2238 size_t len;
2239 const char16_t* str = readString16Inplace(&len);
2240 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002241 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002242 return 0;
2243 } else {
2244 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002245 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002246 }
2247}
2248
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2250{
2251 int32_t size = readInt32();
2252 // watch for potential int overflow from size+1
2253 if (size >= 0 && size < INT32_MAX) {
2254 *outLen = size;
2255 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002256 if (str != nullptr) {
2257 if (str[size] == u'\0') {
2258 return str;
2259 }
2260 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 }
2262 }
2263 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002264 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002265}
2266
Casey Dahlinf0c13772015-10-27 18:33:56 -07002267status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2268{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002269 status_t status = readNullableStrongBinder(val);
2270 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002271 if (!mServiceFuzzing) {
2272 ALOGW("Expecting binder but got null!");
2273 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002274 status = UNEXPECTED_NULL;
2275 }
2276 return status;
2277}
2278
2279status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2280{
Steven Morelanda86a3562019-08-01 23:28:34 +00002281 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002282}
2283
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002284sp<IBinder> Parcel::readStrongBinder() const
2285{
2286 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002287 // Note that a lot of code in Android reads binders by hand with this
2288 // method, and that code has historically been ok with getting nullptr
2289 // back (while ignoring error codes).
2290 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 return val;
2292}
2293
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002294int32_t Parcel::readExceptionCode() const
2295{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002296 binder::Status status;
2297 status.readFromParcel(*this);
2298 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002299}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002300
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002301#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002302native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002303{
2304 int numFds, numInts;
2305 status_t err;
2306 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002307 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002308 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002309 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002310
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002311 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002312 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002313 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002314 }
2315
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002316 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002317 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002318 if (h->data[i] < 0) {
2319 for (int j = 0; j < i; j++) {
2320 close(h->data[j]);
2321 }
2322 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002323 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002324 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002325 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002326 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002327 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002328 native_handle_close(h);
2329 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002330 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002331 }
2332 return h;
2333}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002334#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002335
Frederick Mayle69a0c992022-05-26 20:38:39 +00002336int Parcel::readFileDescriptor() const {
2337 if (const auto* rpcFields = maybeRpcFields()) {
2338 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2339 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002340 if (!mServiceFuzzing) {
2341 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2342 "the object list",
2343 this, mDataPos);
2344 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002345 return BAD_TYPE;
2346 }
2347
2348 int32_t objectType = readInt32();
2349 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2350 return BAD_TYPE;
2351 }
2352
2353 int32_t fdIndex = readInt32();
2354 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2355 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2356 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2357 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2358 return BAD_VALUE;
2359 }
2360 return toRawFd(rpcFields->mFds->at(fdIndex));
2361 }
2362
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002363#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002364 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002365
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002366 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002367 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002371#else // BINDER_WITH_KERNEL_IPC
2372 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2373 return INVALID_OPERATION;
2374#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002375}
2376
Frederick Mayle69a0c992022-05-26 20:38:39 +00002377int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002378 int32_t hasComm = readInt32();
2379 int fd = readFileDescriptor();
2380 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002381 // detach (owned by the binder driver)
2382 int comm = readFileDescriptor();
2383
2384 // warning: this must be kept in sync with:
2385 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2386 enum ParcelFileDescriptorStatus {
2387 DETACHED = 2,
2388 };
2389
2390#if BYTE_ORDER == BIG_ENDIAN
2391 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2392#endif
2393#if BYTE_ORDER == LITTLE_ENDIAN
2394 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2395#endif
2396
2397 ssize_t written = TEMP_FAILURE_RETRY(
2398 ::write(comm, &message, sizeof(message)));
2399
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002400 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002401 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2402 written, strerror(errno));
2403 return BAD_TYPE;
2404 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002405 }
2406 return fd;
2407}
2408
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002409status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002410 int got = readFileDescriptor();
2411
2412 if (got == BAD_TYPE) {
2413 return BAD_TYPE;
2414 }
2415
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002416 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002417 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002418 return BAD_VALUE;
2419 }
2420
2421 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002422
2423 if (val->get() < 0) {
2424 return BAD_VALUE;
2425 }
2426
2427 return OK;
2428}
2429
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002430status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002431 int got = readParcelFileDescriptor();
2432
2433 if (got == BAD_TYPE) {
2434 return BAD_TYPE;
2435 }
2436
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002437 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002438 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002439 return BAD_VALUE;
2440 }
2441
2442 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002443
2444 if (val->get() < 0) {
2445 return BAD_VALUE;
2446 }
2447
2448 return OK;
2449}
Casey Dahlin06673e32015-11-23 13:24:23 -08002450
Jeff Brown5707dbf2011-09-23 21:17:56 -07002451status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2452{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002453#ifdef BINDER_DISABLE_BLOB
2454 (void)len;
2455 (void)outBlob;
2456 return INVALID_OPERATION;
2457#else
Jeff Brown13b16042014-11-11 16:44:25 -08002458 int32_t blobType;
2459 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002460 if (status) return status;
2461
Jeff Brown13b16042014-11-11 16:44:25 -08002462 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002463 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002464 const void* ptr = readInplace(len);
2465 if (!ptr) return BAD_VALUE;
2466
Jeff Brown13b16042014-11-11 16:44:25 -08002467 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002468 return NO_ERROR;
2469 }
2470
Steve Block6807e592011-10-20 11:56:00 +01002471 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002472 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002473 int fd = readFileDescriptor();
2474 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2475
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002476 if (!ashmem_valid(fd)) {
2477 ALOGE("invalid fd");
2478 return BAD_VALUE;
2479 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002480 int size = ashmem_get_size_region(fd);
2481 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002482 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002483 return BAD_VALUE;
2484 }
Yi Kong91635562018-06-07 14:38:36 -07002485 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002486 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002487 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002488
Jeff Brown13b16042014-11-11 16:44:25 -08002489 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002490 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002491#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002492}
2493
Mathias Agopiane1424282013-07-29 21:24:40 -07002494status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002495{
2496 // size
2497 const size_t len = this->readInt32();
2498 const size_t fd_count = this->readInt32();
2499
Andrei Homescu1519b982022-06-09 02:04:44 +00002500 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002501 // don't accept size_t values which may have come from an
2502 // inadvertent conversion from a negative int.
2503 return BAD_VALUE;
2504 }
2505
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002506 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002507 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002508 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002509 return BAD_VALUE;
2510
Yi Kong91635562018-06-07 14:38:36 -07002511 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002512 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002513 fds = new (std::nothrow) int[fd_count];
2514 if (fds == nullptr) {
2515 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2516 return BAD_VALUE;
2517 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002518 }
2519
2520 status_t err = NO_ERROR;
2521 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002522 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002523 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002524 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002525 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 -07002526 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2527 // Close all the file descriptors that were dup-ed.
2528 for (size_t j=0; j<i ;j++) {
2529 close(fds[j]);
2530 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002531 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002532 }
2533
2534 if (err == NO_ERROR) {
2535 err = val.unflatten(buf, len, fds, fd_count);
2536 }
2537
2538 if (fd_count) {
2539 delete [] fds;
2540 }
2541
2542 return err;
2543}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002544
2545#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2547{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002548 const auto* kernelFields = maybeKernelFields();
2549 if (kernelFields == nullptr) {
2550 return nullptr;
2551 }
2552
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 const size_t DPOS = mDataPos;
2554 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2555 const flat_binder_object* obj
2556 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2557 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002558 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002559 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 // the object list, so we don't want to check for it when
2561 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 return obj;
2564 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002565
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002567 binder_size_t* const OBJS = kernelFields->mObjects;
2568 const size_t N = kernelFields->mObjectsSize;
2569 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002575 // Start at the current hint position, looking for an object at
2576 // the current data position.
2577 if (opos < N) {
2578 while (opos < (N-1) && OBJS[opos] < DPOS) {
2579 opos++;
2580 }
2581 } else {
2582 opos = N-1;
2583 }
2584 if (OBJS[opos] == DPOS) {
2585 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002586 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002587 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002588 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002589 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 return obj;
2591 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002592
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593 // Look backwards for it...
2594 while (opos > 0 && OBJS[opos] > DPOS) {
2595 opos--;
2596 }
2597 if (OBJS[opos] == DPOS) {
2598 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002599 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002601 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002602 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 return obj;
2604 }
2605 }
Steven Moreland3a667492023-06-02 21:41:39 +00002606 if (!mServiceFuzzing) {
2607 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2608 "list",
2609 this, DPOS);
2610 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 }
Yi Kong91635562018-06-07 14:38:36 -07002612 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002613}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002614#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615
Frederick Mayled3c595a2022-05-09 23:02:54 +00002616void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002617 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002618#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002619 size_t i = kernelFields->mObjectsSize;
2620 if (i > 0) {
2621 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002622 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002623 while (i > 0) {
2624 i--;
2625 const flat_binder_object* flat =
2626 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2627 if (flat->hdr.type == BINDER_TYPE_FD) {
2628 // ALOGI("Closing fd: %ld", flat->handle);
Steven Moreland02f736f2023-06-22 23:03:57 +00002629 // FDs from the kernel are always owned
2630 FdTagClose(flat->handle, this);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002631 }
2632 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002633#else // BINDER_WITH_KERNEL_IPC
2634 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2635#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002636 } else if (auto* rpcFields = maybeRpcFields()) {
2637 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 }
2639}
2640
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002641uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002643 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644}
2645
2646size_t Parcel::ipcDataSize() const
2647{
2648 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2649}
2650
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002651uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002652{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002653 if (const auto* kernelFields = maybeKernelFields()) {
2654 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2655 }
2656 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002657}
2658
2659size_t Parcel::ipcObjectsCount() const
2660{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002661 if (const auto* kernelFields = maybeKernelFields()) {
2662 return kernelFields->mObjectsSize;
2663 }
2664 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002665}
2666
Frederick Mayled3c595a2022-05-09 23:02:54 +00002667void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2668 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002669 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2670 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2671
Steven Morelandceed9bb2020-12-17 01:01:06 +00002672 freeData();
2673
Frederick Mayled3c595a2022-05-09 23:02:54 +00002674 auto* kernelFields = maybeKernelFields();
2675 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2676
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002677 mData = const_cast<uint8_t*>(data);
2678 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002679 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2680 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002681 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002682
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002683#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002684 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002685 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2686 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002687 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002688 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002689 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002690 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002691 break;
2692 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002693 const flat_binder_object* flat
2694 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2695 uint32_t type = flat->hdr.type;
2696 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2697 type == BINDER_TYPE_FD)) {
2698 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2699 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002700 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002701 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002702 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002703 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2704 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002705
2706 // WARNING: callers of ipcSetDataReference need to make sure they
2707 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002708 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002709 break;
2710 }
Steven Moreland02f736f2023-06-22 23:03:57 +00002711 if (type == BINDER_TYPE_FD) {
2712 // FDs from the kernel are always owned
2713 FdTag(flat->handle, 0, this);
2714 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002715 minOffset = offset + sizeof(flat_binder_object);
2716 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002717 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002718#else // BINDER_WITH_KERNEL_IPC
2719 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2720 "Non-zero objects count passed to Parcel with kernel driver disabled");
2721#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002722}
2723
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002724status_t Parcel::rpcSetDataReference(
2725 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2726 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002727 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002728 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2729 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2730
2731 LOG_ALWAYS_FATAL_IF(session == nullptr);
2732
Frederick Mayle694e9732022-07-15 00:24:58 +00002733 if (objectTableSize != ancillaryFds.size()) {
2734 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2735 relFunc(data, dataSize, nullptr, 0);
2736 return BAD_VALUE;
2737 }
2738 for (size_t i = 0; i < objectTableSize; i++) {
2739 uint32_t minObjectEnd;
2740 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2741 minObjectEnd >= dataSize) {
2742 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2743 objectTable[i], dataSize);
2744 relFunc(data, dataSize, nullptr, 0);
2745 return BAD_VALUE;
2746 }
2747 }
2748
Frederick Mayled3c595a2022-05-09 23:02:54 +00002749 freeData();
2750 markForRpc(session);
2751
Frederick Mayle69a0c992022-05-26 20:38:39 +00002752 auto* rpcFields = maybeRpcFields();
2753 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2754
Frederick Mayled3c595a2022-05-09 23:02:54 +00002755 mData = const_cast<uint8_t*>(data);
2756 mDataSize = mDataCapacity = dataSize;
2757 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002758
Frederick Mayle69a0c992022-05-26 20:38:39 +00002759 rpcFields->mObjectPositions.reserve(objectTableSize);
2760 for (size_t i = 0; i < objectTableSize; i++) {
2761 rpcFields->mObjectPositions.push_back(objectTable[i]);
2762 }
2763 if (!ancillaryFds.empty()) {
2764 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002765 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002766 }
2767
2768 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002769}
2770
Pawan Wagh7063b522022-09-28 18:52:26 +00002771void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002773
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774 if (errorCheck() != NO_ERROR) {
2775 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002776 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002777 } else if (dataSize() > 0) {
2778 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002779 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002780#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002781 if (const auto* kernelFields = maybeKernelFields()) {
2782 const binder_size_t* OBJS = kernelFields->mObjects;
2783 const size_t N = objectsCount();
2784 for (size_t i = 0; i < N; i++) {
2785 const flat_binder_object* flat =
2786 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002787 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002788 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2789 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002790 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002791#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002792 } else {
2793 to << "NULL";
2794 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002795
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002796 to << ")";
2797}
2798
2799void Parcel::releaseObjects()
2800{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002801 auto* kernelFields = maybeKernelFields();
2802 if (kernelFields == nullptr) {
2803 return;
2804 }
2805
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002806#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002807 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002808 if (i == 0) {
2809 return;
2810 }
2811 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002812 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002813 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002814 while (i > 0) {
2815 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002816 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002817 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002818 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002819#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002820}
2821
2822void Parcel::acquireObjects()
2823{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002824 auto* kernelFields = maybeKernelFields();
2825 if (kernelFields == nullptr) {
2826 return;
2827 }
2828
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002829#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002830 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002831 if (i == 0) {
2832 return;
2833 }
2834 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002836 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002837 while (i > 0) {
2838 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002839 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002840 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002841 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002842#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002843}
2844
2845void Parcel::freeData()
2846{
2847 freeDataNoInit();
2848 initState();
2849}
2850
2851void Parcel::freeDataNoInit()
2852{
2853 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002854 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002855 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002856 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002857 // Close FDs before freeing, otherwise they will leak for kernel binder.
2858 closeFileDescriptors();
2859 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002860 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002861 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002862 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002864 if (mData) {
2865 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002866 gParcelGlobalAllocSize -= mDataCapacity;
2867 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002868 if (mDeallocZero) {
2869 zeroMemory(mData, mDataSize);
2870 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002871 free(mData);
2872 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002873 auto* kernelFields = maybeKernelFields();
2874 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002875 }
2876}
2877
2878status_t Parcel::growData(size_t len)
2879{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002880 if (len > INT32_MAX) {
2881 // don't accept size_t values which may have come from an
2882 // inadvertent conversion from a negative int.
2883 return BAD_VALUE;
2884 }
2885
Martijn Coenen93fe5182020-01-22 10:46:25 +01002886 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2887 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002888 size_t newSize = ((mDataSize+len)*3)/2;
2889 return (newSize <= mDataSize)
2890 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002891 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002892}
2893
Steven Morelandf183fdd2020-10-27 00:12:12 +00002894static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2895 if (!zero) {
2896 return (uint8_t*)realloc(data, newCapacity);
2897 }
2898 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2899 if (!newData) {
2900 return nullptr;
2901 }
2902
2903 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2904 zeroMemory(data, oldCapacity);
2905 free(data);
2906 return newData;
2907}
2908
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002909status_t Parcel::restartWrite(size_t desired)
2910{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002911 if (desired > INT32_MAX) {
2912 // don't accept size_t values which may have come from an
2913 // inadvertent conversion from a negative int.
2914 return BAD_VALUE;
2915 }
2916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002917 if (mOwner) {
2918 freeData();
2919 return continueWrite(desired);
2920 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002921
Steven Morelandf183fdd2020-10-27 00:12:12 +00002922 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002923 if (!data && desired > mDataCapacity) {
2924 mError = NO_MEMORY;
2925 return NO_MEMORY;
2926 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002928 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002929
Devin Moore4a0a55e2020-06-04 13:23:10 -07002930 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002931 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002932 if (mDataCapacity > desired) {
2933 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2934 } else {
2935 gParcelGlobalAllocSize += (desired - mDataCapacity);
2936 }
2937
Colin Cross83ec65e2015-12-08 17:15:50 -08002938 if (!mData) {
2939 gParcelGlobalAllocCount++;
2940 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002941 mData = data;
2942 mDataCapacity = desired;
2943 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002944
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002945 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002946 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2947 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2948
Frederick Mayled3c595a2022-05-09 23:02:54 +00002949 if (auto* kernelFields = maybeKernelFields()) {
2950 free(kernelFields->mObjects);
2951 kernelFields->mObjects = nullptr;
2952 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2953 kernelFields->mNextObjectHint = 0;
2954 kernelFields->mObjectsSorted = false;
2955 kernelFields->mHasFds = false;
2956 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002957 } else if (auto* rpcFields = maybeRpcFields()) {
2958 rpcFields->mObjectPositions.clear();
2959 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002960 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002961 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002962
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002963 return NO_ERROR;
2964}
2965
2966status_t Parcel::continueWrite(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
Frederick Mayled3c595a2022-05-09 23:02:54 +00002974 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002975 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002977 // If shrinking, first adjust for any objects that appear
2978 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002979 size_t objectsSize =
2980 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2981 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002982 if (desired == 0) {
2983 objectsSize = 0;
2984 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002985 if (kernelFields) {
2986 while (objectsSize > 0) {
2987 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2988 objectsSize--;
2989 }
2990 } else {
2991 while (objectsSize > 0) {
2992 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2993 objectsSize--;
2994 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002995 }
2996 }
2997 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002998
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002999 if (mOwner) {
3000 // If the size is going to zero, just release the owner's data.
3001 if (desired == 0) {
3002 freeData();
3003 return NO_ERROR;
3004 }
3005
3006 // If there is a different owner, we need to take
3007 // posession.
3008 uint8_t* data = (uint8_t*)malloc(desired);
3009 if (!data) {
3010 mError = NO_MEMORY;
3011 return NO_MEMORY;
3012 }
Yi Kong91635562018-06-07 14:38:36 -07003013 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003014
Frederick Mayle69a0c992022-05-26 20:38:39 +00003015 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07003016 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003017 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09003018 free(data);
3019
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003020 mError = NO_MEMORY;
3021 return NO_MEMORY;
3022 }
3023
3024 // Little hack to only acquire references on objects
3025 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00003026 size_t oldObjectsSize = kernelFields->mObjectsSize;
3027 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003028 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00003029 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003030 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003031 if (rpcFields) {
3032 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3033 free(data);
3034 return status;
3035 }
3036 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003037
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003038 if (mData) {
3039 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3040 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003041 if (objects && kernelFields && kernelFields->mObjects) {
3042 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003043 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003044 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3045 if (kernelFields) {
3046 // TODO(b/239222407): This seems wrong. We should only free FDs when
3047 // they are in a truncated section of the parcel.
3048 closeFileDescriptors();
3049 }
3050 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003051 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003052 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003053
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003054 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003055 gParcelGlobalAllocSize += desired;
3056 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003057
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003058 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003059 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003060 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003061 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003062 if (kernelFields) {
3063 kernelFields->mObjects = objects;
3064 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3065 kernelFields->mNextObjectHint = 0;
3066 kernelFields->mObjectsSorted = false;
3067 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003068
3069 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003070 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003071#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003072 // Need to release refs on any objects we are dropping.
3073 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003074 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3075 const flat_binder_object* flat =
3076 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003077 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003078 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003079 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003080 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003081 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003082 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003083
3084 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003085 free(kernelFields->mObjects);
3086 kernelFields->mObjects = nullptr;
3087 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003088 } else {
3089 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003090 (binder_size_t*)realloc(kernelFields->mObjects,
3091 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003092 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003093 kernelFields->mObjects = objects;
3094 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003095 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003096 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003097 kernelFields->mObjectsSize = objectsSize;
3098 kernelFields->mNextObjectHint = 0;
3099 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003100#else // BINDER_WITH_KERNEL_IPC
3101 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3102#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003103 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003104 if (rpcFields) {
3105 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3106 return status;
3107 }
3108 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003109
3110 // We own the data, so we can just do a realloc().
3111 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003112 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003113 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003114 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3115 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003116 gParcelGlobalAllocSize += desired;
3117 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003118 mData = data;
3119 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003120 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003121 mError = NO_MEMORY;
3122 return NO_MEMORY;
3123 }
3124 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003125 if (mDataSize > desired) {
3126 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003127 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003128 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003129 if (mDataPos > desired) {
3130 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003131 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003132 }
3133 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003134
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003135 } else {
3136 // This is the first data. Easy!
3137 uint8_t* data = (uint8_t*)malloc(desired);
3138 if (!data) {
3139 mError = NO_MEMORY;
3140 return NO_MEMORY;
3141 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003142
Frederick Mayled3c595a2022-05-09 23:02:54 +00003143 if (!(mDataCapacity == 0 &&
3144 (kernelFields == nullptr ||
3145 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3146 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3147 kernelFields ? kernelFields->mObjects : nullptr,
3148 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003149 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003150
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003151 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003152 gParcelGlobalAllocSize += desired;
3153 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003154
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003155 mData = data;
3156 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003157 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3158 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003159 mDataCapacity = desired;
3160 }
3161
3162 return NO_ERROR;
3163}
3164
Frederick Mayle69a0c992022-05-26 20:38:39 +00003165status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3166 auto* rpcFields = maybeRpcFields();
3167 if (newObjectsSize == 0) {
3168 rpcFields->mObjectPositions.clear();
3169 if (rpcFields->mFds) {
3170 rpcFields->mFds->clear();
3171 }
3172 return OK;
3173 }
3174 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3175 uint32_t pos = rpcFields->mObjectPositions.back();
3176 rpcFields->mObjectPositions.pop_back();
3177 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3178 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3179 const auto fdIndex =
3180 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3181 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3182 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3183 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3184 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3185 return BAD_VALUE;
3186 }
3187 // In practice, this always removes the last element.
3188 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3189 }
3190 }
3191 return OK;
3192}
3193
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003194void Parcel::initState()
3195{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003196 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003197 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003198 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003199 mDataSize = 0;
3200 mDataCapacity = 0;
3201 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003202 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3203 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003204 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003205 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003206 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003207 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003208 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003209 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003210}
3211
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003212void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003213 auto* kernelFields = maybeKernelFields();
3214 if (kernelFields == nullptr) {
3215 return;
3216 }
3217 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003218 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003219 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003220}
3221
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003222#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003223size_t Parcel::getBlobAshmemSize() const
3224{
Adrian Roos6bb31142015-10-22 16:46:12 -07003225 // This used to return the size of all blobs that were written to ashmem, now we're returning
3226 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003227 // TODO(b/202029388): Remove method once ABI can be changed.
3228 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003229}
3230
Adrian Rooscbf37262015-10-22 16:12:53 -07003231size_t Parcel::getOpenAshmemSize() const
3232{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003233 auto* kernelFields = maybeKernelFields();
3234 if (kernelFields == nullptr) {
3235 return 0;
3236 }
3237
Steven Morelandc673f1f2021-10-07 18:23:35 -07003238 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003239#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003240 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003241 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003242 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003243
3244 // cookie is compared against zero for historical reasons
3245 // > obj.cookie = takeOwnership ? 1 : 0;
3246 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3247 int size = ashmem_get_size_region(flat->handle);
3248 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3249 ALOGE("Overflow when computing ashmem size.");
3250 return SIZE_MAX;
3251 }
3252 }
3253 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003254#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003255 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003256}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003257#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003258
3259// --- Parcel::Blob ---
3260
3261Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003262 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003263}
3264
3265Parcel::Blob::~Blob() {
3266 release();
3267}
3268
3269void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003270 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003271 ::munmap(mData, mSize);
3272 }
3273 clear();
3274}
3275
Jeff Brown13b16042014-11-11 16:44:25 -08003276void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3277 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003278 mData = data;
3279 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003280 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003281}
3282
3283void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003284 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003285 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003286 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003287 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003288}
3289
Steven Moreland61ff8492019-09-26 16:05:45 -07003290} // namespace android