blob: 6e79fd1f2baa75025ed7d7a34d8c3b8cb06c6a32 [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 Moreland2034eff2021-10-13 11:24:35 -0700414 if (mSession != parcel->mSession) {
415 ALOGE("Cannot append Parcel from one context to another. They may be different formats, "
416 "and objects are specific to a context.");
Steven Moreland67753c32021-04-02 18:45:19 +0000417 return BAD_TYPE;
418 }
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420 status_t err;
Andreas Huber51faf462011-04-13 10:21:56 -0700421 const uint8_t *data = parcel->mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800422 const binder_size_t *objects = parcel->mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700423 size_t size = parcel->mObjectsSize;
424 int startPos = mDataPos;
425 int firstIndex = -1, lastIndex = -2;
426
427 if (len == 0) {
428 return NO_ERROR;
429 }
430
Nick Kralevichb6b14232015-04-02 09:36:02 -0700431 if (len > INT32_MAX) {
432 // don't accept size_t values which may have come from an
433 // inadvertent conversion from a negative int.
434 return BAD_VALUE;
435 }
436
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700437 // range checks against the source parcel size
438 if ((offset > parcel->mDataSize)
439 || (len > parcel->mDataSize)
440 || (offset + len > parcel->mDataSize)) {
441 return BAD_VALUE;
442 }
443
444 // Count objects in range
445 for (int i = 0; i < (int) size; i++) {
446 size_t off = objects[i];
Christopher Tate27182be2015-05-27 17:53:02 -0700447 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700448 if (firstIndex == -1) {
449 firstIndex = i;
450 }
451 lastIndex = i;
452 }
453 }
454 int numObjects = lastIndex - firstIndex + 1;
455
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -0700456 if ((mDataSize+len) > mDataCapacity) {
457 // grow data
458 err = growData(len);
459 if (err != NO_ERROR) {
460 return err;
461 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700462 }
463
464 // append data
465 memcpy(mData + mDataPos, data + offset, len);
466 mDataPos += len;
467 mDataSize += len;
468
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400469 err = NO_ERROR;
470
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700471 if (numObjects > 0) {
Martijn Coenen69390d42018-10-22 15:18:10 +0200472 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700473 // grow objects
474 if (mObjectsCapacity < mObjectsSize + numObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +0100475 if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow
476 if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow
Christopher Tateed7a50c2015-06-08 14:45:14 -0700477 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +0100478 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800479 binder_size_t *objects =
480 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -0700481 if (objects == (binder_size_t*)nullptr) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700482 return NO_MEMORY;
483 }
484 mObjects = objects;
485 mObjectsCapacity = newSize;
486 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700487
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700488 // append and acquire objects
489 int idx = mObjectsSize;
490 for (int i = firstIndex; i <= lastIndex; i++) {
491 size_t off = objects[i] - offset + startPos;
492 mObjects[idx++] = off;
493 mObjectsSize++;
494
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700495 flat_binder_object* flat
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700496 = reinterpret_cast<flat_binder_object*>(mData + off);
Steven Morelandc673f1f2021-10-07 18:23:35 -0700497 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700498
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -0700499 if (flat->hdr.type == BINDER_TYPE_FD) {
Dianne Hackborn8af0f822009-05-22 13:20:23 -0700500 // If this is a file descriptor, we need to dup it so the
501 // new Parcel now owns its own fd, and can declare that we
502 // officially know we have fds.
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800503 flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -0800504 flat->cookie = 1;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700505 mHasFds = mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400506 if (!mAllowFds) {
507 err = FDS_NOT_ALLOWED;
508 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700509 }
510 }
511 }
512
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400513 return err;
514}
515
Dianne Hackborn15feb9b2017-04-10 15:34:35 -0700516int Parcel::compareData(const Parcel& other) {
517 size_t size = dataSize();
518 if (size != other.dataSize()) {
519 return size < other.dataSize() ? -1 : 1;
520 }
521 return memcmp(data(), other.data(), size);
522}
523
Bernardo Rufino897b1652021-10-08 10:30:20 +0100524status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset,
525 size_t len, int* result) const {
526 if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) {
527 // Don't accept size_t values which may have come from an inadvertent conversion from a
528 // negative int.
529 return BAD_VALUE;
530 }
531 size_t thisLimit;
532 if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) {
533 return BAD_VALUE;
534 }
535 size_t otherLimit;
536 if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) {
537 return BAD_VALUE;
538 }
539 *result = memcmp(data() + thisOffset, other.data() + otherOffset, len);
540 return NO_ERROR;
541}
542
Jeff Brown13b16042014-11-11 16:44:25 -0800543bool Parcel::allowFds() const
544{
545 return mAllowFds;
546}
547
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700548bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400549{
550 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700551 if (!allowFds) {
552 mAllowFds = false;
553 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400554 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700555}
556
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700557void Parcel::restoreAllowFds(bool lastValue)
558{
559 mAllowFds = lastValue;
560}
561
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700562bool Parcel::hasFileDescriptors() const
563{
564 if (!mFdsKnown) {
565 scanForFds();
566 }
567 return mHasFds;
568}
569
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100570status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100571 if (len > INT32_MAX || offset > INT32_MAX) {
572 // Don't accept size_t values which may have come from an inadvertent conversion from a
573 // negative int.
574 return BAD_VALUE;
575 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100576 size_t limit;
577 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100578 return BAD_VALUE;
579 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100580 *result = false;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100581 for (size_t i = 0; i < mObjectsSize; i++) {
582 size_t pos = mObjects[i];
583 if (pos < offset) continue;
584 if (pos + sizeof(flat_binder_object) > offset + len) {
585 if (mObjectsSorted) break;
586 else continue;
587 }
588 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
589 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100590 *result = true;
591 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100592 }
593 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100594 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100595}
596
Steven Morelandf183fdd2020-10-27 00:12:12 +0000597void Parcel::markSensitive() const
598{
599 mDeallocZero = true;
600}
601
Steven Moreland5553ac42020-11-11 02:14:45 +0000602void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000603 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
604
Steven Moreland5553ac42020-11-11 02:14:45 +0000605 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700606 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000607 }
608}
609
Steven Morelandc9939062021-05-05 17:57:41 +0000610void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000611 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
612 "format must be set before data is written OR on IPC data");
613
Steven Morelandc9939062021-05-05 17:57:41 +0000614 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
615 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000616}
617
618bool Parcel::isForRpc() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000619 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000620}
621
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000622void Parcel::updateWorkSourceRequestHeaderPosition() const {
623 // Only update the request headers once. We only want to point
624 // to the first headers read/written.
625 if (!mRequestHeaderPresent) {
626 mWorkSourceRequestHeaderPosition = dataPosition();
627 mRequestHeaderPresent = true;
628 }
629}
630
Steven Morelandb6c7e222021-02-18 19:20:14 +0000631#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700632constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
633#else
634constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
635#endif
636
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700637// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700638status_t Parcel::writeInterfaceToken(const String16& interface)
639{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000640 return writeInterfaceToken(interface.string(), interface.size());
641}
642
643status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000644 if (CC_LIKELY(!isForRpc())) {
645 const IPCThreadState* threadState = IPCThreadState::self();
646 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
647 updateWorkSourceRequestHeaderPosition();
648 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
649 : IPCThreadState::kUnsetWorkSource);
650 writeInt32(kHeader);
651 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000652
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700653 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000654 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700655}
656
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000657bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
658{
659 if (!mRequestHeaderPresent) {
660 return false;
661 }
662
663 const size_t initialPosition = dataPosition();
664 setDataPosition(mWorkSourceRequestHeaderPosition);
665 status_t err = writeInt32(uid);
666 setDataPosition(initialPosition);
667 return err == NO_ERROR;
668}
669
Steven Morelandf1b1e492019-05-06 15:05:13 -0700670uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000671{
672 if (!mRequestHeaderPresent) {
673 return IPCThreadState::kUnsetWorkSource;
674 }
675
676 const size_t initialPosition = dataPosition();
677 setDataPosition(mWorkSourceRequestHeaderPosition);
678 uid_t uid = readInt32();
679 setDataPosition(initialPosition);
680 return uid;
681}
682
Mathias Agopian83c04462009-05-22 19:00:22 -0700683bool Parcel::checkInterface(IBinder* binder) const
684{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700685 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700686}
687
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700688bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700689 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700690{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700691 return enforceInterface(interface.string(), interface.size(), threadState);
692}
693
694bool Parcel::enforceInterface(const char16_t* interface,
695 size_t len,
696 IPCThreadState* threadState) const
697{
Steven Moreland3af936a2021-03-26 03:05:38 +0000698 if (CC_LIKELY(!isForRpc())) {
699 // StrictModePolicy.
700 int32_t strictPolicy = readInt32();
701 if (threadState == nullptr) {
702 threadState = IPCThreadState::self();
703 }
704 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
705 // For one-way calls, the callee is running entirely
706 // disconnected from the caller, so disable StrictMode entirely.
707 // Not only does disk/network usage not impact the caller, but
708 // there's no way to communicate back violations anyway.
709 threadState->setStrictModePolicy(0);
710 } else {
711 threadState->setStrictModePolicy(strictPolicy);
712 }
713 // WorkSource.
714 updateWorkSourceRequestHeaderPosition();
715 int32_t workSource = readInt32();
716 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
717 // vendor header
718 int32_t header = readInt32();
719 if (header != kHeader) {
720 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
721 header);
722 return false;
723 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700724 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000725
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100726 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700727 size_t parcel_interface_len;
728 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
729 if (len == parcel_interface_len &&
730 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700731 return true;
732 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700733 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700734 String8(interface, len).string(),
735 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700736 return false;
737 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700738}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700739
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700740size_t Parcel::objectsCount() const
741{
742 return mObjectsSize;
743}
744
745status_t Parcel::errorCheck() const
746{
747 return mError;
748}
749
750void Parcel::setError(status_t err)
751{
752 mError = err;
753}
754
755status_t Parcel::finishWrite(size_t len)
756{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700757 if (len > INT32_MAX) {
758 // don't accept size_t values which may have come from an
759 // inadvertent conversion from a negative int.
760 return BAD_VALUE;
761 }
762
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700763 //printf("Finish write of %d\n", len);
764 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700765 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700766 if (mDataPos > mDataSize) {
767 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700768 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700769 }
770 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
771 return NO_ERROR;
772}
773
774status_t Parcel::writeUnpadded(const void* data, size_t len)
775{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700776 if (len > INT32_MAX) {
777 // don't accept size_t values which may have come from an
778 // inadvertent conversion from a negative int.
779 return BAD_VALUE;
780 }
781
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700782 size_t end = mDataPos + len;
783 if (end < mDataPos) {
784 // integer overflow
785 return BAD_VALUE;
786 }
787
788 if (end <= mDataCapacity) {
789restart_write:
790 memcpy(mData+mDataPos, data, len);
791 return finishWrite(len);
792 }
793
794 status_t err = growData(len);
795 if (err == NO_ERROR) goto restart_write;
796 return err;
797}
798
799status_t Parcel::write(const void* data, size_t len)
800{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700801 if (len > INT32_MAX) {
802 // don't accept size_t values which may have come from an
803 // inadvertent conversion from a negative int.
804 return BAD_VALUE;
805 }
806
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700807 void* const d = writeInplace(len);
808 if (d) {
809 memcpy(d, data, len);
810 return NO_ERROR;
811 }
812 return mError;
813}
814
815void* Parcel::writeInplace(size_t len)
816{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700817 if (len > INT32_MAX) {
818 // don't accept size_t values which may have come from an
819 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700820 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700821 }
822
823 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700824
825 // sanity check for integer overflow
826 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700827 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700828 }
829
830 if ((mDataPos+padded) <= mDataCapacity) {
831restart_write:
832 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
833 uint8_t* const data = mData+mDataPos;
834
835 // Need to pad at end?
836 if (padded != len) {
837#if BYTE_ORDER == BIG_ENDIAN
838 static const uint32_t mask[4] = {
839 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
840 };
841#endif
842#if BYTE_ORDER == LITTLE_ENDIAN
843 static const uint32_t mask[4] = {
844 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
845 };
846#endif
847 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
848 // *reinterpret_cast<void**>(data+padded-4));
849 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
850 }
851
852 finishWrite(padded);
853 return data;
854 }
855
856 status_t err = growData(padded);
857 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700858 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700859}
860
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800861status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
862 const uint8_t* strData = (uint8_t*)str.data();
863 const size_t strLen= str.length();
864 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100865 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800866 return BAD_VALUE;
867 }
868
869 status_t err = writeInt32(utf16Len);
870 if (err) {
871 return err;
872 }
873
874 // Allocate enough bytes to hold our converted string and its terminating NULL.
875 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
876 if (!dst) {
877 return NO_MEMORY;
878 }
879
Sergio Girof4607432016-07-21 14:46:35 +0100880 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800881
882 return NO_ERROR;
883}
884
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900885
Andy Hung49198cf2020-11-18 11:02:39 -0800886status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
887status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800888
Andy Hung49198cf2020-11-18 11:02:39 -0800889status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
890status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700891
Andy Hung49198cf2020-11-18 11:02:39 -0800892status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
893status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
894status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
895status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
896status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
897status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
898status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
899status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
900status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
901status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
902status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
903status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
904status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
905status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
906status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
907status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
908status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
909status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
910status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
911status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
912status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
913status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
914status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
915status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
916status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
917status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
918status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700919
Andy Hung49198cf2020-11-18 11:02:39 -0800920status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800921status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800922 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900923status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800924 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800925status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800926 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900927status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800928 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
929status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800930
Andy Hung49198cf2020-11-18 11:02:39 -0800931status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
932status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
933status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
934
935status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
936status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
937status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
938
939status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
940
941status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
942status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
943
944status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
945status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
946
947status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
948status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
949status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
950status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
951status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
952status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
953status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
954status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
955status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
956status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
957status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
958status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
959status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
960status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
961status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
962status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
963status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
964status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
965status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
966status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
967status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
968status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
969status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
970status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
971status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
972status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
973status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
974
975status_t Parcel::readString16Vector(
976 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
977status_t Parcel::readString16Vector(
978 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
979status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
980status_t Parcel::readUtf8VectorFromUtf16Vector(
981 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
982status_t Parcel::readUtf8VectorFromUtf16Vector(
983 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
984status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
985
986status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
987status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
988status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
989
990status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
991status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
992status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
993
994status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800995
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700996status_t Parcel::writeInt32(int32_t val)
997{
Andreas Huber84a6d042009-08-17 13:33:27 -0700998 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700999}
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001000
1001status_t Parcel::writeUint32(uint32_t val)
1002{
1003 return writeAligned(val);
1004}
1005
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001006status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001007 if (len > INT32_MAX) {
1008 // don't accept size_t values which may have come from an
1009 // inadvertent conversion from a negative int.
1010 return BAD_VALUE;
1011 }
1012
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001013 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001014 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001015 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001016 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -07001017 if (ret == NO_ERROR) {
1018 ret = write(val, len * sizeof(*val));
1019 }
1020 return ret;
1021}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001022status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001023 if (len > INT32_MAX) {
1024 // don't accept size_t values which may have come from an
1025 // inadvertent conversion from a negative int.
1026 return BAD_VALUE;
1027 }
1028
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001029 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001030 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001031 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001032 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001033 if (ret == NO_ERROR) {
1034 ret = write(val, len * sizeof(*val));
1035 }
1036 return ret;
1037}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038
Casey Dahlind6848f52015-10-15 15:44:59 -07001039status_t Parcel::writeBool(bool val)
1040{
1041 return writeInt32(int32_t(val));
1042}
1043
1044status_t Parcel::writeChar(char16_t val)
1045{
1046 return writeInt32(int32_t(val));
1047}
1048
1049status_t Parcel::writeByte(int8_t val)
1050{
1051 return writeInt32(int32_t(val));
1052}
1053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001054status_t Parcel::writeInt64(int64_t val)
1055{
Andreas Huber84a6d042009-08-17 13:33:27 -07001056 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001057}
1058
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001059status_t Parcel::writeUint64(uint64_t val)
1060{
1061 return writeAligned(val);
1062}
1063
Serban Constantinescuf683e012013-11-05 16:53:55 +00001064status_t Parcel::writePointer(uintptr_t val)
1065{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001066 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001067}
1068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069status_t Parcel::writeFloat(float val)
1070{
Andreas Huber84a6d042009-08-17 13:33:27 -07001071 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001072}
1073
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001074#if defined(__mips__) && defined(__mips_hard_float)
1075
1076status_t Parcel::writeDouble(double val)
1077{
1078 union {
1079 double d;
1080 unsigned long long ll;
1081 } u;
1082 u.d = val;
1083 return writeAligned(u.ll);
1084}
1085
1086#else
1087
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088status_t Parcel::writeDouble(double val)
1089{
Andreas Huber84a6d042009-08-17 13:33:27 -07001090 return writeAligned(val);
1091}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001092
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001093#endif
1094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001095status_t Parcel::writeCString(const char* str)
1096{
1097 return write(str, strlen(str)+1);
1098}
1099
1100status_t Parcel::writeString8(const String8& str)
1101{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001102 return writeString8(str.string(), str.size());
1103}
1104
1105status_t Parcel::writeString8(const char* str, size_t len)
1106{
1107 if (str == nullptr) return writeInt32(-1);
1108
Jeff Sharkey18220902020-11-05 08:36:20 -07001109 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001110 status_t err = writeInt32(len);
1111 if (err == NO_ERROR) {
1112 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1113 if (data) {
1114 memcpy(data, str, len);
1115 *reinterpret_cast<char*>(data+len) = 0;
1116 return NO_ERROR;
1117 }
1118 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001119 }
1120 return err;
1121}
1122
1123status_t Parcel::writeString16(const String16& str)
1124{
1125 return writeString16(str.string(), str.size());
1126}
1127
1128status_t Parcel::writeString16(const char16_t* str, size_t len)
1129{
Yi Kong91635562018-06-07 14:38:36 -07001130 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001131
Jeff Sharkey18220902020-11-05 08:36:20 -07001132 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001133 status_t err = writeInt32(len);
1134 if (err == NO_ERROR) {
1135 len *= sizeof(char16_t);
1136 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1137 if (data) {
1138 memcpy(data, str, len);
1139 *reinterpret_cast<char16_t*>(data+len) = 0;
1140 return NO_ERROR;
1141 }
1142 err = mError;
1143 }
1144 return err;
1145}
1146
1147status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1148{
Steven Morelanda86a3562019-08-01 23:28:34 +00001149 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001150}
1151
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001152
Casey Dahlinb9872622015-11-25 15:09:45 -08001153status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1154 if (!parcelable) {
1155 return writeInt32(0);
1156 }
1157
1158 return writeParcelable(*parcelable);
1159}
1160
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001161status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001163 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001164 return BAD_TYPE;
1165
1166 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001167 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001168 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001170 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001171 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001173 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1174 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175
1176 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001177 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 return err;
1179 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001180 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001181 return err;
1182}
1183
Jeff Brown93ff1f92011-11-04 19:01:44 -07001184status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001185{
Steven Moreland5553ac42020-11-11 02:14:45 +00001186 if (isForRpc()) {
1187 ALOGE("Cannot write file descriptor to remote binder.");
1188 return BAD_TYPE;
1189 }
1190
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001191 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001192 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001193 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001194 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001195 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001196 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001197 return writeObject(obj, true);
1198}
1199
1200status_t Parcel::writeDupFileDescriptor(int fd)
1201{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001202 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001203 if (dupFd < 0) {
1204 return -errno;
1205 }
1206 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001207 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001208 close(dupFd);
1209 }
1210 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001211}
1212
Dianne Hackborn1941a402016-08-29 12:30:43 -07001213status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1214{
1215 writeInt32(0);
1216 return writeFileDescriptor(fd, takeOwnership);
1217}
1218
Ryo Hashimotobf551892018-05-31 16:58:35 +09001219status_t Parcel::writeDupParcelFileDescriptor(int fd)
1220{
1221 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1222 if (dupFd < 0) {
1223 return -errno;
1224 }
1225 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1226 if (err != OK) {
1227 close(dupFd);
1228 }
1229 return err;
1230}
1231
Christopher Wiley2cf19952016-04-11 11:09:37 -07001232status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001233 return writeDupFileDescriptor(fd.get());
1234}
1235
Jeff Brown13b16042014-11-11 16:44:25 -08001236status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001237{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001238 if (len > INT32_MAX) {
1239 // don't accept size_t values which may have come from an
1240 // inadvertent conversion from a negative int.
1241 return BAD_VALUE;
1242 }
1243
Jeff Brown13b16042014-11-11 16:44:25 -08001244 status_t status;
1245 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001246 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001247 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001248 if (status) return status;
1249
1250 void* ptr = writeInplace(len);
1251 if (!ptr) return NO_MEMORY;
1252
Jeff Brown13b16042014-11-11 16:44:25 -08001253 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001254 return NO_ERROR;
1255 }
1256
Steve Block6807e592011-10-20 11:56:00 +01001257 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001258 int fd = ashmem_create_region("Parcel Blob", len);
1259 if (fd < 0) return NO_MEMORY;
1260
1261 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1262 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001263 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001264 } else {
Yi Kong91635562018-06-07 14:38:36 -07001265 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001266 if (ptr == MAP_FAILED) {
1267 status = -errno;
1268 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001269 if (!mutableCopy) {
1270 result = ashmem_set_prot_region(fd, PROT_READ);
1271 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001272 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001273 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001274 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001275 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001276 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001277 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001278 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001279 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001280 return NO_ERROR;
1281 }
1282 }
1283 }
1284 }
1285 ::munmap(ptr, len);
1286 }
1287 ::close(fd);
1288 return status;
1289}
1290
Jeff Brown13b16042014-11-11 16:44:25 -08001291status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1292{
1293 // Must match up with what's done in writeBlob.
1294 if (!mAllowFds) return FDS_NOT_ALLOWED;
1295 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1296 if (status) return status;
1297 return writeDupFileDescriptor(fd);
1298}
1299
Mathias Agopiane1424282013-07-29 21:24:40 -07001300status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001301{
1302 status_t err;
1303
1304 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001305 const size_t len = val.getFlattenedSize();
1306 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001307
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001308 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001309 // don't accept size_t values which may have come from an
1310 // inadvertent conversion from a negative int.
1311 return BAD_VALUE;
1312 }
1313
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001314 err = this->writeInt32(len);
1315 if (err) return err;
1316
1317 err = this->writeInt32(fd_count);
1318 if (err) return err;
1319
1320 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001321 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001322 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001323 return BAD_VALUE;
1324
Yi Kong91635562018-06-07 14:38:36 -07001325 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001326 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001327 fds = new (std::nothrow) int[fd_count];
1328 if (fds == nullptr) {
1329 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1330 return BAD_VALUE;
1331 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001332 }
1333
1334 err = val.flatten(buf, len, fds, fd_count);
1335 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1336 err = this->writeDupFileDescriptor( fds[i] );
1337 }
1338
1339 if (fd_count) {
1340 delete [] fds;
1341 }
1342
1343 return err;
1344}
1345
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1347{
1348 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1349 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1350 if (enoughData && enoughObjects) {
1351restart_write:
1352 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001353
Christopher Tate98e67d32015-06-03 18:44:15 -07001354 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001355 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001356 if (!mAllowFds) {
1357 // fail before modifying our object index
1358 return FDS_NOT_ALLOWED;
1359 }
1360 mHasFds = mFdsKnown = true;
1361 }
1362
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001363 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001364 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 mObjects[mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001366 acquire_object(ProcessState::self(), val, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001367 mObjectsSize++;
1368 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001369
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001370 return finishWrite(sizeof(flat_binder_object));
1371 }
1372
1373 if (!enoughData) {
1374 const status_t err = growData(sizeof(val));
1375 if (err != NO_ERROR) return err;
1376 }
1377 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001378 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1379 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001380 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001381 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001382 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001383 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001384 mObjects = objects;
1385 mObjectsCapacity = newSize;
1386 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001387
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001388 goto restart_write;
1389}
1390
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001391status_t Parcel::writeNoException()
1392{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001393 binder::Status status;
1394 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001395}
1396
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001397status_t Parcel::validateReadData(size_t upperBound) const
1398{
1399 // Don't allow non-object reads on object data
1400 if (mObjectsSorted || mObjectsSize <= 1) {
1401data_sorted:
1402 // Expect to check only against the next object
1403 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1404 // For some reason the current read position is greater than the next object
1405 // hint. Iterate until we find the right object
1406 size_t nextObject = mNextObjectHint;
1407 do {
1408 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1409 // Requested info overlaps with an object
1410 ALOGE("Attempt to read from protected data in Parcel %p", this);
1411 return PERMISSION_DENIED;
1412 }
1413 nextObject++;
1414 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1415 mNextObjectHint = nextObject;
1416 }
1417 return NO_ERROR;
1418 }
1419 // Quickly determine if mObjects is sorted.
1420 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1421 binder_size_t* prevObj = currObj;
1422 while (currObj > mObjects) {
1423 prevObj--;
1424 if(*prevObj > *currObj) {
1425 goto data_unsorted;
1426 }
1427 currObj--;
1428 }
1429 mObjectsSorted = true;
1430 goto data_sorted;
1431
1432data_unsorted:
1433 // Insertion Sort mObjects
1434 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1435 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1436 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1437 binder_size_t temp = *iter0;
1438 binder_size_t* iter1 = iter0 - 1;
1439 while (iter1 >= mObjects && *iter1 > temp) {
1440 *(iter1 + 1) = *iter1;
1441 iter1--;
1442 }
1443 *(iter1 + 1) = temp;
1444 }
1445 mNextObjectHint = 0;
1446 mObjectsSorted = true;
1447 goto data_sorted;
1448}
1449
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450status_t Parcel::read(void* outData, size_t len) const
1451{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001452 if (len > INT32_MAX) {
1453 // don't accept size_t values which may have come from an
1454 // inadvertent conversion from a negative int.
1455 return BAD_VALUE;
1456 }
1457
1458 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1459 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001460 if (mObjectsSize > 0) {
1461 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001462 if(err != NO_ERROR) {
1463 // Still increment the data position by the expected length
1464 mDataPos += pad_size(len);
1465 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1466 return err;
1467 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001468 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001469 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001470 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001471 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001472 return NO_ERROR;
1473 }
1474 return NOT_ENOUGH_DATA;
1475}
1476
1477const void* Parcel::readInplace(size_t len) const
1478{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 if (len > INT32_MAX) {
1480 // don't accept size_t values which may have come from an
1481 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001482 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001483 }
1484
1485 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1486 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001487 if (mObjectsSize > 0) {
1488 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001489 if(err != NO_ERROR) {
1490 // Still increment the data position by the expected length
1491 mDataPos += pad_size(len);
1492 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001493 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001494 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001495 }
1496
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001497 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001498 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001499 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001500 return data;
1501 }
Yi Kong91635562018-06-07 14:38:36 -07001502 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001503}
1504
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001505status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1506 if (status_t status = readInt32(size); status != OK) return status;
1507 if (*size < 0) return OK; // may be null, client to handle
1508
1509 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1510
1511 // approximation, can't know max element size (e.g. if it makes heap
1512 // allocations)
1513 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1514 int32_t allocationSize;
1515 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1516
1517 // High limit of 1MB since something this big could never be returned. Could
1518 // probably scope this down, but might impact very specific usecases.
1519 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1520
1521 if (allocationSize >= kMaxAllocationSize) {
1522 return NO_MEMORY;
1523 }
1524
1525 return OK;
1526}
1527
Andreas Huber84a6d042009-08-17 13:33:27 -07001528template<class T>
1529status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001530 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001531
1532 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001533 if (mObjectsSize > 0) {
1534 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001535 if(err != NO_ERROR) {
1536 // Still increment the data position by the expected length
1537 mDataPos += sizeof(T);
1538 return err;
1539 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001540 }
1541
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001542 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001543 mDataPos += sizeof(T);
1544 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001545 return NO_ERROR;
1546 } else {
1547 return NOT_ENOUGH_DATA;
1548 }
1549}
1550
Andreas Huber84a6d042009-08-17 13:33:27 -07001551template<class T>
1552T Parcel::readAligned() const {
1553 T result;
1554 if (readAligned(&result) != NO_ERROR) {
1555 result = 0;
1556 }
1557
1558 return result;
1559}
1560
1561template<class T>
1562status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001563 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001564
1565 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1566restart_write:
1567 *reinterpret_cast<T*>(mData+mDataPos) = val;
1568 return finishWrite(sizeof(val));
1569 }
1570
1571 status_t err = growData(sizeof(val));
1572 if (err == NO_ERROR) goto restart_write;
1573 return err;
1574}
1575
1576status_t Parcel::readInt32(int32_t *pArg) const
1577{
1578 return readAligned(pArg);
1579}
1580
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001581int32_t Parcel::readInt32() const
1582{
Andreas Huber84a6d042009-08-17 13:33:27 -07001583 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001584}
1585
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001586status_t Parcel::readUint32(uint32_t *pArg) const
1587{
1588 return readAligned(pArg);
1589}
1590
1591uint32_t Parcel::readUint32() const
1592{
1593 return readAligned<uint32_t>();
1594}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001595
1596status_t Parcel::readInt64(int64_t *pArg) const
1597{
Andreas Huber84a6d042009-08-17 13:33:27 -07001598 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001599}
1600
1601
1602int64_t Parcel::readInt64() const
1603{
Andreas Huber84a6d042009-08-17 13:33:27 -07001604 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001605}
1606
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001607status_t Parcel::readUint64(uint64_t *pArg) const
1608{
1609 return readAligned(pArg);
1610}
1611
1612uint64_t Parcel::readUint64() const
1613{
1614 return readAligned<uint64_t>();
1615}
1616
Serban Constantinescuf683e012013-11-05 16:53:55 +00001617status_t Parcel::readPointer(uintptr_t *pArg) const
1618{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001619 status_t ret;
1620 binder_uintptr_t ptr;
1621 ret = readAligned(&ptr);
1622 if (!ret)
1623 *pArg = ptr;
1624 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001625}
1626
1627uintptr_t Parcel::readPointer() const
1628{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001629 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001630}
1631
1632
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001633status_t Parcel::readFloat(float *pArg) const
1634{
Andreas Huber84a6d042009-08-17 13:33:27 -07001635 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001636}
1637
1638
1639float Parcel::readFloat() const
1640{
Andreas Huber84a6d042009-08-17 13:33:27 -07001641 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001642}
1643
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001644#if defined(__mips__) && defined(__mips_hard_float)
1645
1646status_t Parcel::readDouble(double *pArg) const
1647{
1648 union {
1649 double d;
1650 unsigned long long ll;
1651 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001652 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001653 status_t status;
1654 status = readAligned(&u.ll);
1655 *pArg = u.d;
1656 return status;
1657}
1658
1659double Parcel::readDouble() const
1660{
1661 union {
1662 double d;
1663 unsigned long long ll;
1664 } u;
1665 u.ll = readAligned<unsigned long long>();
1666 return u.d;
1667}
1668
1669#else
1670
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001671status_t Parcel::readDouble(double *pArg) const
1672{
Andreas Huber84a6d042009-08-17 13:33:27 -07001673 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001674}
1675
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001676double Parcel::readDouble() const
1677{
Andreas Huber84a6d042009-08-17 13:33:27 -07001678 return readAligned<double>();
1679}
1680
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001681#endif
1682
Casey Dahlind6848f52015-10-15 15:44:59 -07001683status_t Parcel::readBool(bool *pArg) const
1684{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001685 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001686 status_t ret = readInt32(&tmp);
1687 *pArg = (tmp != 0);
1688 return ret;
1689}
1690
1691bool Parcel::readBool() const
1692{
1693 return readInt32() != 0;
1694}
1695
1696status_t Parcel::readChar(char16_t *pArg) const
1697{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001698 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001699 status_t ret = readInt32(&tmp);
1700 *pArg = char16_t(tmp);
1701 return ret;
1702}
1703
1704char16_t Parcel::readChar() const
1705{
1706 return char16_t(readInt32());
1707}
1708
1709status_t Parcel::readByte(int8_t *pArg) const
1710{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001711 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001712 status_t ret = readInt32(&tmp);
1713 *pArg = int8_t(tmp);
1714 return ret;
1715}
1716
1717int8_t Parcel::readByte() const
1718{
1719 return int8_t(readInt32());
1720}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001721
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001722status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1723 size_t utf16Size = 0;
1724 const char16_t* src = readString16Inplace(&utf16Size);
1725 if (!src) {
1726 return UNEXPECTED_NULL;
1727 }
1728
1729 // Save ourselves the trouble, we're done.
1730 if (utf16Size == 0u) {
1731 str->clear();
1732 return NO_ERROR;
1733 }
1734
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001735 // Allow for closing '\0'
1736 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1737 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001738 return BAD_VALUE;
1739 }
1740 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001741 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001742 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001743 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1744 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001745 return NO_ERROR;
1746}
1747
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001748const char* Parcel::readCString() const
1749{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001750 if (mDataPos < mDataSize) {
1751 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001752 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1753 // is the string's trailing NUL within the parcel's valid bounds?
1754 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1755 if (eos) {
1756 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001757 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001758 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001759 return str;
1760 }
1761 }
Yi Kong91635562018-06-07 14:38:36 -07001762 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001763}
1764
1765String8 Parcel::readString8() const
1766{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001767 size_t len;
1768 const char* str = readString8Inplace(&len);
1769 if (str) return String8(str, len);
1770 ALOGE("Reading a NULL string not supported here.");
1771 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001772}
1773
1774status_t Parcel::readString8(String8* pArg) const
1775{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001776 size_t len;
1777 const char* str = readString8Inplace(&len);
1778 if (str) {
1779 pArg->setTo(str, len);
1780 return 0;
1781 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001782 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001783 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001784 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001785}
1786
1787const char* Parcel::readString8Inplace(size_t* outLen) const
1788{
1789 int32_t size = readInt32();
1790 // watch for potential int overflow from size+1
1791 if (size >= 0 && size < INT32_MAX) {
1792 *outLen = size;
1793 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001794 if (str != nullptr) {
1795 if (str[size] == '\0') {
1796 return str;
1797 }
1798 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001799 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001800 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001801 *outLen = 0;
1802 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001803}
1804
1805String16 Parcel::readString16() const
1806{
1807 size_t len;
1808 const char16_t* str = readString16Inplace(&len);
1809 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001810 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001811 return String16();
1812}
1813
Casey Dahlinb9872622015-11-25 15:09:45 -08001814
Casey Dahlin451ff582015-10-19 18:12:18 -07001815status_t Parcel::readString16(String16* pArg) const
1816{
1817 size_t len;
1818 const char16_t* str = readString16Inplace(&len);
1819 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001820 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001821 return 0;
1822 } else {
1823 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001824 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001825 }
1826}
1827
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001828const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1829{
1830 int32_t size = readInt32();
1831 // watch for potential int overflow from size+1
1832 if (size >= 0 && size < INT32_MAX) {
1833 *outLen = size;
1834 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001835 if (str != nullptr) {
1836 if (str[size] == u'\0') {
1837 return str;
1838 }
1839 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001840 }
1841 }
1842 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001843 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001844}
1845
Casey Dahlinf0c13772015-10-27 18:33:56 -07001846status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1847{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001848 status_t status = readNullableStrongBinder(val);
1849 if (status == OK && !val->get()) {
1850 status = UNEXPECTED_NULL;
1851 }
1852 return status;
1853}
1854
1855status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1856{
Steven Morelanda86a3562019-08-01 23:28:34 +00001857 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001858}
1859
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001860sp<IBinder> Parcel::readStrongBinder() const
1861{
1862 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001863 // Note that a lot of code in Android reads binders by hand with this
1864 // method, and that code has historically been ok with getting nullptr
1865 // back (while ignoring error codes).
1866 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001867 return val;
1868}
1869
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001870int32_t Parcel::readExceptionCode() const
1871{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001872 binder::Status status;
1873 status.readFromParcel(*this);
1874 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001875}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001876
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001877native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001878{
1879 int numFds, numInts;
1880 status_t err;
1881 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001882 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001883 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001884 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001885
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001886 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001887 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001888 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001889 }
1890
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001891 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001892 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001893 if (h->data[i] < 0) {
1894 for (int j = 0; j < i; j++) {
1895 close(h->data[j]);
1896 }
1897 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001898 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001899 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001900 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001901 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001902 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001903 native_handle_close(h);
1904 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001905 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001906 }
1907 return h;
1908}
1909
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001910int Parcel::readFileDescriptor() const
1911{
1912 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001913
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001914 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001915 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001916 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001917
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001918 return BAD_TYPE;
1919}
1920
Dianne Hackborn1941a402016-08-29 12:30:43 -07001921int Parcel::readParcelFileDescriptor() const
1922{
1923 int32_t hasComm = readInt32();
1924 int fd = readFileDescriptor();
1925 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001926 // detach (owned by the binder driver)
1927 int comm = readFileDescriptor();
1928
1929 // warning: this must be kept in sync with:
1930 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1931 enum ParcelFileDescriptorStatus {
1932 DETACHED = 2,
1933 };
1934
1935#if BYTE_ORDER == BIG_ENDIAN
1936 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1937#endif
1938#if BYTE_ORDER == LITTLE_ENDIAN
1939 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1940#endif
1941
1942 ssize_t written = TEMP_FAILURE_RETRY(
1943 ::write(comm, &message, sizeof(message)));
1944
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001945 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001946 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1947 written, strerror(errno));
1948 return BAD_TYPE;
1949 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001950 }
1951 return fd;
1952}
1953
Christopher Wiley2cf19952016-04-11 11:09:37 -07001954status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001955{
1956 int got = readFileDescriptor();
1957
1958 if (got == BAD_TYPE) {
1959 return BAD_TYPE;
1960 }
1961
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001962 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001963
1964 if (val->get() < 0) {
1965 return BAD_VALUE;
1966 }
1967
1968 return OK;
1969}
1970
Ryo Hashimotobf551892018-05-31 16:58:35 +09001971status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1972{
1973 int got = readParcelFileDescriptor();
1974
1975 if (got == BAD_TYPE) {
1976 return BAD_TYPE;
1977 }
1978
1979 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1980
1981 if (val->get() < 0) {
1982 return BAD_VALUE;
1983 }
1984
1985 return OK;
1986}
Casey Dahlin06673e32015-11-23 13:24:23 -08001987
Jeff Brown5707dbf2011-09-23 21:17:56 -07001988status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1989{
Jeff Brown13b16042014-11-11 16:44:25 -08001990 int32_t blobType;
1991 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001992 if (status) return status;
1993
Jeff Brown13b16042014-11-11 16:44:25 -08001994 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001995 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001996 const void* ptr = readInplace(len);
1997 if (!ptr) return BAD_VALUE;
1998
Jeff Brown13b16042014-11-11 16:44:25 -08001999 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002000 return NO_ERROR;
2001 }
2002
Steve Block6807e592011-10-20 11:56:00 +01002003 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08002004 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002005 int fd = readFileDescriptor();
2006 if (fd == int(BAD_TYPE)) return BAD_VALUE;
2007
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002008 if (!ashmem_valid(fd)) {
2009 ALOGE("invalid fd");
2010 return BAD_VALUE;
2011 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002012 int size = ashmem_get_size_region(fd);
2013 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00002014 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07002015 return BAD_VALUE;
2016 }
Yi Kong91635562018-06-07 14:38:36 -07002017 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08002018 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002019 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002020
Jeff Brown13b16042014-11-11 16:44:25 -08002021 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002022 return NO_ERROR;
2023}
2024
Mathias Agopiane1424282013-07-29 21:24:40 -07002025status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002026{
2027 // size
2028 const size_t len = this->readInt32();
2029 const size_t fd_count = this->readInt32();
2030
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002031 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002032 // don't accept size_t values which may have come from an
2033 // inadvertent conversion from a negative int.
2034 return BAD_VALUE;
2035 }
2036
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002037 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002038 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002039 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002040 return BAD_VALUE;
2041
Yi Kong91635562018-06-07 14:38:36 -07002042 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002043 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002044 fds = new (std::nothrow) int[fd_count];
2045 if (fds == nullptr) {
2046 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2047 return BAD_VALUE;
2048 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002049 }
2050
2051 status_t err = NO_ERROR;
2052 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002053 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002054 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002055 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002056 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 -07002057 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2058 // Close all the file descriptors that were dup-ed.
2059 for (size_t j=0; j<i ;j++) {
2060 close(fds[j]);
2061 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002062 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002063 }
2064
2065 if (err == NO_ERROR) {
2066 err = val.unflatten(buf, len, fds, fd_count);
2067 }
2068
2069 if (fd_count) {
2070 delete [] fds;
2071 }
2072
2073 return err;
2074}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002075const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2076{
2077 const size_t DPOS = mDataPos;
2078 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2079 const flat_binder_object* obj
2080 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2081 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002082 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002083 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002084 // the object list, so we don't want to check for it when
2085 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002086 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002087 return obj;
2088 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002089
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002090 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002091 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 const size_t N = mObjectsSize;
2093 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002094
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002096 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002097 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002098
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002099 // Start at the current hint position, looking for an object at
2100 // the current data position.
2101 if (opos < N) {
2102 while (opos < (N-1) && OBJS[opos] < DPOS) {
2103 opos++;
2104 }
2105 } else {
2106 opos = N-1;
2107 }
2108 if (OBJS[opos] == DPOS) {
2109 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002110 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002111 this, DPOS, opos);
2112 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002113 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002114 return obj;
2115 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002116
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002117 // Look backwards for it...
2118 while (opos > 0 && OBJS[opos] > DPOS) {
2119 opos--;
2120 }
2121 if (OBJS[opos] == DPOS) {
2122 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002123 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 this, DPOS, opos);
2125 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002126 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002127 return obj;
2128 }
2129 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002130 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 -07002131 this, DPOS);
2132 }
Yi Kong91635562018-06-07 14:38:36 -07002133 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002134}
2135
2136void Parcel::closeFileDescriptors()
2137{
2138 size_t i = mObjectsSize;
2139 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002140 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 }
2142 while (i > 0) {
2143 i--;
2144 const flat_binder_object* flat
2145 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002146 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002147 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002148 close(flat->handle);
2149 }
2150 }
2151}
2152
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002153uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002154{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002155 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156}
2157
2158size_t Parcel::ipcDataSize() const
2159{
2160 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2161}
2162
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002163uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002164{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002165 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166}
2167
2168size_t Parcel::ipcObjectsCount() const
2169{
2170 return mObjectsSize;
2171}
2172
2173void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002174 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002175{
Steven Moreland438cce82021-04-02 18:04:08 +00002176 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2177 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2178
Steven Morelandceed9bb2020-12-17 01:01:06 +00002179 freeData();
2180
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002181 mData = const_cast<uint8_t*>(data);
2182 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002183 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002184 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002185 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002186
2187 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002188 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002189 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002190 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002191 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002192 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002193 mObjectsSize = 0;
2194 break;
2195 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002196 const flat_binder_object* flat
2197 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2198 uint32_t type = flat->hdr.type;
2199 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2200 type == BINDER_TYPE_FD)) {
2201 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2202 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2203 // recover gracefully by clearing out the objects, and releasing the objects we do
2204 // know about.
2205 android_errorWriteLog(0x534e4554, "135930648");
2206 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2207 __func__, type, (uint64_t)offset);
2208 releaseObjects();
2209 mObjectsSize = 0;
2210 break;
2211 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002212 minOffset = offset + sizeof(flat_binder_object);
2213 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214 scanForFds();
2215}
2216
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002217void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002218{
2219 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 if (errorCheck() != NO_ERROR) {
2222 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002223 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224 } else if (dataSize() > 0) {
2225 const uint8_t* DATA = data();
2226 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002227 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002228 const size_t N = objectsCount();
2229 for (size_t i=0; i<N; i++) {
2230 const flat_binder_object* flat
2231 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2232 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002233 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002234 << " = " << flat->binder;
2235 }
2236 } else {
2237 to << "NULL";
2238 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002239
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002240 to << ")";
2241}
2242
2243void Parcel::releaseObjects()
2244{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002245 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002246 if (i == 0) {
2247 return;
2248 }
2249 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002251 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 while (i > 0) {
2253 i--;
2254 const flat_binder_object* flat
2255 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002256 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002257 }
2258}
2259
2260void Parcel::acquireObjects()
2261{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002262 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002263 if (i == 0) {
2264 return;
2265 }
2266 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002268 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002269 while (i > 0) {
2270 i--;
2271 const flat_binder_object* flat
2272 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002273 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002274 }
2275}
2276
2277void Parcel::freeData()
2278{
2279 freeDataNoInit();
2280 initState();
2281}
2282
2283void Parcel::freeDataNoInit()
2284{
2285 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002286 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002287 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002288 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002289 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002290 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002291 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002292 if (mData) {
2293 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002294 gParcelGlobalAllocSize -= mDataCapacity;
2295 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002296 if (mDeallocZero) {
2297 zeroMemory(mData, mDataSize);
2298 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002299 free(mData);
2300 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002301 if (mObjects) free(mObjects);
2302 }
2303}
2304
2305status_t Parcel::growData(size_t len)
2306{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002307 if (len > INT32_MAX) {
2308 // don't accept size_t values which may have come from an
2309 // inadvertent conversion from a negative int.
2310 return BAD_VALUE;
2311 }
2312
Martijn Coenen93fe5182020-01-22 10:46:25 +01002313 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2314 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002315 size_t newSize = ((mDataSize+len)*3)/2;
2316 return (newSize <= mDataSize)
2317 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002318 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002319}
2320
Steven Morelandf183fdd2020-10-27 00:12:12 +00002321static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2322 if (!zero) {
2323 return (uint8_t*)realloc(data, newCapacity);
2324 }
2325 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2326 if (!newData) {
2327 return nullptr;
2328 }
2329
2330 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2331 zeroMemory(data, oldCapacity);
2332 free(data);
2333 return newData;
2334}
2335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336status_t Parcel::restartWrite(size_t desired)
2337{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002338 if (desired > INT32_MAX) {
2339 // don't accept size_t values which may have come from an
2340 // inadvertent conversion from a negative int.
2341 return BAD_VALUE;
2342 }
2343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002344 if (mOwner) {
2345 freeData();
2346 return continueWrite(desired);
2347 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002348
Steven Morelandf183fdd2020-10-27 00:12:12 +00002349 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350 if (!data && desired > mDataCapacity) {
2351 mError = NO_MEMORY;
2352 return NO_MEMORY;
2353 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002355 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002356
Devin Moore4a0a55e2020-06-04 13:23:10 -07002357 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002358 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002359 if (mDataCapacity > desired) {
2360 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2361 } else {
2362 gParcelGlobalAllocSize += (desired - mDataCapacity);
2363 }
2364
Colin Cross83ec65e2015-12-08 17:15:50 -08002365 if (!mData) {
2366 gParcelGlobalAllocCount++;
2367 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002368 mData = data;
2369 mDataCapacity = desired;
2370 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002371
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002372 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002373 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2374 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2375
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002376 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002377 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002378 mObjectsSize = mObjectsCapacity = 0;
2379 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002380 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002381 mHasFds = false;
2382 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002383 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002384
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002385 return NO_ERROR;
2386}
2387
2388status_t Parcel::continueWrite(size_t desired)
2389{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002390 if (desired > INT32_MAX) {
2391 // don't accept size_t values which may have come from an
2392 // inadvertent conversion from a negative int.
2393 return BAD_VALUE;
2394 }
2395
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002396 // If shrinking, first adjust for any objects that appear
2397 // after the new data size.
2398 size_t objectsSize = mObjectsSize;
2399 if (desired < mDataSize) {
2400 if (desired == 0) {
2401 objectsSize = 0;
2402 } else {
2403 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002404 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002405 break;
2406 objectsSize--;
2407 }
2408 }
2409 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002410
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002411 if (mOwner) {
2412 // If the size is going to zero, just release the owner's data.
2413 if (desired == 0) {
2414 freeData();
2415 return NO_ERROR;
2416 }
2417
2418 // If there is a different owner, we need to take
2419 // posession.
2420 uint8_t* data = (uint8_t*)malloc(desired);
2421 if (!data) {
2422 mError = NO_MEMORY;
2423 return NO_MEMORY;
2424 }
Yi Kong91635562018-06-07 14:38:36 -07002425 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002426
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002427 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002428 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002429 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002430 free(data);
2431
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002432 mError = NO_MEMORY;
2433 return NO_MEMORY;
2434 }
2435
2436 // Little hack to only acquire references on objects
2437 // we will be keeping.
2438 size_t oldObjectsSize = mObjectsSize;
2439 mObjectsSize = objectsSize;
2440 acquireObjects();
2441 mObjectsSize = oldObjectsSize;
2442 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002443
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002444 if (mData) {
2445 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2446 }
2447 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002448 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002449 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002450 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002451 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002452 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002453
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002454 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002455 gParcelGlobalAllocSize += desired;
2456 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002457
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002458 mData = data;
2459 mObjects = objects;
2460 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002461 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002462 mDataCapacity = desired;
2463 mObjectsSize = mObjectsCapacity = objectsSize;
2464 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002465 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002466
2467 } else if (mData) {
2468 if (objectsSize < mObjectsSize) {
2469 // Need to release refs on any objects we are dropping.
2470 const sp<ProcessState> proc(ProcessState::self());
2471 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2472 const flat_binder_object* flat
2473 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002474 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002475 // will need to rescan because we may have lopped off the only FDs
2476 mFdsKnown = false;
2477 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002478 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002479 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002480
2481 if (objectsSize == 0) {
2482 free(mObjects);
2483 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002484 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002485 } else {
2486 binder_size_t* objects =
2487 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2488 if (objects) {
2489 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002490 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002491 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002492 }
2493 mObjectsSize = objectsSize;
2494 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002495 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002496 }
2497
2498 // We own the data, so we can just do a realloc().
2499 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002500 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002502 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2503 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002504 gParcelGlobalAllocSize += desired;
2505 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002506 mData = data;
2507 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002508 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002509 mError = NO_MEMORY;
2510 return NO_MEMORY;
2511 }
2512 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002513 if (mDataSize > desired) {
2514 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002515 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002516 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002517 if (mDataPos > desired) {
2518 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002519 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002520 }
2521 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002523 } else {
2524 // This is the first data. Easy!
2525 uint8_t* data = (uint8_t*)malloc(desired);
2526 if (!data) {
2527 mError = NO_MEMORY;
2528 return NO_MEMORY;
2529 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002530
Yi Kong91635562018-06-07 14:38:36 -07002531 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002532 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002533 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002535
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002536 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002537 gParcelGlobalAllocSize += desired;
2538 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002539
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002540 mData = data;
2541 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002542 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2543 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002544 mDataCapacity = desired;
2545 }
2546
2547 return NO_ERROR;
2548}
2549
2550void Parcel::initState()
2551{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002552 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002553 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002554 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002555 mDataSize = 0;
2556 mDataCapacity = 0;
2557 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002558 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2559 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandc9939062021-05-05 17:57:41 +00002560 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002561 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002562 mObjectsSize = 0;
2563 mObjectsCapacity = 0;
2564 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002565 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566 mHasFds = false;
2567 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002568 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002569 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002570 mOwner = nullptr;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002571 mWorkSourceRequestHeaderPosition = 0;
2572 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002573
2574 // racing multiple init leads only to multiple identical write
2575 if (gMaxFds == 0) {
2576 struct rlimit result;
2577 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2578 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002579 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002580 } else {
2581 ALOGW("Unable to getrlimit: %s", strerror(errno));
2582 gMaxFds = 1024;
2583 }
2584 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002585}
2586
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002587void Parcel::scanForFds() const {
2588 status_t status = hasFileDescriptorsInRange(0, dataSize(), &mHasFds);
2589 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002590 mFdsKnown = true;
2591}
2592
Dan Sandleraa5c2342015-04-10 10:08:45 -04002593size_t Parcel::getBlobAshmemSize() const
2594{
Adrian Roos6bb31142015-10-22 16:46:12 -07002595 // This used to return the size of all blobs that were written to ashmem, now we're returning
2596 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002597 // TODO(b/202029388): Remove method once ABI can be changed.
2598 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002599}
2600
Adrian Rooscbf37262015-10-22 16:12:53 -07002601size_t Parcel::getOpenAshmemSize() const
2602{
Steven Morelandc673f1f2021-10-07 18:23:35 -07002603 size_t openAshmemSize = 0;
2604 for (size_t i = 0; i < mObjectsSize; i++) {
2605 const flat_binder_object* flat =
2606 reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2607
2608 // cookie is compared against zero for historical reasons
2609 // > obj.cookie = takeOwnership ? 1 : 0;
2610 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2611 int size = ashmem_get_size_region(flat->handle);
2612 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2613 ALOGE("Overflow when computing ashmem size.");
2614 return SIZE_MAX;
2615 }
2616 }
2617 }
2618 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002619}
2620
2621// --- Parcel::Blob ---
2622
2623Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002624 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002625}
2626
2627Parcel::Blob::~Blob() {
2628 release();
2629}
2630
2631void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002632 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002633 ::munmap(mData, mSize);
2634 }
2635 clear();
2636}
2637
Jeff Brown13b16042014-11-11 16:44:25 -08002638void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2639 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002640 mData = data;
2641 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002642 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002643}
2644
2645void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002646 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002647 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002648 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002649 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002650}
2651
Steven Moreland61ff8492019-09-26 16:05:45 -07002652} // namespace android