blob: 7bb4e3e37f2a9c969cee03d55c6886ce865a27bc [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()
66#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
67
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;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000203 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700204
Steven Morelandbf1915b2020-07-16 22:43:02 +0000205 int schedBits = 0;
206 if (!IPCThreadState::self()->backgroundSchedulingDisabled()) {
207 schedBits = schedPolicyMask(SCHED_NORMAL, 19);
Martijn Coenen2b631742017-05-05 11:16:59 -0700208 }
209
Yi Kong91635562018-06-07 14:38:36 -0700210 if (binder != nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700211 if (!local) {
212 BpBinder *proxy = binder->remoteBinder();
Yi Kong91635562018-06-07 14:38:36 -0700213 if (proxy == nullptr) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000214 ALOGE("null proxy");
Steven Moreland5553ac42020-11-11 02:14:45 +0000215 } else {
216 if (proxy->isRpcBinder()) {
Steven Morelanda9231112021-09-22 10:08:14 -0700217 ALOGE("Sending a socket binder over kernel binder is prohibited");
Steven Moreland5553ac42020-11-11 02:14:45 +0000218 return INVALID_OPERATION;
219 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700220 }
Steven Moreland99157622021-09-13 16:27:34 -0700221 const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700222 obj.hdr.type = BINDER_TYPE_HANDLE;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -0800223 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
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 Morelandf0212002018-12-26 13:59:23 -0800234 if (local->isRequestingSid()) {
235 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
236 }
Steven Morelandcf03cf12020-12-04 02:58:40 +0000237 if (local->isInheritRt()) {
238 obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT;
239 }
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700240 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800241 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
242 obj.cookie = reinterpret_cast<uintptr_t>(local);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700243 }
244 } else {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700245 obj.hdr.type = BINDER_TYPE_BINDER;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800246 obj.binder = 0;
247 obj.cookie = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700248 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700249
Steven Morelandbf1915b2020-07-16 22:43:02 +0000250 obj.flags |= schedBits;
251
Steven Moreland34b48cb2020-12-01 22:45:38 +0000252 status_t status = writeObject(obj, false);
253 if (status != OK) return status;
254
255 return finishFlattenBinder(binder);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700256}
257
Steven Morelanda86a3562019-08-01 23:28:34 +0000258status_t Parcel::unflattenBinder(sp<IBinder>* out) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700259{
Steven Moreland5553ac42020-11-11 02:14:45 +0000260 if (isForRpc()) {
Steven Morelandc9939062021-05-05 17:57:41 +0000261 LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel");
Steven Moreland5553ac42020-11-11 02:14:45 +0000262
Steven Moreland5623d1a2021-09-10 15:45:34 -0700263 int32_t isPresent;
264 status_t status = readInt32(&isPresent);
Steven Moreland5553ac42020-11-11 02:14:45 +0000265 if (status != OK) return status;
266
267 sp<IBinder> binder;
268
Steven Moreland5623d1a2021-09-10 15:45:34 -0700269 if (isPresent & 1) {
270 uint64_t addr;
271 if (status_t status = readUint64(&addr); status != OK) return status;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000272 if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder);
273 status != OK)
274 return status;
Steven Morelandd8083312021-09-22 13:37:10 -0700275 if (status_t status = mSession->state()->flushExcessBinderRefs(mSession, addr, binder);
276 status != OK)
277 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000278 }
279
280 return finishUnflattenBinder(binder, out);
281 }
282
Steven Morelanda86a3562019-08-01 23:28:34 +0000283 const flat_binder_object* flat = readObject(false);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700284
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700285 if (flat) {
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700286 switch (flat->hdr.type) {
Steven Morelanda86a3562019-08-01 23:28:34 +0000287 case BINDER_TYPE_BINDER: {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000288 sp<IBinder> binder =
289 sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie));
Steven Morelanda86a3562019-08-01 23:28:34 +0000290 return finishUnflattenBinder(binder, out);
291 }
292 case BINDER_TYPE_HANDLE: {
293 sp<IBinder> binder =
294 ProcessState::self()->getStrongProxyForHandle(flat->handle);
295 return finishUnflattenBinder(binder, out);
296 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700297 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700298 }
299 return BAD_TYPE;
300}
301
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700302// ---------------------------------------------------------------------------
303
304Parcel::Parcel()
305{
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800306 LOG_ALLOC("Parcel %p: constructing", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700307 initState();
308}
309
310Parcel::~Parcel()
311{
312 freeDataNoInit();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800313 LOG_ALLOC("Parcel %p: destroyed", this);
314}
315
316size_t Parcel::getGlobalAllocSize() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600317 return gParcelGlobalAllocSize.load();
Dianne Hackborn7e790af2014-11-11 12:22:53 -0800318}
319
320size_t Parcel::getGlobalAllocCount() {
Jeff Sharkey8994c182020-09-11 12:07:10 -0600321 return gParcelGlobalAllocCount.load();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322}
323
324const uint8_t* Parcel::data() const
325{
326 return mData;
327}
328
329size_t Parcel::dataSize() const
330{
331 return (mDataSize > mDataPos ? mDataSize : mDataPos);
332}
333
334size_t Parcel::dataAvail() const
335{
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700336 size_t result = dataSize() - dataPosition();
337 if (result > INT32_MAX) {
Steven Moreland6adf33c2019-09-25 13:18:09 -0700338 LOG_ALWAYS_FATAL("result too big: %zu", result);
Nick Kralevichcfe27de2015-09-16 09:49:15 -0700339 }
340 return result;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700341}
342
343size_t Parcel::dataPosition() const
344{
345 return mDataPos;
346}
347
348size_t Parcel::dataCapacity() const
349{
350 return mDataCapacity;
351}
352
353status_t Parcel::setDataSize(size_t size)
354{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700355 if (size > INT32_MAX) {
356 // don't accept size_t values which may have come from an
357 // inadvertent conversion from a negative int.
358 return BAD_VALUE;
359 }
360
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700361 status_t err;
362 err = continueWrite(size);
363 if (err == NO_ERROR) {
364 mDataSize = size;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700365 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700366 }
367 return err;
368}
369
370void Parcel::setDataPosition(size_t pos) const
371{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700372 if (pos > INT32_MAX) {
373 // don't accept size_t values which may have come from an
374 // inadvertent conversion from a negative int.
Steven Moreland6adf33c2019-09-25 13:18:09 -0700375 LOG_ALWAYS_FATAL("pos too big: %zu", pos);
Nick Kralevichb6b14232015-04-02 09:36:02 -0700376 }
377
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700378 mDataPos = pos;
379 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -0800380 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700381}
382
383status_t Parcel::setDataCapacity(size_t size)
384{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700385 if (size > INT32_MAX) {
386 // don't accept size_t values which may have come from an
387 // inadvertent conversion from a negative int.
388 return BAD_VALUE;
389 }
390
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700391 if (size > mDataCapacity) return continueWrite(size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392 return NO_ERROR;
393}
394
395status_t Parcel::setData(const uint8_t* buffer, size_t len)
396{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700397 if (len > INT32_MAX) {
398 // don't accept size_t values which may have come from an
399 // inadvertent conversion from a negative int.
400 return BAD_VALUE;
401 }
402
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700403 status_t err = restartWrite(len);
404 if (err == NO_ERROR) {
405 memcpy(const_cast<uint8_t*>(data()), buffer, len);
406 mDataSize = len;
407 mFdsKnown = false;
408 }
409 return err;
410}
411
Andreas Huber51faf462011-04-13 10:21:56 -0700412status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700413{
Steven Moreland67753c32021-04-02 18:45:19 +0000414 if (parcel->isForRpc() != isForRpc()) {
415 ALOGE("Cannot append Parcel of one format to another.");
416 return BAD_TYPE;
417 }
418
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700419 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700420 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800421 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700422 size_t size = parcel->mObjectsSize;
423 int startPos = mDataPos;
424 int firstIndex = -1, lastIndex = -2;
425
426 if (len == 0) {
427 return NO_ERROR;
428 }
429
Nick Kralevichb6b14232015-04-02 09:36:02 -0700430 if (len > INT32_MAX) {
431 // don't accept size_t values which may have come from an
432 // inadvertent conversion from a negative int.
433 return BAD_VALUE;
434 }
435
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700436 // range checks against the source parcel size
437 if ((offset > parcel->mDataSize)
438 || (len > parcel->mDataSize)
439 || (offset + len > parcel->mDataSize)) {
440 return BAD_VALUE;
441 }
442
443 // Count objects in range
444 for (int i = 0; i < (int) size; i++) {
445 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700446 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700447 if (firstIndex == -1) {
448 firstIndex = i;
449 }
450 lastIndex = i;
451 }
452 }
453 int numObjects = lastIndex - firstIndex + 1;
454
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700455 if ((mDataSize+len) > mDataCapacity) {
456 // grow data
457 err = growData(len);
458 if (err != NO_ERROR) {
459 return err;
460 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700461 }
462
463 // append data
464 memcpy(mData + mDataPos, data + offset, len);
465 mDataPos += len;
466 mDataSize += len;
467
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400468 err = NO_ERROR;
469
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700470 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200471 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700472 // grow objects
473 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100474 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
475 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700476 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100477 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800478 binder_size_t *objects =
479 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700480 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700481 return NO_MEMORY;
482 }
483 mObjects = objects;
484 mObjectsCapacity = newSize;
485 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 // append and acquire objects
488 int idx = mObjectsSize;
489 for (int i = firstIndex; i <= lastIndex; i++) {
490 size_t off = objects[i] - offset + startPos;
491 mObjects[idx++] = off;
492 mObjectsSize++;
493
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700494 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700495 = reinterpret_cast<flat_binder_object*>(mData + off);
Steven Morelandc673f1f2021-10-07 18:23:35 -0700496 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700497
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700498 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700499 // If this is a file descriptor, we need to dup it so the
500 // new Parcel now owns its own fd, and can declare that we
501 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800502 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800503 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400505 if (!mAllowFds) {
506 err = FDS_NOT_ALLOWED;
507 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700508 }
509 }
510 }
511
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400512 return err;
513}
514
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700515int Parcel::compareData(const Parcel& other) {
516 size_t size = dataSize();
517 if (size != other.dataSize()) {
518 return size < other.dataSize() ? -1 : 1;
519 }
520 return memcmp(data(), other.data(), size);
521}
522
Jeff Brown13b16042014-11-11 16:44:25 -0800523bool Parcel::allowFds() const
524{
525 return mAllowFds;
526}
527
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700528bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400529{
530 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700531 if (!allowFds) {
532 mAllowFds = false;
533 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400534 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700535}
536
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700537void Parcel::restoreAllowFds(bool lastValue)
538{
539 mAllowFds = lastValue;
540}
541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700542bool Parcel::hasFileDescriptors() const
543{
544 if (!mFdsKnown) {
545 scanForFds();
546 }
547 return mHasFds;
548}
549
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100550status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100551 if (len > INT32_MAX || offset > INT32_MAX) {
552 // Don't accept size_t values which may have come from an inadvertent conversion from a
553 // negative int.
554 return BAD_VALUE;
555 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100556 size_t limit;
557 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100558 return BAD_VALUE;
559 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100560 *result = false;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100561 for (size_t i = 0; i < mObjectsSize; i++) {
562 size_t pos = mObjects[i];
563 if (pos < offset) continue;
564 if (pos + sizeof(flat_binder_object) > offset + len) {
565 if (mObjectsSorted) break;
566 else continue;
567 }
568 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
569 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100570 *result = true;
571 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100572 }
573 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100574 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100575}
576
Steven Morelandf183fdd2020-10-27 00:12:12 +0000577void Parcel::markSensitive() const
578{
579 mDeallocZero = true;
580}
581
Steven Moreland5553ac42020-11-11 02:14:45 +0000582void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000583 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
584
Steven Moreland5553ac42020-11-11 02:14:45 +0000585 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700586 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000587 }
588}
589
Steven Morelandc9939062021-05-05 17:57:41 +0000590void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000591 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
592 "format must be set before data is written OR on IPC data");
593
Steven Morelandc9939062021-05-05 17:57:41 +0000594 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
595 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000596}
597
598bool Parcel::isForRpc() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000599 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000600}
601
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000602void Parcel::updateWorkSourceRequestHeaderPosition() const {
603 // Only update the request headers once. We only want to point
604 // to the first headers read/written.
605 if (!mRequestHeaderPresent) {
606 mWorkSourceRequestHeaderPosition = dataPosition();
607 mRequestHeaderPresent = true;
608 }
609}
610
Steven Morelandb6c7e222021-02-18 19:20:14 +0000611#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700612constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
613#else
614constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
615#endif
616
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700617// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700618status_t Parcel::writeInterfaceToken(const String16& interface)
619{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000620 return writeInterfaceToken(interface.string(), interface.size());
621}
622
623status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000624 if (CC_LIKELY(!isForRpc())) {
625 const IPCThreadState* threadState = IPCThreadState::self();
626 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
627 updateWorkSourceRequestHeaderPosition();
628 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
629 : IPCThreadState::kUnsetWorkSource);
630 writeInt32(kHeader);
631 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700633 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000634 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700635}
636
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000637bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
638{
639 if (!mRequestHeaderPresent) {
640 return false;
641 }
642
643 const size_t initialPosition = dataPosition();
644 setDataPosition(mWorkSourceRequestHeaderPosition);
645 status_t err = writeInt32(uid);
646 setDataPosition(initialPosition);
647 return err == NO_ERROR;
648}
649
Steven Morelandf1b1e492019-05-06 15:05:13 -0700650uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000651{
652 if (!mRequestHeaderPresent) {
653 return IPCThreadState::kUnsetWorkSource;
654 }
655
656 const size_t initialPosition = dataPosition();
657 setDataPosition(mWorkSourceRequestHeaderPosition);
658 uid_t uid = readInt32();
659 setDataPosition(initialPosition);
660 return uid;
661}
662
Mathias Agopian83c04462009-05-22 19:00:22 -0700663bool Parcel::checkInterface(IBinder* binder) const
664{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700665 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700666}
667
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700668bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700669 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700670{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700671 return enforceInterface(interface.string(), interface.size(), threadState);
672}
673
674bool Parcel::enforceInterface(const char16_t* interface,
675 size_t len,
676 IPCThreadState* threadState) const
677{
Steven Moreland3af936a2021-03-26 03:05:38 +0000678 if (CC_LIKELY(!isForRpc())) {
679 // StrictModePolicy.
680 int32_t strictPolicy = readInt32();
681 if (threadState == nullptr) {
682 threadState = IPCThreadState::self();
683 }
684 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
685 // For one-way calls, the callee is running entirely
686 // disconnected from the caller, so disable StrictMode entirely.
687 // Not only does disk/network usage not impact the caller, but
688 // there's no way to communicate back violations anyway.
689 threadState->setStrictModePolicy(0);
690 } else {
691 threadState->setStrictModePolicy(strictPolicy);
692 }
693 // WorkSource.
694 updateWorkSourceRequestHeaderPosition();
695 int32_t workSource = readInt32();
696 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
697 // vendor header
698 int32_t header = readInt32();
699 if (header != kHeader) {
700 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
701 header);
702 return false;
703 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700704 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000705
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100706 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700707 size_t parcel_interface_len;
708 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
709 if (len == parcel_interface_len &&
710 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700711 return true;
712 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700713 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700714 String8(interface, len).string(),
715 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700716 return false;
717 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700718}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700719
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700720size_t Parcel::objectsCount() const
721{
722 return mObjectsSize;
723}
724
725status_t Parcel::errorCheck() const
726{
727 return mError;
728}
729
730void Parcel::setError(status_t err)
731{
732 mError = err;
733}
734
735status_t Parcel::finishWrite(size_t len)
736{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700737 if (len > INT32_MAX) {
738 // don't accept size_t values which may have come from an
739 // inadvertent conversion from a negative int.
740 return BAD_VALUE;
741 }
742
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700743 //printf("Finish write of %d\n", len);
744 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700745 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700746 if (mDataPos > mDataSize) {
747 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700748 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700749 }
750 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
751 return NO_ERROR;
752}
753
754status_t Parcel::writeUnpadded(const void* data, size_t len)
755{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700756 if (len > INT32_MAX) {
757 // don't accept size_t values which may have come from an
758 // inadvertent conversion from a negative int.
759 return BAD_VALUE;
760 }
761
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700762 size_t end = mDataPos + len;
763 if (end < mDataPos) {
764 // integer overflow
765 return BAD_VALUE;
766 }
767
768 if (end <= mDataCapacity) {
769restart_write:
770 memcpy(mData+mDataPos, data, len);
771 return finishWrite(len);
772 }
773
774 status_t err = growData(len);
775 if (err == NO_ERROR) goto restart_write;
776 return err;
777}
778
779status_t Parcel::write(const void* data, size_t len)
780{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700781 if (len > INT32_MAX) {
782 // don't accept size_t values which may have come from an
783 // inadvertent conversion from a negative int.
784 return BAD_VALUE;
785 }
786
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700787 void* const d = writeInplace(len);
788 if (d) {
789 memcpy(d, data, len);
790 return NO_ERROR;
791 }
792 return mError;
793}
794
795void* Parcel::writeInplace(size_t len)
796{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700797 if (len > INT32_MAX) {
798 // don't accept size_t values which may have come from an
799 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700800 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700801 }
802
803 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700804
805 // sanity check for integer overflow
806 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700807 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700808 }
809
810 if ((mDataPos+padded) <= mDataCapacity) {
811restart_write:
812 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
813 uint8_t* const data = mData+mDataPos;
814
815 // Need to pad at end?
816 if (padded != len) {
817#if BYTE_ORDER == BIG_ENDIAN
818 static const uint32_t mask[4] = {
819 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
820 };
821#endif
822#if BYTE_ORDER == LITTLE_ENDIAN
823 static const uint32_t mask[4] = {
824 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
825 };
826#endif
827 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
828 // *reinterpret_cast<void**>(data+padded-4));
829 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
830 }
831
832 finishWrite(padded);
833 return data;
834 }
835
836 status_t err = growData(padded);
837 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700838 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700839}
840
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800841status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
842 const uint8_t* strData = (uint8_t*)str.data();
843 const size_t strLen= str.length();
844 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100845 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800846 return BAD_VALUE;
847 }
848
849 status_t err = writeInt32(utf16Len);
850 if (err) {
851 return err;
852 }
853
854 // Allocate enough bytes to hold our converted string and its terminating NULL.
855 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
856 if (!dst) {
857 return NO_MEMORY;
858 }
859
Sergio Girof4607432016-07-21 14:46:35 +0100860 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800861
862 return NO_ERROR;
863}
864
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900865
Andy Hung49198cf2020-11-18 11:02:39 -0800866status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
867status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800868
Andy Hung49198cf2020-11-18 11:02:39 -0800869status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
870status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700871
Andy Hung49198cf2020-11-18 11:02:39 -0800872status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
873status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
874status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
875status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
876status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
877status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
878status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
879status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
880status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
881status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
882status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
883status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
884status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
885status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
886status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
887status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
888status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
889status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
890status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
891status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
892status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
893status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
894status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
895status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
896status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
897status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
898status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700899
Andy Hung49198cf2020-11-18 11:02:39 -0800900status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800901status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800902 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900903status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800904 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800905status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800906 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900907status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800908 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
909status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800910
Andy Hung49198cf2020-11-18 11:02:39 -0800911status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
912status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
913status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
914
915status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
916status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
917status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
918
919status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
920
921status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
922status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
923
924status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
925status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
926
927status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
928status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
929status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
930status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
931status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
932status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
933status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
934status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
935status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
936status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
937status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
938status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
939status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
940status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
941status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
942status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
943status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
944status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
945status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
946status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
947status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
948status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
949status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
950status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
951status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
952status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
953status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
954
955status_t Parcel::readString16Vector(
956 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
957status_t Parcel::readString16Vector(
958 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
959status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
960status_t Parcel::readUtf8VectorFromUtf16Vector(
961 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
962status_t Parcel::readUtf8VectorFromUtf16Vector(
963 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
964status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
965
966status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
967status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
968status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
969
970status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
971status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
972status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
973
974status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800975
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700976status_t Parcel::writeInt32(int32_t val)
977{
Andreas Huber84a6d042009-08-17 13:33:27 -0700978 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700979}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800980
981status_t Parcel::writeUint32(uint32_t val)
982{
983 return writeAligned(val);
984}
985
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700986status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700987 if (len > INT32_MAX) {
988 // don't accept size_t values which may have come from an
989 // inadvertent conversion from a negative int.
990 return BAD_VALUE;
991 }
992
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700993 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700994 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700995 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700996 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700997 if (ret == NO_ERROR) {
998 ret = write(val, len * sizeof(*val));
999 }
1000 return ret;
1001}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001002status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001003 if (len > INT32_MAX) {
1004 // don't accept size_t values which may have come from an
1005 // inadvertent conversion from a negative int.
1006 return BAD_VALUE;
1007 }
1008
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001009 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001010 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001011 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001012 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001013 if (ret == NO_ERROR) {
1014 ret = write(val, len * sizeof(*val));
1015 }
1016 return ret;
1017}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001018
Casey Dahlind6848f52015-10-15 15:44:59 -07001019status_t Parcel::writeBool(bool val)
1020{
1021 return writeInt32(int32_t(val));
1022}
1023
1024status_t Parcel::writeChar(char16_t val)
1025{
1026 return writeInt32(int32_t(val));
1027}
1028
1029status_t Parcel::writeByte(int8_t val)
1030{
1031 return writeInt32(int32_t(val));
1032}
1033
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001034status_t Parcel::writeInt64(int64_t val)
1035{
Andreas Huber84a6d042009-08-17 13:33:27 -07001036 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001037}
1038
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001039status_t Parcel::writeUint64(uint64_t val)
1040{
1041 return writeAligned(val);
1042}
1043
Serban Constantinescuf683e012013-11-05 16:53:55 +00001044status_t Parcel::writePointer(uintptr_t val)
1045{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001046 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001047}
1048
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001049status_t Parcel::writeFloat(float val)
1050{
Andreas Huber84a6d042009-08-17 13:33:27 -07001051 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001052}
1053
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001054#if defined(__mips__) && defined(__mips_hard_float)
1055
1056status_t Parcel::writeDouble(double val)
1057{
1058 union {
1059 double d;
1060 unsigned long long ll;
1061 } u;
1062 u.d = val;
1063 return writeAligned(u.ll);
1064}
1065
1066#else
1067
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068status_t Parcel::writeDouble(double val)
1069{
Andreas Huber84a6d042009-08-17 13:33:27 -07001070 return writeAligned(val);
1071}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001072
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001073#endif
1074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001075status_t Parcel::writeCString(const char* str)
1076{
1077 return write(str, strlen(str)+1);
1078}
1079
1080status_t Parcel::writeString8(const String8& str)
1081{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001082 return writeString8(str.string(), str.size());
1083}
1084
1085status_t Parcel::writeString8(const char* str, size_t len)
1086{
1087 if (str == nullptr) return writeInt32(-1);
1088
Jeff Sharkey18220902020-11-05 08:36:20 -07001089 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001090 status_t err = writeInt32(len);
1091 if (err == NO_ERROR) {
1092 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1093 if (data) {
1094 memcpy(data, str, len);
1095 *reinterpret_cast<char*>(data+len) = 0;
1096 return NO_ERROR;
1097 }
1098 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 }
1100 return err;
1101}
1102
1103status_t Parcel::writeString16(const String16& str)
1104{
1105 return writeString16(str.string(), str.size());
1106}
1107
1108status_t Parcel::writeString16(const char16_t* str, size_t len)
1109{
Yi Kong91635562018-06-07 14:38:36 -07001110 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001111
Jeff Sharkey18220902020-11-05 08:36:20 -07001112 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001113 status_t err = writeInt32(len);
1114 if (err == NO_ERROR) {
1115 len *= sizeof(char16_t);
1116 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1117 if (data) {
1118 memcpy(data, str, len);
1119 *reinterpret_cast<char16_t*>(data+len) = 0;
1120 return NO_ERROR;
1121 }
1122 err = mError;
1123 }
1124 return err;
1125}
1126
1127status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1128{
Steven Morelanda86a3562019-08-01 23:28:34 +00001129 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001130}
1131
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001132
Casey Dahlinb9872622015-11-25 15:09:45 -08001133status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1134 if (!parcelable) {
1135 return writeInt32(0);
1136 }
1137
1138 return writeParcelable(*parcelable);
1139}
1140
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001141status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001142{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001143 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001144 return BAD_TYPE;
1145
1146 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001147 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001148 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001150 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001151 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001153 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1154 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155
1156 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001157 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 return err;
1159 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001160 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001161 return err;
1162}
1163
Jeff Brown93ff1f92011-11-04 19:01:44 -07001164status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001165{
Steven Moreland5553ac42020-11-11 02:14:45 +00001166 if (isForRpc()) {
1167 ALOGE("Cannot write file descriptor to remote binder.");
1168 return BAD_TYPE;
1169 }
1170
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001171 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001172 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001173 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001174 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001175 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001176 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001177 return writeObject(obj, true);
1178}
1179
1180status_t Parcel::writeDupFileDescriptor(int fd)
1181{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001182 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001183 if (dupFd < 0) {
1184 return -errno;
1185 }
1186 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001187 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001188 close(dupFd);
1189 }
1190 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191}
1192
Dianne Hackborn1941a402016-08-29 12:30:43 -07001193status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1194{
1195 writeInt32(0);
1196 return writeFileDescriptor(fd, takeOwnership);
1197}
1198
Ryo Hashimotobf551892018-05-31 16:58:35 +09001199status_t Parcel::writeDupParcelFileDescriptor(int fd)
1200{
1201 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1202 if (dupFd < 0) {
1203 return -errno;
1204 }
1205 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1206 if (err != OK) {
1207 close(dupFd);
1208 }
1209 return err;
1210}
1211
Christopher Wiley2cf19952016-04-11 11:09:37 -07001212status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001213 return writeDupFileDescriptor(fd.get());
1214}
1215
Jeff Brown13b16042014-11-11 16:44:25 -08001216status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001217{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001218 if (len > INT32_MAX) {
1219 // don't accept size_t values which may have come from an
1220 // inadvertent conversion from a negative int.
1221 return BAD_VALUE;
1222 }
1223
Jeff Brown13b16042014-11-11 16:44:25 -08001224 status_t status;
1225 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001226 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001227 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001228 if (status) return status;
1229
1230 void* ptr = writeInplace(len);
1231 if (!ptr) return NO_MEMORY;
1232
Jeff Brown13b16042014-11-11 16:44:25 -08001233 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001234 return NO_ERROR;
1235 }
1236
Steve Block6807e592011-10-20 11:56:00 +01001237 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001238 int fd = ashmem_create_region("Parcel Blob", len);
1239 if (fd < 0) return NO_MEMORY;
1240
1241 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1242 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001243 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001244 } else {
Yi Kong91635562018-06-07 14:38:36 -07001245 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001246 if (ptr == MAP_FAILED) {
1247 status = -errno;
1248 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001249 if (!mutableCopy) {
1250 result = ashmem_set_prot_region(fd, PROT_READ);
1251 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001252 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001253 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001254 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001255 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001256 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001257 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001259 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001260 return NO_ERROR;
1261 }
1262 }
1263 }
1264 }
1265 ::munmap(ptr, len);
1266 }
1267 ::close(fd);
1268 return status;
1269}
1270
Jeff Brown13b16042014-11-11 16:44:25 -08001271status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1272{
1273 // Must match up with what's done in writeBlob.
1274 if (!mAllowFds) return FDS_NOT_ALLOWED;
1275 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1276 if (status) return status;
1277 return writeDupFileDescriptor(fd);
1278}
1279
Mathias Agopiane1424282013-07-29 21:24:40 -07001280status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001281{
1282 status_t err;
1283
1284 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001285 const size_t len = val.getFlattenedSize();
1286 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001287
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001288 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001289 // don't accept size_t values which may have come from an
1290 // inadvertent conversion from a negative int.
1291 return BAD_VALUE;
1292 }
1293
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001294 err = this->writeInt32(len);
1295 if (err) return err;
1296
1297 err = this->writeInt32(fd_count);
1298 if (err) return err;
1299
1300 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001301 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001302 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001303 return BAD_VALUE;
1304
Yi Kong91635562018-06-07 14:38:36 -07001305 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001306 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001307 fds = new (std::nothrow) int[fd_count];
1308 if (fds == nullptr) {
1309 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1310 return BAD_VALUE;
1311 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001312 }
1313
1314 err = val.flatten(buf, len, fds, fd_count);
1315 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1316 err = this->writeDupFileDescriptor( fds[i] );
1317 }
1318
1319 if (fd_count) {
1320 delete [] fds;
1321 }
1322
1323 return err;
1324}
1325
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001326status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1327{
1328 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1329 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1330 if (enoughData && enoughObjects) {
1331restart_write:
1332 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001333
Christopher Tate98e67d32015-06-03 18:44:15 -07001334 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001335 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001336 if (!mAllowFds) {
1337 // fail before modifying our object index
1338 return FDS_NOT_ALLOWED;
1339 }
1340 mHasFds = mFdsKnown = true;
1341 }
1342
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001343 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001344 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001345 mObjects[mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001346 acquire_object(ProcessState::self(), val, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001347 mObjectsSize++;
1348 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001349
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001350 return finishWrite(sizeof(flat_binder_object));
1351 }
1352
1353 if (!enoughData) {
1354 const status_t err = growData(sizeof(val));
1355 if (err != NO_ERROR) return err;
1356 }
1357 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001358 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1359 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001360 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001361 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001362 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001363 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001364 mObjects = objects;
1365 mObjectsCapacity = newSize;
1366 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001367
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001368 goto restart_write;
1369}
1370
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001371status_t Parcel::writeNoException()
1372{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001373 binder::Status status;
1374 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001375}
1376
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001377status_t Parcel::validateReadData(size_t upperBound) const
1378{
1379 // Don't allow non-object reads on object data
1380 if (mObjectsSorted || mObjectsSize <= 1) {
1381data_sorted:
1382 // Expect to check only against the next object
1383 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1384 // For some reason the current read position is greater than the next object
1385 // hint. Iterate until we find the right object
1386 size_t nextObject = mNextObjectHint;
1387 do {
1388 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1389 // Requested info overlaps with an object
1390 ALOGE("Attempt to read from protected data in Parcel %p", this);
1391 return PERMISSION_DENIED;
1392 }
1393 nextObject++;
1394 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1395 mNextObjectHint = nextObject;
1396 }
1397 return NO_ERROR;
1398 }
1399 // Quickly determine if mObjects is sorted.
1400 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1401 binder_size_t* prevObj = currObj;
1402 while (currObj > mObjects) {
1403 prevObj--;
1404 if(*prevObj > *currObj) {
1405 goto data_unsorted;
1406 }
1407 currObj--;
1408 }
1409 mObjectsSorted = true;
1410 goto data_sorted;
1411
1412data_unsorted:
1413 // Insertion Sort mObjects
1414 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1415 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1416 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1417 binder_size_t temp = *iter0;
1418 binder_size_t* iter1 = iter0 - 1;
1419 while (iter1 >= mObjects && *iter1 > temp) {
1420 *(iter1 + 1) = *iter1;
1421 iter1--;
1422 }
1423 *(iter1 + 1) = temp;
1424 }
1425 mNextObjectHint = 0;
1426 mObjectsSorted = true;
1427 goto data_sorted;
1428}
1429
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001430status_t Parcel::read(void* outData, size_t len) const
1431{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001432 if (len > INT32_MAX) {
1433 // don't accept size_t values which may have come from an
1434 // inadvertent conversion from a negative int.
1435 return BAD_VALUE;
1436 }
1437
1438 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1439 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001440 if (mObjectsSize > 0) {
1441 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001442 if(err != NO_ERROR) {
1443 // Still increment the data position by the expected length
1444 mDataPos += pad_size(len);
1445 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1446 return err;
1447 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001448 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001449 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001450 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001451 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001452 return NO_ERROR;
1453 }
1454 return NOT_ENOUGH_DATA;
1455}
1456
1457const void* Parcel::readInplace(size_t len) const
1458{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001459 if (len > INT32_MAX) {
1460 // don't accept size_t values which may have come from an
1461 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001462 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001463 }
1464
1465 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1466 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001467 if (mObjectsSize > 0) {
1468 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001469 if(err != NO_ERROR) {
1470 // Still increment the data position by the expected length
1471 mDataPos += pad_size(len);
1472 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001473 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001474 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001475 }
1476
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001477 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001478 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001479 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001480 return data;
1481 }
Yi Kong91635562018-06-07 14:38:36 -07001482 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001483}
1484
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001485status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1486 if (status_t status = readInt32(size); status != OK) return status;
1487 if (*size < 0) return OK; // may be null, client to handle
1488
1489 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1490
1491 // approximation, can't know max element size (e.g. if it makes heap
1492 // allocations)
1493 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1494 int32_t allocationSize;
1495 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1496
1497 // High limit of 1MB since something this big could never be returned. Could
1498 // probably scope this down, but might impact very specific usecases.
1499 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1500
1501 if (allocationSize >= kMaxAllocationSize) {
1502 return NO_MEMORY;
1503 }
1504
1505 return OK;
1506}
1507
Andreas Huber84a6d042009-08-17 13:33:27 -07001508template<class T>
1509status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001510 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001511
1512 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001513 if (mObjectsSize > 0) {
1514 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001515 if(err != NO_ERROR) {
1516 // Still increment the data position by the expected length
1517 mDataPos += sizeof(T);
1518 return err;
1519 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001520 }
1521
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001522 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001523 mDataPos += sizeof(T);
1524 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001525 return NO_ERROR;
1526 } else {
1527 return NOT_ENOUGH_DATA;
1528 }
1529}
1530
Andreas Huber84a6d042009-08-17 13:33:27 -07001531template<class T>
1532T Parcel::readAligned() const {
1533 T result;
1534 if (readAligned(&result) != NO_ERROR) {
1535 result = 0;
1536 }
1537
1538 return result;
1539}
1540
1541template<class T>
1542status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001543 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001544
1545 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1546restart_write:
1547 *reinterpret_cast<T*>(mData+mDataPos) = val;
1548 return finishWrite(sizeof(val));
1549 }
1550
1551 status_t err = growData(sizeof(val));
1552 if (err == NO_ERROR) goto restart_write;
1553 return err;
1554}
1555
1556status_t Parcel::readInt32(int32_t *pArg) const
1557{
1558 return readAligned(pArg);
1559}
1560
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001561int32_t Parcel::readInt32() const
1562{
Andreas Huber84a6d042009-08-17 13:33:27 -07001563 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001564}
1565
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001566status_t Parcel::readUint32(uint32_t *pArg) const
1567{
1568 return readAligned(pArg);
1569}
1570
1571uint32_t Parcel::readUint32() const
1572{
1573 return readAligned<uint32_t>();
1574}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001575
1576status_t Parcel::readInt64(int64_t *pArg) const
1577{
Andreas Huber84a6d042009-08-17 13:33:27 -07001578 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001579}
1580
1581
1582int64_t Parcel::readInt64() const
1583{
Andreas Huber84a6d042009-08-17 13:33:27 -07001584 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001585}
1586
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001587status_t Parcel::readUint64(uint64_t *pArg) const
1588{
1589 return readAligned(pArg);
1590}
1591
1592uint64_t Parcel::readUint64() const
1593{
1594 return readAligned<uint64_t>();
1595}
1596
Serban Constantinescuf683e012013-11-05 16:53:55 +00001597status_t Parcel::readPointer(uintptr_t *pArg) const
1598{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001599 status_t ret;
1600 binder_uintptr_t ptr;
1601 ret = readAligned(&ptr);
1602 if (!ret)
1603 *pArg = ptr;
1604 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001605}
1606
1607uintptr_t Parcel::readPointer() const
1608{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001609 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001610}
1611
1612
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001613status_t Parcel::readFloat(float *pArg) const
1614{
Andreas Huber84a6d042009-08-17 13:33:27 -07001615 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001616}
1617
1618
1619float Parcel::readFloat() const
1620{
Andreas Huber84a6d042009-08-17 13:33:27 -07001621 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001622}
1623
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001624#if defined(__mips__) && defined(__mips_hard_float)
1625
1626status_t Parcel::readDouble(double *pArg) const
1627{
1628 union {
1629 double d;
1630 unsigned long long ll;
1631 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001632 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001633 status_t status;
1634 status = readAligned(&u.ll);
1635 *pArg = u.d;
1636 return status;
1637}
1638
1639double Parcel::readDouble() const
1640{
1641 union {
1642 double d;
1643 unsigned long long ll;
1644 } u;
1645 u.ll = readAligned<unsigned long long>();
1646 return u.d;
1647}
1648
1649#else
1650
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001651status_t Parcel::readDouble(double *pArg) const
1652{
Andreas Huber84a6d042009-08-17 13:33:27 -07001653 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001654}
1655
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001656double Parcel::readDouble() const
1657{
Andreas Huber84a6d042009-08-17 13:33:27 -07001658 return readAligned<double>();
1659}
1660
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001661#endif
1662
Casey Dahlind6848f52015-10-15 15:44:59 -07001663status_t Parcel::readBool(bool *pArg) const
1664{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001665 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001666 status_t ret = readInt32(&tmp);
1667 *pArg = (tmp != 0);
1668 return ret;
1669}
1670
1671bool Parcel::readBool() const
1672{
1673 return readInt32() != 0;
1674}
1675
1676status_t Parcel::readChar(char16_t *pArg) const
1677{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001678 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001679 status_t ret = readInt32(&tmp);
1680 *pArg = char16_t(tmp);
1681 return ret;
1682}
1683
1684char16_t Parcel::readChar() const
1685{
1686 return char16_t(readInt32());
1687}
1688
1689status_t Parcel::readByte(int8_t *pArg) const
1690{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001691 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001692 status_t ret = readInt32(&tmp);
1693 *pArg = int8_t(tmp);
1694 return ret;
1695}
1696
1697int8_t Parcel::readByte() const
1698{
1699 return int8_t(readInt32());
1700}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001701
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001702status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1703 size_t utf16Size = 0;
1704 const char16_t* src = readString16Inplace(&utf16Size);
1705 if (!src) {
1706 return UNEXPECTED_NULL;
1707 }
1708
1709 // Save ourselves the trouble, we're done.
1710 if (utf16Size == 0u) {
1711 str->clear();
1712 return NO_ERROR;
1713 }
1714
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001715 // Allow for closing '\0'
1716 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1717 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001718 return BAD_VALUE;
1719 }
1720 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001721 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001722 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001723 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1724 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001725 return NO_ERROR;
1726}
1727
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001728const char* Parcel::readCString() const
1729{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001730 if (mDataPos < mDataSize) {
1731 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001732 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1733 // is the string's trailing NUL within the parcel's valid bounds?
1734 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1735 if (eos) {
1736 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001737 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001738 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001739 return str;
1740 }
1741 }
Yi Kong91635562018-06-07 14:38:36 -07001742 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001743}
1744
1745String8 Parcel::readString8() const
1746{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001747 size_t len;
1748 const char* str = readString8Inplace(&len);
1749 if (str) return String8(str, len);
1750 ALOGE("Reading a NULL string not supported here.");
1751 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001752}
1753
1754status_t Parcel::readString8(String8* pArg) const
1755{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001756 size_t len;
1757 const char* str = readString8Inplace(&len);
1758 if (str) {
1759 pArg->setTo(str, len);
1760 return 0;
1761 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001762 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001763 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001764 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001765}
1766
1767const char* Parcel::readString8Inplace(size_t* outLen) const
1768{
1769 int32_t size = readInt32();
1770 // watch for potential int overflow from size+1
1771 if (size >= 0 && size < INT32_MAX) {
1772 *outLen = size;
1773 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001774 if (str != nullptr) {
1775 if (str[size] == '\0') {
1776 return str;
1777 }
1778 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001779 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001780 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001781 *outLen = 0;
1782 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001783}
1784
1785String16 Parcel::readString16() const
1786{
1787 size_t len;
1788 const char16_t* str = readString16Inplace(&len);
1789 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001790 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001791 return String16();
1792}
1793
Casey Dahlinb9872622015-11-25 15:09:45 -08001794
Casey Dahlin451ff582015-10-19 18:12:18 -07001795status_t Parcel::readString16(String16* pArg) const
1796{
1797 size_t len;
1798 const char16_t* str = readString16Inplace(&len);
1799 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001800 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001801 return 0;
1802 } else {
1803 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001804 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001805 }
1806}
1807
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001808const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1809{
1810 int32_t size = readInt32();
1811 // watch for potential int overflow from size+1
1812 if (size >= 0 && size < INT32_MAX) {
1813 *outLen = size;
1814 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001815 if (str != nullptr) {
1816 if (str[size] == u'\0') {
1817 return str;
1818 }
1819 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001820 }
1821 }
1822 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001823 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001824}
1825
Casey Dahlinf0c13772015-10-27 18:33:56 -07001826status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1827{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001828 status_t status = readNullableStrongBinder(val);
1829 if (status == OK && !val->get()) {
1830 status = UNEXPECTED_NULL;
1831 }
1832 return status;
1833}
1834
1835status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1836{
Steven Morelanda86a3562019-08-01 23:28:34 +00001837 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001838}
1839
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840sp<IBinder> Parcel::readStrongBinder() const
1841{
1842 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001843 // Note that a lot of code in Android reads binders by hand with this
1844 // method, and that code has historically been ok with getting nullptr
1845 // back (while ignoring error codes).
1846 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001847 return val;
1848}
1849
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001850int32_t Parcel::readExceptionCode() const
1851{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001852 binder::Status status;
1853 status.readFromParcel(*this);
1854 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001855}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001856
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001857native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001858{
1859 int numFds, numInts;
1860 status_t err;
1861 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001862 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001863 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001864 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001865
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001866 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001867 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001868 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001869 }
1870
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001871 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001872 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001873 if (h->data[i] < 0) {
1874 for (int j = 0; j < i; j++) {
1875 close(h->data[j]);
1876 }
1877 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001878 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001879 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001880 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001881 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001882 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001883 native_handle_close(h);
1884 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001885 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001886 }
1887 return h;
1888}
1889
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001890int Parcel::readFileDescriptor() const
1891{
1892 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001893
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001894 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001895 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001896 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001897
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001898 return BAD_TYPE;
1899}
1900
Dianne Hackborn1941a402016-08-29 12:30:43 -07001901int Parcel::readParcelFileDescriptor() const
1902{
1903 int32_t hasComm = readInt32();
1904 int fd = readFileDescriptor();
1905 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001906 // detach (owned by the binder driver)
1907 int comm = readFileDescriptor();
1908
1909 // warning: this must be kept in sync with:
1910 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1911 enum ParcelFileDescriptorStatus {
1912 DETACHED = 2,
1913 };
1914
1915#if BYTE_ORDER == BIG_ENDIAN
1916 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1917#endif
1918#if BYTE_ORDER == LITTLE_ENDIAN
1919 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1920#endif
1921
1922 ssize_t written = TEMP_FAILURE_RETRY(
1923 ::write(comm, &message, sizeof(message)));
1924
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001925 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001926 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1927 written, strerror(errno));
1928 return BAD_TYPE;
1929 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001930 }
1931 return fd;
1932}
1933
Christopher Wiley2cf19952016-04-11 11:09:37 -07001934status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001935{
1936 int got = readFileDescriptor();
1937
1938 if (got == BAD_TYPE) {
1939 return BAD_TYPE;
1940 }
1941
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001942 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001943
1944 if (val->get() < 0) {
1945 return BAD_VALUE;
1946 }
1947
1948 return OK;
1949}
1950
Ryo Hashimotobf551892018-05-31 16:58:35 +09001951status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1952{
1953 int got = readParcelFileDescriptor();
1954
1955 if (got == BAD_TYPE) {
1956 return BAD_TYPE;
1957 }
1958
1959 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1960
1961 if (val->get() < 0) {
1962 return BAD_VALUE;
1963 }
1964
1965 return OK;
1966}
Casey Dahlin06673e32015-11-23 13:24:23 -08001967
Jeff Brown5707dbf2011-09-23 21:17:56 -07001968status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1969{
Jeff Brown13b16042014-11-11 16:44:25 -08001970 int32_t blobType;
1971 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001972 if (status) return status;
1973
Jeff Brown13b16042014-11-11 16:44:25 -08001974 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001975 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001976 const void* ptr = readInplace(len);
1977 if (!ptr) return BAD_VALUE;
1978
Jeff Brown13b16042014-11-11 16:44:25 -08001979 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001980 return NO_ERROR;
1981 }
1982
Steve Block6807e592011-10-20 11:56:00 +01001983 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001984 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001985 int fd = readFileDescriptor();
1986 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1987
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001988 if (!ashmem_valid(fd)) {
1989 ALOGE("invalid fd");
1990 return BAD_VALUE;
1991 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001992 int size = ashmem_get_size_region(fd);
1993 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001994 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001995 return BAD_VALUE;
1996 }
Yi Kong91635562018-06-07 14:38:36 -07001997 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001998 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01001999 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002000
Jeff Brown13b16042014-11-11 16:44:25 -08002001 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002002 return NO_ERROR;
2003}
2004
Mathias Agopiane1424282013-07-29 21:24:40 -07002005status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002006{
2007 // size
2008 const size_t len = this->readInt32();
2009 const size_t fd_count = this->readInt32();
2010
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002011 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002012 // don't accept size_t values which may have come from an
2013 // inadvertent conversion from a negative int.
2014 return BAD_VALUE;
2015 }
2016
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002017 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002018 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002019 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002020 return BAD_VALUE;
2021
Yi Kong91635562018-06-07 14:38:36 -07002022 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002023 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002024 fds = new (std::nothrow) int[fd_count];
2025 if (fds == nullptr) {
2026 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2027 return BAD_VALUE;
2028 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002029 }
2030
2031 status_t err = NO_ERROR;
2032 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002033 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002034 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002035 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002036 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 -07002037 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2038 // Close all the file descriptors that were dup-ed.
2039 for (size_t j=0; j<i ;j++) {
2040 close(fds[j]);
2041 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002042 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002043 }
2044
2045 if (err == NO_ERROR) {
2046 err = val.unflatten(buf, len, fds, fd_count);
2047 }
2048
2049 if (fd_count) {
2050 delete [] fds;
2051 }
2052
2053 return err;
2054}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002055const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2056{
2057 const size_t DPOS = mDataPos;
2058 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2059 const flat_binder_object* obj
2060 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2061 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002062 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002063 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002064 // the object list, so we don't want to check for it when
2065 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002066 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002067 return obj;
2068 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002069
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002070 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002071 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002072 const size_t N = mObjectsSize;
2073 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002074
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002076 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002077 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002078
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002079 // Start at the current hint position, looking for an object at
2080 // the current data position.
2081 if (opos < N) {
2082 while (opos < (N-1) && OBJS[opos] < DPOS) {
2083 opos++;
2084 }
2085 } else {
2086 opos = N-1;
2087 }
2088 if (OBJS[opos] == DPOS) {
2089 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002090 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002091 this, DPOS, opos);
2092 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002093 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002094 return obj;
2095 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002096
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 // Look backwards for it...
2098 while (opos > 0 && OBJS[opos] > DPOS) {
2099 opos--;
2100 }
2101 if (OBJS[opos] == DPOS) {
2102 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002103 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002104 this, DPOS, opos);
2105 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002106 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002107 return obj;
2108 }
2109 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002110 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 -07002111 this, DPOS);
2112 }
Yi Kong91635562018-06-07 14:38:36 -07002113 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114}
2115
2116void Parcel::closeFileDescriptors()
2117{
2118 size_t i = mObjectsSize;
2119 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002120 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002121 }
2122 while (i > 0) {
2123 i--;
2124 const flat_binder_object* flat
2125 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002126 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002127 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002128 close(flat->handle);
2129 }
2130 }
2131}
2132
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002133uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002135 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002136}
2137
2138size_t Parcel::ipcDataSize() const
2139{
2140 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2141}
2142
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002143uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002144{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002145 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002146}
2147
2148size_t Parcel::ipcObjectsCount() const
2149{
2150 return mObjectsSize;
2151}
2152
2153void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002154 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002155{
Steven Moreland438cce82021-04-02 18:04:08 +00002156 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2157 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2158
Steven Morelandceed9bb2020-12-17 01:01:06 +00002159 freeData();
2160
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002161 mData = const_cast<uint8_t*>(data);
2162 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002163 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002166
2167 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002168 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002169 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002170 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002171 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002172 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002173 mObjectsSize = 0;
2174 break;
2175 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002176 const flat_binder_object* flat
2177 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2178 uint32_t type = flat->hdr.type;
2179 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2180 type == BINDER_TYPE_FD)) {
2181 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2182 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2183 // recover gracefully by clearing out the objects, and releasing the objects we do
2184 // know about.
2185 android_errorWriteLog(0x534e4554, "135930648");
2186 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2187 __func__, type, (uint64_t)offset);
2188 releaseObjects();
2189 mObjectsSize = 0;
2190 break;
2191 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002192 minOffset = offset + sizeof(flat_binder_object);
2193 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002194 scanForFds();
2195}
2196
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002197void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002198{
2199 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002200
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002201 if (errorCheck() != NO_ERROR) {
2202 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002203 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002204 } else if (dataSize() > 0) {
2205 const uint8_t* DATA = data();
2206 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002207 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002208 const size_t N = objectsCount();
2209 for (size_t i=0; i<N; i++) {
2210 const flat_binder_object* flat
2211 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2212 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002213 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214 << " = " << flat->binder;
2215 }
2216 } else {
2217 to << "NULL";
2218 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002219
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002220 to << ")";
2221}
2222
2223void Parcel::releaseObjects()
2224{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002225 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002226 if (i == 0) {
2227 return;
2228 }
2229 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002230 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002231 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002232 while (i > 0) {
2233 i--;
2234 const flat_binder_object* flat
2235 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002236 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002237 }
2238}
2239
2240void Parcel::acquireObjects()
2241{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002242 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002243 if (i == 0) {
2244 return;
2245 }
2246 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002247 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002248 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002249 while (i > 0) {
2250 i--;
2251 const flat_binder_object* flat
2252 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002253 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002254 }
2255}
2256
2257void Parcel::freeData()
2258{
2259 freeDataNoInit();
2260 initState();
2261}
2262
2263void Parcel::freeDataNoInit()
2264{
2265 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002266 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002267 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002268 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002270 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002271 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002272 if (mData) {
2273 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002274 gParcelGlobalAllocSize -= mDataCapacity;
2275 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002276 if (mDeallocZero) {
2277 zeroMemory(mData, mDataSize);
2278 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002279 free(mData);
2280 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002281 if (mObjects) free(mObjects);
2282 }
2283}
2284
2285status_t Parcel::growData(size_t len)
2286{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002287 if (len > INT32_MAX) {
2288 // don't accept size_t values which may have come from an
2289 // inadvertent conversion from a negative int.
2290 return BAD_VALUE;
2291 }
2292
Martijn Coenen93fe5182020-01-22 10:46:25 +01002293 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2294 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002295 size_t newSize = ((mDataSize+len)*3)/2;
2296 return (newSize <= mDataSize)
2297 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002298 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002299}
2300
Steven Morelandf183fdd2020-10-27 00:12:12 +00002301static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2302 if (!zero) {
2303 return (uint8_t*)realloc(data, newCapacity);
2304 }
2305 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2306 if (!newData) {
2307 return nullptr;
2308 }
2309
2310 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2311 zeroMemory(data, oldCapacity);
2312 free(data);
2313 return newData;
2314}
2315
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002316status_t Parcel::restartWrite(size_t desired)
2317{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002318 if (desired > INT32_MAX) {
2319 // don't accept size_t values which may have come from an
2320 // inadvertent conversion from a negative int.
2321 return BAD_VALUE;
2322 }
2323
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002324 if (mOwner) {
2325 freeData();
2326 return continueWrite(desired);
2327 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002328
Steven Morelandf183fdd2020-10-27 00:12:12 +00002329 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002330 if (!data && desired > mDataCapacity) {
2331 mError = NO_MEMORY;
2332 return NO_MEMORY;
2333 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002334
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002335 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002336
Devin Moore4a0a55e2020-06-04 13:23:10 -07002337 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002338 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002339 if (mDataCapacity > desired) {
2340 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2341 } else {
2342 gParcelGlobalAllocSize += (desired - mDataCapacity);
2343 }
2344
Colin Cross83ec65e2015-12-08 17:15:50 -08002345 if (!mData) {
2346 gParcelGlobalAllocCount++;
2347 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002348 mData = data;
2349 mDataCapacity = desired;
2350 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002351
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002352 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002353 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2354 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2355
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002356 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002357 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002358 mObjectsSize = mObjectsCapacity = 0;
2359 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002360 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002361 mHasFds = false;
2362 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002363 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002364
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002365 return NO_ERROR;
2366}
2367
2368status_t Parcel::continueWrite(size_t desired)
2369{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002370 if (desired > INT32_MAX) {
2371 // don't accept size_t values which may have come from an
2372 // inadvertent conversion from a negative int.
2373 return BAD_VALUE;
2374 }
2375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 // If shrinking, first adjust for any objects that appear
2377 // after the new data size.
2378 size_t objectsSize = mObjectsSize;
2379 if (desired < mDataSize) {
2380 if (desired == 0) {
2381 objectsSize = 0;
2382 } else {
2383 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002384 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 break;
2386 objectsSize--;
2387 }
2388 }
2389 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002391 if (mOwner) {
2392 // If the size is going to zero, just release the owner's data.
2393 if (desired == 0) {
2394 freeData();
2395 return NO_ERROR;
2396 }
2397
2398 // If there is a different owner, we need to take
2399 // posession.
2400 uint8_t* data = (uint8_t*)malloc(desired);
2401 if (!data) {
2402 mError = NO_MEMORY;
2403 return NO_MEMORY;
2404 }
Yi Kong91635562018-06-07 14:38:36 -07002405 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002406
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002408 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002409 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002410 free(data);
2411
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002412 mError = NO_MEMORY;
2413 return NO_MEMORY;
2414 }
2415
2416 // Little hack to only acquire references on objects
2417 // we will be keeping.
2418 size_t oldObjectsSize = mObjectsSize;
2419 mObjectsSize = objectsSize;
2420 acquireObjects();
2421 mObjectsSize = oldObjectsSize;
2422 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002423
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002424 if (mData) {
2425 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2426 }
2427 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002428 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002430 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002431 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002432 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002433
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002434 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002435 gParcelGlobalAllocSize += desired;
2436 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002437
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002438 mData = data;
2439 mObjects = objects;
2440 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002441 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002442 mDataCapacity = desired;
2443 mObjectsSize = mObjectsCapacity = objectsSize;
2444 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002445 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002446
2447 } else if (mData) {
2448 if (objectsSize < mObjectsSize) {
2449 // Need to release refs on any objects we are dropping.
2450 const sp<ProcessState> proc(ProcessState::self());
2451 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2452 const flat_binder_object* flat
2453 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002454 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002455 // will need to rescan because we may have lopped off the only FDs
2456 mFdsKnown = false;
2457 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002458 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002459 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002460
2461 if (objectsSize == 0) {
2462 free(mObjects);
2463 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002464 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002465 } else {
2466 binder_size_t* objects =
2467 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2468 if (objects) {
2469 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002470 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002471 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002472 }
2473 mObjectsSize = objectsSize;
2474 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002475 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002476 }
2477
2478 // We own the data, so we can just do a realloc().
2479 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002480 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002481 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002482 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2483 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002484 gParcelGlobalAllocSize += desired;
2485 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002486 mData = data;
2487 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002488 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002489 mError = NO_MEMORY;
2490 return NO_MEMORY;
2491 }
2492 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002493 if (mDataSize > desired) {
2494 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002495 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002496 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002497 if (mDataPos > desired) {
2498 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002499 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002500 }
2501 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002502
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002503 } else {
2504 // This is the first data. Easy!
2505 uint8_t* data = (uint8_t*)malloc(desired);
2506 if (!data) {
2507 mError = NO_MEMORY;
2508 return NO_MEMORY;
2509 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002510
Yi Kong91635562018-06-07 14:38:36 -07002511 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002512 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002513 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002514 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002516 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002517 gParcelGlobalAllocSize += desired;
2518 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002519
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 mData = data;
2521 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002522 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2523 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002524 mDataCapacity = desired;
2525 }
2526
2527 return NO_ERROR;
2528}
2529
2530void Parcel::initState()
2531{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002532 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002533 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002534 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002535 mDataSize = 0;
2536 mDataCapacity = 0;
2537 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002538 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2539 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandc9939062021-05-05 17:57:41 +00002540 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002541 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002542 mObjectsSize = 0;
2543 mObjectsCapacity = 0;
2544 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002545 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002546 mHasFds = false;
2547 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002548 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002549 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002550 mOwner = nullptr;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002551 mWorkSourceRequestHeaderPosition = 0;
2552 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002553
2554 // racing multiple init leads only to multiple identical write
2555 if (gMaxFds == 0) {
2556 struct rlimit result;
2557 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2558 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002559 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002560 } else {
2561 ALOGW("Unable to getrlimit: %s", strerror(errno));
2562 gMaxFds = 1024;
2563 }
2564 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002565}
2566
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002567void Parcel::scanForFds() const {
2568 status_t status = hasFileDescriptorsInRange(0, dataSize(), &mHasFds);
2569 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002570 mFdsKnown = true;
2571}
2572
Dan Sandleraa5c2342015-04-10 10:08:45 -04002573size_t Parcel::getBlobAshmemSize() const
2574{
Adrian Roos6bb31142015-10-22 16:46:12 -07002575 // This used to return the size of all blobs that were written to ashmem, now we're returning
2576 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002577 // TODO(b/202029388): Remove method once ABI can be changed.
2578 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002579}
2580
Adrian Rooscbf37262015-10-22 16:12:53 -07002581size_t Parcel::getOpenAshmemSize() const
2582{
Steven Morelandc673f1f2021-10-07 18:23:35 -07002583 size_t openAshmemSize = 0;
2584 for (size_t i = 0; i < mObjectsSize; i++) {
2585 const flat_binder_object* flat =
2586 reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2587
2588 // cookie is compared against zero for historical reasons
2589 // > obj.cookie = takeOwnership ? 1 : 0;
2590 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2591 int size = ashmem_get_size_region(flat->handle);
2592 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2593 ALOGE("Overflow when computing ashmem size.");
2594 return SIZE_MAX;
2595 }
2596 }
2597 }
2598 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002599}
2600
2601// --- Parcel::Blob ---
2602
2603Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002604 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002605}
2606
2607Parcel::Blob::~Blob() {
2608 release();
2609}
2610
2611void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002612 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002613 ::munmap(mData, mSize);
2614 }
2615 clear();
2616}
2617
Jeff Brown13b16042014-11-11 16:44:25 -08002618void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2619 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002620 mData = data;
2621 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002622 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002623}
2624
2625void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002626 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002627 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002628 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002629 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002630}
2631
Steven Moreland61ff8492019-09-26 16:05:45 -07002632} // namespace android