blob: 181f4051b78ddd7b8ceb820d37990db6238b0747 [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
Jeff Brown13b16042014-11-11 16:44:25 -0800524bool Parcel::allowFds() const
525{
526 return mAllowFds;
527}
528
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700529bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400530{
531 const bool origValue = mAllowFds;
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700532 if (!allowFds) {
533 mAllowFds = false;
534 }
Dianne Hackborn8938ed22011-09-28 23:19:47 -0400535 return origValue;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700536}
537
Dianne Hackborn7746cc32011-10-03 21:09:35 -0700538void Parcel::restoreAllowFds(bool lastValue)
539{
540 mAllowFds = lastValue;
541}
542
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543bool Parcel::hasFileDescriptors() const
544{
545 if (!mFdsKnown) {
546 scanForFds();
547 }
548 return mHasFds;
549}
550
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100551status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100552 if (len > INT32_MAX || offset > INT32_MAX) {
553 // Don't accept size_t values which may have come from an inadvertent conversion from a
554 // negative int.
555 return BAD_VALUE;
556 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100557 size_t limit;
558 if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) {
Bernardo Rufino22092af2021-10-07 14:09:24 +0100559 return BAD_VALUE;
560 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100561 *result = false;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100562 for (size_t i = 0; i < mObjectsSize; i++) {
563 size_t pos = mObjects[i];
564 if (pos < offset) continue;
565 if (pos + sizeof(flat_binder_object) > offset + len) {
566 if (mObjectsSorted) break;
567 else continue;
568 }
569 const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos);
570 if (flat->hdr.type == BINDER_TYPE_FD) {
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100571 *result = true;
572 break;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100573 }
574 }
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +0100575 return NO_ERROR;
Bernardo Rufino22092af2021-10-07 14:09:24 +0100576}
577
Steven Morelandf183fdd2020-10-27 00:12:12 +0000578void Parcel::markSensitive() const
579{
580 mDeallocZero = true;
581}
582
Steven Moreland5553ac42020-11-11 02:14:45 +0000583void Parcel::markForBinder(const sp<IBinder>& binder) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000584 LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written");
585
Steven Moreland5553ac42020-11-11 02:14:45 +0000586 if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) {
Steven Moreland99157622021-09-13 16:27:34 -0700587 markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession());
Steven Moreland5553ac42020-11-11 02:14:45 +0000588 }
589}
590
Steven Morelandc9939062021-05-05 17:57:41 +0000591void Parcel::markForRpc(const sp<RpcSession>& session) {
Steven Moreland1fda67b2021-04-02 18:35:50 +0000592 LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr,
593 "format must be set before data is written OR on IPC data");
594
Steven Morelandc9939062021-05-05 17:57:41 +0000595 LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session");
596 mSession = session;
Steven Moreland5553ac42020-11-11 02:14:45 +0000597}
598
599bool Parcel::isForRpc() const {
Steven Morelandc9939062021-05-05 17:57:41 +0000600 return mSession != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000601}
602
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000603void Parcel::updateWorkSourceRequestHeaderPosition() const {
604 // Only update the request headers once. We only want to point
605 // to the first headers read/written.
606 if (!mRequestHeaderPresent) {
607 mWorkSourceRequestHeaderPosition = dataPosition();
608 mRequestHeaderPresent = true;
609 }
610}
611
Steven Morelandb6c7e222021-02-18 19:20:14 +0000612#if defined(__ANDROID_VNDK__)
Steven Morelandd70160f2019-07-23 10:20:38 -0700613constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R');
614#else
615constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T');
616#endif
617
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700618// Write RPC headers. (previously just the interface token)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700619status_t Parcel::writeInterfaceToken(const String16& interface)
620{
Steven Morelanddbc76c72020-10-01 18:02:48 +0000621 return writeInterfaceToken(interface.string(), interface.size());
622}
623
624status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) {
Steven Moreland3af936a2021-03-26 03:05:38 +0000625 if (CC_LIKELY(!isForRpc())) {
626 const IPCThreadState* threadState = IPCThreadState::self();
627 writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER);
628 updateWorkSourceRequestHeaderPosition();
629 writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid()
630 : IPCThreadState::kUnsetWorkSource);
631 writeInt32(kHeader);
632 }
Steven Morelanddbc76c72020-10-01 18:02:48 +0000633
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700634 // currently the interface identification token is just its name as a string
Steven Morelanddbc76c72020-10-01 18:02:48 +0000635 return writeString16(str, len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700636}
637
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000638bool Parcel::replaceCallingWorkSourceUid(uid_t uid)
639{
640 if (!mRequestHeaderPresent) {
641 return false;
642 }
643
644 const size_t initialPosition = dataPosition();
645 setDataPosition(mWorkSourceRequestHeaderPosition);
646 status_t err = writeInt32(uid);
647 setDataPosition(initialPosition);
648 return err == NO_ERROR;
649}
650
Steven Morelandf1b1e492019-05-06 15:05:13 -0700651uid_t Parcel::readCallingWorkSourceUid() const
Olivier Gaillarddc848a02019-01-30 17:10:44 +0000652{
653 if (!mRequestHeaderPresent) {
654 return IPCThreadState::kUnsetWorkSource;
655 }
656
657 const size_t initialPosition = dataPosition();
658 setDataPosition(mWorkSourceRequestHeaderPosition);
659 uid_t uid = readInt32();
660 setDataPosition(initialPosition);
661 return uid;
662}
663
Mathias Agopian83c04462009-05-22 19:00:22 -0700664bool Parcel::checkInterface(IBinder* binder) const
665{
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700666 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopian83c04462009-05-22 19:00:22 -0700667}
668
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700669bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrick70081a12010-07-27 09:49:11 -0700670 IPCThreadState* threadState) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700671{
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700672 return enforceInterface(interface.string(), interface.size(), threadState);
673}
674
675bool Parcel::enforceInterface(const char16_t* interface,
676 size_t len,
677 IPCThreadState* threadState) const
678{
Steven Moreland3af936a2021-03-26 03:05:38 +0000679 if (CC_LIKELY(!isForRpc())) {
680 // StrictModePolicy.
681 int32_t strictPolicy = readInt32();
682 if (threadState == nullptr) {
683 threadState = IPCThreadState::self();
684 }
685 if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) {
686 // For one-way calls, the callee is running entirely
687 // disconnected from the caller, so disable StrictMode entirely.
688 // Not only does disk/network usage not impact the caller, but
689 // there's no way to communicate back violations anyway.
690 threadState->setStrictModePolicy(0);
691 } else {
692 threadState->setStrictModePolicy(strictPolicy);
693 }
694 // WorkSource.
695 updateWorkSourceRequestHeaderPosition();
696 int32_t workSource = readInt32();
697 threadState->setCallingWorkSourceUidWithoutPropagation(workSource);
698 // vendor header
699 int32_t header = readInt32();
700 if (header != kHeader) {
701 ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader,
702 header);
703 return false;
704 }
Brad Fitzpatricka877cd82010-07-07 16:06:39 -0700705 }
Steven Moreland3af936a2021-03-26 03:05:38 +0000706
Olivier Gaillard0e0f1de2018-08-16 14:04:09 +0100707 // Interface descriptor.
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700708 size_t parcel_interface_len;
709 const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len);
710 if (len == parcel_interface_len &&
711 (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700712 return true;
713 } else {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700714 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Daniel Colascione0bb330d2019-10-29 16:44:19 -0700715 String8(interface, len).string(),
716 String8(parcel_interface, parcel_interface_len).string());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700717 return false;
718 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700719}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700720
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721size_t Parcel::objectsCount() const
722{
723 return mObjectsSize;
724}
725
726status_t Parcel::errorCheck() const
727{
728 return mError;
729}
730
731void Parcel::setError(status_t err)
732{
733 mError = err;
734}
735
736status_t Parcel::finishWrite(size_t len)
737{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700738 if (len > INT32_MAX) {
739 // don't accept size_t values which may have come from an
740 // inadvertent conversion from a negative int.
741 return BAD_VALUE;
742 }
743
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700744 //printf("Finish write of %d\n", len);
745 mDataPos += len;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700746 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700747 if (mDataPos > mDataSize) {
748 mDataSize = mDataPos;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -0700749 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700750 }
751 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
752 return NO_ERROR;
753}
754
755status_t Parcel::writeUnpadded(const void* data, 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 size_t end = mDataPos + len;
764 if (end < mDataPos) {
765 // integer overflow
766 return BAD_VALUE;
767 }
768
769 if (end <= mDataCapacity) {
770restart_write:
771 memcpy(mData+mDataPos, data, len);
772 return finishWrite(len);
773 }
774
775 status_t err = growData(len);
776 if (err == NO_ERROR) goto restart_write;
777 return err;
778}
779
780status_t Parcel::write(const void* data, size_t len)
781{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700782 if (len > INT32_MAX) {
783 // don't accept size_t values which may have come from an
784 // inadvertent conversion from a negative int.
785 return BAD_VALUE;
786 }
787
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700788 void* const d = writeInplace(len);
789 if (d) {
790 memcpy(d, data, len);
791 return NO_ERROR;
792 }
793 return mError;
794}
795
796void* Parcel::writeInplace(size_t len)
797{
Nick Kralevichb6b14232015-04-02 09:36:02 -0700798 if (len > INT32_MAX) {
799 // don't accept size_t values which may have come from an
800 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -0700801 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -0700802 }
803
804 const size_t padded = pad_size(len);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700805
806 // sanity check for integer overflow
807 if (mDataPos+padded < mDataPos) {
Yi Kong91635562018-06-07 14:38:36 -0700808 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700809 }
810
811 if ((mDataPos+padded) <= mDataCapacity) {
812restart_write:
813 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
814 uint8_t* const data = mData+mDataPos;
815
816 // Need to pad at end?
817 if (padded != len) {
818#if BYTE_ORDER == BIG_ENDIAN
819 static const uint32_t mask[4] = {
820 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
821 };
822#endif
823#if BYTE_ORDER == LITTLE_ENDIAN
824 static const uint32_t mask[4] = {
825 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
826 };
827#endif
828 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
829 // *reinterpret_cast<void**>(data+padded-4));
830 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
831 }
832
833 finishWrite(padded);
834 return data;
835 }
836
837 status_t err = growData(padded);
838 if (err == NO_ERROR) goto restart_write;
Yi Kong91635562018-06-07 14:38:36 -0700839 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700840}
841
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800842status_t Parcel::writeUtf8AsUtf16(const std::string& str) {
843 const uint8_t* strData = (uint8_t*)str.data();
844 const size_t strLen= str.length();
845 const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen);
Sergio Girof4607432016-07-21 14:46:35 +0100846 if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800847 return BAD_VALUE;
848 }
849
850 status_t err = writeInt32(utf16Len);
851 if (err) {
852 return err;
853 }
854
855 // Allocate enough bytes to hold our converted string and its terminating NULL.
856 void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t));
857 if (!dst) {
858 return NO_MEMORY;
859 }
860
Sergio Girof4607432016-07-21 14:46:35 +0100861 utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800862
863 return NO_ERROR;
864}
865
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900866
Andy Hung49198cf2020-11-18 11:02:39 -0800867status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); }
868status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800869
Andy Hung49198cf2020-11-18 11:02:39 -0800870status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); }
871status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700872
Andy Hung49198cf2020-11-18 11:02:39 -0800873status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); }
874status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); }
875status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); }
876status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); }
877status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); }
878status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); }
879status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); }
880status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); }
881status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); }
882status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); }
883status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); }
884status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); }
885status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); }
886status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); }
887status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); }
888status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); }
889status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); }
890status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); }
891status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); }
892status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); }
893status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); }
894status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); }
895status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); }
896status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); }
897status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); }
898status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); }
899status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); }
Casey Dahlin451ff582015-10-19 18:12:18 -0700900
Andy Hung49198cf2020-11-18 11:02:39 -0800901status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); }
Casey Dahlinb9872622015-11-25 15:09:45 -0800902status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800903 const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900904status_t Parcel::writeString16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800905 const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800906status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800907 const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); }
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900908status_t Parcel::writeUtf8VectorAsUtf16Vector(
Andy Hung49198cf2020-11-18 11:02:39 -0800909 const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); }
910status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800911
Andy Hung49198cf2020-11-18 11:02:39 -0800912status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); }
913status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); }
914status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); }
915
916status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); }
917status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); }
918status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); }
919
920status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); }
921
922status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); }
923status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); }
924
925status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); }
926status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); }
927
928status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); }
929status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); }
930status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); }
931status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); }
932status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); }
933status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); }
934status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); }
935status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); }
936status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); }
937status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); }
938status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); }
939status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); }
940status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); }
941status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); }
942status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); }
943status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); }
944status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); }
945status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); }
946status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); }
947status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); }
948status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); }
949status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); }
950status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); }
951status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); }
952status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); }
953status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); }
954status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); }
955
956status_t Parcel::readString16Vector(
957 std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); }
958status_t Parcel::readString16Vector(
959 std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); }
960status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); }
961status_t Parcel::readUtf8VectorFromUtf16Vector(
962 std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); }
963status_t Parcel::readUtf8VectorFromUtf16Vector(
964 std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); }
965status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); }
966
967status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); }
968status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); }
969status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); }
970
971status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); }
972status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); }
973status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); }
974
975status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); }
Christopher Wiley9a5e32f2016-01-28 16:56:53 -0800976
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700977status_t Parcel::writeInt32(int32_t val)
978{
Andreas Huber84a6d042009-08-17 13:33:27 -0700979 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700980}
Dan Stoza41a0f2f2014-12-01 10:01:10 -0800981
982status_t Parcel::writeUint32(uint32_t val)
983{
984 return writeAligned(val);
985}
986
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700987status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -0700988 if (len > INT32_MAX) {
989 // don't accept size_t values which may have come from an
990 // inadvertent conversion from a negative int.
991 return BAD_VALUE;
992 }
993
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700994 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -0700995 return writeInt32(-1);
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700996 }
Chad Brubakere59cb432015-06-30 14:03:55 -0700997 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen5c0106e2013-10-16 10:57:51 -0700998 if (ret == NO_ERROR) {
999 ret = write(val, len * sizeof(*val));
1000 }
1001 return ret;
1002}
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001003status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001004 if (len > INT32_MAX) {
1005 // don't accept size_t values which may have come from an
1006 // inadvertent conversion from a negative int.
1007 return BAD_VALUE;
1008 }
1009
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001010 if (!val) {
Chad Brubakere59cb432015-06-30 14:03:55 -07001011 return writeInt32(-1);
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001012 }
Chad Brubakere59cb432015-06-30 14:03:55 -07001013 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissenf0190bf2014-03-13 14:17:40 -07001014 if (ret == NO_ERROR) {
1015 ret = write(val, len * sizeof(*val));
1016 }
1017 return ret;
1018}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001019
Casey Dahlind6848f52015-10-15 15:44:59 -07001020status_t Parcel::writeBool(bool val)
1021{
1022 return writeInt32(int32_t(val));
1023}
1024
1025status_t Parcel::writeChar(char16_t val)
1026{
1027 return writeInt32(int32_t(val));
1028}
1029
1030status_t Parcel::writeByte(int8_t val)
1031{
1032 return writeInt32(int32_t(val));
1033}
1034
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001035status_t Parcel::writeInt64(int64_t val)
1036{
Andreas Huber84a6d042009-08-17 13:33:27 -07001037 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001038}
1039
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001040status_t Parcel::writeUint64(uint64_t val)
1041{
1042 return writeAligned(val);
1043}
1044
Serban Constantinescuf683e012013-11-05 16:53:55 +00001045status_t Parcel::writePointer(uintptr_t val)
1046{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001047 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescuf683e012013-11-05 16:53:55 +00001048}
1049
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001050status_t Parcel::writeFloat(float val)
1051{
Andreas Huber84a6d042009-08-17 13:33:27 -07001052 return writeAligned(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001053}
1054
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001055#if defined(__mips__) && defined(__mips_hard_float)
1056
1057status_t Parcel::writeDouble(double val)
1058{
1059 union {
1060 double d;
1061 unsigned long long ll;
1062 } u;
1063 u.d = val;
1064 return writeAligned(u.ll);
1065}
1066
1067#else
1068
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001069status_t Parcel::writeDouble(double val)
1070{
Andreas Huber84a6d042009-08-17 13:33:27 -07001071 return writeAligned(val);
1072}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001073
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001074#endif
1075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001076status_t Parcel::writeCString(const char* str)
1077{
1078 return write(str, strlen(str)+1);
1079}
1080
1081status_t Parcel::writeString8(const String8& str)
1082{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001083 return writeString8(str.string(), str.size());
1084}
1085
1086status_t Parcel::writeString8(const char* str, size_t len)
1087{
1088 if (str == nullptr) return writeInt32(-1);
1089
Jeff Sharkey18220902020-11-05 08:36:20 -07001090 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001091 status_t err = writeInt32(len);
1092 if (err == NO_ERROR) {
1093 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char));
1094 if (data) {
1095 memcpy(data, str, len);
1096 *reinterpret_cast<char*>(data+len) = 0;
1097 return NO_ERROR;
1098 }
1099 err = mError;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001100 }
1101 return err;
1102}
1103
1104status_t Parcel::writeString16(const String16& str)
1105{
1106 return writeString16(str.string(), str.size());
1107}
1108
1109status_t Parcel::writeString16(const char16_t* str, size_t len)
1110{
Yi Kong91635562018-06-07 14:38:36 -07001111 if (str == nullptr) return writeInt32(-1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001112
Jeff Sharkey18220902020-11-05 08:36:20 -07001113 // NOTE: Keep this logic in sync with android_os_Parcel.cpp
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001114 status_t err = writeInt32(len);
1115 if (err == NO_ERROR) {
1116 len *= sizeof(char16_t);
1117 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1118 if (data) {
1119 memcpy(data, str, len);
1120 *reinterpret_cast<char16_t*>(data+len) = 0;
1121 return NO_ERROR;
1122 }
1123 err = mError;
1124 }
1125 return err;
1126}
1127
1128status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1129{
Steven Morelanda86a3562019-08-01 23:28:34 +00001130 return flattenBinder(val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001131}
1132
Casey Dahlineb8e15f2015-11-03 13:50:37 -08001133
Casey Dahlinb9872622015-11-25 15:09:45 -08001134status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) {
1135 if (!parcelable) {
1136 return writeInt32(0);
1137 }
1138
1139 return writeParcelable(*parcelable);
1140}
1141
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001142status_t Parcel::writeNativeHandle(const native_handle* handle)
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001143{
Mathias Agopian1d0a95b2009-07-31 16:12:13 -07001144 if (!handle || handle->version != sizeof(native_handle))
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001145 return BAD_TYPE;
1146
1147 status_t err;
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001148 err = writeInt32(handle->numFds);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001149 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001151 err = writeInt32(handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001152 if (err != NO_ERROR) return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001154 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1155 err = writeDupFileDescriptor(handle->data[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156
1157 if (err != NO_ERROR) {
Steve Block9d453682011-12-20 16:23:08 +00001158 ALOGD("write native handle, write dup fd failed");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 return err;
1160 }
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001161 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001162 return err;
1163}
1164
Jeff Brown93ff1f92011-11-04 19:01:44 -07001165status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001166{
Steven Moreland5553ac42020-11-11 02:14:45 +00001167 if (isForRpc()) {
1168 ALOGE("Cannot write file descriptor to remote binder.");
1169 return BAD_TYPE;
1170 }
1171
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001172 flat_binder_object obj;
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001173 obj.hdr.type = BINDER_TYPE_FD;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001174 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevåg07fd0f12014-02-18 21:10:29 -08001175 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001176 obj.handle = fd;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001177 obj.cookie = takeOwnership ? 1 : 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001178 return writeObject(obj, true);
1179}
1180
1181status_t Parcel::writeDupFileDescriptor(int fd)
1182{
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001183 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Jeff Brownd341c712011-11-04 20:19:33 -07001184 if (dupFd < 0) {
1185 return -errno;
1186 }
1187 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
Casey Dahlin06673e32015-11-23 13:24:23 -08001188 if (err != OK) {
Jeff Brownd341c712011-11-04 20:19:33 -07001189 close(dupFd);
1190 }
1191 return err;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001192}
1193
Dianne Hackborn1941a402016-08-29 12:30:43 -07001194status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership)
1195{
1196 writeInt32(0);
1197 return writeFileDescriptor(fd, takeOwnership);
1198}
1199
Ryo Hashimotobf551892018-05-31 16:58:35 +09001200status_t Parcel::writeDupParcelFileDescriptor(int fd)
1201{
1202 int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1203 if (dupFd < 0) {
1204 return -errno;
1205 }
1206 status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/);
1207 if (err != OK) {
1208 close(dupFd);
1209 }
1210 return err;
1211}
1212
Christopher Wiley2cf19952016-04-11 11:09:37 -07001213status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001214 return writeDupFileDescriptor(fd.get());
1215}
1216
Jeff Brown13b16042014-11-11 16:44:25 -08001217status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown5707dbf2011-09-23 21:17:56 -07001218{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001219 if (len > INT32_MAX) {
1220 // don't accept size_t values which may have come from an
1221 // inadvertent conversion from a negative int.
1222 return BAD_VALUE;
1223 }
1224
Jeff Brown13b16042014-11-11 16:44:25 -08001225 status_t status;
1226 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block6807e592011-10-20 11:56:00 +01001227 ALOGV("writeBlob: write in place");
Jeff Brown13b16042014-11-11 16:44:25 -08001228 status = writeInt32(BLOB_INPLACE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001229 if (status) return status;
1230
1231 void* ptr = writeInplace(len);
1232 if (!ptr) return NO_MEMORY;
1233
Jeff Brown13b16042014-11-11 16:44:25 -08001234 outBlob->init(-1, ptr, len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001235 return NO_ERROR;
1236 }
1237
Steve Block6807e592011-10-20 11:56:00 +01001238 ALOGV("writeBlob: write to ashmem");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001239 int fd = ashmem_create_region("Parcel Blob", len);
1240 if (fd < 0) return NO_MEMORY;
1241
1242 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1243 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001244 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001245 } else {
Yi Kong91635562018-06-07 14:38:36 -07001246 void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001247 if (ptr == MAP_FAILED) {
1248 status = -errno;
1249 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001250 if (!mutableCopy) {
1251 result = ashmem_set_prot_region(fd, PROT_READ);
1252 }
Jeff Brown5707dbf2011-09-23 21:17:56 -07001253 if (result < 0) {
Jeff Brownec4e0062011-10-10 14:50:10 -07001254 status = result;
Jeff Brown5707dbf2011-09-23 21:17:56 -07001255 } else {
Jeff Brown13b16042014-11-11 16:44:25 -08001256 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001257 if (!status) {
Jeff Brown93ff1f92011-11-04 19:01:44 -07001258 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001259 if (!status) {
Jeff Brown13b16042014-11-11 16:44:25 -08001260 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001261 return NO_ERROR;
1262 }
1263 }
1264 }
1265 }
1266 ::munmap(ptr, len);
1267 }
1268 ::close(fd);
1269 return status;
1270}
1271
Jeff Brown13b16042014-11-11 16:44:25 -08001272status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1273{
1274 // Must match up with what's done in writeBlob.
1275 if (!mAllowFds) return FDS_NOT_ALLOWED;
1276 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1277 if (status) return status;
1278 return writeDupFileDescriptor(fd);
1279}
1280
Mathias Agopiane1424282013-07-29 21:24:40 -07001281status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001282{
1283 status_t err;
1284
1285 // size if needed
Mathias Agopiane1424282013-07-29 21:24:40 -07001286 const size_t len = val.getFlattenedSize();
1287 const size_t fd_count = val.getFdCount();
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001288
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001289 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07001290 // don't accept size_t values which may have come from an
1291 // inadvertent conversion from a negative int.
1292 return BAD_VALUE;
1293 }
1294
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001295 err = this->writeInt32(len);
1296 if (err) return err;
1297
1298 err = this->writeInt32(fd_count);
1299 if (err) return err;
1300
1301 // payload
Martijn Coenenf8542382018-04-04 11:46:56 +02001302 void* const buf = this->writeInplace(len);
Yi Kong91635562018-06-07 14:38:36 -07001303 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001304 return BAD_VALUE;
1305
Yi Kong91635562018-06-07 14:38:36 -07001306 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001307 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07001308 fds = new (std::nothrow) int[fd_count];
1309 if (fds == nullptr) {
1310 ALOGE("write: failed to allocate requested %zu fds", fd_count);
1311 return BAD_VALUE;
1312 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08001313 }
1314
1315 err = val.flatten(buf, len, fds, fd_count);
1316 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1317 err = this->writeDupFileDescriptor( fds[i] );
1318 }
1319
1320 if (fd_count) {
1321 delete [] fds;
1322 }
1323
1324 return err;
1325}
1326
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001327status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1328{
1329 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1330 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1331 if (enoughData && enoughObjects) {
1332restart_write:
1333 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001334
Christopher Tate98e67d32015-06-03 18:44:15 -07001335 // remember if it's a file descriptor
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001336 if (val.hdr.type == BINDER_TYPE_FD) {
Christopher Tate98e67d32015-06-03 18:44:15 -07001337 if (!mAllowFds) {
1338 // fail before modifying our object index
1339 return FDS_NOT_ALLOWED;
1340 }
1341 mHasFds = mFdsKnown = true;
1342 }
1343
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001344 // Need to write meta-data?
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001345 if (nullMetaData || val.binder != 0) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001346 mObjects[mObjectsSize] = mDataPos;
Steven Morelandc673f1f2021-10-07 18:23:35 -07001347 acquire_object(ProcessState::self(), val, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001348 mObjectsSize++;
1349 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001350
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001351 return finishWrite(sizeof(flat_binder_object));
1352 }
1353
1354 if (!enoughData) {
1355 const status_t err = growData(sizeof(val));
1356 if (err != NO_ERROR) return err;
1357 }
1358 if (!enoughObjects) {
Martijn Coenen93fe5182020-01-22 10:46:25 +01001359 if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow
1360 if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001361 size_t newSize = ((mObjectsSize+2)*3)/2;
Martijn Coenen93fe5182020-01-22 10:46:25 +01001362 if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001363 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Yi Kong91635562018-06-07 14:38:36 -07001364 if (objects == nullptr) return NO_MEMORY;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001365 mObjects = objects;
1366 mObjectsCapacity = newSize;
1367 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001368
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001369 goto restart_write;
1370}
1371
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001372status_t Parcel::writeNoException()
1373{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001374 binder::Status status;
1375 return status.writeToParcel(this);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001376}
1377
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001378status_t Parcel::validateReadData(size_t upperBound) const
1379{
1380 // Don't allow non-object reads on object data
1381 if (mObjectsSorted || mObjectsSize <= 1) {
1382data_sorted:
1383 // Expect to check only against the next object
1384 if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) {
1385 // For some reason the current read position is greater than the next object
1386 // hint. Iterate until we find the right object
1387 size_t nextObject = mNextObjectHint;
1388 do {
1389 if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) {
1390 // Requested info overlaps with an object
1391 ALOGE("Attempt to read from protected data in Parcel %p", this);
1392 return PERMISSION_DENIED;
1393 }
1394 nextObject++;
1395 } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]);
1396 mNextObjectHint = nextObject;
1397 }
1398 return NO_ERROR;
1399 }
1400 // Quickly determine if mObjects is sorted.
1401 binder_size_t* currObj = mObjects + mObjectsSize - 1;
1402 binder_size_t* prevObj = currObj;
1403 while (currObj > mObjects) {
1404 prevObj--;
1405 if(*prevObj > *currObj) {
1406 goto data_unsorted;
1407 }
1408 currObj--;
1409 }
1410 mObjectsSorted = true;
1411 goto data_sorted;
1412
1413data_unsorted:
1414 // Insertion Sort mObjects
1415 // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common,
1416 // switch to std::sort(mObjects, mObjects + mObjectsSize);
1417 for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) {
1418 binder_size_t temp = *iter0;
1419 binder_size_t* iter1 = iter0 - 1;
1420 while (iter1 >= mObjects && *iter1 > temp) {
1421 *(iter1 + 1) = *iter1;
1422 iter1--;
1423 }
1424 *(iter1 + 1) = temp;
1425 }
1426 mNextObjectHint = 0;
1427 mObjectsSorted = true;
1428 goto data_sorted;
1429}
1430
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001431status_t Parcel::read(void* outData, size_t len) const
1432{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001433 if (len > INT32_MAX) {
1434 // don't accept size_t values which may have come from an
1435 // inadvertent conversion from a negative int.
1436 return BAD_VALUE;
1437 }
1438
1439 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1440 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001441 if (mObjectsSize > 0) {
1442 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001443 if(err != NO_ERROR) {
1444 // Still increment the data position by the expected length
1445 mDataPos += pad_size(len);
1446 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1447 return err;
1448 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001449 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001450 memcpy(outData, mData+mDataPos, len);
Nick Kralevichb6b14232015-04-02 09:36:02 -07001451 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001452 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001453 return NO_ERROR;
1454 }
1455 return NOT_ENOUGH_DATA;
1456}
1457
1458const void* Parcel::readInplace(size_t len) const
1459{
Nick Kralevichb6b14232015-04-02 09:36:02 -07001460 if (len > INT32_MAX) {
1461 // don't accept size_t values which may have come from an
1462 // inadvertent conversion from a negative int.
Yi Kong91635562018-06-07 14:38:36 -07001463 return nullptr;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001464 }
1465
1466 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1467 && len <= pad_size(len)) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001468 if (mObjectsSize > 0) {
1469 status_t err = validateReadData(mDataPos + pad_size(len));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001470 if(err != NO_ERROR) {
1471 // Still increment the data position by the expected length
1472 mDataPos += pad_size(len);
1473 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Yi Kong91635562018-06-07 14:38:36 -07001474 return nullptr;
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001475 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001476 }
1477
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001478 const void* data = mData+mDataPos;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001479 mDataPos += pad_size(len);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001480 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001481 return data;
1482 }
Yi Kong91635562018-06-07 14:38:36 -07001483 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001484}
1485
Steven Morelandd4f73fb2021-05-14 19:50:52 +00001486status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const {
1487 if (status_t status = readInt32(size); status != OK) return status;
1488 if (*size < 0) return OK; // may be null, client to handle
1489
1490 LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize);
1491
1492 // approximation, can't know max element size (e.g. if it makes heap
1493 // allocations)
1494 static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64");
1495 int32_t allocationSize;
1496 if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY;
1497
1498 // High limit of 1MB since something this big could never be returned. Could
1499 // probably scope this down, but might impact very specific usecases.
1500 constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000;
1501
1502 if (allocationSize >= kMaxAllocationSize) {
1503 return NO_MEMORY;
1504 }
1505
1506 return OK;
1507}
1508
Andreas Huber84a6d042009-08-17 13:33:27 -07001509template<class T>
1510status_t Parcel::readAligned(T *pArg) const {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001511 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001512
1513 if ((mDataPos+sizeof(T)) <= mDataSize) {
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001514 if (mObjectsSize > 0) {
1515 status_t err = validateReadData(mDataPos + sizeof(T));
Michael Wachenschwanza17f0222018-04-17 16:52:40 -07001516 if(err != NO_ERROR) {
1517 // Still increment the data position by the expected length
1518 mDataPos += sizeof(T);
1519 return err;
1520 }
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08001521 }
1522
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001523 const void* data = mData+mDataPos;
Andreas Huber84a6d042009-08-17 13:33:27 -07001524 mDataPos += sizeof(T);
1525 *pArg = *reinterpret_cast<const T*>(data);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001526 return NO_ERROR;
1527 } else {
1528 return NOT_ENOUGH_DATA;
1529 }
1530}
1531
Andreas Huber84a6d042009-08-17 13:33:27 -07001532template<class T>
1533T Parcel::readAligned() const {
1534 T result;
1535 if (readAligned(&result) != NO_ERROR) {
1536 result = 0;
1537 }
1538
1539 return result;
1540}
1541
1542template<class T>
1543status_t Parcel::writeAligned(T val) {
Elliott Hughes42a9b942020-08-17 15:53:31 -07001544 static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huber84a6d042009-08-17 13:33:27 -07001545
1546 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1547restart_write:
1548 *reinterpret_cast<T*>(mData+mDataPos) = val;
1549 return finishWrite(sizeof(val));
1550 }
1551
1552 status_t err = growData(sizeof(val));
1553 if (err == NO_ERROR) goto restart_write;
1554 return err;
1555}
1556
1557status_t Parcel::readInt32(int32_t *pArg) const
1558{
1559 return readAligned(pArg);
1560}
1561
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001562int32_t Parcel::readInt32() const
1563{
Andreas Huber84a6d042009-08-17 13:33:27 -07001564 return readAligned<int32_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001565}
1566
Dan Stoza41a0f2f2014-12-01 10:01:10 -08001567status_t Parcel::readUint32(uint32_t *pArg) const
1568{
1569 return readAligned(pArg);
1570}
1571
1572uint32_t Parcel::readUint32() const
1573{
1574 return readAligned<uint32_t>();
1575}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001576
1577status_t Parcel::readInt64(int64_t *pArg) const
1578{
Andreas Huber84a6d042009-08-17 13:33:27 -07001579 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001580}
1581
1582
1583int64_t Parcel::readInt64() const
1584{
Andreas Huber84a6d042009-08-17 13:33:27 -07001585 return readAligned<int64_t>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001586}
1587
Ronghua Wu2d13afd2015-03-16 11:11:07 -07001588status_t Parcel::readUint64(uint64_t *pArg) const
1589{
1590 return readAligned(pArg);
1591}
1592
1593uint64_t Parcel::readUint64() const
1594{
1595 return readAligned<uint64_t>();
1596}
1597
Serban Constantinescuf683e012013-11-05 16:53:55 +00001598status_t Parcel::readPointer(uintptr_t *pArg) const
1599{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001600 status_t ret;
1601 binder_uintptr_t ptr;
1602 ret = readAligned(&ptr);
1603 if (!ret)
1604 *pArg = ptr;
1605 return ret;
Serban Constantinescuf683e012013-11-05 16:53:55 +00001606}
1607
1608uintptr_t Parcel::readPointer() const
1609{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08001610 return readAligned<binder_uintptr_t>();
Serban Constantinescuf683e012013-11-05 16:53:55 +00001611}
1612
1613
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001614status_t Parcel::readFloat(float *pArg) const
1615{
Andreas Huber84a6d042009-08-17 13:33:27 -07001616 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001617}
1618
1619
1620float Parcel::readFloat() const
1621{
Andreas Huber84a6d042009-08-17 13:33:27 -07001622 return readAligned<float>();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001623}
1624
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001625#if defined(__mips__) && defined(__mips_hard_float)
1626
1627status_t Parcel::readDouble(double *pArg) const
1628{
1629 union {
1630 double d;
1631 unsigned long long ll;
1632 } u;
Narayan Kamath2c68d382014-06-04 15:04:29 +01001633 u.d = 0;
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001634 status_t status;
1635 status = readAligned(&u.ll);
1636 *pArg = u.d;
1637 return status;
1638}
1639
1640double Parcel::readDouble() const
1641{
1642 union {
1643 double d;
1644 unsigned long long ll;
1645 } u;
1646 u.ll = readAligned<unsigned long long>();
1647 return u.d;
1648}
1649
1650#else
1651
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001652status_t Parcel::readDouble(double *pArg) const
1653{
Andreas Huber84a6d042009-08-17 13:33:27 -07001654 return readAligned(pArg);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001655}
1656
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001657double Parcel::readDouble() const
1658{
Andreas Huber84a6d042009-08-17 13:33:27 -07001659 return readAligned<double>();
1660}
1661
Douglas Leungcc1a4bb2013-01-11 15:00:55 -08001662#endif
1663
Casey Dahlind6848f52015-10-15 15:44:59 -07001664status_t Parcel::readBool(bool *pArg) const
1665{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001666 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001667 status_t ret = readInt32(&tmp);
1668 *pArg = (tmp != 0);
1669 return ret;
1670}
1671
1672bool Parcel::readBool() const
1673{
1674 return readInt32() != 0;
1675}
1676
1677status_t Parcel::readChar(char16_t *pArg) const
1678{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001679 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001680 status_t ret = readInt32(&tmp);
1681 *pArg = char16_t(tmp);
1682 return ret;
1683}
1684
1685char16_t Parcel::readChar() const
1686{
1687 return char16_t(readInt32());
1688}
1689
1690status_t Parcel::readByte(int8_t *pArg) const
1691{
Manoj Gupta6eb62052017-07-12 10:29:15 -07001692 int32_t tmp = 0;
Casey Dahlind6848f52015-10-15 15:44:59 -07001693 status_t ret = readInt32(&tmp);
1694 *pArg = int8_t(tmp);
1695 return ret;
1696}
1697
1698int8_t Parcel::readByte() const
1699{
1700 return int8_t(readInt32());
1701}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001702
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001703status_t Parcel::readUtf8FromUtf16(std::string* str) const {
1704 size_t utf16Size = 0;
1705 const char16_t* src = readString16Inplace(&utf16Size);
1706 if (!src) {
1707 return UNEXPECTED_NULL;
1708 }
1709
1710 // Save ourselves the trouble, we're done.
1711 if (utf16Size == 0u) {
1712 str->clear();
1713 return NO_ERROR;
1714 }
1715
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001716 // Allow for closing '\0'
1717 ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1;
1718 if (utf8Size < 1) {
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001719 return BAD_VALUE;
1720 }
1721 // Note that while it is probably safe to assume string::resize keeps a
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001722 // spare byte around for the trailing null, we still pass the size including the trailing null
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001723 str->resize(utf8Size);
Sergio Giro9b39ebe2016-06-28 18:19:33 +01001724 utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size);
1725 str->resize(utf8Size - 1);
Christopher Wiley9a5e32f2016-01-28 16:56:53 -08001726 return NO_ERROR;
1727}
1728
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001729const char* Parcel::readCString() const
1730{
Steven Morelandd0d4b582019-05-17 13:14:06 -07001731 if (mDataPos < mDataSize) {
1732 const size_t avail = mDataSize-mDataPos;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001733 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1734 // is the string's trailing NUL within the parcel's valid bounds?
1735 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1736 if (eos) {
1737 const size_t len = eos - str;
Nick Kralevichb6b14232015-04-02 09:36:02 -07001738 mDataPos += pad_size(len+1);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07001739 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001740 return str;
1741 }
1742 }
Yi Kong91635562018-06-07 14:38:36 -07001743 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001744}
1745
1746String8 Parcel::readString8() const
1747{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001748 size_t len;
1749 const char* str = readString8Inplace(&len);
1750 if (str) return String8(str, len);
1751 ALOGE("Reading a NULL string not supported here.");
1752 return String8();
Roshan Pius87b64d22016-07-18 12:51:02 -07001753}
1754
1755status_t Parcel::readString8(String8* pArg) const
1756{
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001757 size_t len;
1758 const char* str = readString8Inplace(&len);
1759 if (str) {
1760 pArg->setTo(str, len);
1761 return 0;
1762 } else {
Roshan Pius87b64d22016-07-18 12:51:02 -07001763 *pArg = String8();
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001764 return UNEXPECTED_NULL;
Roshan Pius87b64d22016-07-18 12:51:02 -07001765 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001766}
1767
1768const char* Parcel::readString8Inplace(size_t* outLen) const
1769{
1770 int32_t size = readInt32();
1771 // watch for potential int overflow from size+1
1772 if (size >= 0 && size < INT32_MAX) {
1773 *outLen = size;
1774 const char* str = (const char*)readInplace(size+1);
Steven Moreland61d0f842020-12-04 21:13:03 +00001775 if (str != nullptr) {
1776 if (str[size] == '\0') {
1777 return str;
1778 }
1779 android_errorWriteLog(0x534e4554, "172655291");
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001780 }
Roshan Pius87b64d22016-07-18 12:51:02 -07001781 }
Jeff Sharkey2f8bdb52020-04-19 21:41:26 -06001782 *outLen = 0;
1783 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001784}
1785
1786String16 Parcel::readString16() const
1787{
1788 size_t len;
1789 const char16_t* str = readString16Inplace(&len);
1790 if (str) return String16(str, len);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001791 ALOGE("Reading a NULL string not supported here.");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001792 return String16();
1793}
1794
Casey Dahlinb9872622015-11-25 15:09:45 -08001795
Casey Dahlin451ff582015-10-19 18:12:18 -07001796status_t Parcel::readString16(String16* pArg) const
1797{
1798 size_t len;
1799 const char16_t* str = readString16Inplace(&len);
1800 if (str) {
Casey Dahlin1515ea12015-10-20 16:26:23 -07001801 pArg->setTo(str, len);
Casey Dahlin451ff582015-10-19 18:12:18 -07001802 return 0;
1803 } else {
1804 *pArg = String16();
Christopher Wiley4db672d2015-11-10 09:44:30 -08001805 return UNEXPECTED_NULL;
Casey Dahlin451ff582015-10-19 18:12:18 -07001806 }
1807}
1808
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001809const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1810{
1811 int32_t size = readInt32();
1812 // watch for potential int overflow from size+1
1813 if (size >= 0 && size < INT32_MAX) {
1814 *outLen = size;
1815 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
Steven Moreland61d0f842020-12-04 21:13:03 +00001816 if (str != nullptr) {
1817 if (str[size] == u'\0') {
1818 return str;
1819 }
1820 android_errorWriteLog(0x534e4554, "172655291");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001821 }
1822 }
1823 *outLen = 0;
Yi Kong91635562018-06-07 14:38:36 -07001824 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001825}
1826
Casey Dahlinf0c13772015-10-27 18:33:56 -07001827status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1828{
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001829 status_t status = readNullableStrongBinder(val);
1830 if (status == OK && !val->get()) {
1831 status = UNEXPECTED_NULL;
1832 }
1833 return status;
1834}
1835
1836status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1837{
Steven Morelanda86a3562019-08-01 23:28:34 +00001838 return unflattenBinder(val);
Casey Dahlinf0c13772015-10-27 18:33:56 -07001839}
1840
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001841sp<IBinder> Parcel::readStrongBinder() const
1842{
1843 sp<IBinder> val;
Christopher Wiley35d77ca2016-03-08 10:49:51 -08001844 // Note that a lot of code in Android reads binders by hand with this
1845 // method, and that code has historically been ok with getting nullptr
1846 // back (while ignoring error codes).
1847 readNullableStrongBinder(&val);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001848 return val;
1849}
1850
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001851int32_t Parcel::readExceptionCode() const
1852{
Christopher Wiley09eb7492015-11-09 15:06:15 -08001853 binder::Status status;
1854 status.readFromParcel(*this);
1855 return status.exceptionCode();
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -07001856}
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001857
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001858native_handle* Parcel::readNativeHandle() const
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001859{
1860 int numFds, numInts;
1861 status_t err;
1862 err = readInt32(&numFds);
Yi Kong91635562018-06-07 14:38:36 -07001863 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001864 err = readInt32(&numInts);
Yi Kong91635562018-06-07 14:38:36 -07001865 if (err != NO_ERROR) return nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001866
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001867 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001868 if (!h) {
Yi Kong91635562018-06-07 14:38:36 -07001869 return nullptr;
Adam Lesinskieaac99a2015-05-12 17:35:48 -07001870 }
1871
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001872 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001873 h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Marco Nelissen1de79662016-04-26 08:44:09 -07001874 if (h->data[i] < 0) {
1875 for (int j = 0; j < i; j++) {
1876 close(h->data[j]);
1877 }
1878 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001879 return nullptr;
Marco Nelissen1de79662016-04-26 08:44:09 -07001880 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001881 }
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001882 err = read(h->data + numFds, sizeof(int)*numInts);
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001883 if (err != NO_ERROR) {
Mathias Agopiana47f02a2009-05-21 16:29:38 -07001884 native_handle_close(h);
1885 native_handle_delete(h);
Yi Kong91635562018-06-07 14:38:36 -07001886 h = nullptr;
The Android Open Source Project5f78a482009-01-20 14:03:58 -08001887 }
1888 return h;
1889}
1890
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001891int Parcel::readFileDescriptor() const
1892{
1893 const flat_binder_object* flat = readObject(true);
Casey Dahlin06673e32015-11-23 13:24:23 -08001894
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07001895 if (flat && flat->hdr.type == BINDER_TYPE_FD) {
Casey Dahlin06673e32015-11-23 13:24:23 -08001896 return flat->handle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001897 }
Casey Dahlin06673e32015-11-23 13:24:23 -08001898
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001899 return BAD_TYPE;
1900}
1901
Dianne Hackborn1941a402016-08-29 12:30:43 -07001902int Parcel::readParcelFileDescriptor() const
1903{
1904 int32_t hasComm = readInt32();
1905 int fd = readFileDescriptor();
1906 if (hasComm != 0) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001907 // detach (owned by the binder driver)
1908 int comm = readFileDescriptor();
1909
1910 // warning: this must be kept in sync with:
1911 // frameworks/base/core/java/android/os/ParcelFileDescriptor.java
1912 enum ParcelFileDescriptorStatus {
1913 DETACHED = 2,
1914 };
1915
1916#if BYTE_ORDER == BIG_ENDIAN
1917 const int32_t message = ParcelFileDescriptorStatus::DETACHED;
1918#endif
1919#if BYTE_ORDER == LITTLE_ENDIAN
1920 const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED);
1921#endif
1922
1923 ssize_t written = TEMP_FAILURE_RETRY(
1924 ::write(comm, &message, sizeof(message)));
1925
Krzysztof Kosińskia8406892021-02-02 17:59:43 -08001926 if (written != sizeof(message)) {
Steven Morelandb73806a2018-11-12 19:35:47 -08001927 ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s",
1928 written, strerror(errno));
1929 return BAD_TYPE;
1930 }
Dianne Hackborn1941a402016-08-29 12:30:43 -07001931 }
1932 return fd;
1933}
1934
Christopher Wiley2cf19952016-04-11 11:09:37 -07001935status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const
Casey Dahlin06673e32015-11-23 13:24:23 -08001936{
1937 int got = readFileDescriptor();
1938
1939 if (got == BAD_TYPE) {
1940 return BAD_TYPE;
1941 }
1942
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08001943 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
Casey Dahlin06673e32015-11-23 13:24:23 -08001944
1945 if (val->get() < 0) {
1946 return BAD_VALUE;
1947 }
1948
1949 return OK;
1950}
1951
Ryo Hashimotobf551892018-05-31 16:58:35 +09001952status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const
1953{
1954 int got = readParcelFileDescriptor();
1955
1956 if (got == BAD_TYPE) {
1957 return BAD_TYPE;
1958 }
1959
1960 val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0));
1961
1962 if (val->get() < 0) {
1963 return BAD_VALUE;
1964 }
1965
1966 return OK;
1967}
Casey Dahlin06673e32015-11-23 13:24:23 -08001968
Jeff Brown5707dbf2011-09-23 21:17:56 -07001969status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1970{
Jeff Brown13b16042014-11-11 16:44:25 -08001971 int32_t blobType;
1972 status_t status = readInt32(&blobType);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001973 if (status) return status;
1974
Jeff Brown13b16042014-11-11 16:44:25 -08001975 if (blobType == BLOB_INPLACE) {
Steve Block6807e592011-10-20 11:56:00 +01001976 ALOGV("readBlob: read in place");
Jeff Brown5707dbf2011-09-23 21:17:56 -07001977 const void* ptr = readInplace(len);
1978 if (!ptr) return BAD_VALUE;
1979
Jeff Brown13b16042014-11-11 16:44:25 -08001980 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001981 return NO_ERROR;
1982 }
1983
Steve Block6807e592011-10-20 11:56:00 +01001984 ALOGV("readBlob: read from ashmem");
Jeff Brown13b16042014-11-11 16:44:25 -08001985 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown5707dbf2011-09-23 21:17:56 -07001986 int fd = readFileDescriptor();
1987 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1988
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001989 if (!ashmem_valid(fd)) {
1990 ALOGE("invalid fd");
1991 return BAD_VALUE;
1992 }
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001993 int size = ashmem_get_size_region(fd);
1994 if (size < 0 || size_t(size) < len) {
Jorim Jaggi150b4ef2018-07-13 11:18:30 +00001995 ALOGE("request size %zu does not match fd size %d", len, size);
Marco Nelissen7a96ec42018-06-06 07:37:46 -07001996 return BAD_VALUE;
1997 }
Yi Kong91635562018-06-07 14:38:36 -07001998 void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
Jeff Brown13b16042014-11-11 16:44:25 -08001999 MAP_SHARED, fd, 0);
Narayan Kamath9ea09752014-10-08 17:35:45 +01002000 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002001
Jeff Brown13b16042014-11-11 16:44:25 -08002002 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown5707dbf2011-09-23 21:17:56 -07002003 return NO_ERROR;
2004}
2005
Mathias Agopiane1424282013-07-29 21:24:40 -07002006status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002007{
2008 // size
2009 const size_t len = this->readInt32();
2010 const size_t fd_count = this->readInt32();
2011
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002012 if ((len > INT32_MAX) || (fd_count >= gMaxFds)) {
Nick Kralevichb6b14232015-04-02 09:36:02 -07002013 // don't accept size_t values which may have come from an
2014 // inadvertent conversion from a negative int.
2015 return BAD_VALUE;
2016 }
2017
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002018 // payload
Nick Kralevichb6b14232015-04-02 09:36:02 -07002019 void const* const buf = this->readInplace(pad_size(len));
Yi Kong91635562018-06-07 14:38:36 -07002020 if (buf == nullptr)
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002021 return BAD_VALUE;
2022
Yi Kong91635562018-06-07 14:38:36 -07002023 int* fds = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002024 if (fd_count) {
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002025 fds = new (std::nothrow) int[fd_count];
2026 if (fds == nullptr) {
2027 ALOGE("read: failed to allocate requested %zu fds", fd_count);
2028 return BAD_VALUE;
2029 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002030 }
2031
2032 status_t err = NO_ERROR;
2033 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Fabien Sanglardd84ff312016-10-21 10:58:26 -07002034 int fd = this->readFileDescriptor();
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002035 if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) {
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002036 err = BAD_VALUE;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -08002037 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 -07002038 i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno));
2039 // Close all the file descriptors that were dup-ed.
2040 for (size_t j=0; j<i ;j++) {
2041 close(fds[j]);
2042 }
Jun Jiangabf8a2c2014-04-29 14:22:10 +08002043 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -08002044 }
2045
2046 if (err == NO_ERROR) {
2047 err = val.unflatten(buf, len, fds, fd_count);
2048 }
2049
2050 if (fd_count) {
2051 delete [] fds;
2052 }
2053
2054 return err;
2055}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002056const flat_binder_object* Parcel::readObject(bool nullMetaData) const
2057{
2058 const size_t DPOS = mDataPos;
2059 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
2060 const flat_binder_object* obj
2061 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
2062 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002063 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
The Android Open Source Project5f78a482009-01-20 14:03:58 -08002064 // When transferring a NULL object, we don't write it into
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002065 // the object list, so we don't want to check for it when
2066 // reading.
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002067 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002068 return obj;
2069 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002070
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002071 // Ensure that this object is valid...
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002072 binder_size_t* const OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002073 const size_t N = mObjectsSize;
2074 size_t opos = mNextObjectHint;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002075
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002076 if (N > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002077 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002078 this, DPOS, opos);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002080 // Start at the current hint position, looking for an object at
2081 // the current data position.
2082 if (opos < N) {
2083 while (opos < (N-1) && OBJS[opos] < DPOS) {
2084 opos++;
2085 }
2086 } else {
2087 opos = N-1;
2088 }
2089 if (OBJS[opos] == DPOS) {
2090 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002091 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002092 this, DPOS, opos);
2093 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002094 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002095 return obj;
2096 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002097
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002098 // Look backwards for it...
2099 while (opos > 0 && OBJS[opos] > DPOS) {
2100 opos--;
2101 }
2102 if (OBJS[opos] == DPOS) {
2103 // Found it!
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002104 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002105 this, DPOS, opos);
2106 mNextObjectHint = opos+1;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002107 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002108 return obj;
2109 }
2110 }
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002111 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 -07002112 this, DPOS);
2113 }
Yi Kong91635562018-06-07 14:38:36 -07002114 return nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002115}
2116
2117void Parcel::closeFileDescriptors()
2118{
2119 size_t i = mObjectsSize;
2120 if (i > 0) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002121 //ALOGI("Closing file descriptors for %zu objects...", i);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002122 }
2123 while (i > 0) {
2124 i--;
2125 const flat_binder_object* flat
2126 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002127 if (flat->hdr.type == BINDER_TYPE_FD) {
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002128 //ALOGI("Closing fd: %ld", flat->handle);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129 close(flat->handle);
2130 }
2131 }
2132}
2133
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002134uintptr_t Parcel::ipcData() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002135{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002136 return reinterpret_cast<uintptr_t>(mData);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002137}
2138
2139size_t Parcel::ipcDataSize() const
2140{
2141 return (mDataSize > mDataPos ? mDataSize : mDataPos);
2142}
2143
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002144uintptr_t Parcel::ipcObjects() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002145{
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002146 return reinterpret_cast<uintptr_t>(mObjects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002147}
2148
2149size_t Parcel::ipcObjectsCount() const
2150{
2151 return mObjectsSize;
2152}
2153
2154void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Steven Moreland161fe122020-11-12 23:16:47 +00002155 const binder_size_t* objects, size_t objectsCount, release_func relFunc)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002156{
Steven Moreland438cce82021-04-02 18:04:08 +00002157 // this code uses 'mOwner == nullptr' to understand whether it owns memory
2158 LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function");
2159
Steven Morelandceed9bb2020-12-17 01:01:06 +00002160 freeData();
2161
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002162 mData = const_cast<uint8_t*>(data);
2163 mDataSize = mDataCapacity = dataSize;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002164 mObjects = const_cast<binder_size_t*>(objects);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002165 mObjectsSize = mObjectsCapacity = objectsCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002166 mOwner = relFunc;
Steven Morelandceed9bb2020-12-17 01:01:06 +00002167
2168 binder_size_t minOffset = 0;
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002169 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002170 binder_size_t offset = mObjects[i];
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002171 if (offset < minOffset) {
Dan Albert3bdc5b82014-11-20 11:50:23 -08002172 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevåg6f286112014-02-19 20:42:13 -08002173 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002174 mObjectsSize = 0;
2175 break;
2176 }
Martijn Coenen82c75312019-07-24 15:18:30 +02002177 const flat_binder_object* flat
2178 = reinterpret_cast<const flat_binder_object*>(mData + offset);
2179 uint32_t type = flat->hdr.type;
2180 if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE ||
2181 type == BINDER_TYPE_FD)) {
2182 // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support
2183 // them in libbinder. If we do receive them, it probably means a kernel bug; try to
2184 // recover gracefully by clearing out the objects, and releasing the objects we do
2185 // know about.
2186 android_errorWriteLog(0x534e4554, "135930648");
2187 ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n",
2188 __func__, type, (uint64_t)offset);
2189 releaseObjects();
2190 mObjectsSize = 0;
2191 break;
2192 }
Arve Hjønnevågf50b9ea2014-02-13 19:22:08 -08002193 minOffset = offset + sizeof(flat_binder_object);
2194 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002195 scanForFds();
2196}
2197
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002198void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002199{
2200 to << "Parcel(";
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002201
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002202 if (errorCheck() != NO_ERROR) {
2203 const status_t err = errorCheck();
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002204 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002205 } else if (dataSize() > 0) {
2206 const uint8_t* DATA = data();
2207 to << indent << HexDump(DATA, dataSize()) << dedent;
Steven Moreland8bd01352019-07-15 16:36:14 -07002208 const binder_size_t* OBJS = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002209 const size_t N = objectsCount();
2210 for (size_t i=0; i<N; i++) {
2211 const flat_binder_object* flat
2212 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
2213 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002214 << TypeCode(flat->hdr.type & 0x7f7f7f00)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002215 << " = " << flat->binder;
2216 }
2217 } else {
2218 to << "NULL";
2219 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002220
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002221 to << ")";
2222}
2223
2224void Parcel::releaseObjects()
2225{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002226 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002227 if (i == 0) {
2228 return;
2229 }
2230 sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002231 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002232 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002233 while (i > 0) {
2234 i--;
2235 const flat_binder_object* flat
2236 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002237 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002238 }
2239}
2240
2241void Parcel::acquireObjects()
2242{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002243 size_t i = mObjectsSize;
Martijn Coenen69390d42018-10-22 15:18:10 +02002244 if (i == 0) {
2245 return;
2246 }
2247 const sp<ProcessState> proc(ProcessState::self());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248 uint8_t* const data = mData;
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002249 binder_size_t* const objects = mObjects;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002250 while (i > 0) {
2251 i--;
2252 const flat_binder_object* flat
2253 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Steven Morelandc673f1f2021-10-07 18:23:35 -07002254 acquire_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002255 }
2256}
2257
2258void Parcel::freeData()
2259{
2260 freeDataNoInit();
2261 initState();
2262}
2263
2264void Parcel::freeDataNoInit()
2265{
2266 if (mOwner) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002267 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002268 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002269 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002270 } else {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002271 LOG_ALLOC("Parcel %p: freeing allocated data", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002272 releaseObjects();
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002273 if (mData) {
2274 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002275 gParcelGlobalAllocSize -= mDataCapacity;
2276 gParcelGlobalAllocCount--;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002277 if (mDeallocZero) {
2278 zeroMemory(mData, mDataSize);
2279 }
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002280 free(mData);
2281 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002282 if (mObjects) free(mObjects);
2283 }
2284}
2285
2286status_t Parcel::growData(size_t len)
2287{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002288 if (len > INT32_MAX) {
2289 // don't accept size_t values which may have come from an
2290 // inadvertent conversion from a negative int.
2291 return BAD_VALUE;
2292 }
2293
Martijn Coenen93fe5182020-01-22 10:46:25 +01002294 if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow
2295 if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002296 size_t newSize = ((mDataSize+len)*3)/2;
2297 return (newSize <= mDataSize)
2298 ? (status_t) NO_MEMORY
Steven Moreland042ae822020-05-27 17:45:17 +00002299 : continueWrite(std::max(newSize, (size_t) 128));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300}
2301
Steven Morelandf183fdd2020-10-27 00:12:12 +00002302static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) {
2303 if (!zero) {
2304 return (uint8_t*)realloc(data, newCapacity);
2305 }
2306 uint8_t* newData = (uint8_t*)malloc(newCapacity);
2307 if (!newData) {
2308 return nullptr;
2309 }
2310
2311 memcpy(newData, data, std::min(oldCapacity, newCapacity));
2312 zeroMemory(data, oldCapacity);
2313 free(data);
2314 return newData;
2315}
2316
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002317status_t Parcel::restartWrite(size_t desired)
2318{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002319 if (desired > INT32_MAX) {
2320 // don't accept size_t values which may have come from an
2321 // inadvertent conversion from a negative int.
2322 return BAD_VALUE;
2323 }
2324
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002325 if (mOwner) {
2326 freeData();
2327 return continueWrite(desired);
2328 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002329
Steven Morelandf183fdd2020-10-27 00:12:12 +00002330 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002331 if (!data && desired > mDataCapacity) {
2332 mError = NO_MEMORY;
2333 return NO_MEMORY;
2334 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002335
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002336 releaseObjects();
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002337
Devin Moore4a0a55e2020-06-04 13:23:10 -07002338 if (data || desired == 0) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002339 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Jeff Sharkey8994c182020-09-11 12:07:10 -06002340 if (mDataCapacity > desired) {
2341 gParcelGlobalAllocSize -= (mDataCapacity - desired);
2342 } else {
2343 gParcelGlobalAllocSize += (desired - mDataCapacity);
2344 }
2345
Colin Cross83ec65e2015-12-08 17:15:50 -08002346 if (!mData) {
2347 gParcelGlobalAllocCount++;
2348 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002349 mData = data;
2350 mDataCapacity = desired;
2351 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002352
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002353 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002354 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2355 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2356
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002357 free(mObjects);
Yi Kong91635562018-06-07 14:38:36 -07002358 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 mObjectsSize = mObjectsCapacity = 0;
2360 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002361 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002362 mHasFds = false;
2363 mFdsKnown = true;
Dianne Hackborn8938ed22011-09-28 23:19:47 -04002364 mAllowFds = true;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002365
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002366 return NO_ERROR;
2367}
2368
2369status_t Parcel::continueWrite(size_t desired)
2370{
Nick Kralevichb6b14232015-04-02 09:36:02 -07002371 if (desired > INT32_MAX) {
2372 // don't accept size_t values which may have come from an
2373 // inadvertent conversion from a negative int.
2374 return BAD_VALUE;
2375 }
2376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002377 // If shrinking, first adjust for any objects that appear
2378 // after the new data size.
2379 size_t objectsSize = mObjectsSize;
2380 if (desired < mDataSize) {
2381 if (desired == 0) {
2382 objectsSize = 0;
2383 } else {
2384 while (objectsSize > 0) {
Michael Wachenschwanza6541632017-05-18 22:08:32 +00002385 if (mObjects[objectsSize-1] < desired)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002386 break;
2387 objectsSize--;
2388 }
2389 }
2390 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002391
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002392 if (mOwner) {
2393 // If the size is going to zero, just release the owner's data.
2394 if (desired == 0) {
2395 freeData();
2396 return NO_ERROR;
2397 }
2398
2399 // If there is a different owner, we need to take
2400 // posession.
2401 uint8_t* data = (uint8_t*)malloc(desired);
2402 if (!data) {
2403 mError = NO_MEMORY;
2404 return NO_MEMORY;
2405 }
Yi Kong91635562018-06-07 14:38:36 -07002406 binder_size_t* objects = nullptr;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002407
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002408 if (objectsSize) {
Nick Kraleviche9881a32015-04-28 16:21:30 -07002409 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002410 if (!objects) {
Hyejin Kim3f727c02013-03-09 11:28:54 +09002411 free(data);
2412
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002413 mError = NO_MEMORY;
2414 return NO_MEMORY;
2415 }
2416
2417 // Little hack to only acquire references on objects
2418 // we will be keeping.
2419 size_t oldObjectsSize = mObjectsSize;
2420 mObjectsSize = objectsSize;
2421 acquireObjects();
2422 mObjectsSize = oldObjectsSize;
2423 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002424
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002425 if (mData) {
2426 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2427 }
2428 if (objects && mObjects) {
Arve Hjønnevåg84e625a2014-01-28 20:12:59 -08002429 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002430 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002431 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Steven Moreland161fe122020-11-12 23:16:47 +00002432 mOwner(this, mData, mDataSize, mObjects, mObjectsSize);
Yi Kong91635562018-06-07 14:38:36 -07002433 mOwner = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002434
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002435 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002436 gParcelGlobalAllocSize += desired;
2437 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002438
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002439 mData = data;
2440 mObjects = objects;
2441 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002442 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002443 mDataCapacity = desired;
2444 mObjectsSize = mObjectsCapacity = objectsSize;
2445 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002446 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002447
2448 } else if (mData) {
2449 if (objectsSize < mObjectsSize) {
2450 // Need to release refs on any objects we are dropping.
2451 const sp<ProcessState> proc(ProcessState::self());
2452 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2453 const flat_binder_object* flat
2454 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
Christopher Ferrisdbaa22a2017-07-27 10:38:45 -07002455 if (flat->hdr.type == BINDER_TYPE_FD) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002456 // will need to rescan because we may have lopped off the only FDs
2457 mFdsKnown = false;
2458 }
Steven Morelandc673f1f2021-10-07 18:23:35 -07002459 release_object(proc, *flat, this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002460 }
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002461
2462 if (objectsSize == 0) {
2463 free(mObjects);
2464 mObjects = nullptr;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002465 mObjectsCapacity = 0;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002466 } else {
2467 binder_size_t* objects =
2468 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2469 if (objects) {
2470 mObjects = objects;
Michael Wachenschwanzdaf29a62019-10-15 11:49:22 -07002471 mObjectsCapacity = objectsSize;
Michael Wachenschwanz6af27a82019-06-03 17:24:51 -07002472 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002473 }
2474 mObjectsSize = objectsSize;
2475 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002476 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002477 }
2478
2479 // We own the data, so we can just do a realloc().
2480 if (desired > mDataCapacity) {
Steven Morelandf183fdd2020-10-27 00:12:12 +00002481 uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002482 if (data) {
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002483 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2484 desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002485 gParcelGlobalAllocSize += desired;
2486 gParcelGlobalAllocSize -= mDataCapacity;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002487 mData = data;
2488 mDataCapacity = desired;
Ganesh Mahendranade89892017-09-28 16:56:03 +08002489 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002490 mError = NO_MEMORY;
2491 return NO_MEMORY;
2492 }
2493 } else {
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002494 if (mDataSize > desired) {
2495 mDataSize = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002496 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborn97e2bcd2011-04-13 18:15:56 -07002497 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002498 if (mDataPos > desired) {
2499 mDataPos = desired;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002500 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002501 }
2502 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002503
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002504 } else {
2505 // This is the first data. Easy!
2506 uint8_t* data = (uint8_t*)malloc(desired);
2507 if (!data) {
2508 mError = NO_MEMORY;
2509 return NO_MEMORY;
2510 }
Hyejin Kim3f727c02013-03-09 11:28:54 +09002511
Yi Kong91635562018-06-07 14:38:36 -07002512 if(!(mDataCapacity == 0 && mObjects == nullptr
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002513 && mObjectsCapacity == 0)) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -08002514 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002515 }
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002516
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002517 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002518 gParcelGlobalAllocSize += desired;
2519 gParcelGlobalAllocCount++;
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002520
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002521 mData = data;
2522 mDataSize = mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002523 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2524 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002525 mDataCapacity = desired;
2526 }
2527
2528 return NO_ERROR;
2529}
2530
2531void Parcel::initState()
2532{
Dianne Hackborn7e790af2014-11-11 12:22:53 -08002533 LOG_ALLOC("Parcel %p: initState", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002534 mError = NO_ERROR;
Yi Kong91635562018-06-07 14:38:36 -07002535 mData = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002536 mDataSize = 0;
2537 mDataCapacity = 0;
2538 mDataPos = 0;
Mark Salyzynd4ecccf2014-05-30 16:35:57 -07002539 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2540 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Steven Morelandc9939062021-05-05 17:57:41 +00002541 mSession = nullptr;
Yi Kong91635562018-06-07 14:38:36 -07002542 mObjects = nullptr;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002543 mObjectsSize = 0;
2544 mObjectsCapacity = 0;
2545 mNextObjectHint = 0;
Michael Wachenschwanzc5176812017-11-17 18:25:05 -08002546 mObjectsSorted = false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002547 mHasFds = false;
2548 mFdsKnown = true;
Steven Moreland6e5a7752019-08-05 20:30:14 -07002549 mAllowFds = true;
Steven Morelandf183fdd2020-10-27 00:12:12 +00002550 mDeallocZero = false;
Yi Kong91635562018-06-07 14:38:36 -07002551 mOwner = nullptr;
Olivier Gaillarddc848a02019-01-30 17:10:44 +00002552 mWorkSourceRequestHeaderPosition = 0;
2553 mRequestHeaderPresent = false;
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002554
2555 // racing multiple init leads only to multiple identical write
2556 if (gMaxFds == 0) {
2557 struct rlimit result;
2558 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2559 gMaxFds = (size_t)result.rlim_cur;
Christopher Tatebf14e942016-03-25 14:16:24 -07002560 //ALOGI("parcel fd limit set to %zu", gMaxFds);
Christopher Tatee4e0ae82016-03-24 16:03:44 -07002561 } else {
2562 ALOGW("Unable to getrlimit: %s", strerror(errno));
2563 gMaxFds = 1024;
2564 }
2565 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002566}
2567
Bernardo Rufinobbbd88d2021-10-15 14:54:30 +01002568void Parcel::scanForFds() const {
2569 status_t status = hasFileDescriptorsInRange(0, dataSize(), &mHasFds);
2570 ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002571 mFdsKnown = true;
2572}
2573
Dan Sandleraa5c2342015-04-10 10:08:45 -04002574size_t Parcel::getBlobAshmemSize() const
2575{
Adrian Roos6bb31142015-10-22 16:46:12 -07002576 // This used to return the size of all blobs that were written to ashmem, now we're returning
2577 // the ashmem currently referenced by this Parcel, which should be equivalent.
Steven Morelandc673f1f2021-10-07 18:23:35 -07002578 // TODO(b/202029388): Remove method once ABI can be changed.
2579 return getOpenAshmemSize();
Dan Sandleraa5c2342015-04-10 10:08:45 -04002580}
2581
Adrian Rooscbf37262015-10-22 16:12:53 -07002582size_t Parcel::getOpenAshmemSize() const
2583{
Steven Morelandc673f1f2021-10-07 18:23:35 -07002584 size_t openAshmemSize = 0;
2585 for (size_t i = 0; i < mObjectsSize; i++) {
2586 const flat_binder_object* flat =
2587 reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2588
2589 // cookie is compared against zero for historical reasons
2590 // > obj.cookie = takeOwnership ? 1 : 0;
2591 if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) {
2592 int size = ashmem_get_size_region(flat->handle);
2593 if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) {
2594 ALOGE("Overflow when computing ashmem size.");
2595 return SIZE_MAX;
2596 }
2597 }
2598 }
2599 return openAshmemSize;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002600}
2601
2602// --- Parcel::Blob ---
2603
2604Parcel::Blob::Blob() :
Yi Kong91635562018-06-07 14:38:36 -07002605 mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002606}
2607
2608Parcel::Blob::~Blob() {
2609 release();
2610}
2611
2612void Parcel::Blob::release() {
Jeff Brown13b16042014-11-11 16:44:25 -08002613 if (mFd != -1 && mData) {
Jeff Brown5707dbf2011-09-23 21:17:56 -07002614 ::munmap(mData, mSize);
2615 }
2616 clear();
2617}
2618
Jeff Brown13b16042014-11-11 16:44:25 -08002619void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2620 mFd = fd;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002621 mData = data;
2622 mSize = size;
Jeff Brown13b16042014-11-11 16:44:25 -08002623 mMutable = isMutable;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002624}
2625
2626void Parcel::Blob::clear() {
Jeff Brown13b16042014-11-11 16:44:25 -08002627 mFd = -1;
Yi Kong91635562018-06-07 14:38:36 -07002628 mData = nullptr;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002629 mSize = 0;
Jeff Brown13b16042014-11-11 16:44:25 -08002630 mMutable = false;
Jeff Brown5707dbf2011-09-23 21:17:56 -07002631}
2632
Steven Moreland61ff8492019-09-26 16:05:45 -07002633} // namespace android