blob: 035438267a4d0d8c26d8a8d39698cfb44c5b6341 [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
Mark Salyzynabed7f72016-01-27 08:02:48 -080020#include <errno.h>
Mark Salyzyn70f36652016-02-02 10:27:03 -080021#include <fcntl.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080022#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000023#include <linux/sched.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>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080029#include <sys/stat.h>
30#include <sys/types.h>
Christopher Tatee4e0ae82016-03-24 16:03:44 -070031#include <sys/resource.h>
Mark Salyzyneab2afc2016-01-27 08:02:48 -080032#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>
Mark Salyzynabed7f72016-01-27 08:02:48 -080036#include <binder/IPCThreadState.h>
37#include <binder/Parcel.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070038#include <binder/ProcessState.h>
Steven Moreland6e5a7752019-08-05 20:30:14 -070039#include <binder/Stability.h>
Christopher Wiley09eb7492015-11-09 15:06:15 -080040#include <binder/Status.h>
Mathias Agopian002e1e52013-05-06 20:20:50 -070041#include <binder/TextOutput.h>
42
Frederick Mayle69a0c992022-05-26 20:38:39 +000043#include <android-base/scopeguard.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080044#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000045#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080046#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070048#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000049#include <utils/String8.h>
50#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070051
Steven Moreland5553ac42020-11-11 02:14:45 +000052#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070053#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000054#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000055#include "binder_module.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070057#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080058//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080059#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080060//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070061
62// ---------------------------------------------------------------------------
63
Nick Kralevichb6b14232015-04-02 09:36:02 -070064// This macro should never be used at runtime, as a too large value
65// of s could cause an integer overflow. Instead, you should always
66// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000067#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070068
69static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070070 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070071 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070072 }
73 return PAD_SIZE_UNSAFE(s);
74}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070075
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070076// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060077#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070079namespace android {
80
Steven Moreland7b102262019-08-01 15:48:43 -070081// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000082#ifdef __LP64__
83static_assert(sizeof(Parcel) == 120);
84#else
85static_assert(sizeof(Parcel) == 60);
86#endif
Steven Moreland7b102262019-08-01 15:48:43 -070087
Jeff Sharkey8994c182020-09-11 12:07:10 -060088static std::atomic<size_t> gParcelGlobalAllocCount;
89static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080090
Andrei Homescu1519b982022-06-09 02:04:44 +000091// Maximum number of file descriptors per Parcel.
92constexpr size_t kMaxFds = 1024;
Christopher Tatee4e0ae82016-03-24 16:03:44 -070093
Jeff Brown13b16042014-11-11 16:44:25 -080094// Maximum size of a blob to transfer in-place.
95static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
96
97enum {
98 BLOB_INPLACE = 0,
99 BLOB_ASHMEM_IMMUTABLE = 1,
100 BLOB_ASHMEM_MUTABLE = 2,
101};
102
Steven Morelandc673f1f2021-10-07 18:23:35 -0700103static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
104 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700105 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700106 case BINDER_TYPE_BINDER:
107 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000108 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800109 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 }
111 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700112 case BINDER_TYPE_HANDLE: {
113 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700114 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700115 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
116 b->incStrong(who);
117 }
118 return;
119 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700120 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121 return;
122 }
123 }
124
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700125 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126}
127
Steven Morelandc673f1f2021-10-07 18:23:35 -0700128static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
129 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700130 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700131 case BINDER_TYPE_BINDER:
132 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000133 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800134 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 }
136 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700137 case BINDER_TYPE_HANDLE: {
138 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700139 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700140 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
141 b->decStrong(who);
142 }
143 return;
144 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700145 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800146 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800147 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700148 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149 return;
150 }
151 }
152
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700153 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154}
155
Frederick Mayle69a0c992022-05-26 20:38:39 +0000156static int toRawFd(const std::variant<base::unique_fd, base::borrowed_fd>& v) {
157 return std::visit([](const auto& fd) { return fd.get(); }, v);
158}
159
Frederick Mayled3c595a2022-05-09 23:02:54 +0000160Parcel::RpcFields::RpcFields(const sp<RpcSession>& session) : mSession(session) {
161 LOG_ALWAYS_FATAL_IF(mSession == nullptr);
162}
163
Steven Moreland34b48cb2020-12-01 22:45:38 +0000164status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700165{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700166 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700167 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000168 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169}
170
Steven Morelanda86a3562019-08-01 23:28:34 +0000171status_t Parcel::finishUnflattenBinder(
172 const sp<IBinder>& binder, sp<IBinder>* out) const
173{
174 int32_t stability;
175 status_t status = readInt32(&stability);
176 if (status != OK) return status;
177
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000178 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
179 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000180 if (status != OK) return status;
181
182 *out = binder;
183 return OK;
184}
185
Steven Morelandbf1915b2020-07-16 22:43:02 +0000186static constexpr inline int schedPolicyMask(int policy, int priority) {
187 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
188}
189
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500190status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
191 BBinder* local = nullptr;
192 if (binder) local = binder->localBinder();
193 if (local) local->setParceled();
194
Frederick Mayled3c595a2022-05-09 23:02:54 +0000195 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000196 if (binder) {
197 status_t status = writeInt32(1); // non-null
198 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700199 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000200 // TODO(b/167966510): need to undo this if the Parcel is not sent
Frederick Mayled3c595a2022-05-09 23:02:54 +0000201 status = rpcFields->mSession->state()->onBinderLeaving(rpcFields->mSession, binder,
202 &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000203 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700204 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000205 if (status != OK) return status;
206 } else {
207 status_t status = writeInt32(0); // null
208 if (status != OK) return status;
209 }
210 return finishFlattenBinder(binder);
211 }
212
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700213 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700214
Steven Morelandbf1915b2020-07-16 22:43:02 +0000215 int schedBits = 0;
216 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
217 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700218 }
219
Yi Kong91635562018-06-07 14:38:36 -0700220 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700221 if (!local) {
222 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700223 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000224 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000225 } else {
226 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700227 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000228 return INVALID_OPERATION;
229 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700230 }
Steven Moreland99157622021-09-13 16:27:34 -0700231 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700232 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800233 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000234 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700235 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800236 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700237 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000238 int policy = local->getMinSchedulerPolicy();
239 int priority = local->getMinSchedulerPriority();
240
241 if (policy != 0 || priority != 0) {
242 // override value, since it is set explicitly
243 schedBits = schedPolicyMask(policy, priority);
244 }
Steven Moreland49c27532022-03-01 10:21:07 +0000245 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800246 if (local->isRequestingSid()) {
247 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
248 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000249 if (local->isInheritRt()) {
250 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
251 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700252 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800253 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
254 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700255 }
256 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700257 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000258 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800259 obj.binder = 0;
260 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700262
Steven Morelandbf1915b2020-07-16 22:43:02 +0000263 obj.flags |= schedBits;
264
Steven Moreland34b48cb2020-12-01 22:45:38 +0000265 status_t status = writeObject(obj, false);
266 if (status != OK) return status;
267
268 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700269}
270
Steven Morelanda86a3562019-08-01 23:28:34 +0000271status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700272{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000273 if (const auto* rpcFields = maybeRpcFields()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700274 int32_t isPresent;
275 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000276 if (status != OK) return status;
277
278 sp<IBinder> binder;
279
Steven Moreland5623d1a2021-09-10 15:45:34 -0700280 if (isPresent & 1) {
281 uint64_t addr;
282 if (status_t status = readUint64(&addr); status != OK) return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000283 if (status_t status =
284 rpcFields->mSession->state()->onBinderEntering(rpcFields->mSession, addr,
285 &binder);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000286 status != OK)
287 return status;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000288 if (status_t status =
289 rpcFields->mSession->state()->flushExcessBinderRefs(rpcFields->mSession,
290 addr, binder);
Steven Morelandd8083312021-09-22 13:37:10 -0700291 status != OK)
292 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000293 }
294
295 return finishUnflattenBinder(binder, out);
296 }
297
Steven Morelanda86a3562019-08-01 23:28:34 +0000298 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700301 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000302 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000303 sp<IBinder> binder =
304 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000305 return finishUnflattenBinder(binder, out);
306 }
307 case BINDER_TYPE_HANDLE: {
308 sp<IBinder> binder =
309 ProcessState::self()->getStrongProxyForHandle(flat->handle);
310 return finishUnflattenBinder(binder, out);
311 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700312 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700313 }
314 return BAD_TYPE;
315}
316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700317// ---------------------------------------------------------------------------
318
319Parcel::Parcel()
320{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800321 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 initState();
323}
324
325Parcel::~Parcel()
326{
327 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800328 LOG_ALLOC("Parcel %p: destroyed", this);
329}
330
331size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600332 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800333}
334
335size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600336 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337}
338
339const uint8_t* Parcel::data() const
340{
341 return mData;
342}
343
344size_t Parcel::dataSize() const
345{
346 return (mDataSize > mDataPos ? mDataSize : mDataPos);
347}
348
349size_t Parcel::dataAvail() const
350{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700351 size_t result = dataSize() - dataPosition();
352 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700353 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700354 }
355 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700356}
357
358size_t Parcel::dataPosition() const
359{
360 return mDataPos;
361}
362
363size_t Parcel::dataCapacity() const
364{
365 return mDataCapacity;
366}
367
368status_t Parcel::setDataSize(size_t size)
369{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700370 if (size > INT32_MAX) {
371 // don't accept size_t values which may have come from an
372 // inadvertent conversion from a negative int.
373 return BAD_VALUE;
374 }
375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700376 status_t err;
377 err = continueWrite(size);
378 if (err == NO_ERROR) {
379 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700380 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381 }
382 return err;
383}
384
385void Parcel::setDataPosition(size_t pos) const
386{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700387 if (pos > INT32_MAX) {
388 // don't accept size_t values which may have come from an
389 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700390 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700391 }
392
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700393 mDataPos = pos;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000394 if (const auto* kernelFields = maybeKernelFields()) {
395 kernelFields->mNextObjectHint = 0;
396 kernelFields->mObjectsSorted = false;
397 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700398}
399
400status_t Parcel::setDataCapacity(size_t size)
401{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700402 if (size > INT32_MAX) {
403 // don't accept size_t values which may have come from an
404 // inadvertent conversion from a negative int.
405 return BAD_VALUE;
406 }
407
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700408 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700409 return NO_ERROR;
410}
411
412status_t Parcel::setData(const uint8_t* buffer, size_t len)
413{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700414 if (len > INT32_MAX) {
415 // don't accept size_t values which may have come from an
416 // inadvertent conversion from a negative int.
417 return BAD_VALUE;
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 status_t err = restartWrite(len);
421 if (err == NO_ERROR) {
422 memcpy(const_cast<uint8_t*>(data()), buffer, len);
423 mDataSize = len;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000424 if (auto* kernelFields = maybeKernelFields()) {
425 kernelFields->mFdsKnown = false;
426 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700427 }
428 return err;
429}
430
Frederick Mayled3c595a2022-05-09 23:02:54 +0000431status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) {
432 if (isForRpc() != parcel->isForRpc()) {
Steven Moreland2034eff2021-10-13 11:24:35 -0700433 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
434 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000435 return BAD_TYPE;
436 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000437 if (isForRpc() && maybeRpcFields()->mSession != parcel->maybeRpcFields()->mSession) {
438 ALOGE("Cannot append Parcels from different sessions");
439 return BAD_TYPE;
440 }
Steven Moreland67753c32021-04-02 18:45:19 +0000441
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700442 status_t err;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000443 const uint8_t* data = parcel->mData;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700444 int startPos = mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700445
446 if (len == 0) {
447 return NO_ERROR;
448 }
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 // range checks against the source parcel size
457 if ((offset > parcel->mDataSize)
458 || (len > parcel->mDataSize)
459 || (offset + len > parcel->mDataSize)) {
460 return BAD_VALUE;
461 }
462
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700463 if ((mDataSize+len) > mDataCapacity) {
464 // grow data
465 err = growData(len);
466 if (err != NO_ERROR) {
467 return err;
468 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700469 }
470
471 // append data
472 memcpy(mData + mDataPos, data + offset, len);
473 mDataPos += len;
474 mDataSize += len;
475
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400476 err = NO_ERROR;
477
Frederick Mayled3c595a2022-05-09 23:02:54 +0000478 if (auto* kernelFields = maybeKernelFields()) {
479 auto* otherKernelFields = parcel->maybeKernelFields();
480 LOG_ALWAYS_FATAL_IF(otherKernelFields == nullptr);
481
482 const binder_size_t* objects = otherKernelFields->mObjects;
483 size_t size = otherKernelFields->mObjectsSize;
484 // Count objects in range
485 int firstIndex = -1, lastIndex = -2;
486 for (int i = 0; i < (int)size; i++) {
487 size_t off = objects[i];
488 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
489 if (firstIndex == -1) {
490 firstIndex = i;
491 }
492 lastIndex = i;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700493 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700494 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000495 int numObjects = lastIndex - firstIndex + 1;
496 if (numObjects > 0) {
497 const sp<ProcessState> proc(ProcessState::self());
498 // grow objects
499 if (kernelFields->mObjectsCapacity < kernelFields->mObjectsSize + numObjects) {
500 if ((size_t)numObjects > SIZE_MAX - kernelFields->mObjectsSize)
501 return NO_MEMORY; // overflow
502 if (kernelFields->mObjectsSize + numObjects > SIZE_MAX / 3)
503 return NO_MEMORY; // overflow
504 size_t newSize = ((kernelFields->mObjectsSize + numObjects) * 3) / 2;
505 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
506 binder_size_t* objects = (binder_size_t*)realloc(kernelFields->mObjects,
507 newSize * sizeof(binder_size_t));
508 if (objects == (binder_size_t*)nullptr) {
509 return NO_MEMORY;
510 }
511 kernelFields->mObjects = objects;
512 kernelFields->mObjectsCapacity = newSize;
513 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700514
Frederick Mayled3c595a2022-05-09 23:02:54 +0000515 // append and acquire objects
516 int idx = kernelFields->mObjectsSize;
517 for (int i = firstIndex; i <= lastIndex; i++) {
518 size_t off = objects[i] - offset + startPos;
519 kernelFields->mObjects[idx++] = off;
520 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700521
Frederick Mayled3c595a2022-05-09 23:02:54 +0000522 flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(mData + off);
523 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700524
Frederick Mayled3c595a2022-05-09 23:02:54 +0000525 if (flat->hdr.type == BINDER_TYPE_FD) {
526 // If this is a file descriptor, we need to dup it so the
527 // new Parcel now owns its own fd, and can declare that we
528 // officially know we have fds.
529 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
530 flat->cookie = 1;
531 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
532 if (!mAllowFds) {
533 err = FDS_NOT_ALLOWED;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536 }
537 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000538 } else {
539 auto* rpcFields = maybeRpcFields();
540 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
541 auto* otherRpcFields = parcel->maybeRpcFields();
542 if (otherRpcFields == nullptr) {
543 return BAD_TYPE;
544 }
545 if (rpcFields->mSession != otherRpcFields->mSession) {
546 return BAD_TYPE;
547 }
548
549 const size_t savedDataPos = mDataPos;
550 base::ScopeGuard scopeGuard = [&]() { mDataPos = savedDataPos; };
551
552 rpcFields->mObjectPositions.reserve(otherRpcFields->mObjectPositions.size());
553 if (otherRpcFields->mFds != nullptr) {
554 if (rpcFields->mFds == nullptr) {
555 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
556 }
557 rpcFields->mFds->reserve(otherRpcFields->mFds->size());
558 }
559 for (size_t i = 0; i < otherRpcFields->mObjectPositions.size(); i++) {
560 const binder_size_t objPos = otherRpcFields->mObjectPositions[i];
561 if (offset <= objPos && objPos < offset + len) {
562 size_t newDataPos = objPos - offset + startPos;
563 rpcFields->mObjectPositions.push_back(newDataPos);
564
565 mDataPos = newDataPos;
566 int32_t objectType;
567 if (status_t status = readInt32(&objectType); status != OK) {
568 return status;
569 }
570 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
571 continue;
572 }
573
574 if (!mAllowFds) {
575 return FDS_NOT_ALLOWED;
576 }
577
578 // Read FD, duplicate, and add to list.
579 int32_t fdIndex;
580 if (status_t status = readInt32(&fdIndex); status != OK) {
581 return status;
582 }
583 const auto& oldFd = otherRpcFields->mFds->at(fdIndex);
584 // To match kernel binder behavior, we always dup, even if the
585 // FD was unowned in the source parcel.
586 rpcFields->mFds->emplace_back(
587 base::unique_fd(fcntl(toRawFd(oldFd), F_DUPFD_CLOEXEC, 0)));
588 // Fixup the index in the data.
589 mDataPos = newDataPos + 4;
590 if (status_t status = writeInt32(rpcFields->mFds->size() - 1); status != OK) {
591 return status;
592 }
593 }
594 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700595 }
596
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400597 return err;
598}
599
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700600int Parcel::compareData(const Parcel& other) {
601 size_t size = dataSize();
602 if (size != other.dataSize()) {
603 return size < other.dataSize() ? -1 : 1;
604 }
605 return memcmp(data(), other.data(), size);
606}
607
Bernardo Rufino897b1652021-10-08 10:30:20 +0100608status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
609 size_t len, int* result) const {
610 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
611 // Don't accept size_t values which may have come from an inadvertent conversion from a
612 // negative int.
613 return BAD_VALUE;
614 }
615 size_t thisLimit;
616 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
617 return BAD_VALUE;
618 }
619 size_t otherLimit;
620 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
621 return BAD_VALUE;
622 }
623 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
624 return NO_ERROR;
625}
626
Jeff Brown13b16042014-11-11 16:44:25 -0800627bool Parcel::allowFds() const
628{
629 return mAllowFds;
630}
631
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700632bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400633{
634 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700635 if (!allowFds) {
636 mAllowFds = false;
637 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400638 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700639}
640
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700641void Parcel::restoreAllowFds(bool lastValue)
642{
643 mAllowFds = lastValue;
644}
645
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700646bool Parcel::hasFileDescriptors() const
647{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000648 if (const auto* rpcFields = maybeRpcFields()) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000649 return rpcFields->mFds != nullptr && !rpcFields->mFds->empty();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000650 }
651 auto* kernelFields = maybeKernelFields();
652 if (!kernelFields->mFdsKnown) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 scanForFds();
654 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000655 return kernelFields->mHasFds;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700656}
657
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000658std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
659 std::vector<sp<IBinder>> ret;
660
Frederick Mayled3c595a2022-05-09 23:02:54 +0000661 const auto* kernelFields = maybeKernelFields();
662 if (kernelFields == nullptr) {
663 return ret;
664 }
665
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000666 size_t initPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000667 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
668 binder_size_t offset = kernelFields->mObjects[i];
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000669 const flat_binder_object* flat =
670 reinterpret_cast<const flat_binder_object*>(mData + offset);
671 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
672
673 setDataPosition(offset);
674
675 sp<IBinder> binder = readStrongBinder();
676 if (binder != nullptr) ret.push_back(binder);
677 }
678
679 setDataPosition(initPosition);
680 return ret;
681}
682
683std::vector<int> Parcel::debugReadAllFileDescriptors() const {
684 std::vector<int> ret;
685
Frederick Mayle69a0c992022-05-26 20:38:39 +0000686 if (const auto* kernelFields = maybeKernelFields()) {
687 size_t initPosition = dataPosition();
688 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
689 binder_size_t offset = kernelFields->mObjects[i];
690 const flat_binder_object* flat =
691 reinterpret_cast<const flat_binder_object*>(mData + offset);
692 if (flat->hdr.type != BINDER_TYPE_FD) continue;
693
694 setDataPosition(offset);
695
696 int fd = readFileDescriptor();
697 LOG_ALWAYS_FATAL_IF(fd == -1);
698 ret.push_back(fd);
699 }
700 setDataPosition(initPosition);
701 } else if (const auto* rpcFields = maybeRpcFields(); rpcFields && rpcFields->mFds) {
702 for (const auto& fd : *rpcFields->mFds) {
703 ret.push_back(toRawFd(fd));
704 }
Frederick Mayled3c595a2022-05-09 23:02:54 +0000705 }
706
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000707 return ret;
708}
709
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100710status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100711 if (len > INT32_MAX || offset > INT32_MAX) {
712 // Don't accept size_t values which may have come from an inadvertent conversion from a
713 // negative int.
714 return BAD_VALUE;
715 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100716 size_t limit;
717 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100718 return BAD_VALUE;
719 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100720 *result = false;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000721 if (const auto* kernelFields = maybeKernelFields()) {
722 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
723 size_t pos = kernelFields->mObjects[i];
724 if (pos < offset) continue;
725 if (pos + sizeof(flat_binder_object) > offset + len) {
726 if (kernelFields->mObjectsSorted) {
727 break;
728 } else {
729 continue;
730 }
731 }
732 const flat_binder_object* flat =
733 reinterpret_cast<const flat_binder_object*>(mData + pos);
734 if (flat->hdr.type == BINDER_TYPE_FD) {
735 *result = true;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000736 break;
Frederick Mayled3c595a2022-05-09 23:02:54 +0000737 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100738 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000739 } else if (const auto* rpcFields = maybeRpcFields()) {
740 for (uint32_t pos : rpcFields->mObjectPositions) {
741 if (offset <= pos && pos < limit) {
742 const auto* type = reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
743 if (*type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
744 *result = true;
745 break;
746 }
747 }
Bernardo Rufino22092af2021-10-07 14:09:24 +0100748 }
749 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100750 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100751}
752
Steven Morelandf183fdd2020-10-27 00:12:12 +0000753void Parcel::markSensitive() const
754{
755 mDeallocZero = true;
756}
757
Steven Moreland5553ac42020-11-11 02:14:45 +0000758void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000759 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
760
Steven Moreland5553ac42020-11-11 02:14:45 +0000761 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700762 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000763 }
764}
765
Steven Morelandc9939062021-05-05 17:57:41 +0000766void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000767 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
768 "format must be set before data is written OR on IPC data");
769
Frederick Mayled3c595a2022-05-09 23:02:54 +0000770 mVariantFields.emplace<RpcFields>(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000771}
772
773bool Parcel::isForRpc() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000774 return std::holds_alternative<RpcFields>(mVariantFields);
Steven Moreland5553ac42020-11-11 02:14:45 +0000775}
776
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000777void Parcel::updateWorkSourceRequestHeaderPosition() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000778 auto* kernelFields = maybeKernelFields();
779 if (kernelFields == nullptr) {
780 return;
781 }
782
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000783 // Only update the request headers once. We only want to point
784 // to the first headers read/written.
Frederick Mayled3c595a2022-05-09 23:02:54 +0000785 if (!kernelFields->mRequestHeaderPresent) {
786 kernelFields->mWorkSourceRequestHeaderPosition = dataPosition();
787 kernelFields->mRequestHeaderPresent = true;
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000788 }
789}
790
Steven Morelandb6c7e222021-02-18 19:20:14 +0000791#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700792constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700793#elif defined(__ANDROID_RECOVERY__)
794constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700795#else
796constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
797#endif
798
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700799// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700800status_t Parcel::writeInterfaceToken(const String16& interface)
801{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000802 return writeInterfaceToken(interface.string(), interface.size());
803}
804
805status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Frederick Mayled3c595a2022-05-09 23:02:54 +0000806 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000807 const IPCThreadState* threadState = IPCThreadState::self();
808 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
809 updateWorkSourceRequestHeaderPosition();
810 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
811 : IPCThreadState::kUnsetWorkSource);
812 writeInt32(kHeader);
813 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000814
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700815 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000816 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700817}
818
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000819bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
820{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000821 auto* kernelFields = maybeKernelFields();
822 if (kernelFields == nullptr) {
823 return false;
824 }
825 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000826 return false;
827 }
828
829 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000830 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000831 status_t err = writeInt32(uid);
832 setDataPosition(initialPosition);
833 return err == NO_ERROR;
834}
835
Steven Morelandf1b1e492019-05-06 15:05:13 -0700836uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000837{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000838 auto* kernelFields = maybeKernelFields();
839 if (kernelFields == nullptr) {
840 return false;
841 }
842 if (!kernelFields->mRequestHeaderPresent) {
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000843 return IPCThreadState::kUnsetWorkSource;
844 }
845
846 const size_t initialPosition = dataPosition();
Frederick Mayled3c595a2022-05-09 23:02:54 +0000847 setDataPosition(kernelFields->mWorkSourceRequestHeaderPosition);
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000848 uid_t uid = readInt32();
849 setDataPosition(initialPosition);
850 return uid;
851}
852
Mathias Agopian83c04462009-05-22 19:00:22 -0700853bool Parcel::checkInterface(IBinder* binder) const
854{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700855 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700856}
857
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700858bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700859 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700860{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700861 return enforceInterface(interface.string(), interface.size(), threadState);
862}
863
864bool Parcel::enforceInterface(const char16_t* interface,
865 size_t len,
866 IPCThreadState* threadState) const
867{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000868 if (auto* kernelFields = maybeKernelFields()) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000869 // StrictModePolicy.
870 int32_t strictPolicy = readInt32();
871 if (threadState == nullptr) {
872 threadState = IPCThreadState::self();
873 }
874 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
875 // For one-way calls, the callee is running entirely
876 // disconnected from the caller, so disable StrictMode entirely.
877 // Not only does disk/network usage not impact the caller, but
878 // there's no way to communicate back violations anyway.
879 threadState->setStrictModePolicy(0);
880 } else {
881 threadState->setStrictModePolicy(strictPolicy);
882 }
883 // WorkSource.
884 updateWorkSourceRequestHeaderPosition();
885 int32_t workSource = readInt32();
886 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
887 // vendor header
888 int32_t header = readInt32();
889 if (header != kHeader) {
890 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
891 header);
892 return false;
893 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700894 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000895
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100896 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700897 size_t parcel_interface_len;
898 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
899 if (len == parcel_interface_len &&
900 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700901 return true;
902 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700903 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700904 String8(interface, len).string(),
905 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700906 return false;
907 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700908}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700909
Jooyung Hand23f9502021-12-23 14:39:57 +0900910binder::Status Parcel::enforceNoDataAvail() const {
911 const auto n = dataAvail();
912 if (n == 0) {
913 return binder::Status::ok();
914 }
915 return binder::Status::
916 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
917 String8::format("Parcel data not fully consumed, unread size: %zu",
918 n));
919}
920
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700921size_t Parcel::objectsCount() const
922{
Frederick Mayled3c595a2022-05-09 23:02:54 +0000923 if (const auto* kernelFields = maybeKernelFields()) {
924 return kernelFields->mObjectsSize;
925 }
926 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700927}
928
929status_t Parcel::errorCheck() const
930{
931 return mError;
932}
933
934void Parcel::setError(status_t err)
935{
936 mError = err;
937}
938
939status_t Parcel::finishWrite(size_t len)
940{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700941 if (len > INT32_MAX) {
942 // don't accept size_t values which may have come from an
943 // inadvertent conversion from a negative int.
944 return BAD_VALUE;
945 }
946
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700947 //printf("Finish write of %d\n", len);
948 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700949 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700950 if (mDataPos > mDataSize) {
951 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700952 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700953 }
954 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
955 return NO_ERROR;
956}
957
958status_t Parcel::writeUnpadded(const void* data, size_t len)
959{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700960 if (len > INT32_MAX) {
961 // don't accept size_t values which may have come from an
962 // inadvertent conversion from a negative int.
963 return BAD_VALUE;
964 }
965
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966 size_t end = mDataPos + len;
967 if (end < mDataPos) {
968 // integer overflow
969 return BAD_VALUE;
970 }
971
972 if (end <= mDataCapacity) {
973restart_write:
974 memcpy(mData+mDataPos, data, len);
975 return finishWrite(len);
976 }
977
978 status_t err = growData(len);
979 if (err == NO_ERROR) goto restart_write;
980 return err;
981}
982
983status_t Parcel::write(const void* data, size_t len)
984{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700985 if (len > INT32_MAX) {
986 // don't accept size_t values which may have come from an
987 // inadvertent conversion from a negative int.
988 return BAD_VALUE;
989 }
990
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991 void* const d = writeInplace(len);
992 if (d) {
993 memcpy(d, data, len);
994 return NO_ERROR;
995 }
996 return mError;
997}
998
999void* Parcel::writeInplace(size_t len)
1000{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001001 if (len > INT32_MAX) {
1002 // don't accept size_t values which may have come from an
1003 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001004 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001005 }
1006
1007 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001008
Khalid Ramadanb3d817b2021-11-18 07:02:50 +00001009 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001010 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -07001011 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001012 }
1013
1014 if ((mDataPos+padded) <= mDataCapacity) {
1015restart_write:
1016 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
1017 uint8_t* const data = mData+mDataPos;
1018
1019 // Need to pad at end?
1020 if (padded != len) {
1021#if BYTE_ORDER == BIG_ENDIAN
1022 static const uint32_t mask[4] = {
1023 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
1024 };
1025#endif
1026#if BYTE_ORDER == LITTLE_ENDIAN
1027 static const uint32_t mask[4] = {
1028 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
1029 };
1030#endif
1031 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
1032 // *reinterpret_cast<void**>(data+padded-4));
1033 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
1034 }
1035
1036 finishWrite(padded);
1037 return data;
1038 }
1039
1040 status_t err = growData(padded);
1041 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -07001042 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001043}
1044
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001045status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
1046 const uint8_t* strData = (uint8_t*)str.data();
1047 const size_t strLen= str.length();
1048 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +01001049 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001050 return BAD_VALUE;
1051 }
1052
1053 status_t err = writeInt32(utf16Len);
1054 if (err) {
1055 return err;
1056 }
1057
1058 // Allocate enough bytes to hold our converted string and its terminating NULL.
1059 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
1060 if (!dst) {
1061 return NO_MEMORY;
1062 }
1063
Sergio Girof4607432016-07-21 14:46:35 +01001064 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001065
1066 return NO_ERROR;
1067}
1068
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001069
Andy Hung49198cf2020-11-18 11:02:39 -08001070status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
1071status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001072
Andy Hung49198cf2020-11-18 11:02:39 -08001073status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
1074status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001075
Andy Hung49198cf2020-11-18 11:02:39 -08001076status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
1077status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
1078status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
1079status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
1080status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
1081status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
1082status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
1083status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
1084status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
1085status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
1086status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
1087status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
1088status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
1089status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
1090status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
1091status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
1092status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
1093status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
1094status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
1095status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
1096status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
1097status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
1098status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
1099status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
1100status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
1101status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
1102status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -07001103
Andy Hung49198cf2020-11-18 11:02:39 -08001104status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -08001105status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001106 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001107status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001108 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001109status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001110 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09001111status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -08001112 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
1113status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001114
Andy Hung49198cf2020-11-18 11:02:39 -08001115status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
1116status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
1117status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
1118
1119status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
1120status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1121status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
1122
1123status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
1124
1125status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
1126status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
1127
1128status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1129status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1130
1131status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1132status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1133status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1134status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1135status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1136status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1137status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1138status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1139status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1140status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1141status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1142status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1143status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1144status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1145status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1146status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1147status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1148status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1149status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1150status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1151status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1152status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1153status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1154status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1155status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1156status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1157status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1158
1159status_t Parcel::readString16Vector(
1160 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1161status_t Parcel::readString16Vector(
1162 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1163status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1164status_t Parcel::readUtf8VectorFromUtf16Vector(
1165 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1166status_t Parcel::readUtf8VectorFromUtf16Vector(
1167 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1168status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1169
1170status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1171status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1172status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1173
1174status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1175status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1176status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1177
1178status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001179
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001180status_t Parcel::writeInt32(int32_t val)
1181{
Andreas Huber84a6d042009-08-17 13:33:27 -07001182 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001183}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001184
1185status_t Parcel::writeUint32(uint32_t val)
1186{
1187 return writeAligned(val);
1188}
1189
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001190status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001191 if (len > INT32_MAX) {
1192 // don't accept size_t values which may have come from an
1193 // inadvertent conversion from a negative int.
1194 return BAD_VALUE;
1195 }
1196
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001197 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001198 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001199 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001200 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001201 if (ret == NO_ERROR) {
1202 ret = write(val, len * sizeof(*val));
1203 }
1204 return ret;
1205}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001206status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001207 if (len > INT32_MAX) {
1208 // don't accept size_t values which may have come from an
1209 // inadvertent conversion from a negative int.
1210 return BAD_VALUE;
1211 }
1212
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001213 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001214 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001215 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001216 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001217 if (ret == NO_ERROR) {
1218 ret = write(val, len * sizeof(*val));
1219 }
1220 return ret;
1221}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001222
Casey Dahlind6848f52015-10-15 15:44:59 -07001223status_t Parcel::writeBool(bool val)
1224{
1225 return writeInt32(int32_t(val));
1226}
1227
1228status_t Parcel::writeChar(char16_t val)
1229{
1230 return writeInt32(int32_t(val));
1231}
1232
1233status_t Parcel::writeByte(int8_t val)
1234{
1235 return writeInt32(int32_t(val));
1236}
1237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001238status_t Parcel::writeInt64(int64_t val)
1239{
Andreas Huber84a6d042009-08-17 13:33:27 -07001240 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001241}
1242
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001243status_t Parcel::writeUint64(uint64_t val)
1244{
1245 return writeAligned(val);
1246}
1247
Serban Constantinescuf683e012013-11-05 16:53:55 +00001248status_t Parcel::writePointer(uintptr_t val)
1249{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001250 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001251}
1252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253status_t Parcel::writeFloat(float val)
1254{
Andreas Huber84a6d042009-08-17 13:33:27 -07001255 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001256}
1257
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001258#if defined(__mips__) && defined(__mips_hard_float)
1259
1260status_t Parcel::writeDouble(double val)
1261{
1262 union {
1263 double d;
1264 unsigned long long ll;
1265 } u;
1266 u.d = val;
1267 return writeAligned(u.ll);
1268}
1269
1270#else
1271
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001272status_t Parcel::writeDouble(double val)
1273{
Andreas Huber84a6d042009-08-17 13:33:27 -07001274 return writeAligned(val);
1275}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001276
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001277#endif
1278
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001279status_t Parcel::writeCString(const char* str)
1280{
1281 return write(str, strlen(str)+1);
1282}
1283
1284status_t Parcel::writeString8(const String8& str)
1285{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001286 return writeString8(str.string(), str.size());
1287}
1288
1289status_t Parcel::writeString8(const char* str, size_t len)
1290{
1291 if (str == nullptr) return writeInt32(-1);
1292
Jeff Sharkey18220902020-11-05 08:36:20 -07001293 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001294 status_t err = writeInt32(len);
1295 if (err == NO_ERROR) {
1296 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1297 if (data) {
1298 memcpy(data, str, len);
1299 *reinterpret_cast<char*>(data+len) = 0;
1300 return NO_ERROR;
1301 }
1302 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001303 }
1304 return err;
1305}
1306
1307status_t Parcel::writeString16(const String16& str)
1308{
1309 return writeString16(str.string(), str.size());
1310}
1311
1312status_t Parcel::writeString16(const char16_t* str, size_t len)
1313{
Yi Kong91635562018-06-07 14:38:36 -07001314 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001315
Jeff Sharkey18220902020-11-05 08:36:20 -07001316 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001317 status_t err = writeInt32(len);
1318 if (err == NO_ERROR) {
1319 len *= sizeof(char16_t);
1320 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1321 if (data) {
1322 memcpy(data, str, len);
1323 *reinterpret_cast<char16_t*>(data+len) = 0;
1324 return NO_ERROR;
1325 }
1326 err = mError;
1327 }
1328 return err;
1329}
1330
1331status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1332{
Steven Morelanda86a3562019-08-01 23:28:34 +00001333 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001334}
1335
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001336
Casey Dahlinb9872622015-11-25 15:09:45 -08001337status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1338 if (!parcelable) {
1339 return writeInt32(0);
1340 }
1341
1342 return writeParcelable(*parcelable);
1343}
1344
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001345status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001346{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001347 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001348 return BAD_TYPE;
1349
1350 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001351 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001352 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001354 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001355 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001357 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1358 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359
1360 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001361 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 return err;
1363 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001364 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001365 return err;
1366}
1367
Frederick Mayle69a0c992022-05-26 20:38:39 +00001368status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) {
1369 if (auto* rpcFields = maybeRpcFields()) {
1370 std::variant<base::unique_fd, base::borrowed_fd> fdVariant;
1371 if (takeOwnership) {
1372 fdVariant = base::unique_fd(fd);
1373 } else {
1374 fdVariant = base::borrowed_fd(fd);
1375 }
1376 if (!mAllowFds) {
1377 return FDS_NOT_ALLOWED;
1378 }
1379 switch (rpcFields->mSession->getFileDescriptorTransportMode()) {
1380 case RpcSession::FileDescriptorTransportMode::NONE: {
1381 return FDS_NOT_ALLOWED;
1382 }
1383 case RpcSession::FileDescriptorTransportMode::UNIX: {
1384 if (rpcFields->mFds == nullptr) {
1385 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
1386 }
1387 size_t dataPos = mDataPos;
1388 if (dataPos > UINT32_MAX) {
1389 return NO_MEMORY;
1390 }
1391 if (status_t err = writeInt32(RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR); err != OK) {
1392 return err;
1393 }
1394 if (status_t err = writeInt32(rpcFields->mFds->size()); err != OK) {
1395 return err;
1396 }
1397 rpcFields->mObjectPositions.push_back(dataPos);
1398 rpcFields->mFds->push_back(std::move(fdVariant));
1399 return OK;
1400 }
1401 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001402 }
1403
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001404 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001405 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001406 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001407 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001408 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001409 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001410 return writeObject(obj, true);
1411}
1412
1413status_t Parcel::writeDupFileDescriptor(int fd)
1414{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001415 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001416 if (dupFd < 0) {
1417 return -errno;
1418 }
1419 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001420 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001421 close(dupFd);
1422 }
1423 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001424}
1425
Dianne Hackborn1941a402016-08-29 12:30:43 -07001426status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1427{
1428 writeInt32(0);
1429 return writeFileDescriptor(fd, takeOwnership);
1430}
1431
Ryo Hashimotobf551892018-05-31 16:58:35 +09001432status_t Parcel::writeDupParcelFileDescriptor(int fd)
1433{
1434 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1435 if (dupFd < 0) {
1436 return -errno;
1437 }
1438 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1439 if (err != OK) {
1440 close(dupFd);
1441 }
1442 return err;
1443}
1444
Christopher Wiley2cf19952016-04-11 11:09:37 -07001445status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001446 return writeDupFileDescriptor(fd.get());
1447}
1448
Jeff Brown13b16042014-11-11 16:44:25 -08001449status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001450{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001451 if (len > INT32_MAX) {
1452 // don't accept size_t values which may have come from an
1453 // inadvertent conversion from a negative int.
1454 return BAD_VALUE;
1455 }
1456
Jeff Brown13b16042014-11-11 16:44:25 -08001457 status_t status;
1458 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001459 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001460 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001461 if (status) return status;
1462
1463 void* ptr = writeInplace(len);
1464 if (!ptr) return NO_MEMORY;
1465
Jeff Brown13b16042014-11-11 16:44:25 -08001466 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001467 return NO_ERROR;
1468 }
1469
Steve Block6807e592011-10-20 11:56:00 +01001470 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001471 int fd = ashmem_create_region("Parcel Blob", len);
1472 if (fd < 0) return NO_MEMORY;
1473
1474 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1475 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001476 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001477 } else {
Yi Kong91635562018-06-07 14:38:36 -07001478 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001479 if (ptr == MAP_FAILED) {
1480 status = -errno;
1481 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001482 if (!mutableCopy) {
1483 result = ashmem_set_prot_region(fd, PROT_READ);
1484 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001485 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001486 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001487 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001488 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001489 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001490 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001491 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001492 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001493 return NO_ERROR;
1494 }
1495 }
1496 }
1497 }
1498 ::munmap(ptr, len);
1499 }
1500 ::close(fd);
1501 return status;
1502}
1503
Jeff Brown13b16042014-11-11 16:44:25 -08001504status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1505{
1506 // Must match up with what's done in writeBlob.
1507 if (!mAllowFds) return FDS_NOT_ALLOWED;
1508 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1509 if (status) return status;
1510 return writeDupFileDescriptor(fd);
1511}
1512
Mathias Agopiane1424282013-07-29 21:24:40 -07001513status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001514{
1515 status_t err;
1516
1517 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001518 const size_t len = val.getFlattenedSize();
1519 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001520
Andrei Homescu1519b982022-06-09 02:04:44 +00001521 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001522 // don't accept size_t values which may have come from an
1523 // inadvertent conversion from a negative int.
1524 return BAD_VALUE;
1525 }
1526
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001527 err = this->writeInt32(len);
1528 if (err) return err;
1529
1530 err = this->writeInt32(fd_count);
1531 if (err) return err;
1532
1533 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001534 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001535 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001536 return BAD_VALUE;
1537
Yi Kong91635562018-06-07 14:38:36 -07001538 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001539 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001540 fds = new (std::nothrow) int[fd_count];
1541 if (fds == nullptr) {
1542 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1543 return BAD_VALUE;
1544 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001545 }
1546
1547 err = val.flatten(buf, len, fds, fd_count);
1548 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1549 err = this->writeDupFileDescriptor( fds[i] );
1550 }
1551
1552 if (fd_count) {
1553 delete [] fds;
1554 }
1555
1556 return err;
1557}
1558
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1560{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001561 auto* kernelFields = maybeKernelFields();
1562 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr, "Can't write flat_binder_object to RPC Parcel");
1563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001565 const bool enoughObjects = kernelFields->mObjectsSize < kernelFields->mObjectsCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001566 if (enoughData && enoughObjects) {
1567restart_write:
1568 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001569
Christopher Tate98e67d32015-06-03 18:44:15 -07001570 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001571 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001572 if (!mAllowFds) {
1573 // fail before modifying our object index
1574 return FDS_NOT_ALLOWED;
1575 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001576 kernelFields->mHasFds = kernelFields->mFdsKnown = true;
Christopher Tate98e67d32015-06-03 18:44:15 -07001577 }
1578
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001580 if (nullMetaData || val.binder != 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001581 kernelFields->mObjects[kernelFields->mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001582 acquire_object(ProcessState::self(), val, this);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001583 kernelFields->mObjectsSize++;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001585
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586 return finishWrite(sizeof(flat_binder_object));
1587 }
1588
1589 if (!enoughData) {
1590 const status_t err = growData(sizeof(val));
1591 if (err != NO_ERROR) return err;
1592 }
1593 if (!enoughObjects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001594 if (kernelFields->mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1595 if ((kernelFields->mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
1596 size_t newSize = ((kernelFields->mObjectsSize + 2) * 3) / 2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001597 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Frederick Mayled3c595a2022-05-09 23:02:54 +00001598 binder_size_t* objects =
1599 (binder_size_t*)realloc(kernelFields->mObjects, newSize * sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001600 if (objects == nullptr) return NO_MEMORY;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001601 kernelFields->mObjects = objects;
1602 kernelFields->mObjectsCapacity = newSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001603 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001604
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605 goto restart_write;
1606}
1607
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001608status_t Parcel::writeNoException()
1609{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001610 binder::Status status;
1611 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001612}
1613
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001614status_t Parcel::validateReadData(size_t upperBound) const
1615{
Frederick Mayled3c595a2022-05-09 23:02:54 +00001616 const auto* kernelFields = maybeKernelFields();
1617 if (kernelFields == nullptr) {
1618 // Can't validate RPC Parcel reads because the location of binder
1619 // objects is unknown.
1620 return OK;
1621 }
1622
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001623 // Don't allow non-object reads on object data
Frederick Mayled3c595a2022-05-09 23:02:54 +00001624 if (kernelFields->mObjectsSorted || kernelFields->mObjectsSize <= 1) {
1625 data_sorted:
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001626 // Expect to check only against the next object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001627 if (kernelFields->mNextObjectHint < kernelFields->mObjectsSize &&
1628 upperBound > kernelFields->mObjects[kernelFields->mNextObjectHint]) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001629 // For some reason the current read position is greater than the next object
1630 // hint. Iterate until we find the right object
Frederick Mayled3c595a2022-05-09 23:02:54 +00001631 size_t nextObject = kernelFields->mNextObjectHint;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001632 do {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001633 if (mDataPos < kernelFields->mObjects[nextObject] + sizeof(flat_binder_object)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001634 // Requested info overlaps with an object
1635 ALOGE("Attempt to read from protected data in Parcel %p", this);
1636 return PERMISSION_DENIED;
1637 }
1638 nextObject++;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001639 } while (nextObject < kernelFields->mObjectsSize &&
1640 upperBound > kernelFields->mObjects[nextObject]);
1641 kernelFields->mNextObjectHint = nextObject;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001642 }
1643 return NO_ERROR;
1644 }
1645 // Quickly determine if mObjects is sorted.
Frederick Mayled3c595a2022-05-09 23:02:54 +00001646 binder_size_t* currObj = kernelFields->mObjects + kernelFields->mObjectsSize - 1;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001647 binder_size_t* prevObj = currObj;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001648 while (currObj > kernelFields->mObjects) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001649 prevObj--;
1650 if(*prevObj > *currObj) {
1651 goto data_unsorted;
1652 }
1653 currObj--;
1654 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001655 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001656 goto data_sorted;
1657
1658data_unsorted:
1659 // Insertion Sort mObjects
1660 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1661 // switch to std::sort(mObjects, mObjects + mObjectsSize);
Frederick Mayled3c595a2022-05-09 23:02:54 +00001662 for (binder_size_t* iter0 = kernelFields->mObjects + 1;
1663 iter0 < kernelFields->mObjects + kernelFields->mObjectsSize; iter0++) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001664 binder_size_t temp = *iter0;
1665 binder_size_t* iter1 = iter0 - 1;
Frederick Mayled3c595a2022-05-09 23:02:54 +00001666 while (iter1 >= kernelFields->mObjects && *iter1 > temp) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001667 *(iter1 + 1) = *iter1;
1668 iter1--;
1669 }
1670 *(iter1 + 1) = temp;
1671 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00001672 kernelFields->mNextObjectHint = 0;
1673 kernelFields->mObjectsSorted = true;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001674 goto data_sorted;
1675}
1676
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001677status_t Parcel::read(void* outData, size_t len) const
1678{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001679 if (len > INT32_MAX) {
1680 // don't accept size_t values which may have come from an
1681 // inadvertent conversion from a negative int.
1682 return BAD_VALUE;
1683 }
1684
1685 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1686 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001687 const auto* kernelFields = maybeKernelFields();
1688 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001689 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001690 if(err != NO_ERROR) {
1691 // Still increment the data position by the expected length
1692 mDataPos += pad_size(len);
1693 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1694 return err;
1695 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001696 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001697 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001698 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001699 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001700 return NO_ERROR;
1701 }
1702 return NOT_ENOUGH_DATA;
1703}
1704
1705const void* Parcel::readInplace(size_t len) const
1706{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001707 if (len > INT32_MAX) {
1708 // don't accept size_t values which may have come from an
1709 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001710 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001711 }
1712
1713 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1714 && len <= pad_size(len)) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001715 const auto* kernelFields = maybeKernelFields();
1716 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001717 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001718 if(err != NO_ERROR) {
1719 // Still increment the data position by the expected length
1720 mDataPos += pad_size(len);
1721 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001722 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001723 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001724 }
1725
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001726 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001727 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001728 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729 return data;
1730 }
Yi Kong91635562018-06-07 14:38:36 -07001731 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732}
1733
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001734status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1735 if (status_t status = readInt32(size); status != OK) return status;
1736 if (*size < 0) return OK; // may be null, client to handle
1737
1738 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1739
1740 // approximation, can't know max element size (e.g. if it makes heap
1741 // allocations)
1742 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1743 int32_t allocationSize;
1744 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1745
1746 // High limit of 1MB since something this big could never be returned. Could
1747 // probably scope this down, but might impact very specific usecases.
1748 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1749
1750 if (allocationSize >= kMaxAllocationSize) {
1751 return NO_MEMORY;
1752 }
1753
1754 return OK;
1755}
1756
Andreas Huber84a6d042009-08-17 13:33:27 -07001757template<class T>
1758status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001759 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001760 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001761
1762 if ((mDataPos+sizeof(T)) <= mDataSize) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00001763 const auto* kernelFields = maybeKernelFields();
1764 if (kernelFields != nullptr && kernelFields->mObjectsSize > 0) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001765 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001766 if(err != NO_ERROR) {
1767 // Still increment the data position by the expected length
1768 mDataPos += sizeof(T);
1769 return err;
1770 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001771 }
1772
Andrei Homescue33238e2022-03-29 22:50:00 +00001773 memcpy(pArg, mData + mDataPos, sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001774 mDataPos += sizeof(T);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001775 return NO_ERROR;
1776 } else {
1777 return NOT_ENOUGH_DATA;
1778 }
1779}
1780
Andreas Huber84a6d042009-08-17 13:33:27 -07001781template<class T>
1782T Parcel::readAligned() const {
1783 T result;
1784 if (readAligned(&result) != NO_ERROR) {
1785 result = 0;
1786 }
1787
1788 return result;
1789}
1790
1791template<class T>
1792status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001793 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andrei Homescue33238e2022-03-29 22:50:00 +00001794 static_assert(std::is_trivially_copyable_v<T>);
Andreas Huber84a6d042009-08-17 13:33:27 -07001795
1796 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1797restart_write:
Andrei Homescue33238e2022-03-29 22:50:00 +00001798 memcpy(mData + mDataPos, &val, sizeof(val));
Andreas Huber84a6d042009-08-17 13:33:27 -07001799 return finishWrite(sizeof(val));
1800 }
1801
1802 status_t err = growData(sizeof(val));
1803 if (err == NO_ERROR) goto restart_write;
1804 return err;
1805}
1806
1807status_t Parcel::readInt32(int32_t *pArg) const
1808{
1809 return readAligned(pArg);
1810}
1811
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001812int32_t Parcel::readInt32() const
1813{
Andreas Huber84a6d042009-08-17 13:33:27 -07001814 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815}
1816
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001817status_t Parcel::readUint32(uint32_t *pArg) const
1818{
1819 return readAligned(pArg);
1820}
1821
1822uint32_t Parcel::readUint32() const
1823{
1824 return readAligned<uint32_t>();
1825}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001826
1827status_t Parcel::readInt64(int64_t *pArg) const
1828{
Andreas Huber84a6d042009-08-17 13:33:27 -07001829 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001830}
1831
1832
1833int64_t Parcel::readInt64() const
1834{
Andreas Huber84a6d042009-08-17 13:33:27 -07001835 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001836}
1837
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001838status_t Parcel::readUint64(uint64_t *pArg) const
1839{
1840 return readAligned(pArg);
1841}
1842
1843uint64_t Parcel::readUint64() const
1844{
1845 return readAligned<uint64_t>();
1846}
1847
Serban Constantinescuf683e012013-11-05 16:53:55 +00001848status_t Parcel::readPointer(uintptr_t *pArg) const
1849{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001850 status_t ret;
1851 binder_uintptr_t ptr;
1852 ret = readAligned(&ptr);
1853 if (!ret)
1854 *pArg = ptr;
1855 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001856}
1857
1858uintptr_t Parcel::readPointer() const
1859{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001860 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001861}
1862
1863
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864status_t Parcel::readFloat(float *pArg) const
1865{
Andreas Huber84a6d042009-08-17 13:33:27 -07001866 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867}
1868
1869
1870float Parcel::readFloat() const
1871{
Andreas Huber84a6d042009-08-17 13:33:27 -07001872 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001873}
1874
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001875#if defined(__mips__) && defined(__mips_hard_float)
1876
1877status_t Parcel::readDouble(double *pArg) const
1878{
1879 union {
1880 double d;
1881 unsigned long long ll;
1882 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001883 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001884 status_t status;
1885 status = readAligned(&u.ll);
1886 *pArg = u.d;
1887 return status;
1888}
1889
1890double Parcel::readDouble() const
1891{
1892 union {
1893 double d;
1894 unsigned long long ll;
1895 } u;
1896 u.ll = readAligned<unsigned long long>();
1897 return u.d;
1898}
1899
1900#else
1901
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001902status_t Parcel::readDouble(double *pArg) const
1903{
Andreas Huber84a6d042009-08-17 13:33:27 -07001904 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001905}
1906
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001907double Parcel::readDouble() const
1908{
Andreas Huber84a6d042009-08-17 13:33:27 -07001909 return readAligned<double>();
1910}
1911
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001912#endif
1913
Casey Dahlind6848f52015-10-15 15:44:59 -07001914status_t Parcel::readBool(bool *pArg) const
1915{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001916 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001917 status_t ret = readInt32(&tmp);
1918 *pArg = (tmp != 0);
1919 return ret;
1920}
1921
1922bool Parcel::readBool() const
1923{
1924 return readInt32() != 0;
1925}
1926
1927status_t Parcel::readChar(char16_t *pArg) const
1928{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001929 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001930 status_t ret = readInt32(&tmp);
1931 *pArg = char16_t(tmp);
1932 return ret;
1933}
1934
1935char16_t Parcel::readChar() const
1936{
1937 return char16_t(readInt32());
1938}
1939
1940status_t Parcel::readByte(int8_t *pArg) const
1941{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001942 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001943 status_t ret = readInt32(&tmp);
1944 *pArg = int8_t(tmp);
1945 return ret;
1946}
1947
1948int8_t Parcel::readByte() const
1949{
1950 return int8_t(readInt32());
1951}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001952
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001953status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1954 size_t utf16Size = 0;
1955 const char16_t* src = readString16Inplace(&utf16Size);
1956 if (!src) {
1957 return UNEXPECTED_NULL;
1958 }
1959
1960 // Save ourselves the trouble, we're done.
1961 if (utf16Size == 0u) {
1962 str->clear();
1963 return NO_ERROR;
1964 }
1965
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001966 // Allow for closing '\0'
1967 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1968 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001969 return BAD_VALUE;
1970 }
1971 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001972 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001973 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001974 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1975 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001976 return NO_ERROR;
1977}
1978
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001979const char* Parcel::readCString() const
1980{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001981 if (mDataPos < mDataSize) {
1982 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001983 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1984 // is the string's trailing NUL within the parcel's valid bounds?
1985 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1986 if (eos) {
1987 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001988 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001989 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001990 return str;
1991 }
1992 }
Yi Kong91635562018-06-07 14:38:36 -07001993 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001994}
1995
1996String8 Parcel::readString8() const
1997{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001998 size_t len;
1999 const char* str = readString8Inplace(&len);
2000 if (str) return String8(str, len);
2001 ALOGE("Reading a NULL string not supported here.");
2002 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07002003}
2004
2005status_t Parcel::readString8(String8* pArg) const
2006{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002007 size_t len;
2008 const char* str = readString8Inplace(&len);
2009 if (str) {
2010 pArg->setTo(str, len);
2011 return 0;
2012 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07002013 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002014 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07002015 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002016}
2017
2018const char* Parcel::readString8Inplace(size_t* outLen) const
2019{
2020 int32_t size = readInt32();
2021 // watch for potential int overflow from size+1
2022 if (size >= 0 && size < INT32_MAX) {
2023 *outLen = size;
2024 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00002025 if (str != nullptr) {
2026 if (str[size] == '\0') {
2027 return str;
2028 }
2029 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002030 }
Roshan Pius87b64d22016-07-18 12:51:02 -07002031 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06002032 *outLen = 0;
2033 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002034}
2035
2036String16 Parcel::readString16() const
2037{
2038 size_t len;
2039 const char16_t* str = readString16Inplace(&len);
2040 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002041 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042 return String16();
2043}
2044
Casey Dahlinb9872622015-11-25 15:09:45 -08002045
Casey Dahlin451ff582015-10-19 18:12:18 -07002046status_t Parcel::readString16(String16* pArg) const
2047{
2048 size_t len;
2049 const char16_t* str = readString16Inplace(&len);
2050 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07002051 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07002052 return 0;
2053 } else {
2054 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08002055 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07002056 }
2057}
2058
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002059const char16_t* Parcel::readString16Inplace(size_t* outLen) const
2060{
2061 int32_t size = readInt32();
2062 // watch for potential int overflow from size+1
2063 if (size >= 0 && size < INT32_MAX) {
2064 *outLen = size;
2065 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00002066 if (str != nullptr) {
2067 if (str[size] == u'\0') {
2068 return str;
2069 }
2070 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 }
2072 }
2073 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07002074 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075}
2076
Casey Dahlinf0c13772015-10-27 18:33:56 -07002077status_t Parcel::readStrongBinder(sp<IBinder>* val) const
2078{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002079 status_t status = readNullableStrongBinder(val);
2080 if (status == OK && !val->get()) {
Steven Morelanddce98242022-03-11 00:26:10 +00002081 ALOGW("Expecting binder but got null!");
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002082 status = UNEXPECTED_NULL;
2083 }
2084 return status;
2085}
2086
2087status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
2088{
Steven Morelanda86a3562019-08-01 23:28:34 +00002089 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07002090}
2091
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092sp<IBinder> Parcel::readStrongBinder() const
2093{
2094 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08002095 // Note that a lot of code in Android reads binders by hand with this
2096 // method, and that code has historically been ok with getting nullptr
2097 // back (while ignoring error codes).
2098 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 return val;
2100}
2101
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002102int32_t Parcel::readExceptionCode() const
2103{
Christopher Wiley09eb7492015-11-09 15:06:15 -08002104 binder::Status status;
2105 status.readFromParcel(*this);
2106 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07002107}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002108
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002109native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002110{
2111 int numFds, numInts;
2112 status_t err;
2113 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07002114 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002115 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07002116 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002117
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002118 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002119 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07002120 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07002121 }
2122
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002123 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002124 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07002125 if (h->data[i] < 0) {
2126 for (int j = 0; j < i; j++) {
2127 close(h->data[j]);
2128 }
2129 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002130 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07002131 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002132 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002133 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002134 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07002135 native_handle_close(h);
2136 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07002137 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002138 }
2139 return h;
2140}
2141
Frederick Mayle69a0c992022-05-26 20:38:39 +00002142int Parcel::readFileDescriptor() const {
2143 if (const auto* rpcFields = maybeRpcFields()) {
2144 if (!std::binary_search(rpcFields->mObjectPositions.begin(),
2145 rpcFields->mObjectPositions.end(), mDataPos)) {
2146 ALOGW("Attempt to read file descriptor from Parcel %p at offset %zu that is not in the "
2147 "object list",
2148 this, mDataPos);
2149 return BAD_TYPE;
2150 }
2151
2152 int32_t objectType = readInt32();
2153 if (objectType != RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2154 return BAD_TYPE;
2155 }
2156
2157 int32_t fdIndex = readInt32();
2158 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2159 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2160 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2161 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2162 return BAD_VALUE;
2163 }
2164 return toRawFd(rpcFields->mFds->at(fdIndex));
2165 }
2166
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002167 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08002168
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002169 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08002170 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 }
Casey Dahlin06673e32015-11-23 13:24:23 -08002172
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002173 return BAD_TYPE;
2174}
2175
Frederick Mayle69a0c992022-05-26 20:38:39 +00002176int Parcel::readParcelFileDescriptor() const {
Dianne Hackborn1941a402016-08-29 12:30:43 -07002177 int32_t hasComm = readInt32();
2178 int fd = readFileDescriptor();
2179 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002180 // detach (owned by the binder driver)
2181 int comm = readFileDescriptor();
2182
2183 // warning: this must be kept in sync with:
2184 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
2185 enum ParcelFileDescriptorStatus {
2186 DETACHED = 2,
2187 };
2188
2189#if BYTE_ORDER == BIG_ENDIAN
2190 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
2191#endif
2192#if BYTE_ORDER == LITTLE_ENDIAN
2193 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
2194#endif
2195
2196 ssize_t written = TEMP_FAILURE_RETRY(
2197 ::write(comm, &message, sizeof(message)));
2198
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002199 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002200 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2201 written, strerror(errno));
2202 return BAD_TYPE;
2203 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002204 }
2205 return fd;
2206}
2207
Christopher Wiley2cf19952016-04-11 11:09:37 -07002208status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002209{
2210 int got = readFileDescriptor();
2211
2212 if (got == BAD_TYPE) {
2213 return BAD_TYPE;
2214 }
2215
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002216 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002217
2218 if (val->get() < 0) {
2219 return BAD_VALUE;
2220 }
2221
2222 return OK;
2223}
2224
Ryo Hashimotobf551892018-05-31 16:58:35 +09002225status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2226{
2227 int got = readParcelFileDescriptor();
2228
2229 if (got == BAD_TYPE) {
2230 return BAD_TYPE;
2231 }
2232
2233 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2234
2235 if (val->get() < 0) {
2236 return BAD_VALUE;
2237 }
2238
2239 return OK;
2240}
Casey Dahlin06673e32015-11-23 13:24:23 -08002241
Jeff Brown5707dbf2011-09-23 21:17:56 -07002242status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2243{
Jeff Brown13b16042014-11-11 16:44:25 -08002244 int32_t blobType;
2245 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002246 if (status) return status;
2247
Jeff Brown13b16042014-11-11 16:44:25 -08002248 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002249 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002250 const void* ptr = readInplace(len);
2251 if (!ptr) return BAD_VALUE;
2252
Jeff Brown13b16042014-11-11 16:44:25 -08002253 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002254 return NO_ERROR;
2255 }
2256
Steve Block6807e592011-10-20 11:56:00 +01002257 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002258 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002259 int fd = readFileDescriptor();
2260 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2261
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002262 if (!ashmem_valid(fd)) {
2263 ALOGE("invalid fd");
2264 return BAD_VALUE;
2265 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002266 int size = ashmem_get_size_region(fd);
2267 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002268 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002269 return BAD_VALUE;
2270 }
Yi Kong91635562018-06-07 14:38:36 -07002271 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002272 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002273 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002274
Jeff Brown13b16042014-11-11 16:44:25 -08002275 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002276 return NO_ERROR;
2277}
2278
Mathias Agopiane1424282013-07-29 21:24:40 -07002279status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002280{
2281 // size
2282 const size_t len = this->readInt32();
2283 const size_t fd_count = this->readInt32();
2284
Andrei Homescu1519b982022-06-09 02:04:44 +00002285 if ((len > INT32_MAX) || (fd_count > kMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002286 // don't accept size_t values which may have come from an
2287 // inadvertent conversion from a negative int.
2288 return BAD_VALUE;
2289 }
2290
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002291 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002292 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002293 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002294 return BAD_VALUE;
2295
Yi Kong91635562018-06-07 14:38:36 -07002296 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002297 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002298 fds = new (std::nothrow) int[fd_count];
2299 if (fds == nullptr) {
2300 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2301 return BAD_VALUE;
2302 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002303 }
2304
2305 status_t err = NO_ERROR;
2306 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002307 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002308 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002309 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002310 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 -07002311 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2312 // Close all the file descriptors that were dup-ed.
2313 for (size_t j=0; j<i ;j++) {
2314 close(fds[j]);
2315 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002316 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002317 }
2318
2319 if (err == NO_ERROR) {
2320 err = val.unflatten(buf, len, fds, fd_count);
2321 }
2322
2323 if (fd_count) {
2324 delete [] fds;
2325 }
2326
2327 return err;
2328}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002329const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2330{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002331 const auto* kernelFields = maybeKernelFields();
2332 if (kernelFields == nullptr) {
2333 return nullptr;
2334 }
2335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 const size_t DPOS = mDataPos;
2337 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2338 const flat_binder_object* obj
2339 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2340 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002341 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002342 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002343 // the object list, so we don't want to check for it when
2344 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002345 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 return obj;
2347 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 // Ensure that this object is valid...
Frederick Mayled3c595a2022-05-09 23:02:54 +00002350 binder_size_t* const OBJS = kernelFields->mObjects;
2351 const size_t N = kernelFields->mObjectsSize;
2352 size_t opos = kernelFields->mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002353
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002354 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002355 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002357
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 // Start at the current hint position, looking for an object at
2359 // the current data position.
2360 if (opos < N) {
2361 while (opos < (N-1) && OBJS[opos] < DPOS) {
2362 opos++;
2363 }
2364 } else {
2365 opos = N-1;
2366 }
2367 if (OBJS[opos] == DPOS) {
2368 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002369 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002370 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002371 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002372 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002373 return obj;
2374 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 // Look backwards for it...
2377 while (opos > 0 && OBJS[opos] > DPOS) {
2378 opos--;
2379 }
2380 if (OBJS[opos] == DPOS) {
2381 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002382 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002383 this, DPOS, opos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002384 kernelFields->mNextObjectHint = opos + 1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002385 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 return obj;
2387 }
2388 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002389 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002390 this, DPOS);
2391 }
Yi Kong91635562018-06-07 14:38:36 -07002392 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002393}
2394
Frederick Mayled3c595a2022-05-09 23:02:54 +00002395void Parcel::closeFileDescriptors() {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002396 if (auto* kernelFields = maybeKernelFields()) {
2397 size_t i = kernelFields->mObjectsSize;
2398 if (i > 0) {
2399 // ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002400 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002401 while (i > 0) {
2402 i--;
2403 const flat_binder_object* flat =
2404 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
2405 if (flat->hdr.type == BINDER_TYPE_FD) {
2406 // ALOGI("Closing fd: %ld", flat->handle);
2407 close(flat->handle);
2408 }
2409 }
2410 } else if (auto* rpcFields = maybeRpcFields()) {
2411 rpcFields->mFds.reset();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 }
2413}
2414
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002415uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002416{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002417 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002418}
2419
2420size_t Parcel::ipcDataSize() const
2421{
2422 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2423}
2424
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002425uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002426{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002427 if (const auto* kernelFields = maybeKernelFields()) {
2428 return reinterpret_cast<uintptr_t>(kernelFields->mObjects);
2429 }
2430 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431}
2432
2433size_t Parcel::ipcObjectsCount() const
2434{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002435 if (const auto* kernelFields = maybeKernelFields()) {
2436 return kernelFields->mObjectsSize;
2437 }
2438 return 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439}
2440
Frederick Mayled3c595a2022-05-09 23:02:54 +00002441void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, const binder_size_t* objects,
2442 size_t objectsCount, release_func relFunc) {
Steven Moreland438cce82021-04-02 18:04:08 +00002443 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2444 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2445
Steven Morelandceed9bb2020-12-17 01:01:06 +00002446 freeData();
2447
Frederick Mayled3c595a2022-05-09 23:02:54 +00002448 auto* kernelFields = maybeKernelFields();
2449 LOG_ALWAYS_FATAL_IF(kernelFields == nullptr); // guaranteed by freeData.
2450
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002451 mData = const_cast<uint8_t*>(data);
2452 mDataSize = mDataCapacity = dataSize;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002453 kernelFields->mObjects = const_cast<binder_size_t*>(objects);
2454 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002456
2457 binder_size_t minOffset = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002458 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
2459 binder_size_t offset = kernelFields->mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002460 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002461 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002462 __func__, (uint64_t)offset, (uint64_t)minOffset);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002463 kernelFields->mObjectsSize = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002464 break;
2465 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002466 const flat_binder_object* flat
2467 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2468 uint32_t type = flat->hdr.type;
2469 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2470 type == BINDER_TYPE_FD)) {
2471 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2472 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002473 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002474 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002475 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002476 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2477 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002478
2479 // WARNING: callers of ipcSetDataReference need to make sure they
2480 // don't rely on mObjectsSize in their release_func.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002481 kernelFields->mObjectsSize = 0;
Martijn Coenen82c75312019-07-24 15:18:30 +02002482 break;
2483 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002484 minOffset = offset + sizeof(flat_binder_object);
2485 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 scanForFds();
2487}
2488
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002489status_t Parcel::rpcSetDataReference(
2490 const sp<RpcSession>& session, const uint8_t* data, size_t dataSize,
2491 const uint32_t* objectTable, size_t objectTableSize,
2492 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>&& ancillaryFds,
2493 release_func relFunc) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002494 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2495 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2496
2497 LOG_ALWAYS_FATAL_IF(session == nullptr);
2498
2499 freeData();
2500 markForRpc(session);
2501
Frederick Mayle69a0c992022-05-26 20:38:39 +00002502 auto* rpcFields = maybeRpcFields();
2503 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr); // guaranteed by markForRpc.
2504
Frederick Mayled3c595a2022-05-09 23:02:54 +00002505 mData = const_cast<uint8_t*>(data);
2506 mDataSize = mDataCapacity = dataSize;
2507 mOwner = relFunc;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002508
2509 if (objectTableSize != ancillaryFds.size()) {
2510 ALOGE("objectTableSize=%zu ancillaryFds.size=%zu", objectTableSize, ancillaryFds.size());
2511 freeData(); // don't leak mData
2512 return BAD_VALUE;
2513 }
2514
2515 rpcFields->mObjectPositions.reserve(objectTableSize);
2516 for (size_t i = 0; i < objectTableSize; i++) {
2517 rpcFields->mObjectPositions.push_back(objectTable[i]);
2518 }
2519 if (!ancillaryFds.empty()) {
2520 rpcFields->mFds = std::make_unique<decltype(rpcFields->mFds)::element_type>();
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002521 *rpcFields->mFds = std::move(ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +00002522 }
2523
2524 return OK;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002525}
2526
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002527void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002528{
2529 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002530
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002531 if (errorCheck() != NO_ERROR) {
2532 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002533 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 } else if (dataSize() > 0) {
2535 const uint8_t* DATA = data();
2536 to << indent << HexDump(DATA, dataSize()) << dedent;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002537 if (const auto* kernelFields = maybeKernelFields()) {
2538 const binder_size_t* OBJS = kernelFields->mObjects;
2539 const size_t N = objectsCount();
2540 for (size_t i = 0; i < N; i++) {
2541 const flat_binder_object* flat =
2542 reinterpret_cast<const flat_binder_object*>(DATA + OBJS[i]);
2543 to << endl
2544 << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
2545 << TypeCode(flat->hdr.type & 0x7f7f7f00) << " = " << flat->binder;
2546 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547 }
2548 } else {
2549 to << "NULL";
2550 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002551
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002552 to << ")";
2553}
2554
2555void Parcel::releaseObjects()
2556{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002557 auto* kernelFields = maybeKernelFields();
2558 if (kernelFields == nullptr) {
2559 return;
2560 }
2561
2562 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002563 if (i == 0) {
2564 return;
2565 }
2566 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002567 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002568 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002569 while (i > 0) {
2570 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002571 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002572 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002573 }
2574}
2575
2576void Parcel::acquireObjects()
2577{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002578 auto* kernelFields = maybeKernelFields();
2579 if (kernelFields == nullptr) {
2580 return;
2581 }
2582
2583 size_t i = kernelFields->mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002584 if (i == 0) {
2585 return;
2586 }
2587 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002588 uint8_t* const data = mData;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002589 binder_size_t* const objects = kernelFields->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 while (i > 0) {
2591 i--;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002592 const flat_binder_object* flat = reinterpret_cast<flat_binder_object*>(data + objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002593 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002594 }
2595}
2596
2597void Parcel::freeData()
2598{
2599 freeDataNoInit();
2600 initState();
2601}
2602
2603void Parcel::freeDataNoInit()
2604{
2605 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002606 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002607 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002608 auto* kernelFields = maybeKernelFields();
2609 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2610 kernelFields ? kernelFields->mObjectsSize : 0);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002611 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002612 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002613 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002614 if (mData) {
2615 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002616 gParcelGlobalAllocSize -= mDataCapacity;
2617 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002618 if (mDeallocZero) {
2619 zeroMemory(mData, mDataSize);
2620 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002621 free(mData);
2622 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002623 auto* kernelFields = maybeKernelFields();
2624 if (kernelFields && kernelFields->mObjects) free(kernelFields->mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 }
2626}
2627
2628status_t Parcel::growData(size_t len)
2629{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002630 if (len > INT32_MAX) {
2631 // don't accept size_t values which may have come from an
2632 // inadvertent conversion from a negative int.
2633 return BAD_VALUE;
2634 }
2635
Martijn Coenen93fe5182020-01-22 10:46:25 +01002636 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2637 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002638 size_t newSize = ((mDataSize+len)*3)/2;
2639 return (newSize <= mDataSize)
2640 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002641 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002642}
2643
Steven Morelandf183fdd2020-10-27 00:12:12 +00002644static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2645 if (!zero) {
2646 return (uint8_t*)realloc(data, newCapacity);
2647 }
2648 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2649 if (!newData) {
2650 return nullptr;
2651 }
2652
2653 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2654 zeroMemory(data, oldCapacity);
2655 free(data);
2656 return newData;
2657}
2658
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002659status_t Parcel::restartWrite(size_t desired)
2660{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002661 if (desired > INT32_MAX) {
2662 // don't accept size_t values which may have come from an
2663 // inadvertent conversion from a negative int.
2664 return BAD_VALUE;
2665 }
2666
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002667 if (mOwner) {
2668 freeData();
2669 return continueWrite(desired);
2670 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002671
Steven Morelandf183fdd2020-10-27 00:12:12 +00002672 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002673 if (!data && desired > mDataCapacity) {
2674 mError = NO_MEMORY;
2675 return NO_MEMORY;
2676 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002677
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002678 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002679
Devin Moore4a0a55e2020-06-04 13:23:10 -07002680 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002681 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002682 if (mDataCapacity > desired) {
2683 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2684 } else {
2685 gParcelGlobalAllocSize += (desired - mDataCapacity);
2686 }
2687
Colin Cross83ec65e2015-12-08 17:15:50 -08002688 if (!mData) {
2689 gParcelGlobalAllocCount++;
2690 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002691 mData = data;
2692 mDataCapacity = desired;
2693 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002694
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002695 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002696 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2697 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2698
Frederick Mayled3c595a2022-05-09 23:02:54 +00002699 if (auto* kernelFields = maybeKernelFields()) {
2700 free(kernelFields->mObjects);
2701 kernelFields->mObjects = nullptr;
2702 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = 0;
2703 kernelFields->mNextObjectHint = 0;
2704 kernelFields->mObjectsSorted = false;
2705 kernelFields->mHasFds = false;
2706 kernelFields->mFdsKnown = true;
Frederick Mayle69a0c992022-05-26 20:38:39 +00002707 } else if (auto* rpcFields = maybeRpcFields()) {
2708 rpcFields->mObjectPositions.clear();
2709 rpcFields->mFds.reset();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002710 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002711 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002712
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002713 return NO_ERROR;
2714}
2715
2716status_t Parcel::continueWrite(size_t desired)
2717{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002718 if (desired > INT32_MAX) {
2719 // don't accept size_t values which may have come from an
2720 // inadvertent conversion from a negative int.
2721 return BAD_VALUE;
2722 }
2723
Frederick Mayled3c595a2022-05-09 23:02:54 +00002724 auto* kernelFields = maybeKernelFields();
Frederick Mayle69a0c992022-05-26 20:38:39 +00002725 auto* rpcFields = maybeRpcFields();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002727 // If shrinking, first adjust for any objects that appear
2728 // after the new data size.
Frederick Mayle69a0c992022-05-26 20:38:39 +00002729 size_t objectsSize =
2730 kernelFields ? kernelFields->mObjectsSize : rpcFields->mObjectPositions.size();
2731 if (desired < mDataSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002732 if (desired == 0) {
2733 objectsSize = 0;
2734 } else {
Frederick Mayle69a0c992022-05-26 20:38:39 +00002735 if (kernelFields) {
2736 while (objectsSize > 0) {
2737 if (kernelFields->mObjects[objectsSize - 1] < desired) break;
2738 objectsSize--;
2739 }
2740 } else {
2741 while (objectsSize > 0) {
2742 if (rpcFields->mObjectPositions[objectsSize - 1] < desired) break;
2743 objectsSize--;
2744 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002745 }
2746 }
2747 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002748
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002749 if (mOwner) {
2750 // If the size is going to zero, just release the owner's data.
2751 if (desired == 0) {
2752 freeData();
2753 return NO_ERROR;
2754 }
2755
2756 // If there is a different owner, we need to take
2757 // posession.
2758 uint8_t* data = (uint8_t*)malloc(desired);
2759 if (!data) {
2760 mError = NO_MEMORY;
2761 return NO_MEMORY;
2762 }
Yi Kong91635562018-06-07 14:38:36 -07002763 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002764
Frederick Mayle69a0c992022-05-26 20:38:39 +00002765 if (kernelFields && objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002766 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002767 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002768 free(data);
2769
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002770 mError = NO_MEMORY;
2771 return NO_MEMORY;
2772 }
2773
2774 // Little hack to only acquire references on objects
2775 // we will be keeping.
Frederick Mayled3c595a2022-05-09 23:02:54 +00002776 size_t oldObjectsSize = kernelFields->mObjectsSize;
2777 kernelFields->mObjectsSize = objectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002778 acquireObjects();
Frederick Mayled3c595a2022-05-09 23:02:54 +00002779 kernelFields->mObjectsSize = oldObjectsSize;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002780 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002781 if (rpcFields) {
2782 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2783 free(data);
2784 return status;
2785 }
2786 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002788 if (mData) {
2789 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2790 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002791 if (objects && kernelFields && kernelFields->mObjects) {
2792 memcpy(objects, kernelFields->mObjects, objectsSize * sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002793 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002794 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002795 mOwner(this, mData, mDataSize, kernelFields ? kernelFields->mObjects : nullptr,
2796 kernelFields ? kernelFields->mObjectsSize : 0);
Yi Kong91635562018-06-07 14:38:36 -07002797 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002798
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002799 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002800 gParcelGlobalAllocSize += desired;
2801 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002802
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002803 mData = data;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002804 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002805 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002806 mDataCapacity = desired;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002807 if (kernelFields) {
2808 kernelFields->mObjects = objects;
2809 kernelFields->mObjectsSize = kernelFields->mObjectsCapacity = objectsSize;
2810 kernelFields->mNextObjectHint = 0;
2811 kernelFields->mObjectsSorted = false;
2812 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002813
2814 } else if (mData) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002815 if (kernelFields && objectsSize < kernelFields->mObjectsSize) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002816 // Need to release refs on any objects we are dropping.
2817 const sp<ProcessState> proc(ProcessState::self());
Frederick Mayled3c595a2022-05-09 23:02:54 +00002818 for (size_t i = objectsSize; i < kernelFields->mObjectsSize; i++) {
2819 const flat_binder_object* flat =
2820 reinterpret_cast<flat_binder_object*>(mData + kernelFields->mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002821 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002822 // will need to rescan because we may have lopped off the only FDs
Frederick Mayled3c595a2022-05-09 23:02:54 +00002823 kernelFields->mFdsKnown = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002824 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002825 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002826 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002827
2828 if (objectsSize == 0) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002829 free(kernelFields->mObjects);
2830 kernelFields->mObjects = nullptr;
2831 kernelFields->mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002832 } else {
2833 binder_size_t* objects =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002834 (binder_size_t*)realloc(kernelFields->mObjects,
2835 objectsSize * sizeof(binder_size_t));
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002836 if (objects) {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002837 kernelFields->mObjects = objects;
2838 kernelFields->mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002839 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 }
Frederick Mayled3c595a2022-05-09 23:02:54 +00002841 kernelFields->mObjectsSize = objectsSize;
2842 kernelFields->mNextObjectHint = 0;
2843 kernelFields->mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002844 }
Frederick Mayle69a0c992022-05-26 20:38:39 +00002845 if (rpcFields) {
2846 if (status_t status = truncateRpcObjects(objectsSize); status != OK) {
2847 return status;
2848 }
2849 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002850
2851 // We own the data, so we can just do a realloc().
2852 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002853 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002854 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002855 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2856 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002857 gParcelGlobalAllocSize += desired;
2858 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002859 mData = data;
2860 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002861 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002862 mError = NO_MEMORY;
2863 return NO_MEMORY;
2864 }
2865 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002866 if (mDataSize > desired) {
2867 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002868 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002869 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002870 if (mDataPos > desired) {
2871 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002872 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002873 }
2874 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002875
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002876 } else {
2877 // This is the first data. Easy!
2878 uint8_t* data = (uint8_t*)malloc(desired);
2879 if (!data) {
2880 mError = NO_MEMORY;
2881 return NO_MEMORY;
2882 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002883
Frederick Mayled3c595a2022-05-09 23:02:54 +00002884 if (!(mDataCapacity == 0 &&
2885 (kernelFields == nullptr ||
2886 (kernelFields->mObjects == nullptr && kernelFields->mObjectsCapacity == 0)))) {
2887 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity,
2888 kernelFields ? kernelFields->mObjects : nullptr,
2889 kernelFields ? kernelFields->mObjectsCapacity : 0, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002890 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002891
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002892 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002893 gParcelGlobalAllocSize += desired;
2894 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002895
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002896 mData = data;
2897 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002898 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2899 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002900 mDataCapacity = desired;
2901 }
2902
2903 return NO_ERROR;
2904}
2905
Frederick Mayle69a0c992022-05-26 20:38:39 +00002906status_t Parcel::truncateRpcObjects(size_t newObjectsSize) {
2907 auto* rpcFields = maybeRpcFields();
2908 if (newObjectsSize == 0) {
2909 rpcFields->mObjectPositions.clear();
2910 if (rpcFields->mFds) {
2911 rpcFields->mFds->clear();
2912 }
2913 return OK;
2914 }
2915 while (rpcFields->mObjectPositions.size() > newObjectsSize) {
2916 uint32_t pos = rpcFields->mObjectPositions.back();
2917 rpcFields->mObjectPositions.pop_back();
2918 const auto type = *reinterpret_cast<const RpcFields::ObjectType*>(mData + pos);
2919 if (type == RpcFields::TYPE_NATIVE_FILE_DESCRIPTOR) {
2920 const auto fdIndex =
2921 *reinterpret_cast<const int32_t*>(mData + pos + sizeof(RpcFields::ObjectType));
2922 if (rpcFields->mFds == nullptr || fdIndex < 0 ||
2923 static_cast<size_t>(fdIndex) >= rpcFields->mFds->size()) {
2924 ALOGE("RPC Parcel contains invalid file descriptor index. index=%d fd_count=%zu",
2925 fdIndex, rpcFields->mFds ? rpcFields->mFds->size() : 0);
2926 return BAD_VALUE;
2927 }
2928 // In practice, this always removes the last element.
2929 rpcFields->mFds->erase(rpcFields->mFds->begin() + fdIndex);
2930 }
2931 }
2932 return OK;
2933}
2934
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002935void Parcel::initState()
2936{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002937 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002938 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002939 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002940 mDataSize = 0;
2941 mDataCapacity = 0;
2942 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002943 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2944 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002945 mVariantFields.emplace<KernelFields>();
Steven Moreland6e5a7752019-08-05 20:30:14 -07002946 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002947 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002948 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002949}
2950
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002951void Parcel::scanForFds() const {
Frederick Mayled3c595a2022-05-09 23:02:54 +00002952 auto* kernelFields = maybeKernelFields();
2953 if (kernelFields == nullptr) {
2954 return;
2955 }
2956 status_t status = hasFileDescriptorsInRange(0, dataSize(), &kernelFields->mHasFds);
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002957 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
Frederick Mayled3c595a2022-05-09 23:02:54 +00002958 kernelFields->mFdsKnown = true;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002959}
2960
Dan Sandleraa5c2342015-04-10 10:08:45 -04002961size_t Parcel::getBlobAshmemSize() const
2962{
Adrian Roos6bb31142015-10-22 16:46:12 -07002963 // This used to return the size of all blobs that were written to ashmem, now we're returning
2964 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002965 // TODO(b/202029388): Remove method once ABI can be changed.
2966 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002967}
2968
Adrian Rooscbf37262015-10-22 16:12:53 -07002969size_t Parcel::getOpenAshmemSize() const
2970{
Frederick Mayled3c595a2022-05-09 23:02:54 +00002971 auto* kernelFields = maybeKernelFields();
2972 if (kernelFields == nullptr) {
2973 return 0;
2974 }
2975
Steven Morelandc673f1f2021-10-07 18:23:35 -07002976 size_t openAshmemSize = 0;
Frederick Mayled3c595a2022-05-09 23:02:54 +00002977 for (size_t i = 0; i < kernelFields->mObjectsSize; i++) {
Steven Morelandc673f1f2021-10-07 18:23:35 -07002978 const flat_binder_object* flat =
Frederick Mayled3c595a2022-05-09 23:02:54 +00002979 reinterpret_cast<const flat_binder_object*>(mData + kernelFields->mObjects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002980
2981 // cookie is compared against zero for historical reasons
2982 // > obj.cookie = takeOwnership ? 1 : 0;
2983 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2984 int size = ashmem_get_size_region(flat->handle);
2985 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2986 ALOGE("Overflow when computing ashmem size.");
2987 return SIZE_MAX;
2988 }
2989 }
2990 }
2991 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002992}
2993
2994// --- Parcel::Blob ---
2995
2996Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002997 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002998}
2999
3000Parcel::Blob::~Blob() {
3001 release();
3002}
3003
3004void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08003005 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07003006 ::munmap(mData, mSize);
3007 }
3008 clear();
3009}
3010
Jeff Brown13b16042014-11-11 16:44:25 -08003011void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
3012 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003013 mData = data;
3014 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08003015 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003016}
3017
3018void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08003019 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07003020 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003021 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08003022 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07003023}
3024
Steven Moreland61ff8492019-09-26 16:05:45 -07003025} // namespace android