blob: 7a54a0338955a0840c7efa4cd2a23352133a198c [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
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070071#define LOG_REFS(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000072// #define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080073#define LOG_ALLOC(...)
Andrei Homescua9cca9c2022-05-03 04:48:01 +000074// #define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
76// ---------------------------------------------------------------------------
77
Nick Kralevichb6b14232015-04-02 09:36:02 -070078// This macro should never be used at runtime, as a too large value
79// of s could cause an integer overflow. Instead, you should always
80// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000081#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070082
83static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070084 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070085 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070086 }
87 return PAD_SIZE_UNSAFE(s);
88}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070089
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070090// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060091#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070092
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070093namespace android {
94
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000095using namespace android::binder::impl;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070096using binder::borrowed_fd;
97using binder::unique_fd;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000098
Steven Moreland7b102262019-08-01 15:48:43 -070099// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +0000100#ifdef __LP64__
101static_assert(sizeof(Parcel) == 120);
102#else
103static_assert(sizeof(Parcel) == 60);
104#endif
Steven Moreland7b102262019-08-01 15:48:43 -0700105
Jeff Sharkey8994c182020-09-11 12:07:10 -0600106static std::atomic<size_t> gParcelGlobalAllocCount;
107static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -0800108
Andrei Homescu1519b982022-06-09 02:04:44 +0000109// Maximum number of file descriptors per Parcel.
110constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -0700111
Jeff Brown13b16042014-11-11 16:44:25 -0800112// Maximum size of a blob to transfer in-place.
Tomasz Wasilczyk83780132023-11-29 21:39:29 -0800113[[maybe_unused]] static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
Jeff Brown13b16042014-11-11 16:44:25 -0800114
115enum {
116 BLOB_INPLACE = 0,
117 BLOB_ASHMEM_IMMUTABLE = 1,
118 BLOB_ASHMEM_MUTABLE = 2,
119};
120
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000121#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandc673f1f2021-10-07 18:23:35 -0700122static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
123 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700124 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700125 case BINDER_TYPE_BINDER:
126 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000127 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800128 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 }
130 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 case BINDER_TYPE_HANDLE: {
132 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700133 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700134 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
135 b->incStrong(who);
136 }
137 return;
138 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700139 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 return;
141 }
142 }
143
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700144 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145}
146
Steven Morelandc673f1f2021-10-07 18:23:35 -0700147static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
148 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700149 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700150 case BINDER_TYPE_BINDER:
151 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000152 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800153 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154 }
155 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156 case BINDER_TYPE_HANDLE: {
157 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700158 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
160 b->decStrong(who);
161 }
162 return;
163 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800165 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800166 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700167 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700168 return;
169 }
170 }
171
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700172 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000174#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700175
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700176static int toRawFd(const std::variant<unique_fd, borrowed_fd>& v) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000177 return std::visit([](const auto& fd) { return fd.get(); }, v);
178}
179
Frederick Mayled3c595a2022-05-09 23:02:54 +0000180Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
181 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
182}
183
Steven Moreland34b48cb2020-12-01 22:45:38 +0000184status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700185{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700186 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700187 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000188 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700189}
190
Steven Morelanda86a3562019-08-01 23:28:34 +0000191status_t Parcel::finishUnflattenBinder(
192 const sp<IBinder>& binder, sp<IBinder>* out) const
193{
194 int32_t stability;
195 status_t status = readInt32(&stability);
196 if (status != OK) return status;
197
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000198 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
199 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000200 if (status != OK) return status;
201
202 *out = binder;
203 return OK;
204}
205
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000206#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000207static constexpr inline int schedPolicyMask(int policy, int priority) {
208 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
209}
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000210#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandbf1915b2020-07-16 22:43:02 +0000211
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500212status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
213 BBinder* local = nullptr;
214 if (binder) local = binder->localBinder();
215 if (local) local->setParceled();
216
Frederick Mayled3c595a2022-05-09 23:02:54 +0000217 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000218 if (binder) {
219 status_t status = writeInt32(1); // non-null
220 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700221 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000222 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000223 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
224 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000225 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700226 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000227 if (status != OK) return status;
228 } else {
229 status_t status = writeInt32(0); // null
230 if (status != OK) return status;
231 }
232 return finishFlattenBinder(binder);
233 }
234
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000235#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700236 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700237
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 int schedBits = 0;
239 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
240 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700241 }
242
Yi Kong91635562018-06-07 14:38:36 -0700243 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 if (!local) {
245 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700246 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000247 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000248 } else {
249 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700250 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000251 return INVALID_OPERATION;
252 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 }
Steven Moreland99157622021-09-13 16:27:34 -0700254 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700255 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800256 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000257 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700260 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000261 int policy = local->getMinSchedulerPolicy();
262 int priority = local->getMinSchedulerPriority();
263
264 if (policy != 0 || priority != 0) {
265 // override value, since it is set explicitly
266 schedBits = schedPolicyMask(policy, priority);
267 }
Steven Moreland49c27532022-03-01 10:21:07 +0000268 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800269 if (local->isRequestingSid()) {
270 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
271 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000272 if (local->isInheritRt()) {
273 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
274 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700275 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800276 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
277 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 }
279 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700280 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000281 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800282 obj.binder = 0;
283 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700284 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700285
Steven Morelandbf1915b2020-07-16 22:43:02 +0000286 obj.flags |= schedBits;
287
Steven Moreland34b48cb2020-12-01 22:45:38 +0000288 status_t status = writeObject(obj, false);
289 if (status != OK) return status;
290
291 return finishFlattenBinder(binder);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000292#else // BINDER_WITH_KERNEL_IPC
293 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
294 return INVALID_OPERATION;
295#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700296}
297
Steven Morelanda86a3562019-08-01 23:28:34 +0000298status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700299{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000300 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700301 int32_t isPresent;
302 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000303 if (status != OK) return status;
304
305 sp<IBinder> binder;
306
Steven Moreland5623d1a2021-09-10 15:45:34 -0700307 if (isPresent & 1) {
308 uint64_t addr;
309 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000310 if (status_t status =
311 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
312 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000313 status != OK)
314 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000315 if (status_t status =
316 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
317 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700318 status != OK)
319 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000320 }
321
322 return finishUnflattenBinder(binder, out);
323 }
324
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000325#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelanda86a3562019-08-01 23:28:34 +0000326 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700327
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700328 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700329 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000330 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000331 sp<IBinder> binder =
332 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000333 return finishUnflattenBinder(binder, out);
334 }
335 case BINDER_TYPE_HANDLE: {
336 sp<IBinder> binder =
337 ProcessState::self()->getStrongProxyForHandle(flat->handle);
338 return finishUnflattenBinder(binder, out);
339 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700340 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341 }
342 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000343#else // BINDER_WITH_KERNEL_IPC
344 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
345 return INVALID_OPERATION;
346#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700347}
348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700349// ---------------------------------------------------------------------------
350
351Parcel::Parcel()
352{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800353 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700354 initState();
355}
356
357Parcel::~Parcel()
358{
359 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800360 LOG_ALLOC("Parcel %p: destroyed", this);
361}
362
363size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600364 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800365}
366
367size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600368 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700369}
370
371const uint8_t* Parcel::data() const
372{
373 return mData;
374}
375
376size_t Parcel::dataSize() const
377{
378 return (mDataSize > mDataPos ? mDataSize : mDataPos);
379}
380
Pawan Waghd886a442023-01-21 02:16:38 +0000381size_t Parcel::dataBufferSize() const {
382 return mDataSize;
383}
384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700385size_t Parcel::dataAvail() const
386{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700387 size_t result = dataSize() - dataPosition();
388 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700389 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700390 }
391 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392}
393
394size_t Parcel::dataPosition() const
395{
396 return mDataPos;
397}
398
399size_t Parcel::dataCapacity() const
400{
401 return mDataCapacity;
402}
403
404status_t Parcel::setDataSize(size_t size)
405{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700406 if (size > INT32_MAX) {
407 // don't accept size_t values which may have come from an
408 // inadvertent conversion from a negative int.
409 return BAD_VALUE;
410 }
411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700412 status_t err;
413 err = continueWrite(size);
414 if (err == NO_ERROR) {
415 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700416 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700417 }
418 return err;
419}
420
421void Parcel::setDataPosition(size_t pos) const
422{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700423 if (pos > INT32_MAX) {
424 // don't accept size_t values which may have come from an
425 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700426 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700427 }
428
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700429 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000430 if (const auto* kernelFields = maybeKernelFields()) {
431 kernelFields->mNextObjectHint = 0;
432 kernelFields->mObjectsSorted = false;
433 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700434}
435
436status_t Parcel::setDataCapacity(size_t size)
437{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700438 if (size > INT32_MAX) {
439 // don't accept size_t values which may have come from an
440 // inadvertent conversion from a negative int.
441 return BAD_VALUE;
442 }
443
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700444 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445 return NO_ERROR;
446}
447
448status_t Parcel::setData(const uint8_t* buffer, size_t len)
449{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700450 if (len > INT32_MAX) {
451 // don't accept size_t values which may have come from an
452 // inadvertent conversion from a negative int.
453 return BAD_VALUE;
454 }
455
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700456 status_t err = restartWrite(len);
457 if (err == NO_ERROR) {
458 memcpy(const_cast<uint8_t*>(data()), buffer, len);
459 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000460 if (auto* kernelFields = maybeKernelFields()) {
461 kernelFields->mFdsKnown = false;
462 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700463 }
464 return err;
465}
466
Frederick Mayled3c595a2022-05-09 23:02:54 +0000467status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
468 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700469 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
470 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000471 return BAD_TYPE;
472 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000473 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
474 ALOGE("Cannot append Parcels from different sessions");
475 return BAD_TYPE;
476 }
Steven Moreland67753c32021-04-02 18:45:19 +0000477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700478 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000479 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700480 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481
482 if (len == 0) {
483 return NO_ERROR;
484 }
485
Nick Kralevichb6b14232015-04-02 09:36:02 -0700486 if (len > INT32_MAX) {
487 // don't accept size_t values which may have come from an
488 // inadvertent conversion from a negative int.
489 return BAD_VALUE;
490 }
491
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700492 // range checks against the source parcel size
493 if ((offset > parcel->mDataSize)
494 || (len > parcel->mDataSize)
495 || (offset + len > parcel->mDataSize)) {
496 return BAD_VALUE;
497 }
498
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700499 if ((mDataSize+len) > mDataCapacity) {
500 // grow data
501 err = growData(len);
502 if (err != NO_ERROR) {
503 return err;
504 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 }
506
507 // append data
508 memcpy(mData + mDataPos, data + offset, len);
509 mDataPos += len;
510 mDataSize += len;
511
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400512 err = NO_ERROR;
513
Frederick Mayled3c595a2022-05-09 23:02:54 +0000514 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescu88012462022-07-07 04:30:05 +0000515#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000516 auto* otherKernelFields = parcel->maybeKernelFields();
517 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
518
519 const binder_size_t* objects = otherKernelFields->mObjects;
520 size_t size = otherKernelFields->mObjectsSize;
521 // Count objects in range
522 int firstIndex = -1, lastIndex = -2;
523 for (int i = 0; i < (int)size; i++) {
524 size_t off = objects[i];
525 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
526 if (firstIndex == -1) {
527 firstIndex = i;
528 }
529 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700531 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000532 int numObjects = lastIndex - firstIndex + 1;
533 if (numObjects > 0) {
534 const sp<ProcessState> proc(ProcessState::self());
535 // grow objects
536 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
537 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
538 return NO_MEMORY; // overflow
539 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
540 return NO_MEMORY; // overflow
541 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
542 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
543 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
544 newSize * sizeof(binder_size_t));
545 if (objects == (binder_size_t*)nullptr) {
546 return NO_MEMORY;
547 }
548 kernelFields->mObjects = objects;
549 kernelFields->mObjectsCapacity = newSize;
550 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700551
Frederick Mayled3c595a2022-05-09 23:02:54 +0000552 // append and acquire objects
553 int idx = kernelFields->mObjectsSize;
554 for (int i = firstIndex; i <= lastIndex; i++) {
555 size_t off = objects[i] - offset + startPos;
556 kernelFields->mObjects[idx++] = off;
557 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700558
Frederick Mayled3c595a2022-05-09 23:02:54 +0000559 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
560 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700561
Frederick Mayled3c595a2022-05-09 23:02:54 +0000562 if (flat->hdr.type == BINDER_TYPE_FD) {
563 // If this is a file descriptor, we need to dup it so the
564 // new Parcel now owns its own fd, and can declare that we
565 // officially know we have fds.
566 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
567 flat->cookie = 1;
568 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
569 if (!mAllowFds) {
570 err = FDS_NOT_ALLOWED;
571 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400572 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573 }
574 }
Andrei Homescu88012462022-07-07 04:30:05 +0000575#else
576 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
577 return INVALID_OPERATION;
578#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000579 } else {
580 auto* rpcFields = maybeRpcFields();
581 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
582 auto* otherRpcFields = parcel->maybeRpcFields();
583 if (otherRpcFields == nullptr) {
584 return BAD_TYPE;
585 }
586 if (rpcFields->mSession != otherRpcFields->mSession) {
587 return BAD_TYPE;
588 }
589
590 const size_t savedDataPos = mDataPos;
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +0000591 auto scopeGuard = make_scope_guard([&]() { mDataPos = savedDataPos; });
Frederick Mayle69a0c992022-05-26 20:38:39 +0000592
593 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
594 if (otherRpcFields->mFds != nullptr) {
595 if (rpcFields->mFds == nullptr) {
596 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
597 }
598 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
599 }
600 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
601 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
602 if (offset <= objPos && objPos < offset + len) {
603 size_t newDataPos = objPos - offset + startPos;
604 rpcFields->mObjectPositions.push_back(newDataPos);
605
606 mDataPos = newDataPos;
607 int32_t objectType;
608 if (status_t status = readInt32(&objectType); status != OK) {
609 return status;
610 }
611 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
612 continue;
613 }
614
615 if (!mAllowFds) {
616 return FDS_NOT_ALLOWED;
617 }
618
619 // Read FD, duplicate, and add to list.
620 int32_t fdIndex;
621 if (status_t status = readInt32(&fdIndex); status != OK) {
622 return status;
623 }
Andrei Homescufc221502022-10-08 03:51:17 +0000624 int oldFd = toRawFd(otherRpcFields->mFds->at(fdIndex));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000625 // To match kernel binder behavior, we always dup, even if the
626 // FD was unowned in the source parcel.
Andrei Homescufc221502022-10-08 03:51:17 +0000627 int newFd = -1;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000628 if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) {
Andrei Homescufc221502022-10-08 03:51:17 +0000629 ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status));
630 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700631 rpcFields->mFds->emplace_back(unique_fd(newFd));
Frederick Mayle69a0c992022-05-26 20:38:39 +0000632 // Fixup the index in the data.
633 mDataPos = newDataPos + 4;
634 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
635 return status;
636 }
637 }
638 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639 }
640
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400641 return err;
642}
643
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700644int Parcel::compareData(const Parcel& other) {
645 size_t size = dataSize();
646 if (size != other.dataSize()) {
647 return size < other.dataSize() ? -1 : 1;
648 }
649 return memcmp(data(), other.data(), size);
650}
651
Bernardo Rufino897b1652021-10-08 10:30:20 +0100652status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
653 size_t len, int* result) const {
654 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
655 // Don't accept size_t values which may have come from an inadvertent conversion from a
656 // negative int.
657 return BAD_VALUE;
658 }
659 size_t thisLimit;
660 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
661 return BAD_VALUE;
662 }
663 size_t otherLimit;
664 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
665 return BAD_VALUE;
666 }
667 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
668 return NO_ERROR;
669}
670
Jeff Brown13b16042014-11-11 16:44:25 -0800671bool Parcel::allowFds() const
672{
673 return mAllowFds;
674}
675
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700676bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400677{
678 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700679 if (!allowFds) {
680 mAllowFds = false;
681 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400682 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683}
684
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700685void Parcel::restoreAllowFds(bool lastValue)
686{
687 mAllowFds = lastValue;
688}
689
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690bool Parcel::hasFileDescriptors() const
691{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000692 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000693 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000694 }
695 auto* kernelFields = maybeKernelFields();
696 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700697 scanForFds();
698 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000699 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700}
701
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000702std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
703 std::vector<sp<IBinder>> ret;
704
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000705#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +0000706 const auto* kernelFields = maybeKernelFields();
707 if (kernelFields == nullptr) {
708 return ret;
709 }
710
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000711 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000712 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
713 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000714 const flat_binder_object* flat =
715 reinterpret_cast<const flat_binder_object*>(mData + offset);
716 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
717
718 setDataPosition(offset);
719
720 sp<IBinder> binder = readStrongBinder();
721 if (binder != nullptr) ret.push_back(binder);
722 }
723
724 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000725#endif // BINDER_WITH_KERNEL_IPC
726
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000727 return ret;
728}
729
730std::vector<int> Parcel::debugReadAllFileDescriptors() const {
731 std::vector<int> ret;
732
Frederick Mayle69a0c992022-05-26 20:38:39 +0000733 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000734#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000735 size_t initPosition = dataPosition();
736 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
737 binder_size_t offset = kernelFields->mObjects[i];
738 const flat_binder_object* flat =
739 reinterpret_cast<const flat_binder_object*>(mData + offset);
740 if (flat->hdr.type != BINDER_TYPE_FD) continue;
741
742 setDataPosition(offset);
743
744 int fd = readFileDescriptor();
745 LOG_ALWAYS_FATAL_IF(fd == -1);
746 ret.push_back(fd);
747 }
748 setDataPosition(initPosition);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000749#else
750 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
751#endif
Frederick Mayle69a0c992022-05-26 20:38:39 +0000752 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
753 for (const auto& fd : *rpcFields->mFds) {
754 ret.push_back(toRawFd(fd));
755 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000756 }
757
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000758 return ret;
759}
760
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100761status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100762 if (len > INT32_MAX || offset > INT32_MAX) {
763 // Don't accept size_t values which may have come from an inadvertent conversion from a
764 // negative int.
765 return BAD_VALUE;
766 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100767 size_t limit;
768 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100769 return BAD_VALUE;
770 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100771 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000772 if (const auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000773#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000774 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
775 size_t pos = kernelFields->mObjects[i];
776 if (pos < offset) continue;
777 if (pos + sizeof(flat_binder_object) > offset + len) {
778 if (kernelFields->mObjectsSorted) {
779 break;
780 } else {
781 continue;
782 }
783 }
784 const flat_binder_object* flat =
785 reinterpret_cast<const flat_binder_object*>(mData + pos);
786 if (flat->hdr.type == BINDER_TYPE_FD) {
787 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000788 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000789 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100790 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000791#else
792 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
793 return INVALID_OPERATION;
794#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +0000795 } else if (const auto* rpcFields = maybeRpcFields()) {
796 for (uint32_t pos : rpcFields->mObjectPositions) {
797 if (offset <= pos && pos < limit) {
798 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
799 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
800 *result = true;
801 break;
802 }
803 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100804 }
805 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100806 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100807}
808
Steven Morelandf183fdd2020-10-27 00:12:12 +0000809void Parcel::markSensitive() const
810{
811 mDeallocZero = true;
812}
813
Steven Moreland5553ac42020-11-11 02:14:45 +0000814void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000815 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
816
Steven Moreland5553ac42020-11-11 02:14:45 +0000817 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700818 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000819 }
820}
821
Steven Morelandc9939062021-05-05 17:57:41 +0000822void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000823 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
824 "format must be set before data is written OR on IPC data");
825
Frederick Mayled3c595a2022-05-09 23:02:54 +0000826 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000827}
828
829bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000831}
832
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000833void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000834 auto* kernelFields = maybeKernelFields();
835 if (kernelFields == nullptr) {
836 return;
837 }
838
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000839 // Only update the request headers once. We only want to point
840 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000841 if (!kernelFields->mRequestHeaderPresent) {
842 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
843 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000844 }
845}
846
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000847#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland94e63e12023-12-05 20:29:50 +0000848
849#if defined(__ANDROID__)
850
Steven Morelandb6c7e222021-02-18 19:20:14 +0000851#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700852constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700853#elif defined(__ANDROID_RECOVERY__)
854constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700855#else
856constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
857#endif
Steven Moreland94e63e12023-12-05 20:29:50 +0000858
859#else // ANDROID not defined
860
861// If kernel binder is used in new environments, we need to make sure it's separated
862// out and has a separate header.
863constexpr int32_t kHeader = B_PACK_CHARS('U', 'N', 'K', 'N');
864#endif
865
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000866#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700867
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700868// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700869status_t Parcel::writeInterfaceToken(const String16& interface)
870{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000871 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000872}
873
874status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000875 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000876#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000877 const IPCThreadState* threadState = IPCThreadState::self();
878 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
879 updateWorkSourceRequestHeaderPosition();
880 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
881 : IPCThreadState::kUnsetWorkSource);
882 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000883#else // BINDER_WITH_KERNEL_IPC
884 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
885 return INVALID_OPERATION;
886#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000887 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000888
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700889 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000890 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700891}
892
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000893bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
894{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000895 auto* kernelFields = maybeKernelFields();
896 if (kernelFields == nullptr) {
897 return false;
898 }
899 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000900 return false;
901 }
902
903 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000904 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000905 status_t err = writeInt32(uid);
906 setDataPosition(initialPosition);
907 return err == NO_ERROR;
908}
909
Steven Morelandf1b1e492019-05-06 15:05:13 -0700910uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000911{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000912 auto* kernelFields = maybeKernelFields();
913 if (kernelFields == nullptr) {
914 return false;
915 }
916 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000917 return IPCThreadState::kUnsetWorkSource;
918 }
919
920 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000921 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000922 uid_t uid = readInt32();
923 setDataPosition(initialPosition);
924 return uid;
925}
926
Mathias Agopian83c04462009-05-22 19:00:22 -0700927bool Parcel::checkInterface(IBinder* binder) const
928{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700929 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700930}
931
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700932bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700933 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700934{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000935 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700936}
937
938bool Parcel::enforceInterface(const char16_t* interface,
939 size_t len,
940 IPCThreadState* threadState) const
941{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000942 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000943#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000944 // StrictModePolicy.
945 int32_t strictPolicy = readInt32();
946 if (threadState == nullptr) {
947 threadState = IPCThreadState::self();
948 }
949 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
950 // For one-way calls, the callee is running entirely
951 // disconnected from the caller, so disable StrictMode entirely.
952 // Not only does disk/network usage not impact the caller, but
953 // there's no way to communicate back violations anyway.
954 threadState->setStrictModePolicy(0);
955 } else {
956 threadState->setStrictModePolicy(strictPolicy);
957 }
958 // WorkSource.
959 updateWorkSourceRequestHeaderPosition();
960 int32_t workSource = readInt32();
961 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
962 // vendor header
963 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000964
965 // fuzzers skip this check, because it is for protecting the underlying ABI, but
966 // we don't want it to reduce our coverage
967 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000968 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
969 header);
970 return false;
971 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000972#else // BINDER_WITH_KERNEL_IPC
973 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
974 (void)threadState;
975 return false;
976#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700977 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000978
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100979 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700980 size_t parcel_interface_len;
981 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
982 if (len == parcel_interface_len &&
983 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700984 return true;
985 } else {
Steven Morelande99d8822023-06-02 21:56:39 +0000986 if (mServiceFuzzing) {
987 // ignore. Theoretically, this could cause a few false positives, because
988 // people could assume things about getInterfaceDescriptor if they pass
989 // this point, but it would be extremely fragile. It's more important that
990 // we fuzz with the above things read from the Parcel.
991 return true;
992 } else {
Steven Moreland3a667492023-06-02 21:41:39 +0000993 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000994 String8(interface, len).c_str(),
995 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +0000996 return false;
Steven Moreland3a667492023-06-02 21:41:39 +0000997 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700998 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700999}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001000
Pawan Wagh104654a2022-11-15 21:18:42 +00001001void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
1002 mEnforceNoDataAvail = enforceNoDataAvail;
1003}
1004
Steven Moreland3a667492023-06-02 21:41:39 +00001005void Parcel::setServiceFuzzing() {
1006 mServiceFuzzing = true;
1007}
1008
Steven Moreland418914a2023-06-28 21:47:14 +00001009bool Parcel::isServiceFuzzing() const {
1010 return mServiceFuzzing;
1011}
1012
Jooyung Hand23f9502021-12-23 14:39:57 +09001013binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001014 if (!mEnforceNoDataAvail) {
1015 return binder::Status::ok();
1016 }
1017
Jooyung Hand23f9502021-12-23 14:39:57 +09001018 const auto n = dataAvail();
1019 if (n == 0) {
1020 return binder::Status::ok();
1021 }
1022 return binder::Status::
1023 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1024 String8::format("Parcel data not fully consumed, unread size: %zu",
1025 n));
1026}
1027
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001028size_t Parcel::objectsCount() const
1029{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001030 if (const auto* kernelFields = maybeKernelFields()) {
1031 return kernelFields->mObjectsSize;
1032 }
1033 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034}
1035
1036status_t Parcel::errorCheck() const
1037{
1038 return mError;
1039}
1040
1041void Parcel::setError(status_t err)
1042{
1043 mError = err;
1044}
1045
1046status_t Parcel::finishWrite(size_t len)
1047{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001048 if (len > INT32_MAX) {
1049 // don't accept size_t values which may have come from an
1050 // inadvertent conversion from a negative int.
1051 return BAD_VALUE;
1052 }
1053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054 //printf("Finish write of %d\n", len);
1055 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001056 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057 if (mDataPos > mDataSize) {
1058 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001059 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001060 }
1061 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1062 return NO_ERROR;
1063}
1064
1065status_t Parcel::writeUnpadded(const void* data, size_t len)
1066{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001067 if (len > INT32_MAX) {
1068 // don't accept size_t values which may have come from an
1069 // inadvertent conversion from a negative int.
1070 return BAD_VALUE;
1071 }
1072
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073 size_t end = mDataPos + len;
1074 if (end < mDataPos) {
1075 // integer overflow
1076 return BAD_VALUE;
1077 }
1078
1079 if (end <= mDataCapacity) {
1080restart_write:
1081 memcpy(mData+mDataPos, data, len);
1082 return finishWrite(len);
1083 }
1084
1085 status_t err = growData(len);
1086 if (err == NO_ERROR) goto restart_write;
1087 return err;
1088}
1089
1090status_t Parcel::write(const void* data, size_t len)
1091{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001092 if (len > INT32_MAX) {
1093 // don't accept size_t values which may have come from an
1094 // inadvertent conversion from a negative int.
1095 return BAD_VALUE;
1096 }
1097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098 void* const d = writeInplace(len);
1099 if (d) {
1100 memcpy(d, data, len);
1101 return NO_ERROR;
1102 }
1103 return mError;
1104}
1105
1106void* Parcel::writeInplace(size_t len)
1107{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001108 if (len > INT32_MAX) {
1109 // don't accept size_t values which may have come from an
1110 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001111 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001112 }
1113
1114 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001115
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001116 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001117 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001118 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119 }
1120
1121 if ((mDataPos+padded) <= mDataCapacity) {
1122restart_write:
1123 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1124 uint8_t* const data = mData+mDataPos;
1125
1126 // Need to pad at end?
1127 if (padded != len) {
1128#if BYTE_ORDER == BIG_ENDIAN
1129 static const uint32_t mask[4] = {
1130 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1131 };
1132#endif
1133#if BYTE_ORDER == LITTLE_ENDIAN
1134 static const uint32_t mask[4] = {
1135 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1136 };
1137#endif
1138 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1139 // *reinterpret_cast<void**>(data+padded-4));
1140 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1141 }
1142
1143 finishWrite(padded);
1144 return data;
1145 }
1146
1147 status_t err = growData(padded);
1148 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001149 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001150}
1151
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001152status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1153 const uint8_t* strData = (uint8_t*)str.data();
1154 const size_t strLen= str.length();
1155 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001156 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001157 return BAD_VALUE;
1158 }
1159
1160 status_t err = writeInt32(utf16Len);
1161 if (err) {
1162 return err;
1163 }
1164
1165 // Allocate enough bytes to hold our converted string and its terminating NULL.
1166 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1167 if (!dst) {
1168 return NO_MEMORY;
1169 }
1170
Sergio Girof4607432016-07-21 14:46:35 +01001171 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001172
1173 return NO_ERROR;
1174}
1175
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001176
Andy Hung49198cf2020-11-18 11:02:39 -08001177status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1178status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001179
Andy Hung49198cf2020-11-18 11:02:39 -08001180status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1181status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001182
Andy Hung49198cf2020-11-18 11:02:39 -08001183status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1184status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1185status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1186status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1187status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1188status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1189status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1190status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1191status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1192status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1193status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1194status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1195status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1196status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1197status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1198status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1199status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1200status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1201status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1202status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1203status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1204status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1205status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1206status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1207status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1208status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1209status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001210
Andy Hung49198cf2020-11-18 11:02:39 -08001211status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001212status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001213 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001214status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001215 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001216status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001217 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001218status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001219 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1220status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001221
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001222status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1223 return writeData(val);
1224}
1225status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1226 return writeData(val);
1227}
1228status_t Parcel::writeUniqueFileDescriptorVector(
1229 const std::unique_ptr<std::vector<unique_fd>>& val) {
1230 return writeData(val);
1231}
Andy Hung49198cf2020-11-18 11:02:39 -08001232
1233status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1234status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1235status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1236
1237status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1238
1239status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1240status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1241
1242status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1243status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1244
1245status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1246status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1247status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1248status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1249status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1250status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1251status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1252status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1253status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1254status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1255status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1256status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1257status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1258status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1259status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1260status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1261status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1262status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1263status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1264status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1265status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1266status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1267status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1268status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1269status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1270status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1271status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1272
1273status_t Parcel::readString16Vector(
1274 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1275status_t Parcel::readString16Vector(
1276 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1277status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1278status_t Parcel::readUtf8VectorFromUtf16Vector(
1279 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1280status_t Parcel::readUtf8VectorFromUtf16Vector(
1281 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1282status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1283
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001284status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1285 return readData(val);
1286}
1287status_t Parcel::readUniqueFileDescriptorVector(
1288 std::unique_ptr<std::vector<unique_fd>>* val) const {
1289 return readData(val);
1290}
1291status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1292 return readData(val);
1293}
Andy Hung49198cf2020-11-18 11:02:39 -08001294
1295status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1296status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1297status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1298
1299status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001300
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001301status_t Parcel::writeInt32(int32_t val)
1302{
Andreas Huber84a6d042009-08-17 13:33:27 -07001303 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001304}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001305
1306status_t Parcel::writeUint32(uint32_t val)
1307{
1308 return writeAligned(val);
1309}
1310
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001311status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001312 if (len > INT32_MAX) {
1313 // don't accept size_t values which may have come from an
1314 // inadvertent conversion from a negative int.
1315 return BAD_VALUE;
1316 }
1317
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001318 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001319 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001320 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001321 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001322 if (ret == NO_ERROR) {
1323 ret = write(val, len * sizeof(*val));
1324 }
1325 return ret;
1326}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001327status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001328 if (len > INT32_MAX) {
1329 // don't accept size_t values which may have come from an
1330 // inadvertent conversion from a negative int.
1331 return BAD_VALUE;
1332 }
1333
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001334 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001335 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001336 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001337 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001338 if (ret == NO_ERROR) {
1339 ret = write(val, len * sizeof(*val));
1340 }
1341 return ret;
1342}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001343
Casey Dahlind6848f52015-10-15 15:44:59 -07001344status_t Parcel::writeBool(bool val)
1345{
1346 return writeInt32(int32_t(val));
1347}
1348
1349status_t Parcel::writeChar(char16_t val)
1350{
1351 return writeInt32(int32_t(val));
1352}
1353
1354status_t Parcel::writeByte(int8_t val)
1355{
1356 return writeInt32(int32_t(val));
1357}
1358
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001359status_t Parcel::writeInt64(int64_t val)
1360{
Andreas Huber84a6d042009-08-17 13:33:27 -07001361 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001362}
1363
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001364status_t Parcel::writeUint64(uint64_t val)
1365{
1366 return writeAligned(val);
1367}
1368
Serban Constantinescuf683e012013-11-05 16:53:55 +00001369status_t Parcel::writePointer(uintptr_t val)
1370{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001371 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001372}
1373
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001374status_t Parcel::writeFloat(float val)
1375{
Andreas Huber84a6d042009-08-17 13:33:27 -07001376 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001377}
1378
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001379#if defined(__mips__) && defined(__mips_hard_float)
1380
1381status_t Parcel::writeDouble(double val)
1382{
1383 union {
1384 double d;
1385 unsigned long long ll;
1386 } u;
1387 u.d = val;
1388 return writeAligned(u.ll);
1389}
1390
1391#else
1392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001393status_t Parcel::writeDouble(double val)
1394{
Andreas Huber84a6d042009-08-17 13:33:27 -07001395 return writeAligned(val);
1396}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001397
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001398#endif
1399
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001400status_t Parcel::writeCString(const char* str)
1401{
1402 return write(str, strlen(str)+1);
1403}
1404
1405status_t Parcel::writeString8(const String8& str)
1406{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001407 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001408}
1409
1410status_t Parcel::writeString8(const char* str, size_t len)
1411{
1412 if (str == nullptr) return writeInt32(-1);
1413
Jeff Sharkey18220902020-11-05 08:36:20 -07001414 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001415 status_t err = writeInt32(len);
1416 if (err == NO_ERROR) {
1417 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1418 if (data) {
1419 memcpy(data, str, len);
1420 *reinterpret_cast<char*>(data+len) = 0;
1421 return NO_ERROR;
1422 }
1423 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001424 }
1425 return err;
1426}
1427
1428status_t Parcel::writeString16(const String16& str)
1429{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001430 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431}
1432
1433status_t Parcel::writeString16(const char16_t* str, size_t len)
1434{
Yi Kong91635562018-06-07 14:38:36 -07001435 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001436
Jeff Sharkey18220902020-11-05 08:36:20 -07001437 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001438 status_t err = writeInt32(len);
1439 if (err == NO_ERROR) {
1440 len *= sizeof(char16_t);
1441 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1442 if (data) {
1443 memcpy(data, str, len);
1444 *reinterpret_cast<char16_t*>(data+len) = 0;
1445 return NO_ERROR;
1446 }
1447 err = mError;
1448 }
1449 return err;
1450}
1451
1452status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1453{
Steven Morelanda86a3562019-08-01 23:28:34 +00001454 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001455}
1456
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001457
Casey Dahlinb9872622015-11-25 15:09:45 -08001458status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1459 if (!parcelable) {
1460 return writeInt32(0);
1461 }
1462
1463 return writeParcelable(*parcelable);
1464}
1465
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001466#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001467status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001468{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001469 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001470 return BAD_TYPE;
1471
1472 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001473 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001474 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001476 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001477 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001479 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1480 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481
1482 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001483 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 return err;
1485 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001486 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001487 return err;
1488}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001489#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001490
Frederick Mayle69a0c992022-05-26 20:38:39 +00001491status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1492 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001493 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001494 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001495 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001496 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001497 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001498 }
1499 if (!mAllowFds) {
1500 return FDS_NOT_ALLOWED;
1501 }
1502 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1503 case RpcSession::FileDescriptorTransportMode::NONE: {
1504 return FDS_NOT_ALLOWED;
1505 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001506 case RpcSession::FileDescriptorTransportMode::UNIX:
1507 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001508 if (rpcFields->mFds == nullptr) {
1509 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1510 }
1511 size_t dataPos = mDataPos;
1512 if (dataPos > UINT32_MAX) {
1513 return NO_MEMORY;
1514 }
1515 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1516 return err;
1517 }
1518 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1519 return err;
1520 }
1521 rpcFields->mObjectPositions.push_back(dataPos);
1522 rpcFields->mFds->push_back(std::move(fdVariant));
1523 return OK;
1524 }
1525 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001526 }
1527
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001528#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001529 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001530 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001531 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001532 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001533 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001534 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001535 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001536#else // BINDER_WITH_KERNEL_IPC
1537 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1538 (void)fd;
1539 (void)takeOwnership;
1540 return INVALID_OPERATION;
1541#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542}
1543
1544status_t Parcel::writeDupFileDescriptor(int fd)
1545{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001546 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001547 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001548 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001549 }
1550 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001551 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001552 close(dupFd);
1553 }
1554 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001555}
1556
Dianne Hackborn1941a402016-08-29 12:30:43 -07001557status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1558{
1559 writeInt32(0);
1560 return writeFileDescriptor(fd, takeOwnership);
1561}
1562
Ryo Hashimotobf551892018-05-31 16:58:35 +09001563status_t Parcel::writeDupParcelFileDescriptor(int fd)
1564{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001565 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001566 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001567 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001568 }
1569 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1570 if (err != OK) {
1571 close(dupFd);
1572 }
1573 return err;
1574}
1575
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001576status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001577 return writeDupFileDescriptor(fd.get());
1578}
1579
Jeff Brown13b16042014-11-11 16:44:25 -08001580status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001581{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001582#ifdef BINDER_DISABLE_BLOB
1583 (void)len;
1584 (void)mutableCopy;
1585 (void)outBlob;
1586 return INVALID_OPERATION;
1587#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001588 if (len > INT32_MAX) {
1589 // don't accept size_t values which may have come from an
1590 // inadvertent conversion from a negative int.
1591 return BAD_VALUE;
1592 }
1593
Jeff Brown13b16042014-11-11 16:44:25 -08001594 status_t status;
1595 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001596 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001597 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001598 if (status) return status;
1599
1600 void* ptr = writeInplace(len);
1601 if (!ptr) return NO_MEMORY;
1602
Jeff Brown13b16042014-11-11 16:44:25 -08001603 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001604 return NO_ERROR;
1605 }
1606
Steve Block6807e592011-10-20 11:56:00 +01001607 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001608 int fd = ashmem_create_region("Parcel Blob", len);
1609 if (fd < 0) return NO_MEMORY;
1610
1611 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1612 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001613 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001614 } else {
Yi Kong91635562018-06-07 14:38:36 -07001615 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001616 if (ptr == MAP_FAILED) {
1617 status = -errno;
1618 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001619 if (!mutableCopy) {
1620 result = ashmem_set_prot_region(fd, PROT_READ);
1621 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001622 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001623 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001624 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001625 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001626 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001627 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001628 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001629 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001630 return NO_ERROR;
1631 }
1632 }
1633 }
1634 }
1635 ::munmap(ptr, len);
1636 }
1637 ::close(fd);
1638 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001639#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001640}
1641
Jeff Brown13b16042014-11-11 16:44:25 -08001642status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1643{
1644 // Must match up with what's done in writeBlob.
1645 if (!mAllowFds) return FDS_NOT_ALLOWED;
1646 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1647 if (status) return status;
1648 return writeDupFileDescriptor(fd);
1649}
1650
Mathias Agopiane1424282013-07-29 21:24:40 -07001651status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001652{
1653 status_t err;
1654
1655 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001656 const size_t len = val.getFlattenedSize();
1657 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001658
Andrei Homescu1519b982022-06-09 02:04:44 +00001659 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001660 // don't accept size_t values which may have come from an
1661 // inadvertent conversion from a negative int.
1662 return BAD_VALUE;
1663 }
1664
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001665 err = this->writeInt32(len);
1666 if (err) return err;
1667
1668 err = this->writeInt32(fd_count);
1669 if (err) return err;
1670
1671 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001672 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001673 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001674 return BAD_VALUE;
1675
Yi Kong91635562018-06-07 14:38:36 -07001676 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001677 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001678 fds = new (std::nothrow) int[fd_count];
1679 if (fds == nullptr) {
1680 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1681 return BAD_VALUE;
1682 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001683 }
1684
1685 err = val.flatten(buf, len, fds, fd_count);
1686 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1687 err = this->writeDupFileDescriptor( fds[i] );
1688 }
1689
1690 if (fd_count) {
1691 delete [] fds;
1692 }
1693
1694 return err;
1695}
1696
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1698{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001699 auto* kernelFields = maybeKernelFields();
1700 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1701
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001702#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001703 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001704 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001705 if (enoughData && enoughObjects) {
1706restart_write:
1707 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001708
Christopher Tate98e67d32015-06-03 18:44:15 -07001709 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001710 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001711 if (!mAllowFds) {
1712 // fail before modifying our object index
1713 return FDS_NOT_ALLOWED;
1714 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001715 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001716 }
1717
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001718 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001719 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001720 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001721 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001722 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001723 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001724
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001725 return finishWrite(sizeof(flat_binder_object));
1726 }
1727
1728 if (!enoughData) {
1729 const status_t err = growData(sizeof(val));
1730 if (err != NO_ERROR) return err;
1731 }
1732 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001733 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1734 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1735 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001736 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001737 binder_size_t* objects =
1738 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001739 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001740 kernelFields->mObjects = objects;
1741 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001742 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001743
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001745#else // BINDER_WITH_KERNEL_IPC
1746 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1747 (void)val;
1748 (void)nullMetaData;
1749 return INVALID_OPERATION;
1750#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001751}
1752
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001753status_t Parcel::writeNoException()
1754{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001755 binder::Status status;
1756 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001757}
1758
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001759status_t Parcel::validateReadData(size_t upperBound) const
1760{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001761 const auto* kernelFields = maybeKernelFields();
1762 if (kernelFields == nullptr) {
1763 // Can't validate RPC Parcel reads because the location of binder
1764 // objects is unknown.
1765 return OK;
1766 }
1767
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001768#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001769 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001770 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1771 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001772 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001773 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1774 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001775 // For some reason the current read position is greater than the next object
1776 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001777 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001778 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001779 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001780 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001781 if (!mServiceFuzzing) {
1782 ALOGE("Attempt to read from protected data in Parcel %p", this);
1783 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001784 return PERMISSION_DENIED;
1785 }
1786 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001787 } while (nextObject < kernelFields->mObjectsSize &&
1788 upperBound > kernelFields->mObjects[nextObject]);
1789 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001790 }
1791 return NO_ERROR;
1792 }
1793 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001794 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001795 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001796 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001797 prevObj--;
1798 if(*prevObj > *currObj) {
1799 goto data_unsorted;
1800 }
1801 currObj--;
1802 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001803 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001804 goto data_sorted;
1805
1806data_unsorted:
1807 // Insertion Sort mObjects
1808 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1809 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001810 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1811 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001812 binder_size_t temp = *iter0;
1813 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001814 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001815 *(iter1 + 1) = *iter1;
1816 iter1--;
1817 }
1818 *(iter1 + 1) = temp;
1819 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001820 kernelFields->mNextObjectHint = 0;
1821 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001822 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001823#else // BINDER_WITH_KERNEL_IPC
1824 (void)upperBound;
1825 return NO_ERROR;
1826#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001827}
1828
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001829status_t Parcel::read(void* outData, size_t len) const
1830{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001831 if (len > INT32_MAX) {
1832 // don't accept size_t values which may have come from an
1833 // inadvertent conversion from a negative int.
1834 return BAD_VALUE;
1835 }
1836
1837 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1838 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001839 const auto* kernelFields = maybeKernelFields();
1840 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001841 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001842 if(err != NO_ERROR) {
1843 // Still increment the data position by the expected length
1844 mDataPos += pad_size(len);
1845 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1846 return err;
1847 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001848 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001849 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001850 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001851 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001852 return NO_ERROR;
1853 }
1854 return NOT_ENOUGH_DATA;
1855}
1856
1857const void* Parcel::readInplace(size_t len) const
1858{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001859 if (len > INT32_MAX) {
1860 // don't accept size_t values which may have come from an
1861 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001862 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001863 }
1864
1865 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1866 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001867 const auto* kernelFields = maybeKernelFields();
1868 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001869 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001870 if(err != NO_ERROR) {
1871 // Still increment the data position by the expected length
1872 mDataPos += pad_size(len);
1873 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001874 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001875 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001876 }
1877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001878 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001879 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001880 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001881 return data;
1882 }
Yi Kong91635562018-06-07 14:38:36 -07001883 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884}
1885
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001886status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1887 if (status_t status = readInt32(size); status != OK) return status;
1888 if (*size < 0) return OK; // may be null, client to handle
1889
1890 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1891
1892 // approximation, can't know max element size (e.g. if it makes heap
1893 // allocations)
1894 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1895 int32_t allocationSize;
1896 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1897
1898 // High limit of 1MB since something this big could never be returned. Could
1899 // probably scope this down, but might impact very specific usecases.
1900 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1901
1902 if (allocationSize >= kMaxAllocationSize) {
1903 return NO_MEMORY;
1904 }
1905
1906 return OK;
1907}
1908
Andreas Huber84a6d042009-08-17 13:33:27 -07001909template<class T>
1910status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001911 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001912 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001913
1914 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001915 const auto* kernelFields = maybeKernelFields();
1916 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001917 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001918 if(err != NO_ERROR) {
1919 // Still increment the data position by the expected length
1920 mDataPos += sizeof(T);
1921 return err;
1922 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001923 }
1924
Andrei Homescue33238e2022-03-29 22:50:00 +00001925 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001926 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001927 return NO_ERROR;
1928 } else {
1929 return NOT_ENOUGH_DATA;
1930 }
1931}
1932
Andreas Huber84a6d042009-08-17 13:33:27 -07001933template<class T>
1934T Parcel::readAligned() const {
1935 T result;
1936 if (readAligned(&result) != NO_ERROR) {
1937 result = 0;
1938 }
1939
1940 return result;
1941}
1942
1943template<class T>
1944status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001945 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001946 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001947
1948 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1949restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001950 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001951 return finishWrite(sizeof(val));
1952 }
1953
1954 status_t err = growData(sizeof(val));
1955 if (err == NO_ERROR) goto restart_write;
1956 return err;
1957}
1958
1959status_t Parcel::readInt32(int32_t *pArg) const
1960{
1961 return readAligned(pArg);
1962}
1963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001964int32_t Parcel::readInt32() const
1965{
Andreas Huber84a6d042009-08-17 13:33:27 -07001966 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967}
1968
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001969status_t Parcel::readUint32(uint32_t *pArg) const
1970{
1971 return readAligned(pArg);
1972}
1973
1974uint32_t Parcel::readUint32() const
1975{
1976 return readAligned<uint32_t>();
1977}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001978
1979status_t Parcel::readInt64(int64_t *pArg) const
1980{
Andreas Huber84a6d042009-08-17 13:33:27 -07001981 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001982}
1983
1984
1985int64_t Parcel::readInt64() const
1986{
Andreas Huber84a6d042009-08-17 13:33:27 -07001987 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001988}
1989
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001990status_t Parcel::readUint64(uint64_t *pArg) const
1991{
1992 return readAligned(pArg);
1993}
1994
1995uint64_t Parcel::readUint64() const
1996{
1997 return readAligned<uint64_t>();
1998}
1999
Serban Constantinescuf683e012013-11-05 16:53:55 +00002000status_t Parcel::readPointer(uintptr_t *pArg) const
2001{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002002 status_t ret;
2003 binder_uintptr_t ptr;
2004 ret = readAligned(&ptr);
2005 if (!ret)
2006 *pArg = ptr;
2007 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00002008}
2009
2010uintptr_t Parcel::readPointer() const
2011{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002012 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002013}
2014
2015
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002016status_t Parcel::readFloat(float *pArg) const
2017{
Andreas Huber84a6d042009-08-17 13:33:27 -07002018 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002019}
2020
2021
2022float Parcel::readFloat() const
2023{
Andreas Huber84a6d042009-08-17 13:33:27 -07002024 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002025}
2026
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002027#if defined(__mips__) && defined(__mips_hard_float)
2028
2029status_t Parcel::readDouble(double *pArg) const
2030{
2031 union {
2032 double d;
2033 unsigned long long ll;
2034 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002035 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002036 status_t status;
2037 status = readAligned(&u.ll);
2038 *pArg = u.d;
2039 return status;
2040}
2041
2042double Parcel::readDouble() const
2043{
2044 union {
2045 double d;
2046 unsigned long long ll;
2047 } u;
2048 u.ll = readAligned<unsigned long long>();
2049 return u.d;
2050}
2051
2052#else
2053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054status_t Parcel::readDouble(double *pArg) const
2055{
Andreas Huber84a6d042009-08-17 13:33:27 -07002056 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002057}
2058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059double Parcel::readDouble() const
2060{
Andreas Huber84a6d042009-08-17 13:33:27 -07002061 return readAligned<double>();
2062}
2063
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002064#endif
2065
Casey Dahlind6848f52015-10-15 15:44:59 -07002066status_t Parcel::readBool(bool *pArg) const
2067{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002068 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002069 status_t ret = readInt32(&tmp);
2070 *pArg = (tmp != 0);
2071 return ret;
2072}
2073
2074bool Parcel::readBool() const
2075{
2076 return readInt32() != 0;
2077}
2078
2079status_t Parcel::readChar(char16_t *pArg) const
2080{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002081 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002082 status_t ret = readInt32(&tmp);
2083 *pArg = char16_t(tmp);
2084 return ret;
2085}
2086
2087char16_t Parcel::readChar() const
2088{
2089 return char16_t(readInt32());
2090}
2091
2092status_t Parcel::readByte(int8_t *pArg) const
2093{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002094 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002095 status_t ret = readInt32(&tmp);
2096 *pArg = int8_t(tmp);
2097 return ret;
2098}
2099
2100int8_t Parcel::readByte() const
2101{
2102 return int8_t(readInt32());
2103}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002104
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002105status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2106 size_t utf16Size = 0;
2107 const char16_t* src = readString16Inplace(&utf16Size);
2108 if (!src) {
2109 return UNEXPECTED_NULL;
2110 }
2111
2112 // Save ourselves the trouble, we're done.
2113 if (utf16Size == 0u) {
2114 str->clear();
2115 return NO_ERROR;
2116 }
2117
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002118 // Allow for closing '\0'
2119 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2120 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002121 return BAD_VALUE;
2122 }
2123 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002124 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002125 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002126 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2127 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002128 return NO_ERROR;
2129}
2130
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131const char* Parcel::readCString() const
2132{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002133 if (mDataPos < mDataSize) {
2134 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2136 // is the string's trailing NUL within the parcel's valid bounds?
2137 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2138 if (eos) {
2139 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002140 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002141 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002142 return str;
2143 }
2144 }
Yi Kong91635562018-06-07 14:38:36 -07002145 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146}
2147
2148String8 Parcel::readString8() const
2149{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002150 size_t len;
2151 const char* str = readString8Inplace(&len);
2152 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002153
2154 if (!mServiceFuzzing) {
2155 ALOGE("Reading a NULL string not supported here.");
2156 }
2157
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002158 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002159}
2160
2161status_t Parcel::readString8(String8* pArg) const
2162{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002163 size_t len;
2164 const char* str = readString8Inplace(&len);
2165 if (str) {
2166 pArg->setTo(str, len);
2167 return 0;
2168 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002169 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002170 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002171 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002172}
2173
2174const char* Parcel::readString8Inplace(size_t* outLen) const
2175{
2176 int32_t size = readInt32();
2177 // watch for potential int overflow from size+1
2178 if (size >= 0 && size < INT32_MAX) {
2179 *outLen = size;
2180 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002181 if (str != nullptr) {
2182 if (str[size] == '\0') {
2183 return str;
2184 }
2185 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002186 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002187 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002188 *outLen = 0;
2189 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002190}
2191
2192String16 Parcel::readString16() const
2193{
2194 size_t len;
2195 const char16_t* str = readString16Inplace(&len);
2196 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002197
2198 if (!mServiceFuzzing) {
2199 ALOGE("Reading a NULL string not supported here.");
2200 }
2201
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 return String16();
2203}
2204
Casey Dahlinb9872622015-11-25 15:09:45 -08002205
Casey Dahlin451ff582015-10-19 18:12:18 -07002206status_t Parcel::readString16(String16* pArg) const
2207{
2208 size_t len;
2209 const char16_t* str = readString16Inplace(&len);
2210 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002211 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002212 return 0;
2213 } else {
2214 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002215 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002216 }
2217}
2218
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002219const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2220{
2221 int32_t size = readInt32();
2222 // watch for potential int overflow from size+1
2223 if (size >= 0 && size < INT32_MAX) {
2224 *outLen = size;
2225 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002226 if (str != nullptr) {
2227 if (str[size] == u'\0') {
2228 return str;
2229 }
2230 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 }
2232 }
2233 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002234 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002235}
2236
Casey Dahlinf0c13772015-10-27 18:33:56 -07002237status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2238{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002239 status_t status = readNullableStrongBinder(val);
2240 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002241 if (!mServiceFuzzing) {
2242 ALOGW("Expecting binder but got null!");
2243 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002244 status = UNEXPECTED_NULL;
2245 }
2246 return status;
2247}
2248
2249status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2250{
Steven Morelanda86a3562019-08-01 23:28:34 +00002251 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002252}
2253
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254sp<IBinder> Parcel::readStrongBinder() const
2255{
2256 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002257 // Note that a lot of code in Android reads binders by hand with this
2258 // method, and that code has historically been ok with getting nullptr
2259 // back (while ignoring error codes).
2260 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002261 return val;
2262}
2263
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002264int32_t Parcel::readExceptionCode() const
2265{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002266 binder::Status status;
2267 status.readFromParcel(*this);
2268 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002269}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002270
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002271#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002272native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002273{
2274 int numFds, numInts;
2275 status_t err;
2276 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002277 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002278 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002279 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002280
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002281 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002282 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002283 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002284 }
2285
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002286 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002287 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002288 if (h->data[i] < 0) {
2289 for (int j = 0; j < i; j++) {
2290 close(h->data[j]);
2291 }
2292 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002293 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002294 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002295 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002296 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002297 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002298 native_handle_close(h);
2299 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002300 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002301 }
2302 return h;
2303}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002304#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002305
Frederick Mayle69a0c992022-05-26 20:38:39 +00002306int Parcel::readFileDescriptor() const {
2307 if (const auto* rpcFields = maybeRpcFields()) {
2308 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2309 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002310 if (!mServiceFuzzing) {
2311 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2312 "the object list",
2313 this, mDataPos);
2314 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002315 return BAD_TYPE;
2316 }
2317
2318 int32_t objectType = readInt32();
2319 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2320 return BAD_TYPE;
2321 }
2322
2323 int32_t fdIndex = readInt32();
2324 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2325 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2326 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2327 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2328 return BAD_VALUE;
2329 }
2330 return toRawFd(rpcFields->mFds->at(fdIndex));
2331 }
2332
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002333#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002335
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002336 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002337 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002338 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002339
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002340 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002341#else // BINDER_WITH_KERNEL_IPC
2342 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2343 return INVALID_OPERATION;
2344#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345}
2346
Frederick Mayle69a0c992022-05-26 20:38:39 +00002347int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002348 int32_t hasComm = readInt32();
2349 int fd = readFileDescriptor();
2350 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002351 // detach (owned by the binder driver)
2352 int comm = readFileDescriptor();
2353
2354 // warning: this must be kept in sync with:
2355 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2356 enum ParcelFileDescriptorStatus {
2357 DETACHED = 2,
2358 };
2359
2360#if BYTE_ORDER == BIG_ENDIAN
2361 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2362#endif
2363#if BYTE_ORDER == LITTLE_ENDIAN
2364 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2365#endif
2366
2367 ssize_t written = TEMP_FAILURE_RETRY(
2368 ::write(comm, &message, sizeof(message)));
2369
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002370 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002371 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2372 written, strerror(errno));
2373 return BAD_TYPE;
2374 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002375 }
2376 return fd;
2377}
2378
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002379status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002380 int got = readFileDescriptor();
2381
2382 if (got == BAD_TYPE) {
2383 return BAD_TYPE;
2384 }
2385
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002386 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002387 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002388 return BAD_VALUE;
2389 }
2390
2391 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002392
2393 if (val->get() < 0) {
2394 return BAD_VALUE;
2395 }
2396
2397 return OK;
2398}
2399
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002400status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002401 int got = readParcelFileDescriptor();
2402
2403 if (got == BAD_TYPE) {
2404 return BAD_TYPE;
2405 }
2406
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002407 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002408 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002409 return BAD_VALUE;
2410 }
2411
2412 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002413
2414 if (val->get() < 0) {
2415 return BAD_VALUE;
2416 }
2417
2418 return OK;
2419}
Casey Dahlin06673e32015-11-23 13:24:23 -08002420
Jeff Brown5707dbf2011-09-23 21:17:56 -07002421status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2422{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002423#ifdef BINDER_DISABLE_BLOB
2424 (void)len;
2425 (void)outBlob;
2426 return INVALID_OPERATION;
2427#else
Jeff Brown13b16042014-11-11 16:44:25 -08002428 int32_t blobType;
2429 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002430 if (status) return status;
2431
Jeff Brown13b16042014-11-11 16:44:25 -08002432 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002433 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002434 const void* ptr = readInplace(len);
2435 if (!ptr) return BAD_VALUE;
2436
Jeff Brown13b16042014-11-11 16:44:25 -08002437 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002438 return NO_ERROR;
2439 }
2440
Steve Block6807e592011-10-20 11:56:00 +01002441 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002442 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002443 int fd = readFileDescriptor();
2444 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2445
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002446 if (!ashmem_valid(fd)) {
2447 ALOGE("invalid fd");
2448 return BAD_VALUE;
2449 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002450 int size = ashmem_get_size_region(fd);
2451 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002452 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002453 return BAD_VALUE;
2454 }
Yi Kong91635562018-06-07 14:38:36 -07002455 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002456 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002457 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002458
Jeff Brown13b16042014-11-11 16:44:25 -08002459 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002460 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002461#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002462}
2463
Mathias Agopiane1424282013-07-29 21:24:40 -07002464status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002465{
2466 // size
2467 const size_t len = this->readInt32();
2468 const size_t fd_count = this->readInt32();
2469
Andrei Homescu1519b982022-06-09 02:04:44 +00002470 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002471 // don't accept size_t values which may have come from an
2472 // inadvertent conversion from a negative int.
2473 return BAD_VALUE;
2474 }
2475
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002476 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002477 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002478 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002479 return BAD_VALUE;
2480
Yi Kong91635562018-06-07 14:38:36 -07002481 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002482 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002483 fds = new (std::nothrow) int[fd_count];
2484 if (fds == nullptr) {
2485 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2486 return BAD_VALUE;
2487 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002488 }
2489
2490 status_t err = NO_ERROR;
2491 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002492 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002493 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002494 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002495 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 -07002496 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2497 // Close all the file descriptors that were dup-ed.
2498 for (size_t j=0; j<i ;j++) {
2499 close(fds[j]);
2500 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002501 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002502 }
2503
2504 if (err == NO_ERROR) {
2505 err = val.unflatten(buf, len, fds, fd_count);
2506 }
2507
2508 if (fd_count) {
2509 delete [] fds;
2510 }
2511
2512 return err;
2513}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002514
2515#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002516const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2517{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002518 const auto* kernelFields = maybeKernelFields();
2519 if (kernelFields == nullptr) {
2520 return nullptr;
2521 }
2522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 const size_t DPOS = mDataPos;
2524 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2525 const flat_binder_object* obj
2526 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2527 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002528 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002529 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 // the object list, so we don't want to check for it when
2531 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002532 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 return obj;
2534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002535
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002537 binder_size_t* const OBJS = kernelFields->mObjects;
2538 const size_t N = kernelFields->mObjectsSize;
2539 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002540
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002541 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002542 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002544
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002545 // Start at the current hint position, looking for an object at
2546 // the current data position.
2547 if (opos < N) {
2548 while (opos < (N-1) && OBJS[opos] < DPOS) {
2549 opos++;
2550 }
2551 } else {
2552 opos = N-1;
2553 }
2554 if (OBJS[opos] == DPOS) {
2555 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002556 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002557 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002558 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002559 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 return obj;
2561 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002562
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002563 // Look backwards for it...
2564 while (opos > 0 && OBJS[opos] > DPOS) {
2565 opos--;
2566 }
2567 if (OBJS[opos] == DPOS) {
2568 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002569 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002571 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002572 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 return obj;
2574 }
2575 }
Steven Moreland3a667492023-06-02 21:41:39 +00002576 if (!mServiceFuzzing) {
2577 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2578 "list",
2579 this, DPOS);
2580 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 }
Yi Kong91635562018-06-07 14:38:36 -07002582 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002583}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002584#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585
Frederick Mayled3c595a2022-05-09 23:02:54 +00002586void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002587 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002588#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002589 size_t i = kernelFields->mObjectsSize;
2590 if (i > 0) {
2591 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002592 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002593 while (i > 0) {
2594 i--;
2595 const flat_binder_object* flat =
2596 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2597 if (flat->hdr.type == BINDER_TYPE_FD) {
2598 // ALOGI("Closing fd: %ld", flat->handle);
2599 close(flat->handle);
2600 }
2601 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002602#else // BINDER_WITH_KERNEL_IPC
2603 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2604#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002605 } else if (auto* rpcFields = maybeRpcFields()) {
2606 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002607 }
2608}
2609
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002610uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002612 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002613}
2614
2615size_t Parcel::ipcDataSize() const
2616{
2617 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2618}
2619
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002620uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002622 if (const auto* kernelFields = maybeKernelFields()) {
2623 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2624 }
2625 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002626}
2627
2628size_t Parcel::ipcObjectsCount() const
2629{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002630 if (const auto* kernelFields = maybeKernelFields()) {
2631 return kernelFields->mObjectsSize;
2632 }
2633 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002634}
2635
Frederick Mayled3c595a2022-05-09 23:02:54 +00002636void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2637 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002638 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2639 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2640
Steven Morelandceed9bb2020-12-17 01:01:06 +00002641 freeData();
2642
Frederick Mayled3c595a2022-05-09 23:02:54 +00002643 auto* kernelFields = maybeKernelFields();
2644 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002646 mData = const_cast<uint8_t*>(data);
2647 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002648 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2649 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002650 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002651
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002652#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002653 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002654 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2655 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002656 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002657 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002658 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002659 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002660 break;
2661 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002662 const flat_binder_object* flat
2663 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2664 uint32_t type = flat->hdr.type;
2665 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2666 type == BINDER_TYPE_FD)) {
2667 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2668 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002669 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002670 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002671 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002672 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2673 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002674
2675 // WARNING: callers of ipcSetDataReference need to make sure they
2676 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002677 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002678 break;
2679 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002680 minOffset = offset + sizeof(flat_binder_object);
2681 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002682 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002683#else // BINDER_WITH_KERNEL_IPC
2684 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2685 "Non-zero objects count passed to Parcel with kernel driver disabled");
2686#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002687}
2688
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002689status_t Parcel::rpcSetDataReference(
2690 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2691 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002692 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002693 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2694 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2695
2696 LOG_ALWAYS_FATAL_IF(session == nullptr);
2697
Frederick Mayle694e9732022-07-15 00:24:58 +00002698 if (objectTableSize != ancillaryFds.size()) {
2699 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2700 relFunc(data, dataSize, nullptr, 0);
2701 return BAD_VALUE;
2702 }
2703 for (size_t i = 0; i < objectTableSize; i++) {
2704 uint32_t minObjectEnd;
2705 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2706 minObjectEnd >= dataSize) {
2707 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2708 objectTable[i], dataSize);
2709 relFunc(data, dataSize, nullptr, 0);
2710 return BAD_VALUE;
2711 }
2712 }
2713
Frederick Mayled3c595a2022-05-09 23:02:54 +00002714 freeData();
2715 markForRpc(session);
2716
Frederick Mayle69a0c992022-05-26 20:38:39 +00002717 auto* rpcFields = maybeRpcFields();
2718 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2719
Frederick Mayled3c595a2022-05-09 23:02:54 +00002720 mData = const_cast<uint8_t*>(data);
2721 mDataSize = mDataCapacity = dataSize;
2722 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002723
Frederick Mayle69a0c992022-05-26 20:38:39 +00002724 rpcFields->mObjectPositions.reserve(objectTableSize);
2725 for (size_t i = 0; i < objectTableSize; i++) {
2726 rpcFields->mObjectPositions.push_back(objectTable[i]);
2727 }
2728 if (!ancillaryFds.empty()) {
2729 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002730 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002731 }
2732
2733 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002734}
2735
Pawan Wagh7063b522022-09-28 18:52:26 +00002736void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002737 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002738
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002739 if (errorCheck() != NO_ERROR) {
2740 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002741 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002742 } else if (dataSize() > 0) {
2743 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002744 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002745#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002746 if (const auto* kernelFields = maybeKernelFields()) {
2747 const binder_size_t* OBJS = kernelFields->mObjects;
2748 const size_t N = objectsCount();
2749 for (size_t i = 0; i < N; i++) {
2750 const flat_binder_object* flat =
2751 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002752 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002753 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2754 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002755 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002756#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002757 } else {
2758 to << "NULL";
2759 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002760
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002761 to << ")";
2762}
2763
2764void Parcel::releaseObjects()
2765{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002766 auto* kernelFields = maybeKernelFields();
2767 if (kernelFields == nullptr) {
2768 return;
2769 }
2770
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002771#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002772 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002773 if (i == 0) {
2774 return;
2775 }
2776 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002777 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002778 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002779 while (i > 0) {
2780 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002781 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002782 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002783 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002784#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002785}
2786
2787void Parcel::acquireObjects()
2788{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002789 auto* kernelFields = maybeKernelFields();
2790 if (kernelFields == nullptr) {
2791 return;
2792 }
2793
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002794#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002795 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002796 if (i == 0) {
2797 return;
2798 }
2799 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002800 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002801 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002802 while (i > 0) {
2803 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002804 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002805 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002807#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002808}
2809
2810void Parcel::freeData()
2811{
2812 freeDataNoInit();
2813 initState();
2814}
2815
2816void Parcel::freeDataNoInit()
2817{
2818 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002819 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002820 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002821 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002822 // Close FDs before freeing, otherwise they will leak for kernel binder.
2823 closeFileDescriptors();
2824 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002825 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002827 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002828 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002829 if (mData) {
2830 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002831 gParcelGlobalAllocSize -= mDataCapacity;
2832 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002833 if (mDeallocZero) {
2834 zeroMemory(mData, mDataSize);
2835 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002836 free(mData);
2837 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002838 auto* kernelFields = maybeKernelFields();
2839 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 }
2841}
2842
2843status_t Parcel::growData(size_t len)
2844{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002845 if (len > INT32_MAX) {
2846 // don't accept size_t values which may have come from an
2847 // inadvertent conversion from a negative int.
2848 return BAD_VALUE;
2849 }
2850
Martijn Coenen93fe5182020-01-22 10:46:25 +01002851 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2852 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002853 size_t newSize = ((mDataSize+len)*3)/2;
2854 return (newSize <= mDataSize)
2855 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002856 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002857}
2858
Steven Morelandf183fdd2020-10-27 00:12:12 +00002859static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2860 if (!zero) {
2861 return (uint8_t*)realloc(data, newCapacity);
2862 }
2863 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2864 if (!newData) {
2865 return nullptr;
2866 }
2867
2868 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2869 zeroMemory(data, oldCapacity);
2870 free(data);
2871 return newData;
2872}
2873
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002874status_t Parcel::restartWrite(size_t desired)
2875{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002876 if (desired > INT32_MAX) {
2877 // don't accept size_t values which may have come from an
2878 // inadvertent conversion from a negative int.
2879 return BAD_VALUE;
2880 }
2881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 if (mOwner) {
2883 freeData();
2884 return continueWrite(desired);
2885 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002886
Steven Morelandf183fdd2020-10-27 00:12:12 +00002887 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002888 if (!data && desired > mDataCapacity) {
2889 mError = NO_MEMORY;
2890 return NO_MEMORY;
2891 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002892
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002893 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002894
Devin Moore4a0a55e2020-06-04 13:23:10 -07002895 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002896 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002897 if (mDataCapacity > desired) {
2898 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2899 } else {
2900 gParcelGlobalAllocSize += (desired - mDataCapacity);
2901 }
2902
Colin Cross83ec65e2015-12-08 17:15:50 -08002903 if (!mData) {
2904 gParcelGlobalAllocCount++;
2905 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002906 mData = data;
2907 mDataCapacity = desired;
2908 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002910 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002911 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2912 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2913
Frederick Mayled3c595a2022-05-09 23:02:54 +00002914 if (auto* kernelFields = maybeKernelFields()) {
2915 free(kernelFields->mObjects);
2916 kernelFields->mObjects = nullptr;
2917 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2918 kernelFields->mNextObjectHint = 0;
2919 kernelFields->mObjectsSorted = false;
2920 kernelFields->mHasFds = false;
2921 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002922 } else if (auto* rpcFields = maybeRpcFields()) {
2923 rpcFields->mObjectPositions.clear();
2924 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002925 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002926 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002927
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002928 return NO_ERROR;
2929}
2930
2931status_t Parcel::continueWrite(size_t desired)
2932{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002933 if (desired > INT32_MAX) {
2934 // don't accept size_t values which may have come from an
2935 // inadvertent conversion from a negative int.
2936 return BAD_VALUE;
2937 }
2938
Frederick Mayled3c595a2022-05-09 23:02:54 +00002939 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002940 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002941
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002942 // If shrinking, first adjust for any objects that appear
2943 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002944 size_t objectsSize =
2945 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2946 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002947 if (desired == 0) {
2948 objectsSize = 0;
2949 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002950 if (kernelFields) {
2951 while (objectsSize > 0) {
2952 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2953 objectsSize--;
2954 }
2955 } else {
2956 while (objectsSize > 0) {
2957 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2958 objectsSize--;
2959 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002960 }
2961 }
2962 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002963
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002964 if (mOwner) {
2965 // If the size is going to zero, just release the owner's data.
2966 if (desired == 0) {
2967 freeData();
2968 return NO_ERROR;
2969 }
2970
2971 // If there is a different owner, we need to take
2972 // posession.
2973 uint8_t* data = (uint8_t*)malloc(desired);
2974 if (!data) {
2975 mError = NO_MEMORY;
2976 return NO_MEMORY;
2977 }
Yi Kong91635562018-06-07 14:38:36 -07002978 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002979
Frederick Mayle69a0c992022-05-26 20:38:39 +00002980 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002981 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002982 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002983 free(data);
2984
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002985 mError = NO_MEMORY;
2986 return NO_MEMORY;
2987 }
2988
2989 // Little hack to only acquire references on objects
2990 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002991 size_t oldObjectsSize = kernelFields->mObjectsSize;
2992 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002993 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002994 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002995 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002996 if (rpcFields) {
2997 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2998 free(data);
2999 return status;
3000 }
3001 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003002
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003003 if (mData) {
3004 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
3005 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003006 if (objects && kernelFields && kernelFields->mObjects) {
3007 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003008 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00003009 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
3010 if (kernelFields) {
3011 // TODO(b/239222407): This seems wrong. We should only free FDs when
3012 // they are in a truncated section of the parcel.
3013 closeFileDescriptors();
3014 }
3015 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003016 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003017 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003018
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003019 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003020 gParcelGlobalAllocSize += desired;
3021 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003022
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003023 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003024 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003025 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003026 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003027 if (kernelFields) {
3028 kernelFields->mObjects = objects;
3029 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3030 kernelFields->mNextObjectHint = 0;
3031 kernelFields->mObjectsSorted = false;
3032 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003033
3034 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003035 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003036#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003037 // Need to release refs on any objects we are dropping.
3038 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003039 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3040 const flat_binder_object* flat =
3041 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003042 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003043 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003044 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003045 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003046 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003047 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003048
3049 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003050 free(kernelFields->mObjects);
3051 kernelFields->mObjects = nullptr;
3052 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003053 } else {
3054 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003055 (binder_size_t*)realloc(kernelFields->mObjects,
3056 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003057 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003058 kernelFields->mObjects = objects;
3059 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003060 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003061 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003062 kernelFields->mObjectsSize = objectsSize;
3063 kernelFields->mNextObjectHint = 0;
3064 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003065#else // BINDER_WITH_KERNEL_IPC
3066 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3067#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003068 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003069 if (rpcFields) {
3070 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3071 return status;
3072 }
3073 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003074
3075 // We own the data, so we can just do a realloc().
3076 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003077 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003078 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003079 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3080 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003081 gParcelGlobalAllocSize += desired;
3082 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003083 mData = data;
3084 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003085 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003086 mError = NO_MEMORY;
3087 return NO_MEMORY;
3088 }
3089 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003090 if (mDataSize > desired) {
3091 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003092 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003093 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003094 if (mDataPos > desired) {
3095 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003096 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003097 }
3098 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003099
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003100 } else {
3101 // This is the first data. Easy!
3102 uint8_t* data = (uint8_t*)malloc(desired);
3103 if (!data) {
3104 mError = NO_MEMORY;
3105 return NO_MEMORY;
3106 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003107
Frederick Mayled3c595a2022-05-09 23:02:54 +00003108 if (!(mDataCapacity == 0 &&
3109 (kernelFields == nullptr ||
3110 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3111 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3112 kernelFields ? kernelFields->mObjects : nullptr,
3113 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003114 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003115
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003116 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003117 gParcelGlobalAllocSize += desired;
3118 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003119
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003120 mData = data;
3121 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003122 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3123 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003124 mDataCapacity = desired;
3125 }
3126
3127 return NO_ERROR;
3128}
3129
Frederick Mayle69a0c992022-05-26 20:38:39 +00003130status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3131 auto* rpcFields = maybeRpcFields();
3132 if (newObjectsSize == 0) {
3133 rpcFields->mObjectPositions.clear();
3134 if (rpcFields->mFds) {
3135 rpcFields->mFds->clear();
3136 }
3137 return OK;
3138 }
3139 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3140 uint32_t pos = rpcFields->mObjectPositions.back();
3141 rpcFields->mObjectPositions.pop_back();
3142 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3143 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3144 const auto fdIndex =
3145 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3146 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3147 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3148 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3149 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3150 return BAD_VALUE;
3151 }
3152 // In practice, this always removes the last element.
3153 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3154 }
3155 }
3156 return OK;
3157}
3158
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003159void Parcel::initState()
3160{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003161 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003162 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003163 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003164 mDataSize = 0;
3165 mDataCapacity = 0;
3166 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003167 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3168 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003169 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003170 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003171 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003172 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003173 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003174 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003175}
3176
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003177void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003178 auto* kernelFields = maybeKernelFields();
3179 if (kernelFields == nullptr) {
3180 return;
3181 }
3182 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003183 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003184 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003185}
3186
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003187#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003188size_t Parcel::getBlobAshmemSize() const
3189{
Adrian Roos6bb31142015-10-22 16:46:12 -07003190 // This used to return the size of all blobs that were written to ashmem, now we're returning
3191 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003192 // TODO(b/202029388): Remove method once ABI can be changed.
3193 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003194}
3195
Adrian Rooscbf37262015-10-22 16:12:53 -07003196size_t Parcel::getOpenAshmemSize() const
3197{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003198 auto* kernelFields = maybeKernelFields();
3199 if (kernelFields == nullptr) {
3200 return 0;
3201 }
3202
Steven Morelandc673f1f2021-10-07 18:23:35 -07003203 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003204#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003205 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003206 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003207 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003208
3209 // cookie is compared against zero for historical reasons
3210 // > obj.cookie = takeOwnership ? 1 : 0;
3211 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3212 int size = ashmem_get_size_region(flat->handle);
3213 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3214 ALOGE("Overflow when computing ashmem size.");
3215 return SIZE_MAX;
3216 }
3217 }
3218 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003219#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003220 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003221}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003222#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003223
3224// --- Parcel::Blob ---
3225
3226Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003227 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003228}
3229
3230Parcel::Blob::~Blob() {
3231 release();
3232}
3233
3234void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003235 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003236 ::munmap(mData, mSize);
3237 }
3238 clear();
3239}
3240
Jeff Brown13b16042014-11-11 16:44:25 -08003241void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3242 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003243 mData = data;
3244 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003245 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003246}
3247
3248void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003249 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003250 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003251 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003252 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003253}
3254
Steven Moreland61ff8492019-09-26 16:05:45 -07003255} // namespace android