blob: 0b94fa4be40ea5b474777acc7286569e3c93f133 [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 Morelandb6c7e222021-02-18 19:20:14 +0000848#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700849constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700850#elif defined(__ANDROID_RECOVERY__)
851constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700852#else
853constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
854#endif
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000855#endif // BINDER_WITH_KERNEL_IPC
Steven Morelandd70160f2019-07-23 10:20:38 -0700856
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700857// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700858status_t Parcel::writeInterfaceToken(const String16& interface)
859{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000860 return writeInterfaceToken(interface.c_str(), interface.size());
Steven Morelanddbc76c72020-10-01 18:02:48 +0000861}
862
863status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000864 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000865#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000866 const IPCThreadState* threadState = IPCThreadState::self();
867 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
868 updateWorkSourceRequestHeaderPosition();
869 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
870 : IPCThreadState::kUnsetWorkSource);
871 writeInt32(kHeader);
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000872#else // BINDER_WITH_KERNEL_IPC
873 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
874 return INVALID_OPERATION;
875#endif // BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000876 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000877
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700878 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000879 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700880}
881
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000882bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
883{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000884 auto* kernelFields = maybeKernelFields();
885 if (kernelFields == nullptr) {
886 return false;
887 }
888 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000889 return false;
890 }
891
892 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000893 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000894 status_t err = writeInt32(uid);
895 setDataPosition(initialPosition);
896 return err == NO_ERROR;
897}
898
Steven Morelandf1b1e492019-05-06 15:05:13 -0700899uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000900{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000901 auto* kernelFields = maybeKernelFields();
902 if (kernelFields == nullptr) {
903 return false;
904 }
905 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000906 return IPCThreadState::kUnsetWorkSource;
907 }
908
909 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000910 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000911 uid_t uid = readInt32();
912 setDataPosition(initialPosition);
913 return uid;
914}
915
Mathias Agopian83c04462009-05-22 19:00:22 -0700916bool Parcel::checkInterface(IBinder* binder) const
917{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700918 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700919}
920
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700921bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700922 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700923{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000924 return enforceInterface(interface.c_str(), interface.size(), threadState);
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700925}
926
927bool Parcel::enforceInterface(const char16_t* interface,
928 size_t len,
929 IPCThreadState* threadState) const
930{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000931 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000932#ifdef BINDER_WITH_KERNEL_IPC
Steven Moreland3af936a2021-03-26 03:05:38 +0000933 // StrictModePolicy.
934 int32_t strictPolicy = readInt32();
935 if (threadState == nullptr) {
936 threadState = IPCThreadState::self();
937 }
938 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
939 // For one-way calls, the callee is running entirely
940 // disconnected from the caller, so disable StrictMode entirely.
941 // Not only does disk/network usage not impact the caller, but
942 // there's no way to communicate back violations anyway.
943 threadState->setStrictModePolicy(0);
944 } else {
945 threadState->setStrictModePolicy(strictPolicy);
946 }
947 // WorkSource.
948 updateWorkSourceRequestHeaderPosition();
949 int32_t workSource = readInt32();
950 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
951 // vendor header
952 int32_t header = readInt32();
Steven Moreland3a667492023-06-02 21:41:39 +0000953
954 // fuzzers skip this check, because it is for protecting the underlying ABI, but
955 // we don't want it to reduce our coverage
956 if (header != kHeader && !mServiceFuzzing) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000957 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
958 header);
959 return false;
960 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +0000961#else // BINDER_WITH_KERNEL_IPC
962 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
963 (void)threadState;
964 return false;
965#endif // BINDER_WITH_KERNEL_IPC
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700966 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000967
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100968 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700969 size_t parcel_interface_len;
970 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
971 if (len == parcel_interface_len &&
972 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700973 return true;
974 } else {
Steven Morelande99d8822023-06-02 21:56:39 +0000975 if (mServiceFuzzing) {
976 // ignore. Theoretically, this could cause a few false positives, because
977 // people could assume things about getInterfaceDescriptor if they pass
978 // this point, but it would be extremely fragile. It's more important that
979 // we fuzz with the above things read from the Parcel.
980 return true;
981 } else {
Steven Moreland3a667492023-06-02 21:41:39 +0000982 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +0000983 String8(interface, len).c_str(),
984 String8(parcel_interface, parcel_interface_len).c_str());
Steven Morelande99d8822023-06-02 21:56:39 +0000985 return false;
Steven Moreland3a667492023-06-02 21:41:39 +0000986 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700987 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700988}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700989
Pawan Wagh104654a2022-11-15 21:18:42 +0000990void Parcel::setEnforceNoDataAvail(bool enforceNoDataAvail) {
991 mEnforceNoDataAvail = enforceNoDataAvail;
992}
993
Steven Moreland3a667492023-06-02 21:41:39 +0000994void Parcel::setServiceFuzzing() {
995 mServiceFuzzing = true;
996}
997
Steven Moreland418914a2023-06-28 21:47:14 +0000998bool Parcel::isServiceFuzzing() const {
999 return mServiceFuzzing;
1000}
1001
Jooyung Hand23f9502021-12-23 14:39:57 +09001002binder::Status Parcel::enforceNoDataAvail() const {
Pawan Wagh104654a2022-11-15 21:18:42 +00001003 if (!mEnforceNoDataAvail) {
1004 return binder::Status::ok();
1005 }
1006
Jooyung Hand23f9502021-12-23 14:39:57 +09001007 const auto n = dataAvail();
1008 if (n == 0) {
1009 return binder::Status::ok();
1010 }
1011 return binder::Status::
1012 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
1013 String8::format("Parcel data not fully consumed, unread size: %zu",
1014 n));
1015}
1016
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001017size_t Parcel::objectsCount() const
1018{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001019 if (const auto* kernelFields = maybeKernelFields()) {
1020 return kernelFields->mObjectsSize;
1021 }
1022 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001023}
1024
1025status_t Parcel::errorCheck() const
1026{
1027 return mError;
1028}
1029
1030void Parcel::setError(status_t err)
1031{
1032 mError = err;
1033}
1034
1035status_t Parcel::finishWrite(size_t len)
1036{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001037 if (len > INT32_MAX) {
1038 // don't accept size_t values which may have come from an
1039 // inadvertent conversion from a negative int.
1040 return BAD_VALUE;
1041 }
1042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043 //printf("Finish write of %d\n", len);
1044 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001045 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001046 if (mDataPos > mDataSize) {
1047 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001048 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049 }
1050 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
1051 return NO_ERROR;
1052}
1053
1054status_t Parcel::writeUnpadded(const void* data, size_t len)
1055{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001056 if (len > INT32_MAX) {
1057 // don't accept size_t values which may have come from an
1058 // inadvertent conversion from a negative int.
1059 return BAD_VALUE;
1060 }
1061
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001062 size_t end = mDataPos + len;
1063 if (end < mDataPos) {
1064 // integer overflow
1065 return BAD_VALUE;
1066 }
1067
1068 if (end <= mDataCapacity) {
1069restart_write:
1070 memcpy(mData+mDataPos, data, len);
1071 return finishWrite(len);
1072 }
1073
1074 status_t err = growData(len);
1075 if (err == NO_ERROR) goto restart_write;
1076 return err;
1077}
1078
1079status_t Parcel::write(const void* data, size_t len)
1080{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001081 if (len > INT32_MAX) {
1082 // don't accept size_t values which may have come from an
1083 // inadvertent conversion from a negative int.
1084 return BAD_VALUE;
1085 }
1086
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001087 void* const d = writeInplace(len);
1088 if (d) {
1089 memcpy(d, data, len);
1090 return NO_ERROR;
1091 }
1092 return mError;
1093}
1094
1095void* Parcel::writeInplace(size_t len)
1096{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001097 if (len > INT32_MAX) {
1098 // don't accept size_t values which may have come from an
1099 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001100 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001101 }
1102
1103 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001105 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001106 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001107 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001108 }
1109
1110 if ((mDataPos+padded) <= mDataCapacity) {
1111restart_write:
1112 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1113 uint8_t* const data = mData+mDataPos;
1114
1115 // Need to pad at end?
1116 if (padded != len) {
1117#if BYTE_ORDER == BIG_ENDIAN
1118 static const uint32_t mask[4] = {
1119 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1120 };
1121#endif
1122#if BYTE_ORDER == LITTLE_ENDIAN
1123 static const uint32_t mask[4] = {
1124 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1125 };
1126#endif
1127 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1128 // *reinterpret_cast<void**>(data+padded-4));
1129 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1130 }
1131
1132 finishWrite(padded);
1133 return data;
1134 }
1135
1136 status_t err = growData(padded);
1137 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001138 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001139}
1140
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001141status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1142 const uint8_t* strData = (uint8_t*)str.data();
1143 const size_t strLen= str.length();
1144 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001145 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001146 return BAD_VALUE;
1147 }
1148
1149 status_t err = writeInt32(utf16Len);
1150 if (err) {
1151 return err;
1152 }
1153
1154 // Allocate enough bytes to hold our converted string and its terminating NULL.
1155 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1156 if (!dst) {
1157 return NO_MEMORY;
1158 }
1159
Sergio Girof4607432016-07-21 14:46:35 +01001160 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001161
1162 return NO_ERROR;
1163}
1164
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001165
Andy Hung49198cf2020-11-18 11:02:39 -08001166status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1167status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001168
Andy Hung49198cf2020-11-18 11:02:39 -08001169status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1170status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001171
Andy Hung49198cf2020-11-18 11:02:39 -08001172status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1173status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1174status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1175status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1176status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1177status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1178status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1179status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1180status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1181status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1182status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1183status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1184status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1185status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1186status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1187status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1188status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1189status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1190status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1191status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1192status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1193status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1194status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1195status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1196status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1197status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1198status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001199
Andy Hung49198cf2020-11-18 11:02:39 -08001200status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001201status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001202 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001203status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001204 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001205status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001206 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001207status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001208 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1209status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001210
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001211status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<unique_fd>& val) {
1212 return writeData(val);
1213}
1214status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<unique_fd>>& val) {
1215 return writeData(val);
1216}
1217status_t Parcel::writeUniqueFileDescriptorVector(
1218 const std::unique_ptr<std::vector<unique_fd>>& val) {
1219 return writeData(val);
1220}
Andy Hung49198cf2020-11-18 11:02:39 -08001221
1222status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1223status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1224status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1225
1226status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1227
1228status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1229status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1230
1231status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1232status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1233
1234status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1235status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1236status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1237status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1238status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1239status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1240status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1241status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1242status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1243status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1244status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1245status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1246status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1247status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1248status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1249status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1250status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1251status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1252status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1253status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1254status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1255status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1256status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1257status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1258status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1259status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1260status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1261
1262status_t Parcel::readString16Vector(
1263 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1264status_t Parcel::readString16Vector(
1265 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1266status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1267status_t Parcel::readUtf8VectorFromUtf16Vector(
1268 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1269status_t Parcel::readUtf8VectorFromUtf16Vector(
1270 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1271status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1272
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001273status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<unique_fd>>* val) const {
1274 return readData(val);
1275}
1276status_t Parcel::readUniqueFileDescriptorVector(
1277 std::unique_ptr<std::vector<unique_fd>>* val) const {
1278 return readData(val);
1279}
1280status_t Parcel::readUniqueFileDescriptorVector(std::vector<unique_fd>* val) const {
1281 return readData(val);
1282}
Andy Hung49198cf2020-11-18 11:02:39 -08001283
1284status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1285status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1286status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1287
1288status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001289
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001290status_t Parcel::writeInt32(int32_t val)
1291{
Andreas Huber84a6d042009-08-17 13:33:27 -07001292 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001293}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001294
1295status_t Parcel::writeUint32(uint32_t val)
1296{
1297 return writeAligned(val);
1298}
1299
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001300status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001301 if (len > INT32_MAX) {
1302 // don't accept size_t values which may have come from an
1303 // inadvertent conversion from a negative int.
1304 return BAD_VALUE;
1305 }
1306
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001307 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001308 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001309 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001310 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001311 if (ret == NO_ERROR) {
1312 ret = write(val, len * sizeof(*val));
1313 }
1314 return ret;
1315}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001316status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001317 if (len > INT32_MAX) {
1318 // don't accept size_t values which may have come from an
1319 // inadvertent conversion from a negative int.
1320 return BAD_VALUE;
1321 }
1322
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001323 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001324 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001325 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001326 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001327 if (ret == NO_ERROR) {
1328 ret = write(val, len * sizeof(*val));
1329 }
1330 return ret;
1331}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001332
Casey Dahlind6848f52015-10-15 15:44:59 -07001333status_t Parcel::writeBool(bool val)
1334{
1335 return writeInt32(int32_t(val));
1336}
1337
1338status_t Parcel::writeChar(char16_t val)
1339{
1340 return writeInt32(int32_t(val));
1341}
1342
1343status_t Parcel::writeByte(int8_t val)
1344{
1345 return writeInt32(int32_t(val));
1346}
1347
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348status_t Parcel::writeInt64(int64_t val)
1349{
Andreas Huber84a6d042009-08-17 13:33:27 -07001350 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001351}
1352
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001353status_t Parcel::writeUint64(uint64_t val)
1354{
1355 return writeAligned(val);
1356}
1357
Serban Constantinescuf683e012013-11-05 16:53:55 +00001358status_t Parcel::writePointer(uintptr_t val)
1359{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001360 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001361}
1362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363status_t Parcel::writeFloat(float val)
1364{
Andreas Huber84a6d042009-08-17 13:33:27 -07001365 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001366}
1367
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001368#if defined(__mips__) && defined(__mips_hard_float)
1369
1370status_t Parcel::writeDouble(double val)
1371{
1372 union {
1373 double d;
1374 unsigned long long ll;
1375 } u;
1376 u.d = val;
1377 return writeAligned(u.ll);
1378}
1379
1380#else
1381
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001382status_t Parcel::writeDouble(double val)
1383{
Andreas Huber84a6d042009-08-17 13:33:27 -07001384 return writeAligned(val);
1385}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001386
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001387#endif
1388
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001389status_t Parcel::writeCString(const char* str)
1390{
1391 return write(str, strlen(str)+1);
1392}
1393
1394status_t Parcel::writeString8(const String8& str)
1395{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001396 return writeString8(str.c_str(), str.size());
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001397}
1398
1399status_t Parcel::writeString8(const char* str, size_t len)
1400{
1401 if (str == nullptr) return writeInt32(-1);
1402
Jeff Sharkey18220902020-11-05 08:36:20 -07001403 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001404 status_t err = writeInt32(len);
1405 if (err == NO_ERROR) {
1406 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1407 if (data) {
1408 memcpy(data, str, len);
1409 *reinterpret_cast<char*>(data+len) = 0;
1410 return NO_ERROR;
1411 }
1412 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001413 }
1414 return err;
1415}
1416
1417status_t Parcel::writeString16(const String16& str)
1418{
Tomasz Wasilczyk0bfea2d2023-08-11 00:06:51 +00001419 return writeString16(str.c_str(), str.size());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001420}
1421
1422status_t Parcel::writeString16(const char16_t* str, size_t len)
1423{
Yi Kong91635562018-06-07 14:38:36 -07001424 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001425
Jeff Sharkey18220902020-11-05 08:36:20 -07001426 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001427 status_t err = writeInt32(len);
1428 if (err == NO_ERROR) {
1429 len *= sizeof(char16_t);
1430 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1431 if (data) {
1432 memcpy(data, str, len);
1433 *reinterpret_cast<char16_t*>(data+len) = 0;
1434 return NO_ERROR;
1435 }
1436 err = mError;
1437 }
1438 return err;
1439}
1440
1441status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1442{
Steven Morelanda86a3562019-08-01 23:28:34 +00001443 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444}
1445
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001446
Casey Dahlinb9872622015-11-25 15:09:45 -08001447status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1448 if (!parcelable) {
1449 return writeInt32(0);
1450 }
1451
1452 return writeParcelable(*parcelable);
1453}
1454
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001455#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001456status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001457{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001458 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001459 return BAD_TYPE;
1460
1461 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001462 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001463 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001465 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001466 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001468 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1469 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470
1471 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001472 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 return err;
1474 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001475 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001476 return err;
1477}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00001478#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001479
Frederick Mayle69a0c992022-05-26 20:38:39 +00001480status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1481 if (auto* rpcFields = maybeRpcFields()) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001482 std::variant<unique_fd, borrowed_fd> fdVariant;
Frederick Mayle69a0c992022-05-26 20:38:39 +00001483 if (takeOwnership) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001484 fdVariant = unique_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001485 } else {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001486 fdVariant = borrowed_fd(fd);
Frederick Mayle69a0c992022-05-26 20:38:39 +00001487 }
1488 if (!mAllowFds) {
1489 return FDS_NOT_ALLOWED;
1490 }
1491 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1492 case RpcSession::FileDescriptorTransportMode::NONE: {
1493 return FDS_NOT_ALLOWED;
1494 }
Andrei Homescu1c18a802022-08-17 04:59:01 +00001495 case RpcSession::FileDescriptorTransportMode::UNIX:
1496 case RpcSession::FileDescriptorTransportMode::TRUSTY: {
Frederick Mayle69a0c992022-05-26 20:38:39 +00001497 if (rpcFields->mFds == nullptr) {
1498 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1499 }
1500 size_t dataPos = mDataPos;
1501 if (dataPos > UINT32_MAX) {
1502 return NO_MEMORY;
1503 }
1504 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1505 return err;
1506 }
1507 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1508 return err;
1509 }
1510 rpcFields->mObjectPositions.push_back(dataPos);
1511 rpcFields->mFds->push_back(std::move(fdVariant));
1512 return OK;
1513 }
1514 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001515 }
1516
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001517#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001518 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001519 obj.hdr.type = BINDER_TYPE_FD;
T.J. Mercierca0c2cb2022-12-19 21:56:35 +00001520 obj.flags = 0;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001521 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001523 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001524 return writeObject(obj, true);
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001525#else // BINDER_WITH_KERNEL_IPC
1526 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1527 (void)fd;
1528 (void)takeOwnership;
1529 return INVALID_OPERATION;
1530#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001531}
1532
1533status_t Parcel::writeDupFileDescriptor(int fd)
1534{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001535 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001536 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001537 return err;
Jeff Brownd341c712011-11-04 20:19:33 -07001538 }
1539 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001540 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001541 close(dupFd);
1542 }
1543 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001544}
1545
Dianne Hackborn1941a402016-08-29 12:30:43 -07001546status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1547{
1548 writeInt32(0);
1549 return writeFileDescriptor(fd, takeOwnership);
1550}
1551
Ryo Hashimotobf551892018-05-31 16:58:35 +09001552status_t Parcel::writeDupParcelFileDescriptor(int fd)
1553{
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001554 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00001555 if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00001556 return err;
Ryo Hashimotobf551892018-05-31 16:58:35 +09001557 }
1558 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1559 if (err != OK) {
1560 close(dupFd);
1561 }
1562 return err;
1563}
1564
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001565status_t Parcel::writeUniqueFileDescriptor(const unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001566 return writeDupFileDescriptor(fd.get());
1567}
1568
Jeff Brown13b16042014-11-11 16:44:25 -08001569status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001570{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001571#ifdef BINDER_DISABLE_BLOB
1572 (void)len;
1573 (void)mutableCopy;
1574 (void)outBlob;
1575 return INVALID_OPERATION;
1576#else
Nick Kralevichb6b14232015-04-02 09:36:02 -07001577 if (len > INT32_MAX) {
1578 // don't accept size_t values which may have come from an
1579 // inadvertent conversion from a negative int.
1580 return BAD_VALUE;
1581 }
1582
Jeff Brown13b16042014-11-11 16:44:25 -08001583 status_t status;
1584 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001585 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001586 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001587 if (status) return status;
1588
1589 void* ptr = writeInplace(len);
1590 if (!ptr) return NO_MEMORY;
1591
Jeff Brown13b16042014-11-11 16:44:25 -08001592 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001593 return NO_ERROR;
1594 }
1595
Steve Block6807e592011-10-20 11:56:00 +01001596 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001597 int fd = ashmem_create_region("Parcel Blob", len);
1598 if (fd < 0) return NO_MEMORY;
1599
1600 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1601 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001602 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001603 } else {
Yi Kong91635562018-06-07 14:38:36 -07001604 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001605 if (ptr == MAP_FAILED) {
1606 status = -errno;
1607 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001608 if (!mutableCopy) {
1609 result = ashmem_set_prot_region(fd, PROT_READ);
1610 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001611 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001612 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001613 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001614 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001615 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001616 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001617 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001618 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001619 return NO_ERROR;
1620 }
1621 }
1622 }
1623 }
1624 ::munmap(ptr, len);
1625 }
1626 ::close(fd);
1627 return status;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00001628#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07001629}
1630
Jeff Brown13b16042014-11-11 16:44:25 -08001631status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1632{
1633 // Must match up with what's done in writeBlob.
1634 if (!mAllowFds) return FDS_NOT_ALLOWED;
1635 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1636 if (status) return status;
1637 return writeDupFileDescriptor(fd);
1638}
1639
Mathias Agopiane1424282013-07-29 21:24:40 -07001640status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001641{
1642 status_t err;
1643
1644 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001645 const size_t len = val.getFlattenedSize();
1646 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001647
Andrei Homescu1519b982022-06-09 02:04:44 +00001648 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001649 // don't accept size_t values which may have come from an
1650 // inadvertent conversion from a negative int.
1651 return BAD_VALUE;
1652 }
1653
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001654 err = this->writeInt32(len);
1655 if (err) return err;
1656
1657 err = this->writeInt32(fd_count);
1658 if (err) return err;
1659
1660 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001661 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001662 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001663 return BAD_VALUE;
1664
Yi Kong91635562018-06-07 14:38:36 -07001665 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001666 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001667 fds = new (std::nothrow) int[fd_count];
1668 if (fds == nullptr) {
1669 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1670 return BAD_VALUE;
1671 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001672 }
1673
1674 err = val.flatten(buf, len, fds, fd_count);
1675 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1676 err = this->writeDupFileDescriptor( fds[i] );
1677 }
1678
1679 if (fd_count) {
1680 delete [] fds;
1681 }
1682
1683 return err;
1684}
1685
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001686status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1687{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001688 auto* kernelFields = maybeKernelFields();
1689 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1690
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001691#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001692 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001693 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001694 if (enoughData && enoughObjects) {
1695restart_write:
1696 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001697
Christopher Tate98e67d32015-06-03 18:44:15 -07001698 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001699 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001700 if (!mAllowFds) {
1701 // fail before modifying our object index
1702 return FDS_NOT_ALLOWED;
1703 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001704 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001705 }
1706
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001707 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001708 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001709 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001710 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001711 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001712 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001713
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001714 return finishWrite(sizeof(flat_binder_object));
1715 }
1716
1717 if (!enoughData) {
1718 const status_t err = growData(sizeof(val));
1719 if (err != NO_ERROR) return err;
1720 }
1721 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001722 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1723 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1724 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001725 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001726 binder_size_t* objects =
1727 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001728 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001729 kernelFields->mObjects = objects;
1730 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001731 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001732
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001733 goto restart_write;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001734#else // BINDER_WITH_KERNEL_IPC
1735 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
1736 (void)val;
1737 (void)nullMetaData;
1738 return INVALID_OPERATION;
1739#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740}
1741
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001742status_t Parcel::writeNoException()
1743{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001744 binder::Status status;
1745 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001746}
1747
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001748status_t Parcel::validateReadData(size_t upperBound) const
1749{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001750 const auto* kernelFields = maybeKernelFields();
1751 if (kernelFields == nullptr) {
1752 // Can't validate RPC Parcel reads because the location of binder
1753 // objects is unknown.
1754 return OK;
1755 }
1756
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001757#ifdef BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001758 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001759 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1760 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001761 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001762 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1763 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001764 // For some reason the current read position is greater than the next object
1765 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001766 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001767 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001768 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001769 // Requested info overlaps with an object
Steven Moreland3a667492023-06-02 21:41:39 +00001770 if (!mServiceFuzzing) {
1771 ALOGE("Attempt to read from protected data in Parcel %p", this);
1772 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001773 return PERMISSION_DENIED;
1774 }
1775 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001776 } while (nextObject < kernelFields->mObjectsSize &&
1777 upperBound > kernelFields->mObjects[nextObject]);
1778 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001779 }
1780 return NO_ERROR;
1781 }
1782 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001783 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001784 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001785 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001786 prevObj--;
1787 if(*prevObj > *currObj) {
1788 goto data_unsorted;
1789 }
1790 currObj--;
1791 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001792 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001793 goto data_sorted;
1794
1795data_unsorted:
1796 // Insertion Sort mObjects
1797 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1798 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001799 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1800 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001801 binder_size_t temp = *iter0;
1802 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001803 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001804 *(iter1 + 1) = *iter1;
1805 iter1--;
1806 }
1807 *(iter1 + 1) = temp;
1808 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001809 kernelFields->mNextObjectHint = 0;
1810 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001811 goto data_sorted;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00001812#else // BINDER_WITH_KERNEL_IPC
1813 (void)upperBound;
1814 return NO_ERROR;
1815#endif // BINDER_WITH_KERNEL_IPC
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001816}
1817
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001818status_t Parcel::read(void* outData, size_t len) const
1819{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001820 if (len > INT32_MAX) {
1821 // don't accept size_t values which may have come from an
1822 // inadvertent conversion from a negative int.
1823 return BAD_VALUE;
1824 }
1825
1826 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1827 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001828 const auto* kernelFields = maybeKernelFields();
1829 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001830 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001831 if(err != NO_ERROR) {
1832 // Still increment the data position by the expected length
1833 mDataPos += pad_size(len);
1834 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1835 return err;
1836 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001837 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001838 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001839 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001840 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841 return NO_ERROR;
1842 }
1843 return NOT_ENOUGH_DATA;
1844}
1845
1846const void* Parcel::readInplace(size_t len) const
1847{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001848 if (len > INT32_MAX) {
1849 // don't accept size_t values which may have come from an
1850 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001851 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001852 }
1853
1854 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1855 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001856 const auto* kernelFields = maybeKernelFields();
1857 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001858 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001859 if(err != NO_ERROR) {
1860 // Still increment the data position by the expected length
1861 mDataPos += pad_size(len);
1862 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001863 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001864 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001865 }
1866
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001868 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001869 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001870 return data;
1871 }
Yi Kong91635562018-06-07 14:38:36 -07001872 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873}
1874
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001875status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1876 if (status_t status = readInt32(size); status != OK) return status;
1877 if (*size < 0) return OK; // may be null, client to handle
1878
1879 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1880
1881 // approximation, can't know max element size (e.g. if it makes heap
1882 // allocations)
1883 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1884 int32_t allocationSize;
1885 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1886
1887 // High limit of 1MB since something this big could never be returned. Could
1888 // probably scope this down, but might impact very specific usecases.
1889 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1890
1891 if (allocationSize >= kMaxAllocationSize) {
1892 return NO_MEMORY;
1893 }
1894
1895 return OK;
1896}
1897
Andreas Huber84a6d042009-08-17 13:33:27 -07001898template<class T>
1899status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001900 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001901 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001902
1903 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001904 const auto* kernelFields = maybeKernelFields();
1905 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001906 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001907 if(err != NO_ERROR) {
1908 // Still increment the data position by the expected length
1909 mDataPos += sizeof(T);
1910 return err;
1911 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001912 }
1913
Andrei Homescue33238e2022-03-29 22:50:00 +00001914 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001915 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916 return NO_ERROR;
1917 } else {
1918 return NOT_ENOUGH_DATA;
1919 }
1920}
1921
Andreas Huber84a6d042009-08-17 13:33:27 -07001922template<class T>
1923T Parcel::readAligned() const {
1924 T result;
1925 if (readAligned(&result) != NO_ERROR) {
1926 result = 0;
1927 }
1928
1929 return result;
1930}
1931
1932template<class T>
1933status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001934 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001935 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001936
1937 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1938restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001939 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001940 return finishWrite(sizeof(val));
1941 }
1942
1943 status_t err = growData(sizeof(val));
1944 if (err == NO_ERROR) goto restart_write;
1945 return err;
1946}
1947
1948status_t Parcel::readInt32(int32_t *pArg) const
1949{
1950 return readAligned(pArg);
1951}
1952
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001953int32_t Parcel::readInt32() const
1954{
Andreas Huber84a6d042009-08-17 13:33:27 -07001955 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001956}
1957
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001958status_t Parcel::readUint32(uint32_t *pArg) const
1959{
1960 return readAligned(pArg);
1961}
1962
1963uint32_t Parcel::readUint32() const
1964{
1965 return readAligned<uint32_t>();
1966}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967
1968status_t Parcel::readInt64(int64_t *pArg) const
1969{
Andreas Huber84a6d042009-08-17 13:33:27 -07001970 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001971}
1972
1973
1974int64_t Parcel::readInt64() const
1975{
Andreas Huber84a6d042009-08-17 13:33:27 -07001976 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001977}
1978
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001979status_t Parcel::readUint64(uint64_t *pArg) const
1980{
1981 return readAligned(pArg);
1982}
1983
1984uint64_t Parcel::readUint64() const
1985{
1986 return readAligned<uint64_t>();
1987}
1988
Serban Constantinescuf683e012013-11-05 16:53:55 +00001989status_t Parcel::readPointer(uintptr_t *pArg) const
1990{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001991 status_t ret;
1992 binder_uintptr_t ptr;
1993 ret = readAligned(&ptr);
1994 if (!ret)
1995 *pArg = ptr;
1996 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001997}
1998
1999uintptr_t Parcel::readPointer() const
2000{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002001 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00002002}
2003
2004
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002005status_t Parcel::readFloat(float *pArg) const
2006{
Andreas Huber84a6d042009-08-17 13:33:27 -07002007 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002008}
2009
2010
2011float Parcel::readFloat() const
2012{
Andreas Huber84a6d042009-08-17 13:33:27 -07002013 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002014}
2015
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002016#if defined(__mips__) && defined(__mips_hard_float)
2017
2018status_t Parcel::readDouble(double *pArg) const
2019{
2020 union {
2021 double d;
2022 unsigned long long ll;
2023 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01002024 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002025 status_t status;
2026 status = readAligned(&u.ll);
2027 *pArg = u.d;
2028 return status;
2029}
2030
2031double Parcel::readDouble() const
2032{
2033 union {
2034 double d;
2035 unsigned long long ll;
2036 } u;
2037 u.ll = readAligned<unsigned long long>();
2038 return u.d;
2039}
2040
2041#else
2042
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002043status_t Parcel::readDouble(double *pArg) const
2044{
Andreas Huber84a6d042009-08-17 13:33:27 -07002045 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002046}
2047
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002048double Parcel::readDouble() const
2049{
Andreas Huber84a6d042009-08-17 13:33:27 -07002050 return readAligned<double>();
2051}
2052
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08002053#endif
2054
Casey Dahlind6848f52015-10-15 15:44:59 -07002055status_t Parcel::readBool(bool *pArg) const
2056{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002057 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002058 status_t ret = readInt32(&tmp);
2059 *pArg = (tmp != 0);
2060 return ret;
2061}
2062
2063bool Parcel::readBool() const
2064{
2065 return readInt32() != 0;
2066}
2067
2068status_t Parcel::readChar(char16_t *pArg) const
2069{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002070 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002071 status_t ret = readInt32(&tmp);
2072 *pArg = char16_t(tmp);
2073 return ret;
2074}
2075
2076char16_t Parcel::readChar() const
2077{
2078 return char16_t(readInt32());
2079}
2080
2081status_t Parcel::readByte(int8_t *pArg) const
2082{
Manoj Gupta6eb62052017-07-12 10:29:15 -07002083 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07002084 status_t ret = readInt32(&tmp);
2085 *pArg = int8_t(tmp);
2086 return ret;
2087}
2088
2089int8_t Parcel::readByte() const
2090{
2091 return int8_t(readInt32());
2092}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002093
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002094status_t Parcel::readUtf8FromUtf16(std::string* str) const {
2095 size_t utf16Size = 0;
2096 const char16_t* src = readString16Inplace(&utf16Size);
2097 if (!src) {
2098 return UNEXPECTED_NULL;
2099 }
2100
2101 // Save ourselves the trouble, we're done.
2102 if (utf16Size == 0u) {
2103 str->clear();
2104 return NO_ERROR;
2105 }
2106
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002107 // Allow for closing '\0'
2108 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
2109 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002110 return BAD_VALUE;
2111 }
2112 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002113 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002114 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01002115 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
2116 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08002117 return NO_ERROR;
2118}
2119
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002120const char* Parcel::readCString() const
2121{
Steven Morelandd0d4b582019-05-17 13:14:06 -07002122 if (mDataPos < mDataSize) {
2123 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
2125 // is the string's trailing NUL within the parcel's valid bounds?
2126 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
2127 if (eos) {
2128 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07002129 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002130 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002131 return str;
2132 }
2133 }
Yi Kong91635562018-06-07 14:38:36 -07002134 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135}
2136
2137String8 Parcel::readString8() const
2138{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002139 size_t len;
2140 const char* str = readString8Inplace(&len);
2141 if (str) return String8(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002142
2143 if (!mServiceFuzzing) {
2144 ALOGE("Reading a NULL string not supported here.");
2145 }
2146
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002147 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002148}
2149
2150status_t Parcel::readString8(String8* pArg) const
2151{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002152 size_t len;
2153 const char* str = readString8Inplace(&len);
2154 if (str) {
2155 pArg->setTo(str, len);
2156 return 0;
2157 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002158 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002159 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002160 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002161}
2162
2163const char* Parcel::readString8Inplace(size_t* outLen) const
2164{
2165 int32_t size = readInt32();
2166 // watch for potential int overflow from size+1
2167 if (size >= 0 && size < INT32_MAX) {
2168 *outLen = size;
2169 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002170 if (str != nullptr) {
2171 if (str[size] == '\0') {
2172 return str;
2173 }
2174 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002175 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002176 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002177 *outLen = 0;
2178 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002179}
2180
2181String16 Parcel::readString16() const
2182{
2183 size_t len;
2184 const char16_t* str = readString16Inplace(&len);
2185 if (str) return String16(str, len);
Steven Moreland3a667492023-06-02 21:41:39 +00002186
2187 if (!mServiceFuzzing) {
2188 ALOGE("Reading a NULL string not supported here.");
2189 }
2190
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191 return String16();
2192}
2193
Casey Dahlinb9872622015-11-25 15:09:45 -08002194
Casey Dahlin451ff582015-10-19 18:12:18 -07002195status_t Parcel::readString16(String16* pArg) const
2196{
2197 size_t len;
2198 const char16_t* str = readString16Inplace(&len);
2199 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002200 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002201 return 0;
2202 } else {
2203 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002204 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002205 }
2206}
2207
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2209{
2210 int32_t size = readInt32();
2211 // watch for potential int overflow from size+1
2212 if (size >= 0 && size < INT32_MAX) {
2213 *outLen = size;
2214 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002215 if (str != nullptr) {
2216 if (str[size] == u'\0') {
2217 return str;
2218 }
2219 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 }
2221 }
2222 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002223 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224}
2225
Casey Dahlinf0c13772015-10-27 18:33:56 -07002226status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2227{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002228 status_t status = readNullableStrongBinder(val);
2229 if (status == OK && !val->get()) {
Steven Moreland3a667492023-06-02 21:41:39 +00002230 if (!mServiceFuzzing) {
2231 ALOGW("Expecting binder but got null!");
2232 }
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002233 status = UNEXPECTED_NULL;
2234 }
2235 return status;
2236}
2237
2238status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2239{
Steven Morelanda86a3562019-08-01 23:28:34 +00002240 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002241}
2242
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243sp<IBinder> Parcel::readStrongBinder() const
2244{
2245 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002246 // Note that a lot of code in Android reads binders by hand with this
2247 // method, and that code has historically been ok with getting nullptr
2248 // back (while ignoring error codes).
2249 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 return val;
2251}
2252
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002253int32_t Parcel::readExceptionCode() const
2254{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002255 binder::Status status;
2256 status.readFromParcel(*this);
2257 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002258}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002259
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002260#ifndef BINDER_DISABLE_NATIVE_HANDLE
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002261native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002262{
2263 int numFds, numInts;
2264 status_t err;
2265 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002266 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002267 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002268 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002269
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002270 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002271 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002272 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002273 }
2274
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002275 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002276 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002277 if (h->data[i] < 0) {
2278 for (int j = 0; j < i; j++) {
2279 close(h->data[j]);
2280 }
2281 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002282 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002283 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002284 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002285 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002286 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002287 native_handle_close(h);
2288 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002289 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002290 }
2291 return h;
2292}
Tomasz Wasilczyk232d0b32023-10-09 17:37:16 +00002293#endif
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002294
Frederick Mayle69a0c992022-05-26 20:38:39 +00002295int Parcel::readFileDescriptor() const {
2296 if (const auto* rpcFields = maybeRpcFields()) {
2297 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2298 rpcFields->mObjectPositions.end(), mDataPos)) {
Steven Moreland3a667492023-06-02 21:41:39 +00002299 if (!mServiceFuzzing) {
2300 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in "
2301 "the object list",
2302 this, mDataPos);
2303 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002304 return BAD_TYPE;
2305 }
2306
2307 int32_t objectType = readInt32();
2308 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2309 return BAD_TYPE;
2310 }
2311
2312 int32_t fdIndex = readInt32();
2313 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2314 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2315 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2316 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2317 return BAD_VALUE;
2318 }
2319 return toRawFd(rpcFields->mFds->at(fdIndex));
2320 }
2321
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002322#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002323 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002324
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002325 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002326 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002327 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002328
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329 return BAD_TYPE;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002330#else // BINDER_WITH_KERNEL_IPC
2331 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2332 return INVALID_OPERATION;
2333#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002334}
2335
Frederick Mayle69a0c992022-05-26 20:38:39 +00002336int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002337 int32_t hasComm = readInt32();
2338 int fd = readFileDescriptor();
2339 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002340 // detach (owned by the binder driver)
2341 int comm = readFileDescriptor();
2342
2343 // warning: this must be kept in sync with:
2344 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2345 enum ParcelFileDescriptorStatus {
2346 DETACHED = 2,
2347 };
2348
2349#if BYTE_ORDER == BIG_ENDIAN
2350 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2351#endif
2352#if BYTE_ORDER == LITTLE_ENDIAN
2353 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2354#endif
2355
2356 ssize_t written = TEMP_FAILURE_RETRY(
2357 ::write(comm, &message, sizeof(message)));
2358
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002359 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002360 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2361 written, strerror(errno));
2362 return BAD_TYPE;
2363 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002364 }
2365 return fd;
2366}
2367
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002368status_t Parcel::readUniqueFileDescriptor(unique_fd* val) const {
Casey Dahlin06673e32015-11-23 13:24:23 -08002369 int got = readFileDescriptor();
2370
2371 if (got == BAD_TYPE) {
2372 return BAD_TYPE;
2373 }
2374
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002375 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002376 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002377 return BAD_VALUE;
2378 }
2379
2380 val->reset(dupFd);
Casey Dahlin06673e32015-11-23 13:24:23 -08002381
2382 if (val->get() < 0) {
2383 return BAD_VALUE;
2384 }
2385
2386 return OK;
2387}
2388
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002389status_t Parcel::readUniqueParcelFileDescriptor(unique_fd* val) const {
Ryo Hashimotobf551892018-05-31 16:58:35 +09002390 int got = readParcelFileDescriptor();
2391
2392 if (got == BAD_TYPE) {
2393 return BAD_TYPE;
2394 }
2395
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002396 int dupFd;
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002397 if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) {
Andrei Homescu24ad36e2022-08-04 01:33:33 +00002398 return BAD_VALUE;
2399 }
2400
2401 val->reset(dupFd);
Ryo Hashimotobf551892018-05-31 16:58:35 +09002402
2403 if (val->get() < 0) {
2404 return BAD_VALUE;
2405 }
2406
2407 return OK;
2408}
Casey Dahlin06673e32015-11-23 13:24:23 -08002409
Jeff Brown5707dbf2011-09-23 21:17:56 -07002410status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2411{
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002412#ifdef BINDER_DISABLE_BLOB
2413 (void)len;
2414 (void)outBlob;
2415 return INVALID_OPERATION;
2416#else
Jeff Brown13b16042014-11-11 16:44:25 -08002417 int32_t blobType;
2418 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002419 if (status) return status;
2420
Jeff Brown13b16042014-11-11 16:44:25 -08002421 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002422 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002423 const void* ptr = readInplace(len);
2424 if (!ptr) return BAD_VALUE;
2425
Jeff Brown13b16042014-11-11 16:44:25 -08002426 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002427 return NO_ERROR;
2428 }
2429
Steve Block6807e592011-10-20 11:56:00 +01002430 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002431 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002432 int fd = readFileDescriptor();
2433 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2434
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002435 if (!ashmem_valid(fd)) {
2436 ALOGE("invalid fd");
2437 return BAD_VALUE;
2438 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002439 int size = ashmem_get_size_region(fd);
2440 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002441 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002442 return BAD_VALUE;
2443 }
Yi Kong91635562018-06-07 14:38:36 -07002444 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002445 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002446 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002447
Jeff Brown13b16042014-11-11 16:44:25 -08002448 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002449 return NO_ERROR;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00002450#endif
Jeff Brown5707dbf2011-09-23 21:17:56 -07002451}
2452
Mathias Agopiane1424282013-07-29 21:24:40 -07002453status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002454{
2455 // size
2456 const size_t len = this->readInt32();
2457 const size_t fd_count = this->readInt32();
2458
Andrei Homescu1519b982022-06-09 02:04:44 +00002459 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002460 // don't accept size_t values which may have come from an
2461 // inadvertent conversion from a negative int.
2462 return BAD_VALUE;
2463 }
2464
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002465 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002466 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002467 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002468 return BAD_VALUE;
2469
Yi Kong91635562018-06-07 14:38:36 -07002470 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002471 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002472 fds = new (std::nothrow) int[fd_count];
2473 if (fds == nullptr) {
2474 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2475 return BAD_VALUE;
2476 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002477 }
2478
2479 status_t err = NO_ERROR;
2480 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002481 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002482 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002483 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002484 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 -07002485 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2486 // Close all the file descriptors that were dup-ed.
2487 for (size_t j=0; j<i ;j++) {
2488 close(fds[j]);
2489 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002490 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002491 }
2492
2493 if (err == NO_ERROR) {
2494 err = val.unflatten(buf, len, fds, fd_count);
2495 }
2496
2497 if (fd_count) {
2498 delete [] fds;
2499 }
2500
2501 return err;
2502}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002503
2504#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002505const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2506{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002507 const auto* kernelFields = maybeKernelFields();
2508 if (kernelFields == nullptr) {
2509 return nullptr;
2510 }
2511
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 const size_t DPOS = mDataPos;
2513 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2514 const flat_binder_object* obj
2515 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2516 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002517 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002518 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002519 // the object list, so we don't want to check for it when
2520 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002521 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002522 return obj;
2523 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002524
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002526 binder_size_t* const OBJS = kernelFields->mObjects;
2527 const size_t N = kernelFields->mObjectsSize;
2528 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002529
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002530 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002531 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002533
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 // Start at the current hint position, looking for an object at
2535 // the current data position.
2536 if (opos < N) {
2537 while (opos < (N-1) && OBJS[opos] < DPOS) {
2538 opos++;
2539 }
2540 } else {
2541 opos = N-1;
2542 }
2543 if (OBJS[opos] == DPOS) {
2544 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002545 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002547 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002548 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002549 return obj;
2550 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 // Look backwards for it...
2553 while (opos > 0 && OBJS[opos] > DPOS) {
2554 opos--;
2555 }
2556 if (OBJS[opos] == DPOS) {
2557 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002558 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002559 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002560 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002561 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 return obj;
2563 }
2564 }
Steven Moreland3a667492023-06-02 21:41:39 +00002565 if (!mServiceFuzzing) {
2566 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object "
2567 "list",
2568 this, DPOS);
2569 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 }
Yi Kong91635562018-06-07 14:38:36 -07002571 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002572}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002573#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002574
Frederick Mayled3c595a2022-05-09 23:02:54 +00002575void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002576 if (auto* kernelFields = maybeKernelFields()) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002577#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002578 size_t i = kernelFields->mObjectsSize;
2579 if (i > 0) {
2580 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002581 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002582 while (i > 0) {
2583 i--;
2584 const flat_binder_object* flat =
2585 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2586 if (flat->hdr.type == BINDER_TYPE_FD) {
2587 // ALOGI("Closing fd: %ld", flat->handle);
2588 close(flat->handle);
2589 }
2590 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002591#else // BINDER_WITH_KERNEL_IPC
2592 LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time");
2593#endif // BINDER_WITH_KERNEL_IPC
Frederick Mayle69a0c992022-05-26 20:38:39 +00002594 } else if (auto* rpcFields = maybeRpcFields()) {
2595 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002596 }
2597}
2598
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002599uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002600{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002601 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002602}
2603
2604size_t Parcel::ipcDataSize() const
2605{
2606 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2607}
2608
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002609uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002610{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002611 if (const auto* kernelFields = maybeKernelFields()) {
2612 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2613 }
2614 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002615}
2616
2617size_t Parcel::ipcObjectsCount() const
2618{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002619 if (const auto* kernelFields = maybeKernelFields()) {
2620 return kernelFields->mObjectsSize;
2621 }
2622 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002623}
2624
Frederick Mayled3c595a2022-05-09 23:02:54 +00002625void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2626 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002627 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2628 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2629
Steven Morelandceed9bb2020-12-17 01:01:06 +00002630 freeData();
2631
Frederick Mayled3c595a2022-05-09 23:02:54 +00002632 auto* kernelFields = maybeKernelFields();
2633 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2634
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002635 mData = const_cast<uint8_t*>(data);
2636 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002637 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2638 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002639 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002640
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002641#ifdef BINDER_WITH_KERNEL_IPC
Steven Morelandceed9bb2020-12-17 01:01:06 +00002642 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002643 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2644 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002645 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002646 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002647 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002648 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002649 break;
2650 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002651 const flat_binder_object* flat
2652 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2653 uint32_t type = flat->hdr.type;
2654 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2655 type == BINDER_TYPE_FD)) {
2656 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2657 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002658 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002659 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002660 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002661 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2662 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002663
2664 // WARNING: callers of ipcSetDataReference need to make sure they
2665 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002666 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002667 break;
2668 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002669 minOffset = offset + sizeof(flat_binder_object);
2670 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002671 scanForFds();
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002672#else // BINDER_WITH_KERNEL_IPC
2673 LOG_ALWAYS_FATAL_IF(objectsCount != 0,
2674 "Non-zero objects count passed to Parcel with kernel driver disabled");
2675#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002676}
2677
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002678status_t Parcel::rpcSetDataReference(
2679 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2680 const uint32_t* objectTable, size_t objectTableSize,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002681 std::vector<std::variant<unique_fd, borrowed_fd>>&& ancillaryFds, release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002682 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2683 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2684
2685 LOG_ALWAYS_FATAL_IF(session == nullptr);
2686
Frederick Mayle694e9732022-07-15 00:24:58 +00002687 if (objectTableSize != ancillaryFds.size()) {
2688 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2689 relFunc(data, dataSize, nullptr, 0);
2690 return BAD_VALUE;
2691 }
2692 for (size_t i = 0; i < objectTableSize; i++) {
2693 uint32_t minObjectEnd;
2694 if (__builtin_add_overflow(objectTable[i], sizeof(RpcFields::ObjectType), &minObjectEnd) ||
2695 minObjectEnd >= dataSize) {
2696 ALOGE("received out of range object position: %" PRIu32 " (parcel size is %zu)",
2697 objectTable[i], dataSize);
2698 relFunc(data, dataSize, nullptr, 0);
2699 return BAD_VALUE;
2700 }
2701 }
2702
Frederick Mayled3c595a2022-05-09 23:02:54 +00002703 freeData();
2704 markForRpc(session);
2705
Frederick Mayle69a0c992022-05-26 20:38:39 +00002706 auto* rpcFields = maybeRpcFields();
2707 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2708
Frederick Mayled3c595a2022-05-09 23:02:54 +00002709 mData = const_cast<uint8_t*>(data);
2710 mDataSize = mDataCapacity = dataSize;
2711 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002712
Frederick Mayle69a0c992022-05-26 20:38:39 +00002713 rpcFields->mObjectPositions.reserve(objectTableSize);
2714 for (size_t i = 0; i < objectTableSize; i++) {
2715 rpcFields->mObjectPositions.push_back(objectTable[i]);
2716 }
2717 if (!ancillaryFds.empty()) {
2718 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002719 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002720 }
2721
2722 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002723}
2724
Pawan Wagh7063b522022-09-28 18:52:26 +00002725void Parcel::print(std::ostream& to, uint32_t /*flags*/) const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002726 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002728 if (errorCheck() != NO_ERROR) {
2729 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002730 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002731 } else if (dataSize() > 0) {
2732 const uint8_t* DATA = data();
Pawan Wagh7063b522022-09-28 18:52:26 +00002733 to << "\t" << HexDump(DATA, dataSize());
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002734#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002735 if (const auto* kernelFields = maybeKernelFields()) {
2736 const binder_size_t* OBJS = kernelFields->mObjects;
2737 const size_t N = objectsCount();
2738 for (size_t i = 0; i < N; i++) {
2739 const flat_binder_object* flat =
2740 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
Pawan Wagh7063b522022-09-28 18:52:26 +00002741 to << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Frederick Mayled3c595a2022-05-09 23:02:54 +00002742 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2743 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002744 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002745#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002746 } else {
2747 to << "NULL";
2748 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002749
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002750 to << ")";
2751}
2752
2753void Parcel::releaseObjects()
2754{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002755 auto* kernelFields = maybeKernelFields();
2756 if (kernelFields == nullptr) {
2757 return;
2758 }
2759
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002760#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002761 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002762 if (i == 0) {
2763 return;
2764 }
2765 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002766 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002767 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002768 while (i > 0) {
2769 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002770 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002771 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002772 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002773#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002774}
2775
2776void Parcel::acquireObjects()
2777{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002778 auto* kernelFields = maybeKernelFields();
2779 if (kernelFields == nullptr) {
2780 return;
2781 }
2782
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002783#ifdef BINDER_WITH_KERNEL_IPC
Frederick Mayled3c595a2022-05-09 23:02:54 +00002784 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002785 if (i == 0) {
2786 return;
2787 }
2788 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002789 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002790 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002791 while (i > 0) {
2792 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002793 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002794 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002795 }
Andrei Homescua9cca9c2022-05-03 04:48:01 +00002796#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002797}
2798
2799void Parcel::freeData()
2800{
2801 freeDataNoInit();
2802 initState();
2803}
2804
2805void Parcel::freeDataNoInit()
2806{
2807 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002808 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002809 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002810 auto* kernelFields = maybeKernelFields();
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002811 // Close FDs before freeing, otherwise they will leak for kernel binder.
2812 closeFileDescriptors();
2813 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00002814 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002815 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002816 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002817 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002818 if (mData) {
2819 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002820 gParcelGlobalAllocSize -= mDataCapacity;
2821 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002822 if (mDeallocZero) {
2823 zeroMemory(mData, mDataSize);
2824 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002825 free(mData);
2826 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002827 auto* kernelFields = maybeKernelFields();
2828 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002829 }
2830}
2831
2832status_t Parcel::growData(size_t len)
2833{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002834 if (len > INT32_MAX) {
2835 // don't accept size_t values which may have come from an
2836 // inadvertent conversion from a negative int.
2837 return BAD_VALUE;
2838 }
2839
Martijn Coenen93fe5182020-01-22 10:46:25 +01002840 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2841 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002842 size_t newSize = ((mDataSize+len)*3)/2;
2843 return (newSize <= mDataSize)
2844 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002845 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002846}
2847
Steven Morelandf183fdd2020-10-27 00:12:12 +00002848static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2849 if (!zero) {
2850 return (uint8_t*)realloc(data, newCapacity);
2851 }
2852 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2853 if (!newData) {
2854 return nullptr;
2855 }
2856
2857 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2858 zeroMemory(data, oldCapacity);
2859 free(data);
2860 return newData;
2861}
2862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002863status_t Parcel::restartWrite(size_t desired)
2864{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002865 if (desired > INT32_MAX) {
2866 // don't accept size_t values which may have come from an
2867 // inadvertent conversion from a negative int.
2868 return BAD_VALUE;
2869 }
2870
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002871 if (mOwner) {
2872 freeData();
2873 return continueWrite(desired);
2874 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875
Steven Morelandf183fdd2020-10-27 00:12:12 +00002876 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002877 if (!data && desired > mDataCapacity) {
2878 mError = NO_MEMORY;
2879 return NO_MEMORY;
2880 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002881
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002883
Devin Moore4a0a55e2020-06-04 13:23:10 -07002884 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002885 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002886 if (mDataCapacity > desired) {
2887 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2888 } else {
2889 gParcelGlobalAllocSize += (desired - mDataCapacity);
2890 }
2891
Colin Cross83ec65e2015-12-08 17:15:50 -08002892 if (!mData) {
2893 gParcelGlobalAllocCount++;
2894 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002895 mData = data;
2896 mDataCapacity = desired;
2897 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002899 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002900 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2901 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2902
Frederick Mayled3c595a2022-05-09 23:02:54 +00002903 if (auto* kernelFields = maybeKernelFields()) {
2904 free(kernelFields->mObjects);
2905 kernelFields->mObjects = nullptr;
2906 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2907 kernelFields->mNextObjectHint = 0;
2908 kernelFields->mObjectsSorted = false;
2909 kernelFields->mHasFds = false;
2910 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002911 } else if (auto* rpcFields = maybeRpcFields()) {
2912 rpcFields->mObjectPositions.clear();
2913 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002914 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002915 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002917 return NO_ERROR;
2918}
2919
2920status_t Parcel::continueWrite(size_t desired)
2921{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002922 if (desired > INT32_MAX) {
2923 // don't accept size_t values which may have come from an
2924 // inadvertent conversion from a negative int.
2925 return BAD_VALUE;
2926 }
2927
Frederick Mayled3c595a2022-05-09 23:02:54 +00002928 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002929 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002930
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002931 // If shrinking, first adjust for any objects that appear
2932 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002933 size_t objectsSize =
2934 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2935 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002936 if (desired == 0) {
2937 objectsSize = 0;
2938 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002939 if (kernelFields) {
2940 while (objectsSize > 0) {
2941 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2942 objectsSize--;
2943 }
2944 } else {
2945 while (objectsSize > 0) {
2946 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2947 objectsSize--;
2948 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002949 }
2950 }
2951 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002952
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002953 if (mOwner) {
2954 // If the size is going to zero, just release the owner's data.
2955 if (desired == 0) {
2956 freeData();
2957 return NO_ERROR;
2958 }
2959
2960 // If there is a different owner, we need to take
2961 // posession.
2962 uint8_t* data = (uint8_t*)malloc(desired);
2963 if (!data) {
2964 mError = NO_MEMORY;
2965 return NO_MEMORY;
2966 }
Yi Kong91635562018-06-07 14:38:36 -07002967 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002968
Frederick Mayle69a0c992022-05-26 20:38:39 +00002969 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002970 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002971 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002972 free(data);
2973
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002974 mError = NO_MEMORY;
2975 return NO_MEMORY;
2976 }
2977
2978 // Little hack to only acquire references on objects
2979 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002980 size_t oldObjectsSize = kernelFields->mObjectsSize;
2981 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002982 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002983 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002984 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002985 if (rpcFields) {
2986 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2987 free(data);
2988 return status;
2989 }
2990 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002991
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002992 if (mData) {
2993 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2994 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002995 if (objects && kernelFields && kernelFields->mObjects) {
2996 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002997 }
Frederick Mayle53b6ffe2022-07-15 20:14:01 +00002998 // ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2999 if (kernelFields) {
3000 // TODO(b/239222407): This seems wrong. We should only free FDs when
3001 // they are in a truncated section of the parcel.
3002 closeFileDescriptors();
3003 }
3004 mOwner(mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
Frederick Mayled3c595a2022-05-09 23:02:54 +00003005 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07003006 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003007
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003008 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003009 gParcelGlobalAllocSize += desired;
3010 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003011
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003012 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003013 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003014 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003015 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00003016 if (kernelFields) {
3017 kernelFields->mObjects = objects;
3018 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
3019 kernelFields->mNextObjectHint = 0;
3020 kernelFields->mObjectsSorted = false;
3021 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003022
3023 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003024 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003025#ifdef BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003026 // Need to release refs on any objects we are dropping.
3027 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00003028 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
3029 const flat_binder_object* flat =
3030 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07003031 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003032 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00003033 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003034 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07003035 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003036 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003037
3038 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003039 free(kernelFields->mObjects);
3040 kernelFields->mObjects = nullptr;
3041 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003042 } else {
3043 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003044 (binder_size_t*)realloc(kernelFields->mObjects,
3045 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003046 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003047 kernelFields->mObjects = objects;
3048 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07003049 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003050 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00003051 kernelFields->mObjectsSize = objectsSize;
3052 kernelFields->mNextObjectHint = 0;
3053 kernelFields->mObjectsSorted = false;
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003054#else // BINDER_WITH_KERNEL_IPC
3055 LOG_ALWAYS_FATAL("Non-zero numObjects for RPC Parcel");
3056#endif // BINDER_WITH_KERNEL_IPC
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003057 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00003058 if (rpcFields) {
3059 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
3060 return status;
3061 }
3062 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003063
3064 // We own the data, so we can just do a realloc().
3065 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00003066 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003067 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003068 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
3069 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003070 gParcelGlobalAllocSize += desired;
3071 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003072 mData = data;
3073 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08003074 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003075 mError = NO_MEMORY;
3076 return NO_MEMORY;
3077 }
3078 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003079 if (mDataSize > desired) {
3080 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003081 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07003082 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003083 if (mDataPos > desired) {
3084 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003085 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003086 }
3087 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003088
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003089 } else {
3090 // This is the first data. Easy!
3091 uint8_t* data = (uint8_t*)malloc(desired);
3092 if (!data) {
3093 mError = NO_MEMORY;
3094 return NO_MEMORY;
3095 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09003096
Frederick Mayled3c595a2022-05-09 23:02:54 +00003097 if (!(mDataCapacity == 0 &&
3098 (kernelFields == nullptr ||
3099 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
3100 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
3101 kernelFields ? kernelFields->mObjects : nullptr,
3102 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003103 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003104
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003105 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003106 gParcelGlobalAllocSize += desired;
3107 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003108
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003109 mData = data;
3110 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003111 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
3112 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003113 mDataCapacity = desired;
3114 }
3115
3116 return NO_ERROR;
3117}
3118
Frederick Mayle69a0c992022-05-26 20:38:39 +00003119status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
3120 auto* rpcFields = maybeRpcFields();
3121 if (newObjectsSize == 0) {
3122 rpcFields->mObjectPositions.clear();
3123 if (rpcFields->mFds) {
3124 rpcFields->mFds->clear();
3125 }
3126 return OK;
3127 }
3128 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
3129 uint32_t pos = rpcFields->mObjectPositions.back();
3130 rpcFields->mObjectPositions.pop_back();
3131 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
3132 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
3133 const auto fdIndex =
3134 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
3135 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
3136 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
3137 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
3138 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
3139 return BAD_VALUE;
3140 }
3141 // In practice, this always removes the last element.
3142 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
3143 }
3144 }
3145 return OK;
3146}
3147
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003148void Parcel::initState()
3149{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08003150 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003151 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07003152 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003153 mDataSize = 0;
3154 mDataCapacity = 0;
3155 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07003156 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
3157 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003158 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07003159 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00003160 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07003161 mOwner = nullptr;
Pawan Wagh104654a2022-11-15 21:18:42 +00003162 mEnforceNoDataAvail = true;
Steven Moreland3a667492023-06-02 21:41:39 +00003163 mServiceFuzzing = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003164}
3165
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003166void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00003167 auto* kernelFields = maybeKernelFields();
3168 if (kernelFields == nullptr) {
3169 return;
3170 }
3171 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01003172 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00003173 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003174}
3175
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003176#ifdef BINDER_WITH_KERNEL_IPC
Dan Sandleraa5c2342015-04-10 10:08:45 -04003177size_t Parcel::getBlobAshmemSize() const
3178{
Adrian Roos6bb31142015-10-22 16:46:12 -07003179 // This used to return the size of all blobs that were written to ashmem, now we're returning
3180 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07003181 // TODO(b/202029388): Remove method once ABI can be changed.
3182 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04003183}
3184
Adrian Rooscbf37262015-10-22 16:12:53 -07003185size_t Parcel::getOpenAshmemSize() const
3186{
Frederick Mayled3c595a2022-05-09 23:02:54 +00003187 auto* kernelFields = maybeKernelFields();
3188 if (kernelFields == nullptr) {
3189 return 0;
3190 }
3191
Steven Morelandc673f1f2021-10-07 18:23:35 -07003192 size_t openAshmemSize = 0;
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003193#ifndef BINDER_DISABLE_BLOB
Frederick Mayled3c595a2022-05-09 23:02:54 +00003194 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07003195 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00003196 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07003197
3198 // cookie is compared against zero for historical reasons
3199 // > obj.cookie = takeOwnership ? 1 : 0;
3200 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
3201 int size = ashmem_get_size_region(flat->handle);
3202 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
3203 ALOGE("Overflow when computing ashmem size.");
3204 return SIZE_MAX;
3205 }
3206 }
3207 }
Tomasz Wasilczykbca8f942023-10-09 17:42:37 +00003208#endif
Steven Morelandc673f1f2021-10-07 18:23:35 -07003209 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003210}
Andrei Homescua9cca9c2022-05-03 04:48:01 +00003211#endif // BINDER_WITH_KERNEL_IPC
Jeff Brown5707dbf2011-09-23 21:17:56 -07003212
3213// --- Parcel::Blob ---
3214
3215Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07003216 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003217}
3218
3219Parcel::Blob::~Blob() {
3220 release();
3221}
3222
3223void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003224 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003225 ::munmap(mData, mSize);
3226 }
3227 clear();
3228}
3229
Jeff Brown13b16042014-11-11 16:44:25 -08003230void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3231 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003232 mData = data;
3233 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003234 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003235}
3236
3237void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003238 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003239 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003240 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003241 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003242}
3243
Steven Moreland61ff8492019-09-26 16:05:45 -07003244} // namespace android