blob: be50a7500ab7ab43273e11f20576d1cac9686e02 [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
Mark Salyzynabed7f72016-01-27 08:02:48 -080043#include <cutils/ashmem.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000044#include <cutils/compiler.h>
Mark Salyzynabed7f72016-01-27 08:02:48 -080045#include <utils/Flattenable.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070046#include <utils/Log.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070047#include <utils/String16.h>
Steven Moreland3af936a2021-03-26 03:05:38 +000048#include <utils/String8.h>
49#include <utils/misc.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070050
Steven Moreland5553ac42020-11-11 02:14:45 +000051#include "RpcState.h"
Steven Morelanda4853cd2019-07-12 15:44:37 -070052#include "Static.h"
Steven Morelandf183fdd2020-10-27 00:12:12 +000053#include "Utils.h"
Steven Moreland6ba5a252021-05-04 22:49:00 +000054#include "binder_module.h"
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070055
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070056#define LOG_REFS(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080057//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn7e790af2014-11-11 12:22:53 -080058#define LOG_ALLOC(...)
Mark Salyzyne93390b2016-01-27 08:02:48 -080059//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070060
61// ---------------------------------------------------------------------------
62
Nick Kralevichb6b14232015-04-02 09:36:02 -070063// This macro should never be used at runtime, as a too large value
64// of s could cause an integer overflow. Instead, you should always
65// use the wrapper function pad_size()
Andrei Homescuf7f2c172022-03-08 22:52:49 +000066#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3UL)
Nick Kralevichb6b14232015-04-02 09:36:02 -070067
68static size_t pad_size(size_t s) {
Steven Moreland28723ae2019-04-01 18:52:30 -070069 if (s > (std::numeric_limits<size_t>::max() - 3)) {
Steven Moreland6adf33c2019-09-25 13:18:09 -070070 LOG_ALWAYS_FATAL("pad size too big %zu", s);
Nick Kralevichb6b14232015-04-02 09:36:02 -070071 }
72 return PAD_SIZE_UNSAFE(s);
73}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070074
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070075// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkey05827be2018-06-26 10:52:38 -060076#define STRICT_MODE_PENALTY_GATHER (1 << 31)
Brad Fitzpatricka877cd82010-07-07 16:06:39 -070077
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070078namespace android {
79
Steven Moreland7b102262019-08-01 15:48:43 -070080// many things compile this into prebuilts on the stack
Steven Moreland90c1f9a2021-05-03 18:27:24 +000081#ifdef __LP64__
82static_assert(sizeof(Parcel) == 120);
83#else
84static_assert(sizeof(Parcel) == 60);
85#endif
Steven Moreland7b102262019-08-01 15:48:43 -070086
Jeff Sharkey8994c182020-09-11 12:07:10 -060087static std::atomic<size_t> gParcelGlobalAllocCount;
88static std::atomic<size_t> gParcelGlobalAllocSize;
Dianne Hackborna4cff882014-11-13 17:07:40 -080089
Christopher Tatee4e0ae82016-03-24 16:03:44 -070090static size_t gMaxFds = 0;
91
Jeff Brown13b16042014-11-11 16:44:25 -080092// Maximum size of a blob to transfer in-place.
93static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
94
95enum {
96 BLOB_INPLACE = 0,
97 BLOB_ASHMEM_IMMUTABLE = 1,
98 BLOB_ASHMEM_MUTABLE = 2,
99};
100
Steven Morelandc673f1f2021-10-07 18:23:35 -0700101static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
102 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700103 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700104 case BINDER_TYPE_BINDER:
105 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000106 LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800107 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700108 }
109 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 case BINDER_TYPE_HANDLE: {
111 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700112 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700113 LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get());
114 b->incStrong(who);
115 }
116 return;
117 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700118 case BINDER_TYPE_FD: {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700119 return;
120 }
121 }
122
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700123 ALOGD("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124}
125
Steven Morelandc673f1f2021-10-07 18:23:35 -0700126static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj,
127 const void* who) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700128 switch (obj.hdr.type) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700129 case BINDER_TYPE_BINDER:
130 if (obj.binder) {
yuxic05af3b2021-08-24 02:52:15 +0000131 LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800132 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700133 }
134 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 case BINDER_TYPE_HANDLE: {
136 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
Yi Kong91635562018-06-07 14:38:36 -0700137 if (b != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700138 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
139 b->decStrong(who);
140 }
141 return;
142 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700143 case BINDER_TYPE_FD: {
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800144 if (obj.cookie != 0) { // owned
Mark Salyzynb454d8f2016-01-27 08:02:48 -0800145 close(obj.handle);
Adrian Rooscbf37262015-10-22 16:12:53 -0700146 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700147 return;
148 }
149 }
150
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700151 ALOGE("Invalid object type 0x%08x", obj.hdr.type);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700152}
153
Steven Moreland34b48cb2020-12-01 22:45:38 +0000154status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700155{
Steven Moreland6e5a7752019-08-05 20:30:14 -0700156 internal::Stability::tryMarkCompilationUnit(binder.get());
Steven Moreland16a41062021-07-23 13:35:25 -0700157 int16_t rep = internal::Stability::getRepr(binder.get());
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000158 return writeInt32(rep);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700159}
160
Steven Morelanda86a3562019-08-01 23:28:34 +0000161status_t Parcel::finishUnflattenBinder(
162 const sp<IBinder>& binder, sp<IBinder>* out) const
163{
164 int32_t stability;
165 status_t status = readInt32(&stability);
166 if (status != OK) return status;
167
Steven Moreland14e4cfa2021-06-03 21:40:45 +0000168 status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability),
169 true /*log*/);
Steven Morelanda86a3562019-08-01 23:28:34 +0000170 if (status != OK) return status;
171
172 *out = binder;
173 return OK;
174}
175
Steven Morelandbf1915b2020-07-16 22:43:02 +0000176static constexpr inline int schedPolicyMask(int policy, int priority) {
177 return (priority & FLAT_BINDER_FLAG_PRIORITY_MASK) | ((policy & 3) << FLAT_BINDER_FLAG_SCHED_POLICY_SHIFT);
178}
179
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500180status_t Parcel::flattenBinder(const sp<IBinder>& binder) {
181 BBinder* local = nullptr;
182 if (binder) local = binder->localBinder();
183 if (local) local->setParceled();
184
Steven Moreland5553ac42020-11-11 02:14:45 +0000185 if (isForRpc()) {
186 if (binder) {
187 status_t status = writeInt32(1); // non-null
188 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700189 uint64_t address;
Steven Morelanda5036f02021-06-08 02:26:57 +0000190 // TODO(b/167966510): need to undo this if the Parcel is not sent
Steven Morelandc9939062021-05-05 17:57:41 +0000191 status = mSession->state()->onBinderLeaving(mSession, binder, &address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000192 if (status != OK) return status;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700193 status = writeUint64(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000194 if (status != OK) return status;
195 } else {
196 status_t status = writeInt32(0); // null
197 if (status != OK) return status;
198 }
199 return finishFlattenBinder(binder);
200 }
201
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700202 flat_binder_object obj;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700203
Steven Morelandbf1915b2020-07-16 22:43:02 +0000204 int schedBits = 0;
205 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
206 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700207 }
208
Yi Kong91635562018-06-07 14:38:36 -0700209 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700210 if (!local) {
211 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700212 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000213 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000214 } else {
215 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700216 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000217 return INVALID_OPERATION;
218 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700219 }
Steven Moreland99157622021-09-13 16:27:34 -0700220 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700221 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800222 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Steven Moreland49c27532022-03-01 10:21:07 +0000223 obj.flags = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700224 obj.handle = handle;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800225 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700226 } else {
Steven Morelandbf1915b2020-07-16 22:43:02 +0000227 int policy = local->getMinSchedulerPolicy();
228 int priority = local->getMinSchedulerPriority();
229
230 if (policy != 0 || priority != 0) {
231 // override value, since it is set explicitly
232 schedBits = schedPolicyMask(policy, priority);
233 }
Steven Moreland49c27532022-03-01 10:21:07 +0000234 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Steven Morelandf0212002018-12-26 13:59:23 -0800235 if (local->isRequestingSid()) {
236 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
237 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000238 if (local->isInheritRt()) {
239 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
240 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700241 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800242 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
243 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700244 }
245 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700246 obj.hdr.type = BINDER_TYPE_BINDER;
Steven Moreland49c27532022-03-01 10:21:07 +0000247 obj.flags = 0;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800248 obj.binder = 0;
249 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700250 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700251
Steven Morelandbf1915b2020-07-16 22:43:02 +0000252 obj.flags |= schedBits;
253
Steven Moreland34b48cb2020-12-01 22:45:38 +0000254 status_t status = writeObject(obj, false);
255 if (status != OK) return status;
256
257 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700258}
259
Steven Morelanda86a3562019-08-01 23:28:34 +0000260status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700261{
Steven Moreland5553ac42020-11-11 02:14:45 +0000262 if (isForRpc()) {
Steven Morelandc9939062021-05-05 17:57:41 +0000263 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000264
Steven Moreland5623d1a2021-09-10 15:45:34 -0700265 int32_t isPresent;
266 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000267 if (status != OK) return status;
268
269 sp<IBinder> binder;
270
Steven Moreland5623d1a2021-09-10 15:45:34 -0700271 if (isPresent & 1) {
272 uint64_t addr;
273 if (status_t status = readUint64(&addr); status != OK) return status;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000274 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
275 status != OK)
276 return status;
Steven Morelandd8083312021-09-22 13:37:10 -0700277 if (status_t status = mSession->state()->flushExcessBinderRefs(mSession, addr, binder);
278 status != OK)
279 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000280 }
281
282 return finishUnflattenBinder(binder, out);
283 }
284
Steven Morelanda86a3562019-08-01 23:28:34 +0000285 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700286
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700288 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000289 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000290 sp<IBinder> binder =
291 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000292 return finishUnflattenBinder(binder, out);
293 }
294 case BINDER_TYPE_HANDLE: {
295 sp<IBinder> binder =
296 ProcessState::self()->getStrongProxyForHandle(flat->handle);
297 return finishUnflattenBinder(binder, out);
298 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700299 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700300 }
301 return BAD_TYPE;
302}
303
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700304// ---------------------------------------------------------------------------
305
306Parcel::Parcel()
307{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800308 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700309 initState();
310}
311
312Parcel::~Parcel()
313{
314 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800315 LOG_ALLOC("Parcel %p: destroyed", this);
316}
317
318size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600319 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800320}
321
322size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600323 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700324}
325
326const uint8_t* Parcel::data() const
327{
328 return mData;
329}
330
331size_t Parcel::dataSize() const
332{
333 return (mDataSize > mDataPos ? mDataSize : mDataPos);
334}
335
336size_t Parcel::dataAvail() const
337{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700338 size_t result = dataSize() - dataPosition();
339 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700340 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700341 }
342 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700343}
344
345size_t Parcel::dataPosition() const
346{
347 return mDataPos;
348}
349
350size_t Parcel::dataCapacity() const
351{
352 return mDataCapacity;
353}
354
355status_t Parcel::setDataSize(size_t size)
356{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700357 if (size > INT32_MAX) {
358 // don't accept size_t values which may have come from an
359 // inadvertent conversion from a negative int.
360 return BAD_VALUE;
361 }
362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700363 status_t err;
364 err = continueWrite(size);
365 if (err == NO_ERROR) {
366 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700367 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700368 }
369 return err;
370}
371
372void Parcel::setDataPosition(size_t pos) const
373{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700374 if (pos > INT32_MAX) {
375 // don't accept size_t values which may have come from an
376 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700377 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700378 }
379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380 mDataPos = pos;
381 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800382 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700383}
384
385status_t Parcel::setDataCapacity(size_t size)
386{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700387 if (size > INT32_MAX) {
388 // don't accept size_t values which may have come from an
389 // inadvertent conversion from a negative int.
390 return BAD_VALUE;
391 }
392
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700393 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700394 return NO_ERROR;
395}
396
397status_t Parcel::setData(const uint8_t* buffer, size_t len)
398{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700399 if (len > INT32_MAX) {
400 // don't accept size_t values which may have come from an
401 // inadvertent conversion from a negative int.
402 return BAD_VALUE;
403 }
404
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700405 status_t err = restartWrite(len);
406 if (err == NO_ERROR) {
407 memcpy(const_cast<uint8_t*>(data()), buffer, len);
408 mDataSize = len;
409 mFdsKnown = false;
410 }
411 return err;
412}
413
Andreas Huber51faf462011-04-13 10:21:56 -0700414status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700415{
Steven Moreland2034eff2021-10-13 11:24:35 -0700416 if (mSession != parcel->mSession) {
417 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
418 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000419 return BAD_TYPE;
420 }
421
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700423 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800424 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700425 size_t size = parcel->mObjectsSize;
426 int startPos = mDataPos;
427 int firstIndex = -1, lastIndex = -2;
428
429 if (len == 0) {
430 return NO_ERROR;
431 }
432
Nick Kralevichb6b14232015-04-02 09:36:02 -0700433 if (len > INT32_MAX) {
434 // don't accept size_t values which may have come from an
435 // inadvertent conversion from a negative int.
436 return BAD_VALUE;
437 }
438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700439 // range checks against the source parcel size
440 if ((offset > parcel->mDataSize)
441 || (len > parcel->mDataSize)
442 || (offset + len > parcel->mDataSize)) {
443 return BAD_VALUE;
444 }
445
446 // Count objects in range
447 for (int i = 0; i < (int) size; i++) {
448 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700449 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700450 if (firstIndex == -1) {
451 firstIndex = i;
452 }
453 lastIndex = i;
454 }
455 }
456 int numObjects = lastIndex - firstIndex + 1;
457
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700458 if ((mDataSize+len) > mDataCapacity) {
459 // grow data
460 err = growData(len);
461 if (err != NO_ERROR) {
462 return err;
463 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700464 }
465
466 // append data
467 memcpy(mData + mDataPos, data + offset, len);
468 mDataPos += len;
469 mDataSize += len;
470
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400471 err = NO_ERROR;
472
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200474 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475 // grow objects
476 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100477 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
478 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700479 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100480 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800481 binder_size_t *objects =
482 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700483 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700484 return NO_MEMORY;
485 }
486 mObjects = objects;
487 mObjectsCapacity = newSize;
488 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700489
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700490 // append and acquire objects
491 int idx = mObjectsSize;
492 for (int i = firstIndex; i <= lastIndex; i++) {
493 size_t off = objects[i] - offset + startPos;
494 mObjects[idx++] = off;
495 mObjectsSize++;
496
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700497 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498 = reinterpret_cast<flat_binder_object*>(mData + off);
Steven Morelandc673f1f2021-10-07 18:23:35 -0700499 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700500
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700501 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700502 // If this is a file descriptor, we need to dup it so the
503 // new Parcel now owns its own fd, and can declare that we
504 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800505 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800506 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700507 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400508 if (!mAllowFds) {
509 err = FDS_NOT_ALLOWED;
510 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700511 }
512 }
513 }
514
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400515 return err;
516}
517
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700518int Parcel::compareData(const Parcel& other) {
519 size_t size = dataSize();
520 if (size != other.dataSize()) {
521 return size < other.dataSize() ? -1 : 1;
522 }
523 return memcmp(data(), other.data(), size);
524}
525
Bernardo Rufino897b1652021-10-08 10:30:20 +0100526status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
527 size_t len, int* result) const {
528 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
529 // Don't accept size_t values which may have come from an inadvertent conversion from a
530 // negative int.
531 return BAD_VALUE;
532 }
533 size_t thisLimit;
534 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
535 return BAD_VALUE;
536 }
537 size_t otherLimit;
538 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
539 return BAD_VALUE;
540 }
541 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
542 return NO_ERROR;
543}
544
Jeff Brown13b16042014-11-11 16:44:25 -0800545bool Parcel::allowFds() const
546{
547 return mAllowFds;
548}
549
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700550bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400551{
552 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700553 if (!allowFds) {
554 mAllowFds = false;
555 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400556 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700557}
558
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700559void Parcel::restoreAllowFds(bool lastValue)
560{
561 mAllowFds = lastValue;
562}
563
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700564bool Parcel::hasFileDescriptors() const
565{
566 if (!mFdsKnown) {
567 scanForFds();
568 }
569 return mHasFds;
570}
571
Steven Moreland4b2f18d2022-03-24 00:32:25 +0000572std::vector<sp<IBinder>> Parcel::debugReadAllStrongBinders() const {
573 std::vector<sp<IBinder>> ret;
574
575 size_t initPosition = dataPosition();
576 for (size_t i = 0; i < mObjectsSize; i++) {
577 binder_size_t offset = mObjects[i];
578 const flat_binder_object* flat =
579 reinterpret_cast<const flat_binder_object*>(mData + offset);
580 if (flat->hdr.type != BINDER_TYPE_BINDER) continue;
581
582 setDataPosition(offset);
583
584 sp<IBinder> binder = readStrongBinder();
585 if (binder != nullptr) ret.push_back(binder);
586 }
587
588 setDataPosition(initPosition);
589 return ret;
590}
591
592std::vector<int> Parcel::debugReadAllFileDescriptors() const {
593 std::vector<int> ret;
594
595 size_t initPosition = dataPosition();
596 for (size_t i = 0; i < mObjectsSize; i++) {
597 binder_size_t offset = mObjects[i];
598 const flat_binder_object* flat =
599 reinterpret_cast<const flat_binder_object*>(mData + offset);
600 if (flat->hdr.type != BINDER_TYPE_FD) continue;
601
602 setDataPosition(offset);
603
604 int fd = readFileDescriptor();
605 LOG_ALWAYS_FATAL_IF(fd == -1);
606 ret.push_back(fd);
607 }
608
609 setDataPosition(initPosition);
610 return ret;
611}
612
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100613status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100614 if (len > INT32_MAX || offset > INT32_MAX) {
615 // Don't accept size_t values which may have come from an inadvertent conversion from a
616 // negative int.
617 return BAD_VALUE;
618 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100619 size_t limit;
620 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100621 return BAD_VALUE;
622 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100623 *result = false;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100624 for (size_t i = 0; i < mObjectsSize; i++) {
625 size_t pos = mObjects[i];
626 if (pos < offset) continue;
627 if (pos + sizeof(flat_binder_object) > offset + len) {
628 if (mObjectsSorted) break;
629 else continue;
630 }
631 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
632 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100633 *result = true;
634 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100635 }
636 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100637 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100638}
639
Steven Morelandf183fdd2020-10-27 00:12:12 +0000640void Parcel::markSensitive() const
641{
642 mDeallocZero = true;
643}
644
Steven Moreland5553ac42020-11-11 02:14:45 +0000645void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000646 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
647
Steven Moreland5553ac42020-11-11 02:14:45 +0000648 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700649 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000650 }
651}
652
Steven Morelandc9939062021-05-05 17:57:41 +0000653void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000654 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
655 "format must be set before data is written OR on IPC data");
656
Steven Morelandc9939062021-05-05 17:57:41 +0000657 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
658 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000659}
660
661bool Parcel::isForRpc() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000662 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000663}
664
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000665void Parcel::updateWorkSourceRequestHeaderPosition() const {
666 // Only update the request headers once. We only want to point
667 // to the first headers read/written.
668 if (!mRequestHeaderPresent) {
669 mWorkSourceRequestHeaderPosition = dataPosition();
670 mRequestHeaderPresent = true;
671 }
672}
673
Steven Morelandb6c7e222021-02-18 19:20:14 +0000674#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700675constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
Yifan Hong70786532021-10-20 21:41:18 -0700676#elif defined(__ANDROID_RECOVERY__)
677constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O');
Steven Morelandd70160f2019-07-23 10:20:38 -0700678#else
679constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
680#endif
681
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700682// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700683status_t Parcel::writeInterfaceToken(const String16& interface)
684{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000685 return writeInterfaceToken(interface.string(), interface.size());
686}
687
688status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000689 if (CC_LIKELY(!isForRpc())) {
690 const IPCThreadState* threadState = IPCThreadState::self();
691 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
692 updateWorkSourceRequestHeaderPosition();
693 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
694 : IPCThreadState::kUnsetWorkSource);
695 writeInt32(kHeader);
696 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000697
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700698 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000699 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700700}
701
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000702bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
703{
704 if (!mRequestHeaderPresent) {
705 return false;
706 }
707
708 const size_t initialPosition = dataPosition();
709 setDataPosition(mWorkSourceRequestHeaderPosition);
710 status_t err = writeInt32(uid);
711 setDataPosition(initialPosition);
712 return err == NO_ERROR;
713}
714
Steven Morelandf1b1e492019-05-06 15:05:13 -0700715uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000716{
717 if (!mRequestHeaderPresent) {
718 return IPCThreadState::kUnsetWorkSource;
719 }
720
721 const size_t initialPosition = dataPosition();
722 setDataPosition(mWorkSourceRequestHeaderPosition);
723 uid_t uid = readInt32();
724 setDataPosition(initialPosition);
725 return uid;
726}
727
Mathias Agopian83c04462009-05-22 19:00:22 -0700728bool Parcel::checkInterface(IBinder* binder) const
729{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700730 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700731}
732
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700733bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700734 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700735{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700736 return enforceInterface(interface.string(), interface.size(), threadState);
737}
738
739bool Parcel::enforceInterface(const char16_t* interface,
740 size_t len,
741 IPCThreadState* threadState) const
742{
Steven Moreland3af936a2021-03-26 03:05:38 +0000743 if (CC_LIKELY(!isForRpc())) {
744 // StrictModePolicy.
745 int32_t strictPolicy = readInt32();
746 if (threadState == nullptr) {
747 threadState = IPCThreadState::self();
748 }
749 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
750 // For one-way calls, the callee is running entirely
751 // disconnected from the caller, so disable StrictMode entirely.
752 // Not only does disk/network usage not impact the caller, but
753 // there's no way to communicate back violations anyway.
754 threadState->setStrictModePolicy(0);
755 } else {
756 threadState->setStrictModePolicy(strictPolicy);
757 }
758 // WorkSource.
759 updateWorkSourceRequestHeaderPosition();
760 int32_t workSource = readInt32();
761 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
762 // vendor header
763 int32_t header = readInt32();
764 if (header != kHeader) {
765 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
766 header);
767 return false;
768 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700769 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000770
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100771 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700772 size_t parcel_interface_len;
773 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
774 if (len == parcel_interface_len &&
775 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700776 return true;
777 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700778 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700779 String8(interface, len).string(),
780 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700781 return false;
782 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700783}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700784
Jooyung Hand23f9502021-12-23 14:39:57 +0900785binder::Status Parcel::enforceNoDataAvail() const {
786 const auto n = dataAvail();
787 if (n == 0) {
788 return binder::Status::ok();
789 }
790 return binder::Status::
791 fromExceptionCode(binder::Status::Exception::EX_BAD_PARCELABLE,
792 String8::format("Parcel data not fully consumed, unread size: %zu",
793 n));
794}
795
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700796size_t Parcel::objectsCount() const
797{
798 return mObjectsSize;
799}
800
801status_t Parcel::errorCheck() const
802{
803 return mError;
804}
805
806void Parcel::setError(status_t err)
807{
808 mError = err;
809}
810
811status_t Parcel::finishWrite(size_t len)
812{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700813 if (len > INT32_MAX) {
814 // don't accept size_t values which may have come from an
815 // inadvertent conversion from a negative int.
816 return BAD_VALUE;
817 }
818
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700819 //printf("Finish write of %d\n", len);
820 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700821 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700822 if (mDataPos > mDataSize) {
823 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700824 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700825 }
826 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
827 return NO_ERROR;
828}
829
830status_t Parcel::writeUnpadded(const void* data, size_t len)
831{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700832 if (len > INT32_MAX) {
833 // don't accept size_t values which may have come from an
834 // inadvertent conversion from a negative int.
835 return BAD_VALUE;
836 }
837
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700838 size_t end = mDataPos + len;
839 if (end < mDataPos) {
840 // integer overflow
841 return BAD_VALUE;
842 }
843
844 if (end <= mDataCapacity) {
845restart_write:
846 memcpy(mData+mDataPos, data, len);
847 return finishWrite(len);
848 }
849
850 status_t err = growData(len);
851 if (err == NO_ERROR) goto restart_write;
852 return err;
853}
854
855status_t Parcel::write(const void* data, size_t len)
856{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700857 if (len > INT32_MAX) {
858 // don't accept size_t values which may have come from an
859 // inadvertent conversion from a negative int.
860 return BAD_VALUE;
861 }
862
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700863 void* const d = writeInplace(len);
864 if (d) {
865 memcpy(d, data, len);
866 return NO_ERROR;
867 }
868 return mError;
869}
870
871void* Parcel::writeInplace(size_t len)
872{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700873 if (len > INT32_MAX) {
874 // don't accept size_t values which may have come from an
875 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700876 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700877 }
878
879 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700880
Khalid Ramadanb3d817b2021-11-18 07:02:50 +0000881 // check for integer overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700882 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700883 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700884 }
885
886 if ((mDataPos+padded) <= mDataCapacity) {
887restart_write:
888 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
889 uint8_t* const data = mData+mDataPos;
890
891 // Need to pad at end?
892 if (padded != len) {
893#if BYTE_ORDER == BIG_ENDIAN
894 static const uint32_t mask[4] = {
895 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
896 };
897#endif
898#if BYTE_ORDER == LITTLE_ENDIAN
899 static const uint32_t mask[4] = {
900 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
901 };
902#endif
903 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
904 // *reinterpret_cast<void**>(data+padded-4));
905 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
906 }
907
908 finishWrite(padded);
909 return data;
910 }
911
912 status_t err = growData(padded);
913 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700914 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700915}
916
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800917status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
918 const uint8_t* strData = (uint8_t*)str.data();
919 const size_t strLen= str.length();
920 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100921 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800922 return BAD_VALUE;
923 }
924
925 status_t err = writeInt32(utf16Len);
926 if (err) {
927 return err;
928 }
929
930 // Allocate enough bytes to hold our converted string and its terminating NULL.
931 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
932 if (!dst) {
933 return NO_MEMORY;
934 }
935
Sergio Girof4607432016-07-21 14:46:35 +0100936 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800937
938 return NO_ERROR;
939}
940
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900941
Andy Hung49198cf2020-11-18 11:02:39 -0800942status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
943status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800944
Andy Hung49198cf2020-11-18 11:02:39 -0800945status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
946status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700947
Andy Hung49198cf2020-11-18 11:02:39 -0800948status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
949status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
950status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
951status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
952status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
953status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
954status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
955status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
956status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
957status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
958status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
959status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
960status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
961status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
962status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
963status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
964status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
965status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
966status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
967status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
968status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
969status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
970status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
971status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
972status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
973status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
974status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700975
Andy Hung49198cf2020-11-18 11:02:39 -0800976status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800977status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800978 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900979status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800980 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800981status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800982 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900983status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800984 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
985status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800986
Andy Hung49198cf2020-11-18 11:02:39 -0800987status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
988status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
989status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
990
991status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
992status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
993status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
994
995status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
996
997status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
998status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
999
1000status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
1001status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
1002
1003status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
1004status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
1005status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
1006status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
1007status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
1008status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
1009status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
1010status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
1011status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
1012status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
1013status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
1014status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
1015status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
1016status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
1017status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
1018status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
1019status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
1020status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
1021status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
1022status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
1023status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
1024status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
1025status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
1026status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
1027status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
1028status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
1029status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
1030
1031status_t Parcel::readString16Vector(
1032 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
1033status_t Parcel::readString16Vector(
1034 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
1035status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
1036status_t Parcel::readUtf8VectorFromUtf16Vector(
1037 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
1038status_t Parcel::readUtf8VectorFromUtf16Vector(
1039 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
1040status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
1041
1042status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
1043status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
1044status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
1045
1046status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1047status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
1048status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
1049
1050status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001051
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052status_t Parcel::writeInt32(int32_t val)
1053{
Andreas Huber84a6d042009-08-17 13:33:27 -07001054 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001055}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001056
1057status_t Parcel::writeUint32(uint32_t val)
1058{
1059 return writeAligned(val);
1060}
1061
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001062status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001063 if (len > INT32_MAX) {
1064 // don't accept size_t values which may have come from an
1065 // inadvertent conversion from a negative int.
1066 return BAD_VALUE;
1067 }
1068
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001069 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001070 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001071 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001072 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001073 if (ret == NO_ERROR) {
1074 ret = write(val, len * sizeof(*val));
1075 }
1076 return ret;
1077}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001078status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001079 if (len > INT32_MAX) {
1080 // don't accept size_t values which may have come from an
1081 // inadvertent conversion from a negative int.
1082 return BAD_VALUE;
1083 }
1084
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001085 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001086 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001087 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001088 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001089 if (ret == NO_ERROR) {
1090 ret = write(val, len * sizeof(*val));
1091 }
1092 return ret;
1093}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094
Casey Dahlind6848f52015-10-15 15:44:59 -07001095status_t Parcel::writeBool(bool val)
1096{
1097 return writeInt32(int32_t(val));
1098}
1099
1100status_t Parcel::writeChar(char16_t val)
1101{
1102 return writeInt32(int32_t(val));
1103}
1104
1105status_t Parcel::writeByte(int8_t val)
1106{
1107 return writeInt32(int32_t(val));
1108}
1109
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001110status_t Parcel::writeInt64(int64_t val)
1111{
Andreas Huber84a6d042009-08-17 13:33:27 -07001112 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001113}
1114
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001115status_t Parcel::writeUint64(uint64_t val)
1116{
1117 return writeAligned(val);
1118}
1119
Serban Constantinescuf683e012013-11-05 16:53:55 +00001120status_t Parcel::writePointer(uintptr_t val)
1121{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001122 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001123}
1124
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001125status_t Parcel::writeFloat(float val)
1126{
Andreas Huber84a6d042009-08-17 13:33:27 -07001127 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001128}
1129
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001130#if defined(__mips__) && defined(__mips_hard_float)
1131
1132status_t Parcel::writeDouble(double val)
1133{
1134 union {
1135 double d;
1136 unsigned long long ll;
1137 } u;
1138 u.d = val;
1139 return writeAligned(u.ll);
1140}
1141
1142#else
1143
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001144status_t Parcel::writeDouble(double val)
1145{
Andreas Huber84a6d042009-08-17 13:33:27 -07001146 return writeAligned(val);
1147}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001148
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001149#endif
1150
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001151status_t Parcel::writeCString(const char* str)
1152{
1153 return write(str, strlen(str)+1);
1154}
1155
1156status_t Parcel::writeString8(const String8& str)
1157{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001158 return writeString8(str.string(), str.size());
1159}
1160
1161status_t Parcel::writeString8(const char* str, size_t len)
1162{
1163 if (str == nullptr) return writeInt32(-1);
1164
Jeff Sharkey18220902020-11-05 08:36:20 -07001165 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001166 status_t err = writeInt32(len);
1167 if (err == NO_ERROR) {
1168 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1169 if (data) {
1170 memcpy(data, str, len);
1171 *reinterpret_cast<char*>(data+len) = 0;
1172 return NO_ERROR;
1173 }
1174 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001175 }
1176 return err;
1177}
1178
1179status_t Parcel::writeString16(const String16& str)
1180{
1181 return writeString16(str.string(), str.size());
1182}
1183
1184status_t Parcel::writeString16(const char16_t* str, size_t len)
1185{
Yi Kong91635562018-06-07 14:38:36 -07001186 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001187
Jeff Sharkey18220902020-11-05 08:36:20 -07001188 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001189 status_t err = writeInt32(len);
1190 if (err == NO_ERROR) {
1191 len *= sizeof(char16_t);
1192 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1193 if (data) {
1194 memcpy(data, str, len);
1195 *reinterpret_cast<char16_t*>(data+len) = 0;
1196 return NO_ERROR;
1197 }
1198 err = mError;
1199 }
1200 return err;
1201}
1202
1203status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1204{
Steven Morelanda86a3562019-08-01 23:28:34 +00001205 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001206}
1207
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001208
Casey Dahlinb9872622015-11-25 15:09:45 -08001209status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1210 if (!parcelable) {
1211 return writeInt32(0);
1212 }
1213
1214 return writeParcelable(*parcelable);
1215}
1216
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001217status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001218{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001219 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001220 return BAD_TYPE;
1221
1222 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001223 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001224 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001226 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001227 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001229 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1230 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
1232 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001233 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 return err;
1235 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001236 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001237 return err;
1238}
1239
Jeff Brown93ff1f92011-11-04 19:01:44 -07001240status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001241{
Steven Moreland5553ac42020-11-11 02:14:45 +00001242 if (isForRpc()) {
1243 ALOGE("Cannot write file descriptor to remote binder.");
1244 return BAD_TYPE;
1245 }
1246
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001247 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001248 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001249 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001250 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001251 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001252 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001253 return writeObject(obj, true);
1254}
1255
1256status_t Parcel::writeDupFileDescriptor(int fd)
1257{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001258 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001259 if (dupFd < 0) {
1260 return -errno;
1261 }
1262 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001263 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001264 close(dupFd);
1265 }
1266 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001267}
1268
Dianne Hackborn1941a402016-08-29 12:30:43 -07001269status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1270{
1271 writeInt32(0);
1272 return writeFileDescriptor(fd, takeOwnership);
1273}
1274
Ryo Hashimotobf551892018-05-31 16:58:35 +09001275status_t Parcel::writeDupParcelFileDescriptor(int fd)
1276{
1277 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1278 if (dupFd < 0) {
1279 return -errno;
1280 }
1281 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1282 if (err != OK) {
1283 close(dupFd);
1284 }
1285 return err;
1286}
1287
Christopher Wiley2cf19952016-04-11 11:09:37 -07001288status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001289 return writeDupFileDescriptor(fd.get());
1290}
1291
Jeff Brown13b16042014-11-11 16:44:25 -08001292status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001293{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001294 if (len > INT32_MAX) {
1295 // don't accept size_t values which may have come from an
1296 // inadvertent conversion from a negative int.
1297 return BAD_VALUE;
1298 }
1299
Jeff Brown13b16042014-11-11 16:44:25 -08001300 status_t status;
1301 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001302 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001303 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001304 if (status) return status;
1305
1306 void* ptr = writeInplace(len);
1307 if (!ptr) return NO_MEMORY;
1308
Jeff Brown13b16042014-11-11 16:44:25 -08001309 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001310 return NO_ERROR;
1311 }
1312
Steve Block6807e592011-10-20 11:56:00 +01001313 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001314 int fd = ashmem_create_region("Parcel Blob", len);
1315 if (fd < 0) return NO_MEMORY;
1316
1317 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1318 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001319 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001320 } else {
Yi Kong91635562018-06-07 14:38:36 -07001321 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001322 if (ptr == MAP_FAILED) {
1323 status = -errno;
1324 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001325 if (!mutableCopy) {
1326 result = ashmem_set_prot_region(fd, PROT_READ);
1327 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001328 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001329 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001330 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001331 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001332 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001333 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001334 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001335 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001336 return NO_ERROR;
1337 }
1338 }
1339 }
1340 }
1341 ::munmap(ptr, len);
1342 }
1343 ::close(fd);
1344 return status;
1345}
1346
Jeff Brown13b16042014-11-11 16:44:25 -08001347status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1348{
1349 // Must match up with what's done in writeBlob.
1350 if (!mAllowFds) return FDS_NOT_ALLOWED;
1351 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1352 if (status) return status;
1353 return writeDupFileDescriptor(fd);
1354}
1355
Mathias Agopiane1424282013-07-29 21:24:40 -07001356status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001357{
1358 status_t err;
1359
1360 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001361 const size_t len = val.getFlattenedSize();
1362 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001363
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001364 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001365 // don't accept size_t values which may have come from an
1366 // inadvertent conversion from a negative int.
1367 return BAD_VALUE;
1368 }
1369
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001370 err = this->writeInt32(len);
1371 if (err) return err;
1372
1373 err = this->writeInt32(fd_count);
1374 if (err) return err;
1375
1376 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001377 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001378 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001379 return BAD_VALUE;
1380
Yi Kong91635562018-06-07 14:38:36 -07001381 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001382 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001383 fds = new (std::nothrow) int[fd_count];
1384 if (fds == nullptr) {
1385 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1386 return BAD_VALUE;
1387 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001388 }
1389
1390 err = val.flatten(buf, len, fds, fd_count);
1391 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1392 err = this->writeDupFileDescriptor( fds[i] );
1393 }
1394
1395 if (fd_count) {
1396 delete [] fds;
1397 }
1398
1399 return err;
1400}
1401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001402status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1403{
1404 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1405 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1406 if (enoughData && enoughObjects) {
1407restart_write:
1408 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001409
Christopher Tate98e67d32015-06-03 18:44:15 -07001410 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001411 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001412 if (!mAllowFds) {
1413 // fail before modifying our object index
1414 return FDS_NOT_ALLOWED;
1415 }
1416 mHasFds = mFdsKnown = true;
1417 }
1418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001419 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001420 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001421 mObjects[mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001422 acquire_object(ProcessState::self(), val, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001423 mObjectsSize++;
1424 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001425
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001426 return finishWrite(sizeof(flat_binder_object));
1427 }
1428
1429 if (!enoughData) {
1430 const status_t err = growData(sizeof(val));
1431 if (err != NO_ERROR) return err;
1432 }
1433 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001434 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1435 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001436 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001437 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001438 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001439 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001440 mObjects = objects;
1441 mObjectsCapacity = newSize;
1442 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001444 goto restart_write;
1445}
1446
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001447status_t Parcel::writeNoException()
1448{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001449 binder::Status status;
1450 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001451}
1452
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001453status_t Parcel::validateReadData(size_t upperBound) const
1454{
1455 // Don't allow non-object reads on object data
1456 if (mObjectsSorted || mObjectsSize <= 1) {
1457data_sorted:
1458 // Expect to check only against the next object
1459 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1460 // For some reason the current read position is greater than the next object
1461 // hint. Iterate until we find the right object
1462 size_t nextObject = mNextObjectHint;
1463 do {
1464 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1465 // Requested info overlaps with an object
1466 ALOGE("Attempt to read from protected data in Parcel %p", this);
1467 return PERMISSION_DENIED;
1468 }
1469 nextObject++;
1470 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1471 mNextObjectHint = nextObject;
1472 }
1473 return NO_ERROR;
1474 }
1475 // Quickly determine if mObjects is sorted.
1476 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1477 binder_size_t* prevObj = currObj;
1478 while (currObj > mObjects) {
1479 prevObj--;
1480 if(*prevObj > *currObj) {
1481 goto data_unsorted;
1482 }
1483 currObj--;
1484 }
1485 mObjectsSorted = true;
1486 goto data_sorted;
1487
1488data_unsorted:
1489 // Insertion Sort mObjects
1490 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1491 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1492 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1493 binder_size_t temp = *iter0;
1494 binder_size_t* iter1 = iter0 - 1;
1495 while (iter1 >= mObjects && *iter1 > temp) {
1496 *(iter1 + 1) = *iter1;
1497 iter1--;
1498 }
1499 *(iter1 + 1) = temp;
1500 }
1501 mNextObjectHint = 0;
1502 mObjectsSorted = true;
1503 goto data_sorted;
1504}
1505
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001506status_t Parcel::read(void* outData, size_t len) const
1507{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001508 if (len > INT32_MAX) {
1509 // don't accept size_t values which may have come from an
1510 // inadvertent conversion from a negative int.
1511 return BAD_VALUE;
1512 }
1513
1514 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1515 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001516 if (mObjectsSize > 0) {
1517 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001518 if(err != NO_ERROR) {
1519 // Still increment the data position by the expected length
1520 mDataPos += pad_size(len);
1521 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1522 return err;
1523 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001524 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001526 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001527 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001528 return NO_ERROR;
1529 }
1530 return NOT_ENOUGH_DATA;
1531}
1532
1533const void* Parcel::readInplace(size_t len) const
1534{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001535 if (len > INT32_MAX) {
1536 // don't accept size_t values which may have come from an
1537 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001538 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001539 }
1540
1541 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1542 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001543 if (mObjectsSize > 0) {
1544 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001545 if(err != NO_ERROR) {
1546 // Still increment the data position by the expected length
1547 mDataPos += pad_size(len);
1548 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001549 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001550 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001551 }
1552
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001553 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001554 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001555 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001556 return data;
1557 }
Yi Kong91635562018-06-07 14:38:36 -07001558 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001559}
1560
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001561status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1562 if (status_t status = readInt32(size); status != OK) return status;
1563 if (*size < 0) return OK; // may be null, client to handle
1564
1565 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1566
1567 // approximation, can't know max element size (e.g. if it makes heap
1568 // allocations)
1569 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1570 int32_t allocationSize;
1571 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1572
1573 // High limit of 1MB since something this big could never be returned. Could
1574 // probably scope this down, but might impact very specific usecases.
1575 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1576
1577 if (allocationSize >= kMaxAllocationSize) {
1578 return NO_MEMORY;
1579 }
1580
1581 return OK;
1582}
1583
Andreas Huber84a6d042009-08-17 13:33:27 -07001584template<class T>
1585status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001586 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001587
1588 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001589 if (mObjectsSize > 0) {
1590 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001591 if(err != NO_ERROR) {
1592 // Still increment the data position by the expected length
1593 mDataPos += sizeof(T);
1594 return err;
1595 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001596 }
1597
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001598 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001599 mDataPos += sizeof(T);
1600 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001601 return NO_ERROR;
1602 } else {
1603 return NOT_ENOUGH_DATA;
1604 }
1605}
1606
Andreas Huber84a6d042009-08-17 13:33:27 -07001607template<class T>
1608T Parcel::readAligned() const {
1609 T result;
1610 if (readAligned(&result) != NO_ERROR) {
1611 result = 0;
1612 }
1613
1614 return result;
1615}
1616
1617template<class T>
1618status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001619 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001620
1621 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1622restart_write:
1623 *reinterpret_cast<T*>(mData+mDataPos) = val;
1624 return finishWrite(sizeof(val));
1625 }
1626
1627 status_t err = growData(sizeof(val));
1628 if (err == NO_ERROR) goto restart_write;
1629 return err;
1630}
1631
1632status_t Parcel::readInt32(int32_t *pArg) const
1633{
1634 return readAligned(pArg);
1635}
1636
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001637int32_t Parcel::readInt32() const
1638{
Andreas Huber84a6d042009-08-17 13:33:27 -07001639 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001640}
1641
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001642status_t Parcel::readUint32(uint32_t *pArg) const
1643{
1644 return readAligned(pArg);
1645}
1646
1647uint32_t Parcel::readUint32() const
1648{
1649 return readAligned<uint32_t>();
1650}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651
1652status_t Parcel::readInt64(int64_t *pArg) const
1653{
Andreas Huber84a6d042009-08-17 13:33:27 -07001654 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001655}
1656
1657
1658int64_t Parcel::readInt64() const
1659{
Andreas Huber84a6d042009-08-17 13:33:27 -07001660 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001661}
1662
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001663status_t Parcel::readUint64(uint64_t *pArg) const
1664{
1665 return readAligned(pArg);
1666}
1667
1668uint64_t Parcel::readUint64() const
1669{
1670 return readAligned<uint64_t>();
1671}
1672
Serban Constantinescuf683e012013-11-05 16:53:55 +00001673status_t Parcel::readPointer(uintptr_t *pArg) const
1674{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001675 status_t ret;
1676 binder_uintptr_t ptr;
1677 ret = readAligned(&ptr);
1678 if (!ret)
1679 *pArg = ptr;
1680 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001681}
1682
1683uintptr_t Parcel::readPointer() const
1684{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001685 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001686}
1687
1688
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001689status_t Parcel::readFloat(float *pArg) const
1690{
Andreas Huber84a6d042009-08-17 13:33:27 -07001691 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001692}
1693
1694
1695float Parcel::readFloat() const
1696{
Andreas Huber84a6d042009-08-17 13:33:27 -07001697 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001698}
1699
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001700#if defined(__mips__) && defined(__mips_hard_float)
1701
1702status_t Parcel::readDouble(double *pArg) const
1703{
1704 union {
1705 double d;
1706 unsigned long long ll;
1707 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001708 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001709 status_t status;
1710 status = readAligned(&u.ll);
1711 *pArg = u.d;
1712 return status;
1713}
1714
1715double Parcel::readDouble() const
1716{
1717 union {
1718 double d;
1719 unsigned long long ll;
1720 } u;
1721 u.ll = readAligned<unsigned long long>();
1722 return u.d;
1723}
1724
1725#else
1726
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001727status_t Parcel::readDouble(double *pArg) const
1728{
Andreas Huber84a6d042009-08-17 13:33:27 -07001729 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001730}
1731
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732double Parcel::readDouble() const
1733{
Andreas Huber84a6d042009-08-17 13:33:27 -07001734 return readAligned<double>();
1735}
1736
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001737#endif
1738
Casey Dahlind6848f52015-10-15 15:44:59 -07001739status_t Parcel::readBool(bool *pArg) const
1740{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001741 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001742 status_t ret = readInt32(&tmp);
1743 *pArg = (tmp != 0);
1744 return ret;
1745}
1746
1747bool Parcel::readBool() const
1748{
1749 return readInt32() != 0;
1750}
1751
1752status_t Parcel::readChar(char16_t *pArg) const
1753{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001754 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001755 status_t ret = readInt32(&tmp);
1756 *pArg = char16_t(tmp);
1757 return ret;
1758}
1759
1760char16_t Parcel::readChar() const
1761{
1762 return char16_t(readInt32());
1763}
1764
1765status_t Parcel::readByte(int8_t *pArg) const
1766{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001767 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001768 status_t ret = readInt32(&tmp);
1769 *pArg = int8_t(tmp);
1770 return ret;
1771}
1772
1773int8_t Parcel::readByte() const
1774{
1775 return int8_t(readInt32());
1776}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001777
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001778status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1779 size_t utf16Size = 0;
1780 const char16_t* src = readString16Inplace(&utf16Size);
1781 if (!src) {
1782 return UNEXPECTED_NULL;
1783 }
1784
1785 // Save ourselves the trouble, we're done.
1786 if (utf16Size == 0u) {
1787 str->clear();
1788 return NO_ERROR;
1789 }
1790
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001791 // Allow for closing '\0'
1792 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1793 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001794 return BAD_VALUE;
1795 }
1796 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001797 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001798 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001799 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1800 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001801 return NO_ERROR;
1802}
1803
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001804const char* Parcel::readCString() const
1805{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001806 if (mDataPos < mDataSize) {
1807 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1809 // is the string's trailing NUL within the parcel's valid bounds?
1810 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1811 if (eos) {
1812 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001813 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001814 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001815 return str;
1816 }
1817 }
Yi Kong91635562018-06-07 14:38:36 -07001818 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001819}
1820
1821String8 Parcel::readString8() const
1822{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001823 size_t len;
1824 const char* str = readString8Inplace(&len);
1825 if (str) return String8(str, len);
1826 ALOGE("Reading a NULL string not supported here.");
1827 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001828}
1829
1830status_t Parcel::readString8(String8* pArg) const
1831{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001832 size_t len;
1833 const char* str = readString8Inplace(&len);
1834 if (str) {
1835 pArg->setTo(str, len);
1836 return 0;
1837 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001838 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001839 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001840 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001841}
1842
1843const char* Parcel::readString8Inplace(size_t* outLen) const
1844{
1845 int32_t size = readInt32();
1846 // watch for potential int overflow from size+1
1847 if (size >= 0 && size < INT32_MAX) {
1848 *outLen = size;
1849 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001850 if (str != nullptr) {
1851 if (str[size] == '\0') {
1852 return str;
1853 }
1854 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001855 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001856 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001857 *outLen = 0;
1858 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001859}
1860
1861String16 Parcel::readString16() const
1862{
1863 size_t len;
1864 const char16_t* str = readString16Inplace(&len);
1865 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001866 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 return String16();
1868}
1869
Casey Dahlinb9872622015-11-25 15:09:45 -08001870
Casey Dahlin451ff582015-10-19 18:12:18 -07001871status_t Parcel::readString16(String16* pArg) const
1872{
1873 size_t len;
1874 const char16_t* str = readString16Inplace(&len);
1875 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001876 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001877 return 0;
1878 } else {
1879 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001880 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001881 }
1882}
1883
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001884const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1885{
1886 int32_t size = readInt32();
1887 // watch for potential int overflow from size+1
1888 if (size >= 0 && size < INT32_MAX) {
1889 *outLen = size;
1890 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001891 if (str != nullptr) {
1892 if (str[size] == u'\0') {
1893 return str;
1894 }
1895 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 }
1897 }
1898 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001899 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001900}
1901
Casey Dahlinf0c13772015-10-27 18:33:56 -07001902status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1903{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001904 status_t status = readNullableStrongBinder(val);
1905 if (status == OK && !val->get()) {
Steven Morelanddce98242022-03-11 00:26:10 +00001906 ALOGW("Expecting binder but got null!");
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001907 status = UNEXPECTED_NULL;
1908 }
1909 return status;
1910}
1911
1912status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1913{
Steven Morelanda86a3562019-08-01 23:28:34 +00001914 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001915}
1916
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001917sp<IBinder> Parcel::readStrongBinder() const
1918{
1919 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001920 // Note that a lot of code in Android reads binders by hand with this
1921 // method, and that code has historically been ok with getting nullptr
1922 // back (while ignoring error codes).
1923 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001924 return val;
1925}
1926
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001927int32_t Parcel::readExceptionCode() const
1928{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001929 binder::Status status;
1930 status.readFromParcel(*this);
1931 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001932}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001933
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001934native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001935{
1936 int numFds, numInts;
1937 status_t err;
1938 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001939 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001940 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001941 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001942
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001943 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001944 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001945 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001946 }
1947
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001948 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001949 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001950 if (h->data[i] < 0) {
1951 for (int j = 0; j < i; j++) {
1952 close(h->data[j]);
1953 }
1954 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001955 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001956 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001957 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001958 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001959 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001960 native_handle_close(h);
1961 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001962 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001963 }
1964 return h;
1965}
1966
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001967int Parcel::readFileDescriptor() const
1968{
1969 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001970
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001971 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001972 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001973 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001974
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001975 return BAD_TYPE;
1976}
1977
Dianne Hackborn1941a402016-08-29 12:30:43 -07001978int Parcel::readParcelFileDescriptor() const
1979{
1980 int32_t hasComm = readInt32();
1981 int fd = readFileDescriptor();
1982 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001983 // detach (owned by the binder driver)
1984 int comm = readFileDescriptor();
1985
1986 // warning: this must be kept in sync with:
1987 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1988 enum ParcelFileDescriptorStatus {
1989 DETACHED = 2,
1990 };
1991
1992#if BYTE_ORDER == BIG_ENDIAN
1993 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1994#endif
1995#if BYTE_ORDER == LITTLE_ENDIAN
1996 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1997#endif
1998
1999 ssize_t written = TEMP_FAILURE_RETRY(
2000 ::write(comm, &message, sizeof(message)));
2001
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08002002 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08002003 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
2004 written, strerror(errno));
2005 return BAD_TYPE;
2006 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07002007 }
2008 return fd;
2009}
2010
Christopher Wiley2cf19952016-04-11 11:09:37 -07002011status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08002012{
2013 int got = readFileDescriptor();
2014
2015 if (got == BAD_TYPE) {
2016 return BAD_TYPE;
2017 }
2018
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002019 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08002020
2021 if (val->get() < 0) {
2022 return BAD_VALUE;
2023 }
2024
2025 return OK;
2026}
2027
Ryo Hashimotobf551892018-05-31 16:58:35 +09002028status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
2029{
2030 int got = readParcelFileDescriptor();
2031
2032 if (got == BAD_TYPE) {
2033 return BAD_TYPE;
2034 }
2035
2036 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
2037
2038 if (val->get() < 0) {
2039 return BAD_VALUE;
2040 }
2041
2042 return OK;
2043}
Casey Dahlin06673e32015-11-23 13:24:23 -08002044
Jeff Brown5707dbf2011-09-23 21:17:56 -07002045status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
2046{
Jeff Brown13b16042014-11-11 16:44:25 -08002047 int32_t blobType;
2048 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002049 if (status) return status;
2050
Jeff Brown13b16042014-11-11 16:44:25 -08002051 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01002052 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07002053 const void* ptr = readInplace(len);
2054 if (!ptr) return BAD_VALUE;
2055
Jeff Brown13b16042014-11-11 16:44:25 -08002056 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002057 return NO_ERROR;
2058 }
2059
Steve Block6807e592011-10-20 11:56:00 +01002060 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002061 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002062 int fd = readFileDescriptor();
2063 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2064
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002065 if (!ashmem_valid(fd)) {
2066 ALOGE("invalid fd");
2067 return BAD_VALUE;
2068 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002069 int size = ashmem_get_size_region(fd);
2070 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002071 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002072 return BAD_VALUE;
2073 }
Yi Kong91635562018-06-07 14:38:36 -07002074 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002075 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002076 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002077
Jeff Brown13b16042014-11-11 16:44:25 -08002078 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002079 return NO_ERROR;
2080}
2081
Mathias Agopiane1424282013-07-29 21:24:40 -07002082status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002083{
2084 // size
2085 const size_t len = this->readInt32();
2086 const size_t fd_count = this->readInt32();
2087
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002088 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002089 // don't accept size_t values which may have come from an
2090 // inadvertent conversion from a negative int.
2091 return BAD_VALUE;
2092 }
2093
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002094 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002095 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002096 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002097 return BAD_VALUE;
2098
Yi Kong91635562018-06-07 14:38:36 -07002099 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002100 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002101 fds = new (std::nothrow) int[fd_count];
2102 if (fds == nullptr) {
2103 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2104 return BAD_VALUE;
2105 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002106 }
2107
2108 status_t err = NO_ERROR;
2109 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002110 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002111 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002112 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002113 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 -07002114 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2115 // Close all the file descriptors that were dup-ed.
2116 for (size_t j=0; j<i ;j++) {
2117 close(fds[j]);
2118 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002119 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002120 }
2121
2122 if (err == NO_ERROR) {
2123 err = val.unflatten(buf, len, fds, fd_count);
2124 }
2125
2126 if (fd_count) {
2127 delete [] fds;
2128 }
2129
2130 return err;
2131}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002132const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2133{
2134 const size_t DPOS = mDataPos;
2135 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2136 const flat_binder_object* obj
2137 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2138 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002139 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002140 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 // the object list, so we don't want to check for it when
2142 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002143 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144 return obj;
2145 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002146
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002148 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149 const size_t N = mObjectsSize;
2150 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002151
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002153 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156 // Start at the current hint position, looking for an object at
2157 // the current data position.
2158 if (opos < N) {
2159 while (opos < (N-1) && OBJS[opos] < DPOS) {
2160 opos++;
2161 }
2162 } else {
2163 opos = N-1;
2164 }
2165 if (OBJS[opos] == DPOS) {
2166 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002167 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002168 this, DPOS, opos);
2169 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002170 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002171 return obj;
2172 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002173
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002174 // Look backwards for it...
2175 while (opos > 0 && OBJS[opos] > DPOS) {
2176 opos--;
2177 }
2178 if (OBJS[opos] == DPOS) {
2179 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002180 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 this, DPOS, opos);
2182 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002183 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 return obj;
2185 }
2186 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002187 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 -07002188 this, DPOS);
2189 }
Yi Kong91635562018-06-07 14:38:36 -07002190 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002191}
2192
2193void Parcel::closeFileDescriptors()
2194{
2195 size_t i = mObjectsSize;
2196 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002197 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198 }
2199 while (i > 0) {
2200 i--;
2201 const flat_binder_object* flat
2202 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002203 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002204 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 close(flat->handle);
2206 }
2207 }
2208}
2209
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002210uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002211{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002212 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002213}
2214
2215size_t Parcel::ipcDataSize() const
2216{
2217 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2218}
2219
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002220uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002222 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002223}
2224
2225size_t Parcel::ipcObjectsCount() const
2226{
2227 return mObjectsSize;
2228}
2229
2230void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002231 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232{
Steven Moreland438cce82021-04-02 18:04:08 +00002233 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2234 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2235
Steven Morelandceed9bb2020-12-17 01:01:06 +00002236 freeData();
2237
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 mData = const_cast<uint8_t*>(data);
2239 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002240 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002241 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002243
2244 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002245 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002246 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002247 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002248 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002249 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002250 mObjectsSize = 0;
2251 break;
2252 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002253 const flat_binder_object* flat
2254 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2255 uint32_t type = flat->hdr.type;
2256 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2257 type == BINDER_TYPE_FD)) {
2258 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2259 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
Steven Morelandf2e0a952021-11-01 18:17:23 -07002260 // recover gracefully by clearing out the objects.
Martijn Coenen82c75312019-07-24 15:18:30 +02002261 android_errorWriteLog(0x534e4554, "135930648");
Steven Morelandf2e0a952021-11-01 18:17:23 -07002262 android_errorWriteLog(0x534e4554, "203847542");
Martijn Coenen82c75312019-07-24 15:18:30 +02002263 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2264 __func__, type, (uint64_t)offset);
Steven Morelandf2e0a952021-11-01 18:17:23 -07002265
2266 // WARNING: callers of ipcSetDataReference need to make sure they
2267 // don't rely on mObjectsSize in their release_func.
Martijn Coenen82c75312019-07-24 15:18:30 +02002268 mObjectsSize = 0;
2269 break;
2270 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002271 minOffset = offset + sizeof(flat_binder_object);
2272 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 scanForFds();
2274}
2275
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002276void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002277{
2278 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002279
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002280 if (errorCheck() != NO_ERROR) {
2281 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002282 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002283 } else if (dataSize() > 0) {
2284 const uint8_t* DATA = data();
2285 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002286 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002287 const size_t N = objectsCount();
2288 for (size_t i=0; i<N; i++) {
2289 const flat_binder_object* flat
2290 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2291 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002292 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002293 << " = " << flat->binder;
2294 }
2295 } else {
2296 to << "NULL";
2297 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002298
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299 to << ")";
2300}
2301
2302void Parcel::releaseObjects()
2303{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002304 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002305 if (i == 0) {
2306 return;
2307 }
2308 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002309 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002310 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002311 while (i > 0) {
2312 i--;
2313 const flat_binder_object* flat
2314 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002315 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316 }
2317}
2318
2319void Parcel::acquireObjects()
2320{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002321 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002322 if (i == 0) {
2323 return;
2324 }
2325 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002326 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002327 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 while (i > 0) {
2329 i--;
2330 const flat_binder_object* flat
2331 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002332 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002333 }
2334}
2335
2336void Parcel::freeData()
2337{
2338 freeDataNoInit();
2339 initState();
2340}
2341
2342void Parcel::freeDataNoInit()
2343{
2344 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002345 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002346 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002347 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002349 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002351 if (mData) {
2352 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002353 gParcelGlobalAllocSize -= mDataCapacity;
2354 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002355 if (mDeallocZero) {
2356 zeroMemory(mData, mDataSize);
2357 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002358 free(mData);
2359 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002360 if (mObjects) free(mObjects);
2361 }
2362}
2363
2364status_t Parcel::growData(size_t len)
2365{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002366 if (len > INT32_MAX) {
2367 // don't accept size_t values which may have come from an
2368 // inadvertent conversion from a negative int.
2369 return BAD_VALUE;
2370 }
2371
Martijn Coenen93fe5182020-01-22 10:46:25 +01002372 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2373 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374 size_t newSize = ((mDataSize+len)*3)/2;
2375 return (newSize <= mDataSize)
2376 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002377 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378}
2379
Steven Morelandf183fdd2020-10-27 00:12:12 +00002380static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2381 if (!zero) {
2382 return (uint8_t*)realloc(data, newCapacity);
2383 }
2384 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2385 if (!newData) {
2386 return nullptr;
2387 }
2388
2389 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2390 zeroMemory(data, oldCapacity);
2391 free(data);
2392 return newData;
2393}
2394
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002395status_t Parcel::restartWrite(size_t desired)
2396{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002397 if (desired > INT32_MAX) {
2398 // don't accept size_t values which may have come from an
2399 // inadvertent conversion from a negative int.
2400 return BAD_VALUE;
2401 }
2402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002403 if (mOwner) {
2404 freeData();
2405 return continueWrite(desired);
2406 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002407
Steven Morelandf183fdd2020-10-27 00:12:12 +00002408 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if (!data && desired > mDataCapacity) {
2410 mError = NO_MEMORY;
2411 return NO_MEMORY;
2412 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002413
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002414 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002415
Devin Moore4a0a55e2020-06-04 13:23:10 -07002416 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002417 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002418 if (mDataCapacity > desired) {
2419 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2420 } else {
2421 gParcelGlobalAllocSize += (desired - mDataCapacity);
2422 }
2423
Colin Cross83ec65e2015-12-08 17:15:50 -08002424 if (!mData) {
2425 gParcelGlobalAllocCount++;
2426 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 mData = data;
2428 mDataCapacity = desired;
2429 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002431 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002432 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2433 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2434
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002435 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002436 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002437 mObjectsSize = mObjectsCapacity = 0;
2438 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002439 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002440 mHasFds = false;
2441 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002442 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 return NO_ERROR;
2445}
2446
2447status_t Parcel::continueWrite(size_t desired)
2448{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002449 if (desired > INT32_MAX) {
2450 // don't accept size_t values which may have come from an
2451 // inadvertent conversion from a negative int.
2452 return BAD_VALUE;
2453 }
2454
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 // If shrinking, first adjust for any objects that appear
2456 // after the new data size.
2457 size_t objectsSize = mObjectsSize;
2458 if (desired < mDataSize) {
2459 if (desired == 0) {
2460 objectsSize = 0;
2461 } else {
2462 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002463 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002464 break;
2465 objectsSize--;
2466 }
2467 }
2468 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002469
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002470 if (mOwner) {
2471 // If the size is going to zero, just release the owner's data.
2472 if (desired == 0) {
2473 freeData();
2474 return NO_ERROR;
2475 }
2476
2477 // If there is a different owner, we need to take
2478 // posession.
2479 uint8_t* data = (uint8_t*)malloc(desired);
2480 if (!data) {
2481 mError = NO_MEMORY;
2482 return NO_MEMORY;
2483 }
Yi Kong91635562018-06-07 14:38:36 -07002484 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002485
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002487 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002488 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002489 free(data);
2490
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002491 mError = NO_MEMORY;
2492 return NO_MEMORY;
2493 }
2494
2495 // Little hack to only acquire references on objects
2496 // we will be keeping.
2497 size_t oldObjectsSize = mObjectsSize;
2498 mObjectsSize = objectsSize;
2499 acquireObjects();
2500 mObjectsSize = oldObjectsSize;
2501 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 if (mData) {
2504 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2505 }
2506 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002507 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002508 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002509 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002510 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002511 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002513 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002514 gParcelGlobalAllocSize += desired;
2515 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002516
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 mData = data;
2518 mObjects = objects;
2519 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002520 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mDataCapacity = desired;
2522 mObjectsSize = mObjectsCapacity = objectsSize;
2523 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002524 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525
2526 } else if (mData) {
2527 if (objectsSize < mObjectsSize) {
2528 // Need to release refs on any objects we are dropping.
2529 const sp<ProcessState> proc(ProcessState::self());
2530 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2531 const flat_binder_object* flat
2532 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002533 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 // will need to rescan because we may have lopped off the only FDs
2535 mFdsKnown = false;
2536 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002537 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002538 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002539
2540 if (objectsSize == 0) {
2541 free(mObjects);
2542 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002543 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002544 } else {
2545 binder_size_t* objects =
2546 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2547 if (objects) {
2548 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002549 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002550 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002551 }
2552 mObjectsSize = objectsSize;
2553 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002554 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 }
2556
2557 // We own the data, so we can just do a realloc().
2558 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002559 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002560 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002561 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2562 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002563 gParcelGlobalAllocSize += desired;
2564 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565 mData = data;
2566 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002567 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002568 mError = NO_MEMORY;
2569 return NO_MEMORY;
2570 }
2571 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002572 if (mDataSize > desired) {
2573 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002574 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002575 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002576 if (mDataPos > desired) {
2577 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002578 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002579 }
2580 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002581
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002582 } else {
2583 // This is the first data. Easy!
2584 uint8_t* data = (uint8_t*)malloc(desired);
2585 if (!data) {
2586 mError = NO_MEMORY;
2587 return NO_MEMORY;
2588 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002589
Yi Kong91635562018-06-07 14:38:36 -07002590 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002591 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002592 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002593 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002594
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002595 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002596 gParcelGlobalAllocSize += desired;
2597 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002598
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002599 mData = data;
2600 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002601 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2602 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002603 mDataCapacity = desired;
2604 }
2605
2606 return NO_ERROR;
2607}
2608
2609void Parcel::initState()
2610{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002611 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002612 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002613 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002614 mDataSize = 0;
2615 mDataCapacity = 0;
2616 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002617 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2618 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandc9939062021-05-05 17:57:41 +00002619 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002620 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002621 mObjectsSize = 0;
2622 mObjectsCapacity = 0;
2623 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002624 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002625 mHasFds = false;
2626 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002627 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002628 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002629 mOwner = nullptr;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002630 mWorkSourceRequestHeaderPosition = 0;
2631 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002632
2633 // racing multiple init leads only to multiple identical write
2634 if (gMaxFds == 0) {
2635 struct rlimit result;
2636 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2637 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002638 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002639 } else {
2640 ALOGW("Unable to getrlimit: %s", strerror(errno));
2641 gMaxFds = 1024;
2642 }
2643 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002644}
2645
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002646void Parcel::scanForFds() const {
2647 status_t status = hasFileDescriptorsInRange(0, dataSize(), &mHasFds);
2648 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002649 mFdsKnown = true;
2650}
2651
Dan Sandleraa5c2342015-04-10 10:08:45 -04002652size_t Parcel::getBlobAshmemSize() const
2653{
Adrian Roos6bb31142015-10-22 16:46:12 -07002654 // This used to return the size of all blobs that were written to ashmem, now we're returning
2655 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002656 // TODO(b/202029388): Remove method once ABI can be changed.
2657 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002658}
2659
Adrian Rooscbf37262015-10-22 16:12:53 -07002660size_t Parcel::getOpenAshmemSize() const
2661{
Steven Morelandc673f1f2021-10-07 18:23:35 -07002662 size_t openAshmemSize = 0;
2663 for (size_t i = 0; i < mObjectsSize; i++) {
2664 const flat_binder_object* flat =
2665 reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2666
2667 // cookie is compared against zero for historical reasons
2668 // > obj.cookie = takeOwnership ? 1 : 0;
2669 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2670 int size = ashmem_get_size_region(flat->handle);
2671 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2672 ALOGE("Overflow when computing ashmem size.");
2673 return SIZE_MAX;
2674 }
2675 }
2676 }
2677 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002678}
2679
2680// --- Parcel::Blob ---
2681
2682Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002683 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002684}
2685
2686Parcel::Blob::~Blob() {
2687 release();
2688}
2689
2690void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002691 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002692 ::munmap(mData, mSize);
2693 }
2694 clear();
2695}
2696
Jeff Brown13b16042014-11-11 16:44:25 -08002697void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2698 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002699 mData = data;
2700 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002701 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002702}
2703
2704void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002705 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002706 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002707 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002708 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002709}
2710
Steven Moreland61ff8492019-09-26 16:05:45 -07002711} // namespace android