The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Mark Salyzyn | 70f3665 | 2016-02-02 10:27:03 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Mark Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 22 | #include <inttypes.h> |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 23 | #include <linux/sched.h> |
Mark Salyzyn | 70f3665 | 2016-02-02 10:27:03 -0800 | [diff] [blame] | 24 | #include <pthread.h> |
Mark Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <sys/mman.h> |
Mark Salyzyn | eab2afc | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Mark Salyzyn | eab2afc | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 32 | #include <unistd.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 34 | #include <binder/Binder.h> |
| 35 | #include <binder/BpBinder.h> |
Mark Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 36 | #include <binder/IPCThreadState.h> |
| 37 | #include <binder/Parcel.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 38 | #include <binder/ProcessState.h> |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 39 | #include <binder/Stability.h> |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 40 | #include <binder/Status.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 41 | #include <binder/TextOutput.h> |
| 42 | |
Mark Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 43 | #include <cutils/ashmem.h> |
Steven Moreland | 3af936a | 2021-03-26 03:05:38 +0000 | [diff] [blame] | 44 | #include <cutils/compiler.h> |
Mark Salyzyn | abed7f7 | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 45 | #include <utils/Flattenable.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | #include <utils/Log.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | #include <utils/String16.h> |
Steven Moreland | 3af936a | 2021-03-26 03:05:38 +0000 | [diff] [blame] | 48 | #include <utils/String8.h> |
| 49 | #include <utils/misc.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 51 | #include "RpcState.h" |
Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 52 | #include "Static.h" |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 53 | #include "Utils.h" |
Steven Moreland | 6ba5a25 | 2021-05-04 22:49:00 +0000 | [diff] [blame] | 54 | #include "binder_module.h" |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | #define LOG_REFS(...) |
Mark Salyzyn | e93390b | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 57 | //#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 58 | #define LOG_ALLOC(...) |
Mark Salyzyn | e93390b | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 59 | //#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | |
| 61 | // --------------------------------------------------------------------------- |
| 62 | |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 63 | // 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 | |
| 68 | static size_t pad_size(size_t s) { |
Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 69 | if (s > (std::numeric_limits<size_t>::max() - 3)) { |
Steven Moreland | 6adf33c | 2019-09-25 13:18:09 -0700 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL("pad size too big %zu", s); |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 71 | } |
| 72 | return PAD_SIZE_UNSAFE(s); |
| 73 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 75 | // Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER |
Jeff Sharkey | 05827be | 2018-06-26 10:52:38 -0600 | [diff] [blame] | 76 | #define STRICT_MODE_PENALTY_GATHER (1 << 31) |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 77 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | namespace android { |
| 79 | |
Steven Moreland | 7b10226 | 2019-08-01 15:48:43 -0700 | [diff] [blame] | 80 | // many things compile this into prebuilts on the stack |
Steven Moreland | 90c1f9a | 2021-05-03 18:27:24 +0000 | [diff] [blame] | 81 | #ifdef __LP64__ |
| 82 | static_assert(sizeof(Parcel) == 120); |
| 83 | #else |
| 84 | static_assert(sizeof(Parcel) == 60); |
| 85 | #endif |
Steven Moreland | 7b10226 | 2019-08-01 15:48:43 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | 8994c18 | 2020-09-11 12:07:10 -0600 | [diff] [blame] | 87 | static std::atomic<size_t> gParcelGlobalAllocCount; |
| 88 | static std::atomic<size_t> gParcelGlobalAllocSize; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 89 | |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 90 | static size_t gMaxFds = 0; |
| 91 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 92 | // Maximum size of a blob to transfer in-place. |
| 93 | static const size_t BLOB_INPLACE_LIMIT = 16 * 1024; |
| 94 | |
| 95 | enum { |
| 96 | BLOB_INPLACE = 0, |
| 97 | BLOB_ASHMEM_IMMUTABLE = 1, |
| 98 | BLOB_ASHMEM_MUTABLE = 2, |
| 99 | }; |
| 100 | |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 101 | static void acquire_object(const sp<ProcessState>& proc, const flat_binder_object& obj, |
| 102 | const void* who) { |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 103 | switch (obj.hdr.type) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | case BINDER_TYPE_BINDER: |
| 105 | if (obj.binder) { |
yuxi | c05af3b | 2021-08-24 02:52:15 +0000 | [diff] [blame] | 106 | LOG_REFS("Parcel %p acquiring reference on local %llu", who, obj.cookie); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 107 | reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | } |
| 109 | return; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | case BINDER_TYPE_HANDLE: { |
| 111 | const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 112 | if (b != nullptr) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get()); |
| 114 | b->incStrong(who); |
| 115 | } |
| 116 | return; |
| 117 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | case BINDER_TYPE_FD: { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | } |
| 122 | |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 123 | ALOGD("Invalid object type 0x%08x", obj.hdr.type); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 126 | static void release_object(const sp<ProcessState>& proc, const flat_binder_object& obj, |
| 127 | const void* who) { |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 128 | switch (obj.hdr.type) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | case BINDER_TYPE_BINDER: |
| 130 | if (obj.binder) { |
yuxi | c05af3b | 2021-08-24 02:52:15 +0000 | [diff] [blame] | 131 | LOG_REFS("Parcel %p releasing reference on local %llu", who, obj.cookie); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 132 | reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | } |
| 134 | return; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | case BINDER_TYPE_HANDLE: { |
| 136 | const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 137 | if (b != nullptr) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get()); |
| 139 | b->decStrong(who); |
| 140 | } |
| 141 | return; |
| 142 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | case BINDER_TYPE_FD: { |
Mark Salyzyn | b454d8f | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 144 | if (obj.cookie != 0) { // owned |
Mark Salyzyn | b454d8f | 2016-01-27 08:02:48 -0800 | [diff] [blame] | 145 | close(obj.handle); |
Adrian Roos | cbf3726 | 2015-10-22 16:12:53 -0700 | [diff] [blame] | 146 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | } |
| 150 | |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 151 | ALOGE("Invalid object type 0x%08x", obj.hdr.type); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Steven Moreland | 34b48cb | 2020-12-01 22:45:38 +0000 | [diff] [blame] | 154 | status_t Parcel::finishFlattenBinder(const sp<IBinder>& binder) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | { |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 156 | internal::Stability::tryMarkCompilationUnit(binder.get()); |
Steven Moreland | 16a4106 | 2021-07-23 13:35:25 -0700 | [diff] [blame] | 157 | int16_t rep = internal::Stability::getRepr(binder.get()); |
Steven Moreland | 14e4cfa | 2021-06-03 21:40:45 +0000 | [diff] [blame] | 158 | return writeInt32(rep); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 161 | status_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 Moreland | 14e4cfa | 2021-06-03 21:40:45 +0000 | [diff] [blame] | 168 | status = internal::Stability::setRepr(binder.get(), static_cast<int16_t>(stability), |
| 169 | true /*log*/); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 170 | if (status != OK) return status; |
| 171 | |
| 172 | *out = binder; |
| 173 | return OK; |
| 174 | } |
| 175 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 176 | static 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 Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 180 | status_t Parcel::flattenBinder(const sp<IBinder>& binder) { |
| 181 | BBinder* local = nullptr; |
| 182 | if (binder) local = binder->localBinder(); |
| 183 | if (local) local->setParceled(); |
| 184 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 185 | if (isForRpc()) { |
| 186 | if (binder) { |
| 187 | status_t status = writeInt32(1); // non-null |
| 188 | if (status != OK) return status; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 189 | uint64_t address; |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 190 | // TODO(b/167966510): need to undo this if the Parcel is not sent |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 191 | status = mSession->state()->onBinderLeaving(mSession, binder, &address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 192 | if (status != OK) return status; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 193 | status = writeUint64(address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 194 | 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 Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | flat_binder_object obj; |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 203 | obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 204 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 205 | int schedBits = 0; |
| 206 | if (!IPCThreadState::self()->backgroundSchedulingDisabled()) { |
| 207 | schedBits = schedPolicyMask(SCHED_NORMAL, 19); |
Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 210 | if (binder != nullptr) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | if (!local) { |
| 212 | BpBinder *proxy = binder->remoteBinder(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 213 | if (proxy == nullptr) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 214 | ALOGE("null proxy"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 215 | } else { |
| 216 | if (proxy->isRpcBinder()) { |
Steven Moreland | a923111 | 2021-09-22 10:08:14 -0700 | [diff] [blame] | 217 | ALOGE("Sending a socket binder over kernel binder is prohibited"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 218 | return INVALID_OPERATION; |
| 219 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | } |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 221 | const int32_t handle = proxy ? proxy->getPrivateAccessor().binderHandle() : 0; |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 222 | obj.hdr.type = BINDER_TYPE_HANDLE; |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 223 | obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 224 | obj.handle = handle; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 225 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | } else { |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 227 | 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 Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 234 | if (local->isRequestingSid()) { |
| 235 | obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX; |
| 236 | } |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 237 | if (local->isInheritRt()) { |
| 238 | obj.flags |= FLAT_BINDER_FLAG_INHERIT_RT; |
| 239 | } |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 240 | obj.hdr.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 241 | obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs()); |
| 242 | obj.cookie = reinterpret_cast<uintptr_t>(local); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 243 | } |
| 244 | } else { |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 245 | obj.hdr.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 246 | obj.binder = 0; |
| 247 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 248 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 249 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 250 | obj.flags |= schedBits; |
| 251 | |
Steven Moreland | 34b48cb | 2020-12-01 22:45:38 +0000 | [diff] [blame] | 252 | status_t status = writeObject(obj, false); |
| 253 | if (status != OK) return status; |
| 254 | |
| 255 | return finishFlattenBinder(binder); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 258 | status_t Parcel::unflattenBinder(sp<IBinder>* out) const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 260 | if (isForRpc()) { |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 261 | LOG_ALWAYS_FATAL_IF(mSession == nullptr, "RpcSession required to read from remote parcel"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 262 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 263 | int32_t isPresent; |
| 264 | status_t status = readInt32(&isPresent); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 265 | if (status != OK) return status; |
| 266 | |
| 267 | sp<IBinder> binder; |
| 268 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 269 | if (isPresent & 1) { |
| 270 | uint64_t addr; |
| 271 | if (status_t status = readUint64(&addr); status != OK) return status; |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 272 | if (status_t status = mSession->state()->onBinderEntering(mSession, addr, &binder); |
| 273 | status != OK) |
| 274 | return status; |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 275 | if (status_t status = mSession->state()->flushExcessBinderRefs(mSession, addr, binder); |
| 276 | status != OK) |
| 277 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return finishUnflattenBinder(binder, out); |
| 281 | } |
| 282 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 283 | const flat_binder_object* flat = readObject(false); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 284 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 285 | if (flat) { |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 286 | switch (flat->hdr.type) { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 287 | case BINDER_TYPE_BINDER: { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 288 | sp<IBinder> binder = |
| 289 | sp<IBinder>::fromExisting(reinterpret_cast<IBinder*>(flat->cookie)); |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 290 | 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 Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 297 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | } |
| 299 | return BAD_TYPE; |
| 300 | } |
| 301 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 302 | // --------------------------------------------------------------------------- |
| 303 | |
| 304 | Parcel::Parcel() |
| 305 | { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 306 | LOG_ALLOC("Parcel %p: constructing", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | initState(); |
| 308 | } |
| 309 | |
| 310 | Parcel::~Parcel() |
| 311 | { |
| 312 | freeDataNoInit(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 313 | LOG_ALLOC("Parcel %p: destroyed", this); |
| 314 | } |
| 315 | |
| 316 | size_t Parcel::getGlobalAllocSize() { |
Jeff Sharkey | 8994c18 | 2020-09-11 12:07:10 -0600 | [diff] [blame] | 317 | return gParcelGlobalAllocSize.load(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | size_t Parcel::getGlobalAllocCount() { |
Jeff Sharkey | 8994c18 | 2020-09-11 12:07:10 -0600 | [diff] [blame] | 321 | return gParcelGlobalAllocCount.load(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | const uint8_t* Parcel::data() const |
| 325 | { |
| 326 | return mData; |
| 327 | } |
| 328 | |
| 329 | size_t Parcel::dataSize() const |
| 330 | { |
| 331 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 332 | } |
| 333 | |
| 334 | size_t Parcel::dataAvail() const |
| 335 | { |
Nick Kralevich | cfe27de | 2015-09-16 09:49:15 -0700 | [diff] [blame] | 336 | size_t result = dataSize() - dataPosition(); |
| 337 | if (result > INT32_MAX) { |
Steven Moreland | 6adf33c | 2019-09-25 13:18:09 -0700 | [diff] [blame] | 338 | LOG_ALWAYS_FATAL("result too big: %zu", result); |
Nick Kralevich | cfe27de | 2015-09-16 09:49:15 -0700 | [diff] [blame] | 339 | } |
| 340 | return result; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | size_t Parcel::dataPosition() const |
| 344 | { |
| 345 | return mDataPos; |
| 346 | } |
| 347 | |
| 348 | size_t Parcel::dataCapacity() const |
| 349 | { |
| 350 | return mDataCapacity; |
| 351 | } |
| 352 | |
| 353 | status_t Parcel::setDataSize(size_t size) |
| 354 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 355 | 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 Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | status_t err; |
| 362 | err = continueWrite(size); |
| 363 | if (err == NO_ERROR) { |
| 364 | mDataSize = size; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 365 | ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 366 | } |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | void Parcel::setDataPosition(size_t pos) const |
| 371 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 372 | 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 Moreland | 6adf33c | 2019-09-25 13:18:09 -0700 | [diff] [blame] | 375 | LOG_ALWAYS_FATAL("pos too big: %zu", pos); |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 376 | } |
| 377 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 378 | mDataPos = pos; |
| 379 | mNextObjectHint = 0; |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 380 | mObjectsSorted = false; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | status_t Parcel::setDataCapacity(size_t size) |
| 384 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 385 | 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 Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 391 | if (size > mDataCapacity) return continueWrite(size); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 392 | return NO_ERROR; |
| 393 | } |
| 394 | |
| 395 | status_t Parcel::setData(const uint8_t* buffer, size_t len) |
| 396 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 397 | 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 Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 403 | 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 Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 412 | status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 413 | { |
Steven Moreland | 2034eff | 2021-10-13 11:24:35 -0700 | [diff] [blame] | 414 | 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 Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 417 | return BAD_TYPE; |
| 418 | } |
| 419 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | status_t err; |
Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 421 | const uint8_t *data = parcel->mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 422 | const binder_size_t *objects = parcel->mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 423 | 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 Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 431 | 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 Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | // 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 Tate | 27182be | 2015-05-27 17:53:02 -0700 | [diff] [blame] | 447 | if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 448 | if (firstIndex == -1) { |
| 449 | firstIndex = i; |
| 450 | } |
| 451 | lastIndex = i; |
| 452 | } |
| 453 | } |
| 454 | int numObjects = lastIndex - firstIndex + 1; |
| 455 | |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 456 | if ((mDataSize+len) > mDataCapacity) { |
| 457 | // grow data |
| 458 | err = growData(len); |
| 459 | if (err != NO_ERROR) { |
| 460 | return err; |
| 461 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // append data |
| 465 | memcpy(mData + mDataPos, data + offset, len); |
| 466 | mDataPos += len; |
| 467 | mDataSize += len; |
| 468 | |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 469 | err = NO_ERROR; |
| 470 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 471 | if (numObjects > 0) { |
Martijn Coenen | 69390d4 | 2018-10-22 15:18:10 +0200 | [diff] [blame] | 472 | const sp<ProcessState> proc(ProcessState::self()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | // grow objects |
| 474 | if (mObjectsCapacity < mObjectsSize + numObjects) { |
Martijn Coenen | 93fe518 | 2020-01-22 10:46:25 +0100 | [diff] [blame] | 475 | if ((size_t) numObjects > SIZE_MAX - mObjectsSize) return NO_MEMORY; // overflow |
| 476 | if (mObjectsSize + numObjects > SIZE_MAX / 3) return NO_MEMORY; // overflow |
Christopher Tate | ed7a50c | 2015-06-08 14:45:14 -0700 | [diff] [blame] | 477 | size_t newSize = ((mObjectsSize + numObjects)*3)/2; |
Martijn Coenen | 93fe518 | 2020-01-22 10:46:25 +0100 | [diff] [blame] | 478 | if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 479 | binder_size_t *objects = |
| 480 | (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 481 | if (objects == (binder_size_t*)nullptr) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | return NO_MEMORY; |
| 483 | } |
| 484 | mObjects = objects; |
| 485 | mObjectsCapacity = newSize; |
| 486 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 487 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | // 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 Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 495 | flat_binder_object* flat |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 496 | = reinterpret_cast<flat_binder_object*>(mData + off); |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 497 | acquire_object(proc, *flat, this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 499 | if (flat->hdr.type == BINDER_TYPE_FD) { |
Dianne Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 500 | // 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 Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 503 | flat->handle = fcntl(flat->handle, F_DUPFD_CLOEXEC, 0); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 504 | flat->cookie = 1; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 505 | mHasFds = mFdsKnown = true; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 506 | if (!mAllowFds) { |
| 507 | err = FDS_NOT_ALLOWED; |
| 508 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 513 | return err; |
| 514 | } |
| 515 | |
Dianne Hackborn | 15feb9b | 2017-04-10 15:34:35 -0700 | [diff] [blame] | 516 | int Parcel::compareData(const Parcel& other) { |
| 517 | size_t size = dataSize(); |
| 518 | if (size != other.dataSize()) { |
| 519 | return size < other.dataSize() ? -1 : 1; |
| 520 | } |
| 521 | return memcmp(data(), other.data(), size); |
| 522 | } |
| 523 | |
Bernardo Rufino | 897b165 | 2021-10-08 10:30:20 +0100 | [diff] [blame] | 524 | status_t Parcel::compareDataInRange(size_t thisOffset, const Parcel& other, size_t otherOffset, |
| 525 | size_t len, int* result) const { |
| 526 | if (len > INT32_MAX || thisOffset > INT32_MAX || otherOffset > INT32_MAX) { |
| 527 | // Don't accept size_t values which may have come from an inadvertent conversion from a |
| 528 | // negative int. |
| 529 | return BAD_VALUE; |
| 530 | } |
| 531 | size_t thisLimit; |
| 532 | if (__builtin_add_overflow(thisOffset, len, &thisLimit) || thisLimit > mDataSize) { |
| 533 | return BAD_VALUE; |
| 534 | } |
| 535 | size_t otherLimit; |
| 536 | if (__builtin_add_overflow(otherOffset, len, &otherLimit) || otherLimit > other.mDataSize) { |
| 537 | return BAD_VALUE; |
| 538 | } |
| 539 | *result = memcmp(data() + thisOffset, other.data() + otherOffset, len); |
| 540 | return NO_ERROR; |
| 541 | } |
| 542 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 543 | bool Parcel::allowFds() const |
| 544 | { |
| 545 | return mAllowFds; |
| 546 | } |
| 547 | |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 548 | bool Parcel::pushAllowFds(bool allowFds) |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 549 | { |
| 550 | const bool origValue = mAllowFds; |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 551 | if (!allowFds) { |
| 552 | mAllowFds = false; |
| 553 | } |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 554 | return origValue; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 557 | void Parcel::restoreAllowFds(bool lastValue) |
| 558 | { |
| 559 | mAllowFds = lastValue; |
| 560 | } |
| 561 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 562 | bool Parcel::hasFileDescriptors() const |
| 563 | { |
| 564 | if (!mFdsKnown) { |
| 565 | scanForFds(); |
| 566 | } |
| 567 | return mHasFds; |
| 568 | } |
| 569 | |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 570 | status_t Parcel::hasFileDescriptorsInRange(size_t offset, size_t len, bool* result) const { |
Bernardo Rufino | 22092af | 2021-10-07 14:09:24 +0100 | [diff] [blame] | 571 | if (len > INT32_MAX || offset > INT32_MAX) { |
| 572 | // Don't accept size_t values which may have come from an inadvertent conversion from a |
| 573 | // negative int. |
| 574 | return BAD_VALUE; |
| 575 | } |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 576 | size_t limit; |
| 577 | if (__builtin_add_overflow(offset, len, &limit) || limit > mDataSize) { |
Bernardo Rufino | 22092af | 2021-10-07 14:09:24 +0100 | [diff] [blame] | 578 | return BAD_VALUE; |
| 579 | } |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 580 | *result = false; |
Bernardo Rufino | 22092af | 2021-10-07 14:09:24 +0100 | [diff] [blame] | 581 | for (size_t i = 0; i < mObjectsSize; i++) { |
| 582 | size_t pos = mObjects[i]; |
| 583 | if (pos < offset) continue; |
| 584 | if (pos + sizeof(flat_binder_object) > offset + len) { |
| 585 | if (mObjectsSorted) break; |
| 586 | else continue; |
| 587 | } |
| 588 | const flat_binder_object* flat = reinterpret_cast<const flat_binder_object*>(mData + pos); |
| 589 | if (flat->hdr.type == BINDER_TYPE_FD) { |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 590 | *result = true; |
| 591 | break; |
Bernardo Rufino | 22092af | 2021-10-07 14:09:24 +0100 | [diff] [blame] | 592 | } |
| 593 | } |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 594 | return NO_ERROR; |
Bernardo Rufino | 22092af | 2021-10-07 14:09:24 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 597 | void Parcel::markSensitive() const |
| 598 | { |
| 599 | mDeallocZero = true; |
| 600 | } |
| 601 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 602 | void Parcel::markForBinder(const sp<IBinder>& binder) { |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 603 | LOG_ALWAYS_FATAL_IF(mData != nullptr, "format must be set before data is written"); |
| 604 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 605 | if (binder && binder->remoteBinder() && binder->remoteBinder()->isRpcBinder()) { |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 606 | markForRpc(binder->remoteBinder()->getPrivateAccessor().rpcSession()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 610 | void Parcel::markForRpc(const sp<RpcSession>& session) { |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 611 | LOG_ALWAYS_FATAL_IF(mData != nullptr && mOwner == nullptr, |
| 612 | "format must be set before data is written OR on IPC data"); |
| 613 | |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 614 | LOG_ALWAYS_FATAL_IF(session == nullptr, "markForRpc requires session"); |
| 615 | mSession = session; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | bool Parcel::isForRpc() const { |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 619 | return mSession != nullptr; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Olivier Gaillard | dc848a0 | 2019-01-30 17:10:44 +0000 | [diff] [blame] | 622 | void Parcel::updateWorkSourceRequestHeaderPosition() const { |
| 623 | // Only update the request headers once. We only want to point |
| 624 | // to the first headers read/written. |
| 625 | if (!mRequestHeaderPresent) { |
| 626 | mWorkSourceRequestHeaderPosition = dataPosition(); |
| 627 | mRequestHeaderPresent = true; |
| 628 | } |
| 629 | } |
| 630 | |
Steven Moreland | b6c7e22 | 2021-02-18 19:20:14 +0000 | [diff] [blame] | 631 | #if defined(__ANDROID_VNDK__) |
Steven Moreland | d70160f | 2019-07-23 10:20:38 -0700 | [diff] [blame] | 632 | constexpr int32_t kHeader = B_PACK_CHARS('V', 'N', 'D', 'R'); |
Yifan Hong | 7078653 | 2021-10-20 21:41:18 -0700 | [diff] [blame] | 633 | #elif defined(__ANDROID_RECOVERY__) |
| 634 | constexpr int32_t kHeader = B_PACK_CHARS('R', 'E', 'C', 'O'); |
Steven Moreland | d70160f | 2019-07-23 10:20:38 -0700 | [diff] [blame] | 635 | #else |
| 636 | constexpr int32_t kHeader = B_PACK_CHARS('S', 'Y', 'S', 'T'); |
| 637 | #endif |
| 638 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 639 | // Write RPC headers. (previously just the interface token) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 640 | status_t Parcel::writeInterfaceToken(const String16& interface) |
| 641 | { |
Steven Moreland | dbc76c7 | 2020-10-01 18:02:48 +0000 | [diff] [blame] | 642 | return writeInterfaceToken(interface.string(), interface.size()); |
| 643 | } |
| 644 | |
| 645 | status_t Parcel::writeInterfaceToken(const char16_t* str, size_t len) { |
Steven Moreland | 3af936a | 2021-03-26 03:05:38 +0000 | [diff] [blame] | 646 | if (CC_LIKELY(!isForRpc())) { |
| 647 | const IPCThreadState* threadState = IPCThreadState::self(); |
| 648 | writeInt32(threadState->getStrictModePolicy() | STRICT_MODE_PENALTY_GATHER); |
| 649 | updateWorkSourceRequestHeaderPosition(); |
| 650 | writeInt32(threadState->shouldPropagateWorkSource() ? threadState->getCallingWorkSourceUid() |
| 651 | : IPCThreadState::kUnsetWorkSource); |
| 652 | writeInt32(kHeader); |
| 653 | } |
Steven Moreland | dbc76c7 | 2020-10-01 18:02:48 +0000 | [diff] [blame] | 654 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 655 | // currently the interface identification token is just its name as a string |
Steven Moreland | dbc76c7 | 2020-10-01 18:02:48 +0000 | [diff] [blame] | 656 | return writeString16(str, len); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Olivier Gaillard | dc848a0 | 2019-01-30 17:10:44 +0000 | [diff] [blame] | 659 | bool Parcel::replaceCallingWorkSourceUid(uid_t uid) |
| 660 | { |
| 661 | if (!mRequestHeaderPresent) { |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | const size_t initialPosition = dataPosition(); |
| 666 | setDataPosition(mWorkSourceRequestHeaderPosition); |
| 667 | status_t err = writeInt32(uid); |
| 668 | setDataPosition(initialPosition); |
| 669 | return err == NO_ERROR; |
| 670 | } |
| 671 | |
Steven Moreland | f1b1e49 | 2019-05-06 15:05:13 -0700 | [diff] [blame] | 672 | uid_t Parcel::readCallingWorkSourceUid() const |
Olivier Gaillard | dc848a0 | 2019-01-30 17:10:44 +0000 | [diff] [blame] | 673 | { |
| 674 | if (!mRequestHeaderPresent) { |
| 675 | return IPCThreadState::kUnsetWorkSource; |
| 676 | } |
| 677 | |
| 678 | const size_t initialPosition = dataPosition(); |
| 679 | setDataPosition(mWorkSourceRequestHeaderPosition); |
| 680 | uid_t uid = readInt32(); |
| 681 | setDataPosition(initialPosition); |
| 682 | return uid; |
| 683 | } |
| 684 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 685 | bool Parcel::checkInterface(IBinder* binder) const |
| 686 | { |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 687 | return enforceInterface(binder->getInterfaceDescriptor()); |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 690 | bool Parcel::enforceInterface(const String16& interface, |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 691 | IPCThreadState* threadState) const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 692 | { |
Daniel Colascione | 0bb330d | 2019-10-29 16:44:19 -0700 | [diff] [blame] | 693 | return enforceInterface(interface.string(), interface.size(), threadState); |
| 694 | } |
| 695 | |
| 696 | bool Parcel::enforceInterface(const char16_t* interface, |
| 697 | size_t len, |
| 698 | IPCThreadState* threadState) const |
| 699 | { |
Steven Moreland | 3af936a | 2021-03-26 03:05:38 +0000 | [diff] [blame] | 700 | if (CC_LIKELY(!isForRpc())) { |
| 701 | // StrictModePolicy. |
| 702 | int32_t strictPolicy = readInt32(); |
| 703 | if (threadState == nullptr) { |
| 704 | threadState = IPCThreadState::self(); |
| 705 | } |
| 706 | if ((threadState->getLastTransactionBinderFlags() & IBinder::FLAG_ONEWAY) != 0) { |
| 707 | // For one-way calls, the callee is running entirely |
| 708 | // disconnected from the caller, so disable StrictMode entirely. |
| 709 | // Not only does disk/network usage not impact the caller, but |
| 710 | // there's no way to communicate back violations anyway. |
| 711 | threadState->setStrictModePolicy(0); |
| 712 | } else { |
| 713 | threadState->setStrictModePolicy(strictPolicy); |
| 714 | } |
| 715 | // WorkSource. |
| 716 | updateWorkSourceRequestHeaderPosition(); |
| 717 | int32_t workSource = readInt32(); |
| 718 | threadState->setCallingWorkSourceUidWithoutPropagation(workSource); |
| 719 | // vendor header |
| 720 | int32_t header = readInt32(); |
| 721 | if (header != kHeader) { |
| 722 | ALOGE("Expecting header 0x%x but found 0x%x. Mixing copies of libbinder?", kHeader, |
| 723 | header); |
| 724 | return false; |
| 725 | } |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 726 | } |
Steven Moreland | 3af936a | 2021-03-26 03:05:38 +0000 | [diff] [blame] | 727 | |
Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 728 | // Interface descriptor. |
Daniel Colascione | 0bb330d | 2019-10-29 16:44:19 -0700 | [diff] [blame] | 729 | size_t parcel_interface_len; |
| 730 | const char16_t* parcel_interface = readString16Inplace(&parcel_interface_len); |
| 731 | if (len == parcel_interface_len && |
| 732 | (!len || !memcmp(parcel_interface, interface, len * sizeof (char16_t)))) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 733 | return true; |
| 734 | } else { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 735 | ALOGW("**** enforceInterface() expected '%s' but read '%s'", |
Daniel Colascione | 0bb330d | 2019-10-29 16:44:19 -0700 | [diff] [blame] | 736 | String8(interface, len).string(), |
| 737 | String8(parcel_interface, parcel_interface_len).string()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 738 | return false; |
| 739 | } |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 740 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 741 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 742 | size_t Parcel::objectsCount() const |
| 743 | { |
| 744 | return mObjectsSize; |
| 745 | } |
| 746 | |
| 747 | status_t Parcel::errorCheck() const |
| 748 | { |
| 749 | return mError; |
| 750 | } |
| 751 | |
| 752 | void Parcel::setError(status_t err) |
| 753 | { |
| 754 | mError = err; |
| 755 | } |
| 756 | |
| 757 | status_t Parcel::finishWrite(size_t len) |
| 758 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 759 | if (len > INT32_MAX) { |
| 760 | // don't accept size_t values which may have come from an |
| 761 | // inadvertent conversion from a negative int. |
| 762 | return BAD_VALUE; |
| 763 | } |
| 764 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 765 | //printf("Finish write of %d\n", len); |
| 766 | mDataPos += len; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 767 | ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 768 | if (mDataPos > mDataSize) { |
| 769 | mDataSize = mDataPos; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 770 | ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 771 | } |
| 772 | //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); |
| 773 | return NO_ERROR; |
| 774 | } |
| 775 | |
| 776 | status_t Parcel::writeUnpadded(const void* data, size_t len) |
| 777 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 778 | if (len > INT32_MAX) { |
| 779 | // don't accept size_t values which may have come from an |
| 780 | // inadvertent conversion from a negative int. |
| 781 | return BAD_VALUE; |
| 782 | } |
| 783 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 784 | size_t end = mDataPos + len; |
| 785 | if (end < mDataPos) { |
| 786 | // integer overflow |
| 787 | return BAD_VALUE; |
| 788 | } |
| 789 | |
| 790 | if (end <= mDataCapacity) { |
| 791 | restart_write: |
| 792 | memcpy(mData+mDataPos, data, len); |
| 793 | return finishWrite(len); |
| 794 | } |
| 795 | |
| 796 | status_t err = growData(len); |
| 797 | if (err == NO_ERROR) goto restart_write; |
| 798 | return err; |
| 799 | } |
| 800 | |
| 801 | status_t Parcel::write(const void* data, size_t len) |
| 802 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 803 | if (len > INT32_MAX) { |
| 804 | // don't accept size_t values which may have come from an |
| 805 | // inadvertent conversion from a negative int. |
| 806 | return BAD_VALUE; |
| 807 | } |
| 808 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | void* const d = writeInplace(len); |
| 810 | if (d) { |
| 811 | memcpy(d, data, len); |
| 812 | return NO_ERROR; |
| 813 | } |
| 814 | return mError; |
| 815 | } |
| 816 | |
| 817 | void* Parcel::writeInplace(size_t len) |
| 818 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 819 | if (len > INT32_MAX) { |
| 820 | // don't accept size_t values which may have come from an |
| 821 | // inadvertent conversion from a negative int. |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 822 | return nullptr; |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | const size_t padded = pad_size(len); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 826 | |
Khalid Ramadan | b3d817b | 2021-11-18 07:02:50 +0000 | [diff] [blame^] | 827 | // check for integer overflow |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 828 | if (mDataPos+padded < mDataPos) { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 829 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if ((mDataPos+padded) <= mDataCapacity) { |
| 833 | restart_write: |
| 834 | //printf("Writing %ld bytes, padded to %ld\n", len, padded); |
| 835 | uint8_t* const data = mData+mDataPos; |
| 836 | |
| 837 | // Need to pad at end? |
| 838 | if (padded != len) { |
| 839 | #if BYTE_ORDER == BIG_ENDIAN |
| 840 | static const uint32_t mask[4] = { |
| 841 | 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 |
| 842 | }; |
| 843 | #endif |
| 844 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 845 | static const uint32_t mask[4] = { |
| 846 | 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff |
| 847 | }; |
| 848 | #endif |
| 849 | //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], |
| 850 | // *reinterpret_cast<void**>(data+padded-4)); |
| 851 | *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; |
| 852 | } |
| 853 | |
| 854 | finishWrite(padded); |
| 855 | return data; |
| 856 | } |
| 857 | |
| 858 | status_t err = growData(padded); |
| 859 | if (err == NO_ERROR) goto restart_write; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 860 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 863 | status_t Parcel::writeUtf8AsUtf16(const std::string& str) { |
| 864 | const uint8_t* strData = (uint8_t*)str.data(); |
| 865 | const size_t strLen= str.length(); |
| 866 | const ssize_t utf16Len = utf8_to_utf16_length(strData, strLen); |
Sergio Giro | f460743 | 2016-07-21 14:46:35 +0100 | [diff] [blame] | 867 | if (utf16Len < 0 || utf16Len > std::numeric_limits<int32_t>::max()) { |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 868 | return BAD_VALUE; |
| 869 | } |
| 870 | |
| 871 | status_t err = writeInt32(utf16Len); |
| 872 | if (err) { |
| 873 | return err; |
| 874 | } |
| 875 | |
| 876 | // Allocate enough bytes to hold our converted string and its terminating NULL. |
| 877 | void* dst = writeInplace((utf16Len + 1) * sizeof(char16_t)); |
| 878 | if (!dst) { |
| 879 | return NO_MEMORY; |
| 880 | } |
| 881 | |
Sergio Giro | f460743 | 2016-07-21 14:46:35 +0100 | [diff] [blame] | 882 | utf8_to_utf16(strData, strLen, (char16_t*)dst, (size_t) utf16Len + 1); |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 883 | |
| 884 | return NO_ERROR; |
| 885 | } |
| 886 | |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 887 | |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 888 | status_t Parcel::writeUtf8AsUtf16(const std::optional<std::string>& str) { return writeData(str); } |
| 889 | status_t Parcel::writeUtf8AsUtf16(const std::unique_ptr<std::string>& str) { return writeData(str); } |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 890 | |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 891 | status_t Parcel::writeString16(const std::optional<String16>& str) { return writeData(str); } |
| 892 | status_t Parcel::writeString16(const std::unique_ptr<String16>& str) { return writeData(str); } |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 893 | |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 894 | status_t Parcel::writeByteVector(const std::vector<int8_t>& val) { return writeData(val); } |
| 895 | status_t Parcel::writeByteVector(const std::optional<std::vector<int8_t>>& val) { return writeData(val); } |
| 896 | status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<int8_t>>& val) { return writeData(val); } |
| 897 | status_t Parcel::writeByteVector(const std::vector<uint8_t>& val) { return writeData(val); } |
| 898 | status_t Parcel::writeByteVector(const std::optional<std::vector<uint8_t>>& val) { return writeData(val); } |
| 899 | status_t Parcel::writeByteVector(const std::unique_ptr<std::vector<uint8_t>>& val){ return writeData(val); } |
| 900 | status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val) { return writeData(val); } |
| 901 | status_t Parcel::writeInt32Vector(const std::optional<std::vector<int32_t>>& val) { return writeData(val); } |
| 902 | status_t Parcel::writeInt32Vector(const std::unique_ptr<std::vector<int32_t>>& val) { return writeData(val); } |
| 903 | status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val) { return writeData(val); } |
| 904 | status_t Parcel::writeInt64Vector(const std::optional<std::vector<int64_t>>& val) { return writeData(val); } |
| 905 | status_t Parcel::writeInt64Vector(const std::unique_ptr<std::vector<int64_t>>& val) { return writeData(val); } |
| 906 | status_t Parcel::writeUint64Vector(const std::vector<uint64_t>& val) { return writeData(val); } |
| 907 | status_t Parcel::writeUint64Vector(const std::optional<std::vector<uint64_t>>& val) { return writeData(val); } |
| 908 | status_t Parcel::writeUint64Vector(const std::unique_ptr<std::vector<uint64_t>>& val) { return writeData(val); } |
| 909 | status_t Parcel::writeFloatVector(const std::vector<float>& val) { return writeData(val); } |
| 910 | status_t Parcel::writeFloatVector(const std::optional<std::vector<float>>& val) { return writeData(val); } |
| 911 | status_t Parcel::writeFloatVector(const std::unique_ptr<std::vector<float>>& val) { return writeData(val); } |
| 912 | status_t Parcel::writeDoubleVector(const std::vector<double>& val) { return writeData(val); } |
| 913 | status_t Parcel::writeDoubleVector(const std::optional<std::vector<double>>& val) { return writeData(val); } |
| 914 | status_t Parcel::writeDoubleVector(const std::unique_ptr<std::vector<double>>& val) { return writeData(val); } |
| 915 | status_t Parcel::writeBoolVector(const std::vector<bool>& val) { return writeData(val); } |
| 916 | status_t Parcel::writeBoolVector(const std::optional<std::vector<bool>>& val) { return writeData(val); } |
| 917 | status_t Parcel::writeBoolVector(const std::unique_ptr<std::vector<bool>>& val) { return writeData(val); } |
| 918 | status_t Parcel::writeCharVector(const std::vector<char16_t>& val) { return writeData(val); } |
| 919 | status_t Parcel::writeCharVector(const std::optional<std::vector<char16_t>>& val) { return writeData(val); } |
| 920 | status_t Parcel::writeCharVector(const std::unique_ptr<std::vector<char16_t>>& val) { return writeData(val); } |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 921 | |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 922 | status_t Parcel::writeString16Vector(const std::vector<String16>& val) { return writeData(val); } |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame] | 923 | status_t Parcel::writeString16Vector( |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 924 | const std::optional<std::vector<std::optional<String16>>>& val) { return writeData(val); } |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 925 | status_t Parcel::writeString16Vector( |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 926 | const std::unique_ptr<std::vector<std::unique_ptr<String16>>>& val) { return writeData(val); } |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 927 | status_t Parcel::writeUtf8VectorAsUtf16Vector( |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 928 | const std::optional<std::vector<std::optional<std::string>>>& val) { return writeData(val); } |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 929 | status_t Parcel::writeUtf8VectorAsUtf16Vector( |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 930 | const std::unique_ptr<std::vector<std::unique_ptr<std::string>>>& val) { return writeData(val); } |
| 931 | status_t Parcel::writeUtf8VectorAsUtf16Vector(const std::vector<std::string>& val) { return writeData(val); } |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 932 | |
Andy Hung | 49198cf | 2020-11-18 11:02:39 -0800 | [diff] [blame] | 933 | status_t Parcel::writeUniqueFileDescriptorVector(const std::vector<base::unique_fd>& val) { return writeData(val); } |
| 934 | status_t Parcel::writeUniqueFileDescriptorVector(const std::optional<std::vector<base::unique_fd>>& val) { return writeData(val); } |
| 935 | status_t Parcel::writeUniqueFileDescriptorVector(const std::unique_ptr<std::vector<base::unique_fd>>& val) { return writeData(val); } |
| 936 | |
| 937 | status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val) { return writeData(val); } |
| 938 | status_t Parcel::writeStrongBinderVector(const std::optional<std::vector<sp<IBinder>>>& val) { return writeData(val); } |
| 939 | status_t Parcel::writeStrongBinderVector(const std::unique_ptr<std::vector<sp<IBinder>>>& val) { return writeData(val); } |
| 940 | |
| 941 | status_t Parcel::writeParcelable(const Parcelable& parcelable) { return writeData(parcelable); } |
| 942 | |
| 943 | status_t Parcel::readUtf8FromUtf16(std::optional<std::string>* str) const { return readData(str); } |
| 944 | status_t Parcel::readUtf8FromUtf16(std::unique_ptr<std::string>* str) const { return readData(str); } |
| 945 | |
| 946 | status_t Parcel::readString16(std::optional<String16>* pArg) const { return readData(pArg); } |
| 947 | status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const { return readData(pArg); } |
| 948 | |
| 949 | status_t Parcel::readByteVector(std::vector<int8_t>* val) const { return readData(val); } |
| 950 | status_t Parcel::readByteVector(std::vector<uint8_t>* val) const { return readData(val); } |
| 951 | status_t Parcel::readByteVector(std::optional<std::vector<int8_t>>* val) const { return readData(val); } |
| 952 | status_t Parcel::readByteVector(std::unique_ptr<std::vector<int8_t>>* val) const { return readData(val); } |
| 953 | status_t Parcel::readByteVector(std::optional<std::vector<uint8_t>>* val) const { return readData(val); } |
| 954 | status_t Parcel::readByteVector(std::unique_ptr<std::vector<uint8_t>>* val) const { return readData(val); } |
| 955 | status_t Parcel::readInt32Vector(std::optional<std::vector<int32_t>>* val) const { return readData(val); } |
| 956 | status_t Parcel::readInt32Vector(std::unique_ptr<std::vector<int32_t>>* val) const { return readData(val); } |
| 957 | status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const { return readData(val); } |
| 958 | status_t Parcel::readInt64Vector(std::optional<std::vector<int64_t>>* val) const { return readData(val); } |
| 959 | status_t Parcel::readInt64Vector(std::unique_ptr<std::vector<int64_t>>* val) const { return readData(val); } |
| 960 | status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const { return readData(val); } |
| 961 | status_t Parcel::readUint64Vector(std::optional<std::vector<uint64_t>>* val) const { return readData(val); } |
| 962 | status_t Parcel::readUint64Vector(std::unique_ptr<std::vector<uint64_t>>* val) const { return readData(val); } |
| 963 | status_t Parcel::readUint64Vector(std::vector<uint64_t>* val) const { return readData(val); } |
| 964 | status_t Parcel::readFloatVector(std::optional<std::vector<float>>* val) const { return readData(val); } |
| 965 | status_t Parcel::readFloatVector(std::unique_ptr<std::vector<float>>* val) const { return readData(val); } |
| 966 | status_t Parcel::readFloatVector(std::vector<float>* val) const { return readData(val); } |
| 967 | status_t Parcel::readDoubleVector(std::optional<std::vector<double>>* val) const { return readData(val); } |
| 968 | status_t Parcel::readDoubleVector(std::unique_ptr<std::vector<double>>* val) const { return readData(val); } |
| 969 | status_t Parcel::readDoubleVector(std::vector<double>* val) const { return readData(val); } |
| 970 | status_t Parcel::readBoolVector(std::optional<std::vector<bool>>* val) const { return readData(val); } |
| 971 | status_t Parcel::readBoolVector(std::unique_ptr<std::vector<bool>>* val) const { return readData(val); } |
| 972 | status_t Parcel::readBoolVector(std::vector<bool>* val) const { return readData(val); } |
| 973 | status_t Parcel::readCharVector(std::optional<std::vector<char16_t>>* val) const { return readData(val); } |
| 974 | status_t Parcel::readCharVector(std::unique_ptr<std::vector<char16_t>>* val) const { return readData(val); } |
| 975 | status_t Parcel::readCharVector(std::vector<char16_t>* val) const { return readData(val); } |
| 976 | |
| 977 | status_t Parcel::readString16Vector( |
| 978 | std::optional<std::vector<std::optional<String16>>>* val) const { return readData(val); } |
| 979 | status_t Parcel::readString16Vector( |
| 980 | std::unique_ptr<std::vector<std::unique_ptr<String16>>>* val) const { return readData(val); } |
| 981 | status_t Parcel::readString16Vector(std::vector<String16>* val) const { return readData(val); } |
| 982 | status_t Parcel::readUtf8VectorFromUtf16Vector( |
| 983 | std::optional<std::vector<std::optional<std::string>>>* val) const { return readData(val); } |
| 984 | status_t Parcel::readUtf8VectorFromUtf16Vector( |
| 985 | std::unique_ptr<std::vector<std::unique_ptr<std::string>>>* val) const { return readData(val); } |
| 986 | status_t Parcel::readUtf8VectorFromUtf16Vector(std::vector<std::string>* val) const { return readData(val); } |
| 987 | |
| 988 | status_t Parcel::readUniqueFileDescriptorVector(std::optional<std::vector<base::unique_fd>>* val) const { return readData(val); } |
| 989 | status_t Parcel::readUniqueFileDescriptorVector(std::unique_ptr<std::vector<base::unique_fd>>* val) const { return readData(val); } |
| 990 | status_t Parcel::readUniqueFileDescriptorVector(std::vector<base::unique_fd>* val) const { return readData(val); } |
| 991 | |
| 992 | status_t Parcel::readStrongBinderVector(std::optional<std::vector<sp<IBinder>>>* val) const { return readData(val); } |
| 993 | status_t Parcel::readStrongBinderVector(std::unique_ptr<std::vector<sp<IBinder>>>* val) const { return readData(val); } |
| 994 | status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const { return readData(val); } |
| 995 | |
| 996 | status_t Parcel::readParcelable(Parcelable* parcelable) const { return readData(parcelable); } |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 997 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 998 | status_t Parcel::writeInt32(int32_t val) |
| 999 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1000 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1001 | } |
Dan Stoza | 41a0f2f | 2014-12-01 10:01:10 -0800 | [diff] [blame] | 1002 | |
| 1003 | status_t Parcel::writeUint32(uint32_t val) |
| 1004 | { |
| 1005 | return writeAligned(val); |
| 1006 | } |
| 1007 | |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 1008 | status_t Parcel::writeInt32Array(size_t len, const int32_t *val) { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1009 | if (len > INT32_MAX) { |
| 1010 | // don't accept size_t values which may have come from an |
| 1011 | // inadvertent conversion from a negative int. |
| 1012 | return BAD_VALUE; |
| 1013 | } |
| 1014 | |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 1015 | if (!val) { |
Chad Brubaker | e59cb43 | 2015-06-30 14:03:55 -0700 | [diff] [blame] | 1016 | return writeInt32(-1); |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 1017 | } |
Chad Brubaker | e59cb43 | 2015-06-30 14:03:55 -0700 | [diff] [blame] | 1018 | status_t ret = writeInt32(static_cast<uint32_t>(len)); |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 1019 | if (ret == NO_ERROR) { |
| 1020 | ret = write(val, len * sizeof(*val)); |
| 1021 | } |
| 1022 | return ret; |
| 1023 | } |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 1024 | status_t Parcel::writeByteArray(size_t len, const uint8_t *val) { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1025 | if (len > INT32_MAX) { |
| 1026 | // don't accept size_t values which may have come from an |
| 1027 | // inadvertent conversion from a negative int. |
| 1028 | return BAD_VALUE; |
| 1029 | } |
| 1030 | |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 1031 | if (!val) { |
Chad Brubaker | e59cb43 | 2015-06-30 14:03:55 -0700 | [diff] [blame] | 1032 | return writeInt32(-1); |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 1033 | } |
Chad Brubaker | e59cb43 | 2015-06-30 14:03:55 -0700 | [diff] [blame] | 1034 | status_t ret = writeInt32(static_cast<uint32_t>(len)); |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 1035 | if (ret == NO_ERROR) { |
| 1036 | ret = write(val, len * sizeof(*val)); |
| 1037 | } |
| 1038 | return ret; |
| 1039 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1040 | |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 1041 | status_t Parcel::writeBool(bool val) |
| 1042 | { |
| 1043 | return writeInt32(int32_t(val)); |
| 1044 | } |
| 1045 | |
| 1046 | status_t Parcel::writeChar(char16_t val) |
| 1047 | { |
| 1048 | return writeInt32(int32_t(val)); |
| 1049 | } |
| 1050 | |
| 1051 | status_t Parcel::writeByte(int8_t val) |
| 1052 | { |
| 1053 | return writeInt32(int32_t(val)); |
| 1054 | } |
| 1055 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1056 | status_t Parcel::writeInt64(int64_t val) |
| 1057 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1058 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Ronghua Wu | 2d13afd | 2015-03-16 11:11:07 -0700 | [diff] [blame] | 1061 | status_t Parcel::writeUint64(uint64_t val) |
| 1062 | { |
| 1063 | return writeAligned(val); |
| 1064 | } |
| 1065 | |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1066 | status_t Parcel::writePointer(uintptr_t val) |
| 1067 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1068 | return writeAligned<binder_uintptr_t>(val); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1071 | status_t Parcel::writeFloat(float val) |
| 1072 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1073 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1076 | #if defined(__mips__) && defined(__mips_hard_float) |
| 1077 | |
| 1078 | status_t Parcel::writeDouble(double val) |
| 1079 | { |
| 1080 | union { |
| 1081 | double d; |
| 1082 | unsigned long long ll; |
| 1083 | } u; |
| 1084 | u.d = val; |
| 1085 | return writeAligned(u.ll); |
| 1086 | } |
| 1087 | |
| 1088 | #else |
| 1089 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1090 | status_t Parcel::writeDouble(double val) |
| 1091 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1092 | return writeAligned(val); |
| 1093 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1094 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1095 | #endif |
| 1096 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1097 | status_t Parcel::writeCString(const char* str) |
| 1098 | { |
| 1099 | return write(str, strlen(str)+1); |
| 1100 | } |
| 1101 | |
| 1102 | status_t Parcel::writeString8(const String8& str) |
| 1103 | { |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1104 | return writeString8(str.string(), str.size()); |
| 1105 | } |
| 1106 | |
| 1107 | status_t Parcel::writeString8(const char* str, size_t len) |
| 1108 | { |
| 1109 | if (str == nullptr) return writeInt32(-1); |
| 1110 | |
Jeff Sharkey | 1822090 | 2020-11-05 08:36:20 -0700 | [diff] [blame] | 1111 | // NOTE: Keep this logic in sync with android_os_Parcel.cpp |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1112 | status_t err = writeInt32(len); |
| 1113 | if (err == NO_ERROR) { |
| 1114 | uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char)); |
| 1115 | if (data) { |
| 1116 | memcpy(data, str, len); |
| 1117 | *reinterpret_cast<char*>(data+len) = 0; |
| 1118 | return NO_ERROR; |
| 1119 | } |
| 1120 | err = mError; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1121 | } |
| 1122 | return err; |
| 1123 | } |
| 1124 | |
| 1125 | status_t Parcel::writeString16(const String16& str) |
| 1126 | { |
| 1127 | return writeString16(str.string(), str.size()); |
| 1128 | } |
| 1129 | |
| 1130 | status_t Parcel::writeString16(const char16_t* str, size_t len) |
| 1131 | { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1132 | if (str == nullptr) return writeInt32(-1); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1133 | |
Jeff Sharkey | 1822090 | 2020-11-05 08:36:20 -0700 | [diff] [blame] | 1134 | // NOTE: Keep this logic in sync with android_os_Parcel.cpp |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1135 | status_t err = writeInt32(len); |
| 1136 | if (err == NO_ERROR) { |
| 1137 | len *= sizeof(char16_t); |
| 1138 | uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); |
| 1139 | if (data) { |
| 1140 | memcpy(data, str, len); |
| 1141 | *reinterpret_cast<char16_t*>(data+len) = 0; |
| 1142 | return NO_ERROR; |
| 1143 | } |
| 1144 | err = mError; |
| 1145 | } |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
| 1149 | status_t Parcel::writeStrongBinder(const sp<IBinder>& val) |
| 1150 | { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 1151 | return flattenBinder(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Casey Dahlin | eb8e15f | 2015-11-03 13:50:37 -0800 | [diff] [blame] | 1154 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame] | 1155 | status_t Parcel::writeRawNullableParcelable(const Parcelable* parcelable) { |
| 1156 | if (!parcelable) { |
| 1157 | return writeInt32(0); |
| 1158 | } |
| 1159 | |
| 1160 | return writeParcelable(*parcelable); |
| 1161 | } |
| 1162 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1163 | status_t Parcel::writeNativeHandle(const native_handle* handle) |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1164 | { |
Mathias Agopian | 1d0a95b | 2009-07-31 16:12:13 -0700 | [diff] [blame] | 1165 | if (!handle || handle->version != sizeof(native_handle)) |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1166 | return BAD_TYPE; |
| 1167 | |
| 1168 | status_t err; |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1169 | err = writeInt32(handle->numFds); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1170 | if (err != NO_ERROR) return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1172 | err = writeInt32(handle->numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1173 | if (err != NO_ERROR) return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1175 | for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) |
| 1176 | err = writeDupFileDescriptor(handle->data[i]); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | |
| 1178 | if (err != NO_ERROR) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1179 | ALOGD("write native handle, write dup fd failed"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | return err; |
| 1181 | } |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1182 | err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1183 | return err; |
| 1184 | } |
| 1185 | |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 1186 | status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1187 | { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1188 | if (isForRpc()) { |
| 1189 | ALOGE("Cannot write file descriptor to remote binder."); |
| 1190 | return BAD_TYPE; |
| 1191 | } |
| 1192 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1193 | flat_binder_object obj; |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 1194 | obj.hdr.type = BINDER_TYPE_FD; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1195 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 1196 | obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */ |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1197 | obj.handle = fd; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1198 | obj.cookie = takeOwnership ? 1 : 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1199 | return writeObject(obj, true); |
| 1200 | } |
| 1201 | |
| 1202 | status_t Parcel::writeDupFileDescriptor(int fd) |
| 1203 | { |
Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 1204 | int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
Jeff Brown | d341c71 | 2011-11-04 20:19:33 -0700 | [diff] [blame] | 1205 | if (dupFd < 0) { |
| 1206 | return -errno; |
| 1207 | } |
| 1208 | status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1209 | if (err != OK) { |
Jeff Brown | d341c71 | 2011-11-04 20:19:33 -0700 | [diff] [blame] | 1210 | close(dupFd); |
| 1211 | } |
| 1212 | return err; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 1215 | status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership) |
| 1216 | { |
| 1217 | writeInt32(0); |
| 1218 | return writeFileDescriptor(fd, takeOwnership); |
| 1219 | } |
| 1220 | |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1221 | status_t Parcel::writeDupParcelFileDescriptor(int fd) |
| 1222 | { |
| 1223 | int dupFd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 1224 | if (dupFd < 0) { |
| 1225 | return -errno; |
| 1226 | } |
| 1227 | status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/); |
| 1228 | if (err != OK) { |
| 1229 | close(dupFd); |
| 1230 | } |
| 1231 | return err; |
| 1232 | } |
| 1233 | |
Christopher Wiley | 2cf1995 | 2016-04-11 11:09:37 -0700 | [diff] [blame] | 1234 | status_t Parcel::writeUniqueFileDescriptor(const base::unique_fd& fd) { |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1235 | return writeDupFileDescriptor(fd.get()); |
| 1236 | } |
| 1237 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1238 | status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob) |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1239 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1240 | if (len > INT32_MAX) { |
| 1241 | // don't accept size_t values which may have come from an |
| 1242 | // inadvertent conversion from a negative int. |
| 1243 | return BAD_VALUE; |
| 1244 | } |
| 1245 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1246 | status_t status; |
| 1247 | if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1248 | ALOGV("writeBlob: write in place"); |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1249 | status = writeInt32(BLOB_INPLACE); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1250 | if (status) return status; |
| 1251 | |
| 1252 | void* ptr = writeInplace(len); |
| 1253 | if (!ptr) return NO_MEMORY; |
| 1254 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1255 | outBlob->init(-1, ptr, len, false); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1256 | return NO_ERROR; |
| 1257 | } |
| 1258 | |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1259 | ALOGV("writeBlob: write to ashmem"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1260 | int fd = ashmem_create_region("Parcel Blob", len); |
| 1261 | if (fd < 0) return NO_MEMORY; |
| 1262 | |
| 1263 | int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
| 1264 | if (result < 0) { |
Jeff Brown | ec4e006 | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 1265 | status = result; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1266 | } else { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1267 | void* ptr = ::mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1268 | if (ptr == MAP_FAILED) { |
| 1269 | status = -errno; |
| 1270 | } else { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1271 | if (!mutableCopy) { |
| 1272 | result = ashmem_set_prot_region(fd, PROT_READ); |
| 1273 | } |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1274 | if (result < 0) { |
Jeff Brown | ec4e006 | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 1275 | status = result; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1276 | } else { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1277 | status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1278 | if (!status) { |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 1279 | status = writeFileDescriptor(fd, true /*takeOwnership*/); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1280 | if (!status) { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1281 | outBlob->init(fd, ptr, len, mutableCopy); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1282 | return NO_ERROR; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | ::munmap(ptr, len); |
| 1288 | } |
| 1289 | ::close(fd); |
| 1290 | return status; |
| 1291 | } |
| 1292 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1293 | status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd) |
| 1294 | { |
| 1295 | // Must match up with what's done in writeBlob. |
| 1296 | if (!mAllowFds) return FDS_NOT_ALLOWED; |
| 1297 | status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE); |
| 1298 | if (status) return status; |
| 1299 | return writeDupFileDescriptor(fd); |
| 1300 | } |
| 1301 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 1302 | status_t Parcel::write(const FlattenableHelperInterface& val) |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1303 | { |
| 1304 | status_t err; |
| 1305 | |
| 1306 | // size if needed |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 1307 | const size_t len = val.getFlattenedSize(); |
| 1308 | const size_t fd_count = val.getFdCount(); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1309 | |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 1310 | if ((len > INT32_MAX) || (fd_count >= gMaxFds)) { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1311 | // don't accept size_t values which may have come from an |
| 1312 | // inadvertent conversion from a negative int. |
| 1313 | return BAD_VALUE; |
| 1314 | } |
| 1315 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1316 | err = this->writeInt32(len); |
| 1317 | if (err) return err; |
| 1318 | |
| 1319 | err = this->writeInt32(fd_count); |
| 1320 | if (err) return err; |
| 1321 | |
| 1322 | // payload |
Martijn Coenen | f854238 | 2018-04-04 11:46:56 +0200 | [diff] [blame] | 1323 | void* const buf = this->writeInplace(len); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1324 | if (buf == nullptr) |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1325 | return BAD_VALUE; |
| 1326 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1327 | int* fds = nullptr; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1328 | if (fd_count) { |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 1329 | fds = new (std::nothrow) int[fd_count]; |
| 1330 | if (fds == nullptr) { |
| 1331 | ALOGE("write: failed to allocate requested %zu fds", fd_count); |
| 1332 | return BAD_VALUE; |
| 1333 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | err = val.flatten(buf, len, fds, fd_count); |
| 1337 | for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { |
| 1338 | err = this->writeDupFileDescriptor( fds[i] ); |
| 1339 | } |
| 1340 | |
| 1341 | if (fd_count) { |
| 1342 | delete [] fds; |
| 1343 | } |
| 1344 | |
| 1345 | return err; |
| 1346 | } |
| 1347 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1348 | status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) |
| 1349 | { |
| 1350 | const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; |
| 1351 | const bool enoughObjects = mObjectsSize < mObjectsCapacity; |
| 1352 | if (enoughData && enoughObjects) { |
| 1353 | restart_write: |
| 1354 | *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1355 | |
Christopher Tate | 98e67d3 | 2015-06-03 18:44:15 -0700 | [diff] [blame] | 1356 | // remember if it's a file descriptor |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 1357 | if (val.hdr.type == BINDER_TYPE_FD) { |
Christopher Tate | 98e67d3 | 2015-06-03 18:44:15 -0700 | [diff] [blame] | 1358 | if (!mAllowFds) { |
| 1359 | // fail before modifying our object index |
| 1360 | return FDS_NOT_ALLOWED; |
| 1361 | } |
| 1362 | mHasFds = mFdsKnown = true; |
| 1363 | } |
| 1364 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1365 | // Need to write meta-data? |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1366 | if (nullMetaData || val.binder != 0) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1367 | mObjects[mObjectsSize] = mDataPos; |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 1368 | acquire_object(ProcessState::self(), val, this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1369 | mObjectsSize++; |
| 1370 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1371 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1372 | return finishWrite(sizeof(flat_binder_object)); |
| 1373 | } |
| 1374 | |
| 1375 | if (!enoughData) { |
| 1376 | const status_t err = growData(sizeof(val)); |
| 1377 | if (err != NO_ERROR) return err; |
| 1378 | } |
| 1379 | if (!enoughObjects) { |
Martijn Coenen | 93fe518 | 2020-01-22 10:46:25 +0100 | [diff] [blame] | 1380 | if (mObjectsSize > SIZE_MAX - 2) return NO_MEMORY; // overflow |
| 1381 | if ((mObjectsSize + 2) > SIZE_MAX / 3) return NO_MEMORY; // overflow |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1382 | size_t newSize = ((mObjectsSize+2)*3)/2; |
Martijn Coenen | 93fe518 | 2020-01-22 10:46:25 +0100 | [diff] [blame] | 1383 | if (newSize > SIZE_MAX / sizeof(binder_size_t)) return NO_MEMORY; // overflow |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1384 | binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1385 | if (objects == nullptr) return NO_MEMORY; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1386 | mObjects = objects; |
| 1387 | mObjectsCapacity = newSize; |
| 1388 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1389 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1390 | goto restart_write; |
| 1391 | } |
| 1392 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1393 | status_t Parcel::writeNoException() |
| 1394 | { |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 1395 | binder::Status status; |
| 1396 | return status.writeToParcel(this); |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1399 | status_t Parcel::validateReadData(size_t upperBound) const |
| 1400 | { |
| 1401 | // Don't allow non-object reads on object data |
| 1402 | if (mObjectsSorted || mObjectsSize <= 1) { |
| 1403 | data_sorted: |
| 1404 | // Expect to check only against the next object |
| 1405 | if (mNextObjectHint < mObjectsSize && upperBound > mObjects[mNextObjectHint]) { |
| 1406 | // For some reason the current read position is greater than the next object |
| 1407 | // hint. Iterate until we find the right object |
| 1408 | size_t nextObject = mNextObjectHint; |
| 1409 | do { |
| 1410 | if (mDataPos < mObjects[nextObject] + sizeof(flat_binder_object)) { |
| 1411 | // Requested info overlaps with an object |
| 1412 | ALOGE("Attempt to read from protected data in Parcel %p", this); |
| 1413 | return PERMISSION_DENIED; |
| 1414 | } |
| 1415 | nextObject++; |
| 1416 | } while (nextObject < mObjectsSize && upperBound > mObjects[nextObject]); |
| 1417 | mNextObjectHint = nextObject; |
| 1418 | } |
| 1419 | return NO_ERROR; |
| 1420 | } |
| 1421 | // Quickly determine if mObjects is sorted. |
| 1422 | binder_size_t* currObj = mObjects + mObjectsSize - 1; |
| 1423 | binder_size_t* prevObj = currObj; |
| 1424 | while (currObj > mObjects) { |
| 1425 | prevObj--; |
| 1426 | if(*prevObj > *currObj) { |
| 1427 | goto data_unsorted; |
| 1428 | } |
| 1429 | currObj--; |
| 1430 | } |
| 1431 | mObjectsSorted = true; |
| 1432 | goto data_sorted; |
| 1433 | |
| 1434 | data_unsorted: |
| 1435 | // Insertion Sort mObjects |
| 1436 | // Great for mostly sorted lists. If randomly sorted or reverse ordered mObjects become common, |
| 1437 | // switch to std::sort(mObjects, mObjects + mObjectsSize); |
| 1438 | for (binder_size_t* iter0 = mObjects + 1; iter0 < mObjects + mObjectsSize; iter0++) { |
| 1439 | binder_size_t temp = *iter0; |
| 1440 | binder_size_t* iter1 = iter0 - 1; |
| 1441 | while (iter1 >= mObjects && *iter1 > temp) { |
| 1442 | *(iter1 + 1) = *iter1; |
| 1443 | iter1--; |
| 1444 | } |
| 1445 | *(iter1 + 1) = temp; |
| 1446 | } |
| 1447 | mNextObjectHint = 0; |
| 1448 | mObjectsSorted = true; |
| 1449 | goto data_sorted; |
| 1450 | } |
| 1451 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1452 | status_t Parcel::read(void* outData, size_t len) const |
| 1453 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1454 | if (len > INT32_MAX) { |
| 1455 | // don't accept size_t values which may have come from an |
| 1456 | // inadvertent conversion from a negative int. |
| 1457 | return BAD_VALUE; |
| 1458 | } |
| 1459 | |
| 1460 | if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize |
| 1461 | && len <= pad_size(len)) { |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1462 | if (mObjectsSize > 0) { |
| 1463 | status_t err = validateReadData(mDataPos + pad_size(len)); |
Michael Wachenschwanz | a17f022 | 2018-04-17 16:52:40 -0700 | [diff] [blame] | 1464 | if(err != NO_ERROR) { |
| 1465 | // Still increment the data position by the expected length |
| 1466 | mDataPos += pad_size(len); |
| 1467 | ALOGV("read Setting data pos of %p to %zu", this, mDataPos); |
| 1468 | return err; |
| 1469 | } |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1470 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1471 | memcpy(outData, mData+mDataPos, len); |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1472 | mDataPos += pad_size(len); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1473 | ALOGV("read Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1474 | return NO_ERROR; |
| 1475 | } |
| 1476 | return NOT_ENOUGH_DATA; |
| 1477 | } |
| 1478 | |
| 1479 | const void* Parcel::readInplace(size_t len) const |
| 1480 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1481 | if (len > INT32_MAX) { |
| 1482 | // don't accept size_t values which may have come from an |
| 1483 | // inadvertent conversion from a negative int. |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1484 | return nullptr; |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize |
| 1488 | && len <= pad_size(len)) { |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1489 | if (mObjectsSize > 0) { |
| 1490 | status_t err = validateReadData(mDataPos + pad_size(len)); |
Michael Wachenschwanz | a17f022 | 2018-04-17 16:52:40 -0700 | [diff] [blame] | 1491 | if(err != NO_ERROR) { |
| 1492 | // Still increment the data position by the expected length |
| 1493 | mDataPos += pad_size(len); |
| 1494 | ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1495 | return nullptr; |
Michael Wachenschwanz | a17f022 | 2018-04-17 16:52:40 -0700 | [diff] [blame] | 1496 | } |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1499 | const void* data = mData+mDataPos; |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1500 | mDataPos += pad_size(len); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1501 | ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1502 | return data; |
| 1503 | } |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1504 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Steven Moreland | d4f73fb | 2021-05-14 19:50:52 +0000 | [diff] [blame] | 1507 | status_t Parcel::readOutVectorSizeWithCheck(size_t elmSize, int32_t* size) const { |
| 1508 | if (status_t status = readInt32(size); status != OK) return status; |
| 1509 | if (*size < 0) return OK; // may be null, client to handle |
| 1510 | |
| 1511 | LOG_ALWAYS_FATAL_IF(elmSize > INT32_MAX, "Cannot have element as big as %zu", elmSize); |
| 1512 | |
| 1513 | // approximation, can't know max element size (e.g. if it makes heap |
| 1514 | // allocations) |
| 1515 | static_assert(sizeof(int) == sizeof(int32_t), "Android is LP64"); |
| 1516 | int32_t allocationSize; |
| 1517 | if (__builtin_smul_overflow(elmSize, *size, &allocationSize)) return NO_MEMORY; |
| 1518 | |
| 1519 | // High limit of 1MB since something this big could never be returned. Could |
| 1520 | // probably scope this down, but might impact very specific usecases. |
| 1521 | constexpr int32_t kMaxAllocationSize = 1 * 1000 * 1000; |
| 1522 | |
| 1523 | if (allocationSize >= kMaxAllocationSize) { |
| 1524 | return NO_MEMORY; |
| 1525 | } |
| 1526 | |
| 1527 | return OK; |
| 1528 | } |
| 1529 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1530 | template<class T> |
| 1531 | status_t Parcel::readAligned(T *pArg) const { |
Elliott Hughes | 42a9b94 | 2020-08-17 15:53:31 -0700 | [diff] [blame] | 1532 | static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1533 | |
| 1534 | if ((mDataPos+sizeof(T)) <= mDataSize) { |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1535 | if (mObjectsSize > 0) { |
| 1536 | status_t err = validateReadData(mDataPos + sizeof(T)); |
Michael Wachenschwanz | a17f022 | 2018-04-17 16:52:40 -0700 | [diff] [blame] | 1537 | if(err != NO_ERROR) { |
| 1538 | // Still increment the data position by the expected length |
| 1539 | mDataPos += sizeof(T); |
| 1540 | return err; |
| 1541 | } |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1544 | const void* data = mData+mDataPos; |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1545 | mDataPos += sizeof(T); |
| 1546 | *pArg = *reinterpret_cast<const T*>(data); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1547 | return NO_ERROR; |
| 1548 | } else { |
| 1549 | return NOT_ENOUGH_DATA; |
| 1550 | } |
| 1551 | } |
| 1552 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1553 | template<class T> |
| 1554 | T Parcel::readAligned() const { |
| 1555 | T result; |
| 1556 | if (readAligned(&result) != NO_ERROR) { |
| 1557 | result = 0; |
| 1558 | } |
| 1559 | |
| 1560 | return result; |
| 1561 | } |
| 1562 | |
| 1563 | template<class T> |
| 1564 | status_t Parcel::writeAligned(T val) { |
Elliott Hughes | 42a9b94 | 2020-08-17 15:53:31 -0700 | [diff] [blame] | 1565 | static_assert(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T)); |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1566 | |
| 1567 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 1568 | restart_write: |
| 1569 | *reinterpret_cast<T*>(mData+mDataPos) = val; |
| 1570 | return finishWrite(sizeof(val)); |
| 1571 | } |
| 1572 | |
| 1573 | status_t err = growData(sizeof(val)); |
| 1574 | if (err == NO_ERROR) goto restart_write; |
| 1575 | return err; |
| 1576 | } |
| 1577 | |
| 1578 | status_t Parcel::readInt32(int32_t *pArg) const |
| 1579 | { |
| 1580 | return readAligned(pArg); |
| 1581 | } |
| 1582 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1583 | int32_t Parcel::readInt32() const |
| 1584 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1585 | return readAligned<int32_t>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
Dan Stoza | 41a0f2f | 2014-12-01 10:01:10 -0800 | [diff] [blame] | 1588 | status_t Parcel::readUint32(uint32_t *pArg) const |
| 1589 | { |
| 1590 | return readAligned(pArg); |
| 1591 | } |
| 1592 | |
| 1593 | uint32_t Parcel::readUint32() const |
| 1594 | { |
| 1595 | return readAligned<uint32_t>(); |
| 1596 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1597 | |
| 1598 | status_t Parcel::readInt64(int64_t *pArg) const |
| 1599 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1600 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | int64_t Parcel::readInt64() const |
| 1605 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1606 | return readAligned<int64_t>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Ronghua Wu | 2d13afd | 2015-03-16 11:11:07 -0700 | [diff] [blame] | 1609 | status_t Parcel::readUint64(uint64_t *pArg) const |
| 1610 | { |
| 1611 | return readAligned(pArg); |
| 1612 | } |
| 1613 | |
| 1614 | uint64_t Parcel::readUint64() const |
| 1615 | { |
| 1616 | return readAligned<uint64_t>(); |
| 1617 | } |
| 1618 | |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1619 | status_t Parcel::readPointer(uintptr_t *pArg) const |
| 1620 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1621 | status_t ret; |
| 1622 | binder_uintptr_t ptr; |
| 1623 | ret = readAligned(&ptr); |
| 1624 | if (!ret) |
| 1625 | *pArg = ptr; |
| 1626 | return ret; |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | uintptr_t Parcel::readPointer() const |
| 1630 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1631 | return readAligned<binder_uintptr_t>(); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1635 | status_t Parcel::readFloat(float *pArg) const |
| 1636 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1637 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | |
| 1641 | float Parcel::readFloat() const |
| 1642 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1643 | return readAligned<float>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1646 | #if defined(__mips__) && defined(__mips_hard_float) |
| 1647 | |
| 1648 | status_t Parcel::readDouble(double *pArg) const |
| 1649 | { |
| 1650 | union { |
| 1651 | double d; |
| 1652 | unsigned long long ll; |
| 1653 | } u; |
Narayan Kamath | 2c68d38 | 2014-06-04 15:04:29 +0100 | [diff] [blame] | 1654 | u.d = 0; |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1655 | status_t status; |
| 1656 | status = readAligned(&u.ll); |
| 1657 | *pArg = u.d; |
| 1658 | return status; |
| 1659 | } |
| 1660 | |
| 1661 | double Parcel::readDouble() const |
| 1662 | { |
| 1663 | union { |
| 1664 | double d; |
| 1665 | unsigned long long ll; |
| 1666 | } u; |
| 1667 | u.ll = readAligned<unsigned long long>(); |
| 1668 | return u.d; |
| 1669 | } |
| 1670 | |
| 1671 | #else |
| 1672 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1673 | status_t Parcel::readDouble(double *pArg) const |
| 1674 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1675 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1678 | double Parcel::readDouble() const |
| 1679 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1680 | return readAligned<double>(); |
| 1681 | } |
| 1682 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1683 | #endif |
| 1684 | |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 1685 | status_t Parcel::readBool(bool *pArg) const |
| 1686 | { |
Manoj Gupta | 6eb6205 | 2017-07-12 10:29:15 -0700 | [diff] [blame] | 1687 | int32_t tmp = 0; |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 1688 | status_t ret = readInt32(&tmp); |
| 1689 | *pArg = (tmp != 0); |
| 1690 | return ret; |
| 1691 | } |
| 1692 | |
| 1693 | bool Parcel::readBool() const |
| 1694 | { |
| 1695 | return readInt32() != 0; |
| 1696 | } |
| 1697 | |
| 1698 | status_t Parcel::readChar(char16_t *pArg) const |
| 1699 | { |
Manoj Gupta | 6eb6205 | 2017-07-12 10:29:15 -0700 | [diff] [blame] | 1700 | int32_t tmp = 0; |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 1701 | status_t ret = readInt32(&tmp); |
| 1702 | *pArg = char16_t(tmp); |
| 1703 | return ret; |
| 1704 | } |
| 1705 | |
| 1706 | char16_t Parcel::readChar() const |
| 1707 | { |
| 1708 | return char16_t(readInt32()); |
| 1709 | } |
| 1710 | |
| 1711 | status_t Parcel::readByte(int8_t *pArg) const |
| 1712 | { |
Manoj Gupta | 6eb6205 | 2017-07-12 10:29:15 -0700 | [diff] [blame] | 1713 | int32_t tmp = 0; |
Casey Dahlin | d6848f5 | 2015-10-15 15:44:59 -0700 | [diff] [blame] | 1714 | status_t ret = readInt32(&tmp); |
| 1715 | *pArg = int8_t(tmp); |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | int8_t Parcel::readByte() const |
| 1720 | { |
| 1721 | return int8_t(readInt32()); |
| 1722 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1723 | |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 1724 | status_t Parcel::readUtf8FromUtf16(std::string* str) const { |
| 1725 | size_t utf16Size = 0; |
| 1726 | const char16_t* src = readString16Inplace(&utf16Size); |
| 1727 | if (!src) { |
| 1728 | return UNEXPECTED_NULL; |
| 1729 | } |
| 1730 | |
| 1731 | // Save ourselves the trouble, we're done. |
| 1732 | if (utf16Size == 0u) { |
| 1733 | str->clear(); |
| 1734 | return NO_ERROR; |
| 1735 | } |
| 1736 | |
Sergio Giro | 9b39ebe | 2016-06-28 18:19:33 +0100 | [diff] [blame] | 1737 | // Allow for closing '\0' |
| 1738 | ssize_t utf8Size = utf16_to_utf8_length(src, utf16Size) + 1; |
| 1739 | if (utf8Size < 1) { |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 1740 | return BAD_VALUE; |
| 1741 | } |
| 1742 | // Note that while it is probably safe to assume string::resize keeps a |
Sergio Giro | 9b39ebe | 2016-06-28 18:19:33 +0100 | [diff] [blame] | 1743 | // spare byte around for the trailing null, we still pass the size including the trailing null |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 1744 | str->resize(utf8Size); |
Sergio Giro | 9b39ebe | 2016-06-28 18:19:33 +0100 | [diff] [blame] | 1745 | utf16_to_utf8(src, utf16Size, &((*str)[0]), utf8Size); |
| 1746 | str->resize(utf8Size - 1); |
Christopher Wiley | 9a5e32f | 2016-01-28 16:56:53 -0800 | [diff] [blame] | 1747 | return NO_ERROR; |
| 1748 | } |
| 1749 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1750 | const char* Parcel::readCString() const |
| 1751 | { |
Steven Moreland | d0d4b58 | 2019-05-17 13:14:06 -0700 | [diff] [blame] | 1752 | if (mDataPos < mDataSize) { |
| 1753 | const size_t avail = mDataSize-mDataPos; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1754 | const char* str = reinterpret_cast<const char*>(mData+mDataPos); |
| 1755 | // is the string's trailing NUL within the parcel's valid bounds? |
| 1756 | const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); |
| 1757 | if (eos) { |
| 1758 | const size_t len = eos - str; |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 1759 | mDataPos += pad_size(len+1); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1760 | ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1761 | return str; |
| 1762 | } |
| 1763 | } |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1764 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | String8 Parcel::readString8() const |
| 1768 | { |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1769 | size_t len; |
| 1770 | const char* str = readString8Inplace(&len); |
| 1771 | if (str) return String8(str, len); |
| 1772 | ALOGE("Reading a NULL string not supported here."); |
| 1773 | return String8(); |
Roshan Pius | 87b64d2 | 2016-07-18 12:51:02 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | status_t Parcel::readString8(String8* pArg) const |
| 1777 | { |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1778 | size_t len; |
| 1779 | const char* str = readString8Inplace(&len); |
| 1780 | if (str) { |
| 1781 | pArg->setTo(str, len); |
| 1782 | return 0; |
| 1783 | } else { |
Roshan Pius | 87b64d2 | 2016-07-18 12:51:02 -0700 | [diff] [blame] | 1784 | *pArg = String8(); |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1785 | return UNEXPECTED_NULL; |
Roshan Pius | 87b64d2 | 2016-07-18 12:51:02 -0700 | [diff] [blame] | 1786 | } |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | const char* Parcel::readString8Inplace(size_t* outLen) const |
| 1790 | { |
| 1791 | int32_t size = readInt32(); |
| 1792 | // watch for potential int overflow from size+1 |
| 1793 | if (size >= 0 && size < INT32_MAX) { |
| 1794 | *outLen = size; |
| 1795 | const char* str = (const char*)readInplace(size+1); |
Steven Moreland | 61d0f84 | 2020-12-04 21:13:03 +0000 | [diff] [blame] | 1796 | if (str != nullptr) { |
| 1797 | if (str[size] == '\0') { |
| 1798 | return str; |
| 1799 | } |
| 1800 | android_errorWriteLog(0x534e4554, "172655291"); |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1801 | } |
Roshan Pius | 87b64d2 | 2016-07-18 12:51:02 -0700 | [diff] [blame] | 1802 | } |
Jeff Sharkey | 2f8bdb5 | 2020-04-19 21:41:26 -0600 | [diff] [blame] | 1803 | *outLen = 0; |
| 1804 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | String16 Parcel::readString16() const |
| 1808 | { |
| 1809 | size_t len; |
| 1810 | const char16_t* str = readString16Inplace(&len); |
| 1811 | if (str) return String16(str, len); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1812 | ALOGE("Reading a NULL string not supported here."); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1813 | return String16(); |
| 1814 | } |
| 1815 | |
Casey Dahlin | b987262 | 2015-11-25 15:09:45 -0800 | [diff] [blame] | 1816 | |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 1817 | status_t Parcel::readString16(String16* pArg) const |
| 1818 | { |
| 1819 | size_t len; |
| 1820 | const char16_t* str = readString16Inplace(&len); |
| 1821 | if (str) { |
Casey Dahlin | 1515ea1 | 2015-10-20 16:26:23 -0700 | [diff] [blame] | 1822 | pArg->setTo(str, len); |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 1823 | return 0; |
| 1824 | } else { |
| 1825 | *pArg = String16(); |
Christopher Wiley | 4db672d | 2015-11-10 09:44:30 -0800 | [diff] [blame] | 1826 | return UNEXPECTED_NULL; |
Casey Dahlin | 451ff58 | 2015-10-19 18:12:18 -0700 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1830 | const char16_t* Parcel::readString16Inplace(size_t* outLen) const |
| 1831 | { |
| 1832 | int32_t size = readInt32(); |
| 1833 | // watch for potential int overflow from size+1 |
| 1834 | if (size >= 0 && size < INT32_MAX) { |
| 1835 | *outLen = size; |
| 1836 | const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); |
Steven Moreland | 61d0f84 | 2020-12-04 21:13:03 +0000 | [diff] [blame] | 1837 | if (str != nullptr) { |
| 1838 | if (str[size] == u'\0') { |
| 1839 | return str; |
| 1840 | } |
| 1841 | android_errorWriteLog(0x534e4554, "172655291"); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | *outLen = 0; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1845 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 1848 | status_t Parcel::readStrongBinder(sp<IBinder>* val) const |
| 1849 | { |
Christopher Wiley | 35d77ca | 2016-03-08 10:49:51 -0800 | [diff] [blame] | 1850 | status_t status = readNullableStrongBinder(val); |
| 1851 | if (status == OK && !val->get()) { |
| 1852 | status = UNEXPECTED_NULL; |
| 1853 | } |
| 1854 | return status; |
| 1855 | } |
| 1856 | |
| 1857 | status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const |
| 1858 | { |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 1859 | return unflattenBinder(val); |
Casey Dahlin | f0c1377 | 2015-10-27 18:33:56 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1862 | sp<IBinder> Parcel::readStrongBinder() const |
| 1863 | { |
| 1864 | sp<IBinder> val; |
Christopher Wiley | 35d77ca | 2016-03-08 10:49:51 -0800 | [diff] [blame] | 1865 | // Note that a lot of code in Android reads binders by hand with this |
| 1866 | // method, and that code has historically been ok with getting nullptr |
| 1867 | // back (while ignoring error codes). |
| 1868 | readNullableStrongBinder(&val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1869 | return val; |
| 1870 | } |
| 1871 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1872 | int32_t Parcel::readExceptionCode() const |
| 1873 | { |
Christopher Wiley | 09eb749 | 2015-11-09 15:06:15 -0800 | [diff] [blame] | 1874 | binder::Status status; |
| 1875 | status.readFromParcel(*this); |
| 1876 | return status.exceptionCode(); |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1877 | } |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1878 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1879 | native_handle* Parcel::readNativeHandle() const |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1880 | { |
| 1881 | int numFds, numInts; |
| 1882 | status_t err; |
| 1883 | err = readInt32(&numFds); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1884 | if (err != NO_ERROR) return nullptr; |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1885 | err = readInt32(&numInts); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1886 | if (err != NO_ERROR) return nullptr; |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1887 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1888 | native_handle* h = native_handle_create(numFds, numInts); |
Adam Lesinski | eaac99a | 2015-05-12 17:35:48 -0700 | [diff] [blame] | 1889 | if (!h) { |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1890 | return nullptr; |
Adam Lesinski | eaac99a | 2015-05-12 17:35:48 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1893 | for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { |
Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 1894 | h->data[i] = fcntl(readFileDescriptor(), F_DUPFD_CLOEXEC, 0); |
Marco Nelissen | 1de7966 | 2016-04-26 08:44:09 -0700 | [diff] [blame] | 1895 | if (h->data[i] < 0) { |
| 1896 | for (int j = 0; j < i; j++) { |
| 1897 | close(h->data[j]); |
| 1898 | } |
| 1899 | native_handle_delete(h); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1900 | return nullptr; |
Marco Nelissen | 1de7966 | 2016-04-26 08:44:09 -0700 | [diff] [blame] | 1901 | } |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1902 | } |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1903 | err = read(h->data + numFds, sizeof(int)*numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1904 | if (err != NO_ERROR) { |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1905 | native_handle_close(h); |
| 1906 | native_handle_delete(h); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 1907 | h = nullptr; |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1908 | } |
| 1909 | return h; |
| 1910 | } |
| 1911 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1912 | int Parcel::readFileDescriptor() const |
| 1913 | { |
| 1914 | const flat_binder_object* flat = readObject(true); |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1915 | |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 1916 | if (flat && flat->hdr.type == BINDER_TYPE_FD) { |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1917 | return flat->handle; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1918 | } |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1919 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1920 | return BAD_TYPE; |
| 1921 | } |
| 1922 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 1923 | int Parcel::readParcelFileDescriptor() const |
| 1924 | { |
| 1925 | int32_t hasComm = readInt32(); |
| 1926 | int fd = readFileDescriptor(); |
| 1927 | if (hasComm != 0) { |
Steven Moreland | b73806a | 2018-11-12 19:35:47 -0800 | [diff] [blame] | 1928 | // detach (owned by the binder driver) |
| 1929 | int comm = readFileDescriptor(); |
| 1930 | |
| 1931 | // warning: this must be kept in sync with: |
| 1932 | // frameworks/base/core/java/android/os/ParcelFileDescriptor.java |
| 1933 | enum ParcelFileDescriptorStatus { |
| 1934 | DETACHED = 2, |
| 1935 | }; |
| 1936 | |
| 1937 | #if BYTE_ORDER == BIG_ENDIAN |
| 1938 | const int32_t message = ParcelFileDescriptorStatus::DETACHED; |
| 1939 | #endif |
| 1940 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 1941 | const int32_t message = __builtin_bswap32(ParcelFileDescriptorStatus::DETACHED); |
| 1942 | #endif |
| 1943 | |
| 1944 | ssize_t written = TEMP_FAILURE_RETRY( |
| 1945 | ::write(comm, &message, sizeof(message))); |
| 1946 | |
Krzysztof Kosiński | a840689 | 2021-02-02 17:59:43 -0800 | [diff] [blame] | 1947 | if (written != sizeof(message)) { |
Steven Moreland | b73806a | 2018-11-12 19:35:47 -0800 | [diff] [blame] | 1948 | ALOGW("Failed to detach ParcelFileDescriptor written: %zd err: %s", |
| 1949 | written, strerror(errno)); |
| 1950 | return BAD_TYPE; |
| 1951 | } |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 1952 | } |
| 1953 | return fd; |
| 1954 | } |
| 1955 | |
Christopher Wiley | 2cf1995 | 2016-04-11 11:09:37 -0700 | [diff] [blame] | 1956 | status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1957 | { |
| 1958 | int got = readFileDescriptor(); |
| 1959 | |
| 1960 | if (got == BAD_TYPE) { |
| 1961 | return BAD_TYPE; |
| 1962 | } |
| 1963 | |
Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 1964 | val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0)); |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1965 | |
| 1966 | if (val->get() < 0) { |
| 1967 | return BAD_VALUE; |
| 1968 | } |
| 1969 | |
| 1970 | return OK; |
| 1971 | } |
| 1972 | |
Ryo Hashimoto | bf55189 | 2018-05-31 16:58:35 +0900 | [diff] [blame] | 1973 | status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const |
| 1974 | { |
| 1975 | int got = readParcelFileDescriptor(); |
| 1976 | |
| 1977 | if (got == BAD_TYPE) { |
| 1978 | return BAD_TYPE; |
| 1979 | } |
| 1980 | |
| 1981 | val->reset(fcntl(got, F_DUPFD_CLOEXEC, 0)); |
| 1982 | |
| 1983 | if (val->get() < 0) { |
| 1984 | return BAD_VALUE; |
| 1985 | } |
| 1986 | |
| 1987 | return OK; |
| 1988 | } |
Casey Dahlin | 06673e3 | 2015-11-23 13:24:23 -0800 | [diff] [blame] | 1989 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1990 | status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const |
| 1991 | { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1992 | int32_t blobType; |
| 1993 | status_t status = readInt32(&blobType); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1994 | if (status) return status; |
| 1995 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 1996 | if (blobType == BLOB_INPLACE) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1997 | ALOGV("readBlob: read in place"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1998 | const void* ptr = readInplace(len); |
| 1999 | if (!ptr) return BAD_VALUE; |
| 2000 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2001 | outBlob->init(-1, const_cast<void*>(ptr), len, false); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2002 | return NO_ERROR; |
| 2003 | } |
| 2004 | |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2005 | ALOGV("readBlob: read from ashmem"); |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2006 | bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2007 | int fd = readFileDescriptor(); |
| 2008 | if (fd == int(BAD_TYPE)) return BAD_VALUE; |
| 2009 | |
Jorim Jaggi | 150b4ef | 2018-07-13 11:18:30 +0000 | [diff] [blame] | 2010 | if (!ashmem_valid(fd)) { |
| 2011 | ALOGE("invalid fd"); |
| 2012 | return BAD_VALUE; |
| 2013 | } |
Marco Nelissen | 7a96ec4 | 2018-06-06 07:37:46 -0700 | [diff] [blame] | 2014 | int size = ashmem_get_size_region(fd); |
| 2015 | if (size < 0 || size_t(size) < len) { |
Jorim Jaggi | 150b4ef | 2018-07-13 11:18:30 +0000 | [diff] [blame] | 2016 | ALOGE("request size %zu does not match fd size %d", len, size); |
Marco Nelissen | 7a96ec4 | 2018-06-06 07:37:46 -0700 | [diff] [blame] | 2017 | return BAD_VALUE; |
| 2018 | } |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2019 | void* ptr = ::mmap(nullptr, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ, |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2020 | MAP_SHARED, fd, 0); |
Narayan Kamath | 9ea0975 | 2014-10-08 17:35:45 +0100 | [diff] [blame] | 2021 | if (ptr == MAP_FAILED) return NO_MEMORY; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2022 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2023 | outBlob->init(fd, ptr, len, isMutable); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2024 | return NO_ERROR; |
| 2025 | } |
| 2026 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 2027 | status_t Parcel::read(FlattenableHelperInterface& val) const |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2028 | { |
| 2029 | // size |
| 2030 | const size_t len = this->readInt32(); |
| 2031 | const size_t fd_count = this->readInt32(); |
| 2032 | |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 2033 | if ((len > INT32_MAX) || (fd_count >= gMaxFds)) { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 2034 | // don't accept size_t values which may have come from an |
| 2035 | // inadvertent conversion from a negative int. |
| 2036 | return BAD_VALUE; |
| 2037 | } |
| 2038 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2039 | // payload |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 2040 | void const* const buf = this->readInplace(pad_size(len)); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2041 | if (buf == nullptr) |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2042 | return BAD_VALUE; |
| 2043 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2044 | int* fds = nullptr; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2045 | if (fd_count) { |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 2046 | fds = new (std::nothrow) int[fd_count]; |
| 2047 | if (fds == nullptr) { |
| 2048 | ALOGE("read: failed to allocate requested %zu fds", fd_count); |
| 2049 | return BAD_VALUE; |
| 2050 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | status_t err = NO_ERROR; |
| 2054 | for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { |
Fabien Sanglard | d84ff31 | 2016-10-21 10:58:26 -0700 | [diff] [blame] | 2055 | int fd = this->readFileDescriptor(); |
Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 2056 | if (fd < 0 || ((fds[i] = fcntl(fd, F_DUPFD_CLOEXEC, 0)) < 0)) { |
Jun Jiang | abf8a2c | 2014-04-29 14:22:10 +0800 | [diff] [blame] | 2057 | err = BAD_VALUE; |
Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 2058 | ALOGE("fcntl(F_DUPFD_CLOEXEC) failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s", |
Fabien Sanglard | d84ff31 | 2016-10-21 10:58:26 -0700 | [diff] [blame] | 2059 | i, fds[i], fd_count, strerror(fd < 0 ? -fd : errno)); |
| 2060 | // Close all the file descriptors that were dup-ed. |
| 2061 | for (size_t j=0; j<i ;j++) { |
| 2062 | close(fds[j]); |
| 2063 | } |
Jun Jiang | abf8a2c | 2014-04-29 14:22:10 +0800 | [diff] [blame] | 2064 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | if (err == NO_ERROR) { |
| 2068 | err = val.unflatten(buf, len, fds, fd_count); |
| 2069 | } |
| 2070 | |
| 2071 | if (fd_count) { |
| 2072 | delete [] fds; |
| 2073 | } |
| 2074 | |
| 2075 | return err; |
| 2076 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2077 | const flat_binder_object* Parcel::readObject(bool nullMetaData) const |
| 2078 | { |
| 2079 | const size_t DPOS = mDataPos; |
| 2080 | if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { |
| 2081 | const flat_binder_object* obj |
| 2082 | = reinterpret_cast<const flat_binder_object*>(mData+DPOS); |
| 2083 | mDataPos = DPOS + sizeof(flat_binder_object); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2084 | if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) { |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 2085 | // When transferring a NULL object, we don't write it into |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2086 | // the object list, so we don't want to check for it when |
| 2087 | // reading. |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2088 | ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2089 | return obj; |
| 2090 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2091 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2092 | // Ensure that this object is valid... |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2093 | binder_size_t* const OBJS = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2094 | const size_t N = mObjectsSize; |
| 2095 | size_t opos = mNextObjectHint; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2096 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2097 | if (N > 0) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2098 | ALOGV("Parcel %p looking for obj at %zu, hint=%zu", |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2099 | this, DPOS, opos); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2100 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2101 | // Start at the current hint position, looking for an object at |
| 2102 | // the current data position. |
| 2103 | if (opos < N) { |
| 2104 | while (opos < (N-1) && OBJS[opos] < DPOS) { |
| 2105 | opos++; |
| 2106 | } |
| 2107 | } else { |
| 2108 | opos = N-1; |
| 2109 | } |
| 2110 | if (OBJS[opos] == DPOS) { |
| 2111 | // Found it! |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2112 | ALOGV("Parcel %p found obj %zu at index %zu with forward search", |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2113 | this, DPOS, opos); |
| 2114 | mNextObjectHint = opos+1; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2115 | ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2116 | return obj; |
| 2117 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2118 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2119 | // Look backwards for it... |
| 2120 | while (opos > 0 && OBJS[opos] > DPOS) { |
| 2121 | opos--; |
| 2122 | } |
| 2123 | if (OBJS[opos] == DPOS) { |
| 2124 | // Found it! |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2125 | ALOGV("Parcel %p found obj %zu at index %zu with backward search", |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2126 | this, DPOS, opos); |
| 2127 | mNextObjectHint = opos+1; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2128 | ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2129 | return obj; |
| 2130 | } |
| 2131 | } |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 2132 | ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list", |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2133 | this, DPOS); |
| 2134 | } |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2135 | return nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | void Parcel::closeFileDescriptors() |
| 2139 | { |
| 2140 | size_t i = mObjectsSize; |
| 2141 | if (i > 0) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2142 | //ALOGI("Closing file descriptors for %zu objects...", i); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2143 | } |
| 2144 | while (i > 0) { |
| 2145 | i--; |
| 2146 | const flat_binder_object* flat |
| 2147 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 2148 | if (flat->hdr.type == BINDER_TYPE_FD) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2149 | //ALOGI("Closing fd: %ld", flat->handle); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2150 | close(flat->handle); |
| 2151 | } |
| 2152 | } |
| 2153 | } |
| 2154 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2155 | uintptr_t Parcel::ipcData() const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2156 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2157 | return reinterpret_cast<uintptr_t>(mData); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | size_t Parcel::ipcDataSize() const |
| 2161 | { |
| 2162 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 2163 | } |
| 2164 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2165 | uintptr_t Parcel::ipcObjects() const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2166 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2167 | return reinterpret_cast<uintptr_t>(mObjects); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | size_t Parcel::ipcObjectsCount() const |
| 2171 | { |
| 2172 | return mObjectsSize; |
| 2173 | } |
| 2174 | |
| 2175 | void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 2176 | const binder_size_t* objects, size_t objectsCount, release_func relFunc) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2177 | { |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 2178 | // this code uses 'mOwner == nullptr' to understand whether it owns memory |
| 2179 | LOG_ALWAYS_FATAL_IF(relFunc == nullptr, "must provide cleanup function"); |
| 2180 | |
Steven Moreland | ceed9bb | 2020-12-17 01:01:06 +0000 | [diff] [blame] | 2181 | freeData(); |
| 2182 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2183 | mData = const_cast<uint8_t*>(data); |
| 2184 | mDataSize = mDataCapacity = dataSize; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2185 | mObjects = const_cast<binder_size_t*>(objects); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2186 | mObjectsSize = mObjectsCapacity = objectsCount; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2187 | mOwner = relFunc; |
Steven Moreland | ceed9bb | 2020-12-17 01:01:06 +0000 | [diff] [blame] | 2188 | |
| 2189 | binder_size_t minOffset = 0; |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 2190 | for (size_t i = 0; i < mObjectsSize; i++) { |
Arve Hjønnevåg | 6f28611 | 2014-02-19 20:42:13 -0800 | [diff] [blame] | 2191 | binder_size_t offset = mObjects[i]; |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 2192 | if (offset < minOffset) { |
Dan Albert | 3bdc5b8 | 2014-11-20 11:50:23 -0800 | [diff] [blame] | 2193 | ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n", |
Arve Hjønnevåg | 6f28611 | 2014-02-19 20:42:13 -0800 | [diff] [blame] | 2194 | __func__, (uint64_t)offset, (uint64_t)minOffset); |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 2195 | mObjectsSize = 0; |
| 2196 | break; |
| 2197 | } |
Martijn Coenen | 82c7531 | 2019-07-24 15:18:30 +0200 | [diff] [blame] | 2198 | const flat_binder_object* flat |
| 2199 | = reinterpret_cast<const flat_binder_object*>(mData + offset); |
| 2200 | uint32_t type = flat->hdr.type; |
| 2201 | if (!(type == BINDER_TYPE_BINDER || type == BINDER_TYPE_HANDLE || |
| 2202 | type == BINDER_TYPE_FD)) { |
| 2203 | // We should never receive other types (eg BINDER_TYPE_FDA) as long as we don't support |
| 2204 | // them in libbinder. If we do receive them, it probably means a kernel bug; try to |
| 2205 | // recover gracefully by clearing out the objects, and releasing the objects we do |
| 2206 | // know about. |
| 2207 | android_errorWriteLog(0x534e4554, "135930648"); |
| 2208 | ALOGE("%s: unsupported type object (%" PRIu32 ") at offset %" PRIu64 "\n", |
| 2209 | __func__, type, (uint64_t)offset); |
| 2210 | releaseObjects(); |
| 2211 | mObjectsSize = 0; |
| 2212 | break; |
| 2213 | } |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 2214 | minOffset = offset + sizeof(flat_binder_object); |
| 2215 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2216 | scanForFds(); |
| 2217 | } |
| 2218 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 2219 | void Parcel::print(TextOutput& to, uint32_t /*flags*/) const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2220 | { |
| 2221 | to << "Parcel("; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2222 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2223 | if (errorCheck() != NO_ERROR) { |
| 2224 | const status_t err = errorCheck(); |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 2225 | to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\""; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2226 | } else if (dataSize() > 0) { |
| 2227 | const uint8_t* DATA = data(); |
| 2228 | to << indent << HexDump(DATA, dataSize()) << dedent; |
Steven Moreland | 8bd0135 | 2019-07-15 16:36:14 -0700 | [diff] [blame] | 2229 | const binder_size_t* OBJS = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2230 | const size_t N = objectsCount(); |
| 2231 | for (size_t i=0; i<N; i++) { |
| 2232 | const flat_binder_object* flat |
| 2233 | = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); |
| 2234 | to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 2235 | << TypeCode(flat->hdr.type & 0x7f7f7f00) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2236 | << " = " << flat->binder; |
| 2237 | } |
| 2238 | } else { |
| 2239 | to << "NULL"; |
| 2240 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2241 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2242 | to << ")"; |
| 2243 | } |
| 2244 | |
| 2245 | void Parcel::releaseObjects() |
| 2246 | { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2247 | size_t i = mObjectsSize; |
Martijn Coenen | 69390d4 | 2018-10-22 15:18:10 +0200 | [diff] [blame] | 2248 | if (i == 0) { |
| 2249 | return; |
| 2250 | } |
| 2251 | sp<ProcessState> proc(ProcessState::self()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2252 | uint8_t* const data = mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2253 | binder_size_t* const objects = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2254 | while (i > 0) { |
| 2255 | i--; |
| 2256 | const flat_binder_object* flat |
| 2257 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 2258 | release_object(proc, *flat, this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | void Parcel::acquireObjects() |
| 2263 | { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2264 | size_t i = mObjectsSize; |
Martijn Coenen | 69390d4 | 2018-10-22 15:18:10 +0200 | [diff] [blame] | 2265 | if (i == 0) { |
| 2266 | return; |
| 2267 | } |
| 2268 | const sp<ProcessState> proc(ProcessState::self()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2269 | uint8_t* const data = mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2270 | binder_size_t* const objects = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2271 | while (i > 0) { |
| 2272 | i--; |
| 2273 | const flat_binder_object* flat |
| 2274 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 2275 | acquire_object(proc, *flat, this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | void Parcel::freeData() |
| 2280 | { |
| 2281 | freeDataNoInit(); |
| 2282 | initState(); |
| 2283 | } |
| 2284 | |
| 2285 | void Parcel::freeDataNoInit() |
| 2286 | { |
| 2287 | if (mOwner) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2288 | LOG_ALLOC("Parcel %p: freeing other owner data", this); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2289 | //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 2290 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2291 | } else { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2292 | LOG_ALLOC("Parcel %p: freeing allocated data", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2293 | releaseObjects(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2294 | if (mData) { |
| 2295 | LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity); |
Jeff Sharkey | 8994c18 | 2020-09-11 12:07:10 -0600 | [diff] [blame] | 2296 | gParcelGlobalAllocSize -= mDataCapacity; |
| 2297 | gParcelGlobalAllocCount--; |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 2298 | if (mDeallocZero) { |
| 2299 | zeroMemory(mData, mDataSize); |
| 2300 | } |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2301 | free(mData); |
| 2302 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2303 | if (mObjects) free(mObjects); |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | status_t Parcel::growData(size_t len) |
| 2308 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 2309 | if (len > INT32_MAX) { |
| 2310 | // don't accept size_t values which may have come from an |
| 2311 | // inadvertent conversion from a negative int. |
| 2312 | return BAD_VALUE; |
| 2313 | } |
| 2314 | |
Martijn Coenen | 93fe518 | 2020-01-22 10:46:25 +0100 | [diff] [blame] | 2315 | if (len > SIZE_MAX - mDataSize) return NO_MEMORY; // overflow |
| 2316 | if (mDataSize + len > SIZE_MAX / 3) return NO_MEMORY; // overflow |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2317 | size_t newSize = ((mDataSize+len)*3)/2; |
| 2318 | return (newSize <= mDataSize) |
| 2319 | ? (status_t) NO_MEMORY |
Steven Moreland | 042ae82 | 2020-05-27 17:45:17 +0000 | [diff] [blame] | 2320 | : continueWrite(std::max(newSize, (size_t) 128)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 2323 | static uint8_t* reallocZeroFree(uint8_t* data, size_t oldCapacity, size_t newCapacity, bool zero) { |
| 2324 | if (!zero) { |
| 2325 | return (uint8_t*)realloc(data, newCapacity); |
| 2326 | } |
| 2327 | uint8_t* newData = (uint8_t*)malloc(newCapacity); |
| 2328 | if (!newData) { |
| 2329 | return nullptr; |
| 2330 | } |
| 2331 | |
| 2332 | memcpy(newData, data, std::min(oldCapacity, newCapacity)); |
| 2333 | zeroMemory(data, oldCapacity); |
| 2334 | free(data); |
| 2335 | return newData; |
| 2336 | } |
| 2337 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2338 | status_t Parcel::restartWrite(size_t desired) |
| 2339 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 2340 | if (desired > INT32_MAX) { |
| 2341 | // don't accept size_t values which may have come from an |
| 2342 | // inadvertent conversion from a negative int. |
| 2343 | return BAD_VALUE; |
| 2344 | } |
| 2345 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2346 | if (mOwner) { |
| 2347 | freeData(); |
| 2348 | return continueWrite(desired); |
| 2349 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2350 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 2351 | uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2352 | if (!data && desired > mDataCapacity) { |
| 2353 | mError = NO_MEMORY; |
| 2354 | return NO_MEMORY; |
| 2355 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2356 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2357 | releaseObjects(); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2358 | |
Devin Moore | 4a0a55e | 2020-06-04 13:23:10 -0700 | [diff] [blame] | 2359 | if (data || desired == 0) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2360 | LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired); |
Jeff Sharkey | 8994c18 | 2020-09-11 12:07:10 -0600 | [diff] [blame] | 2361 | if (mDataCapacity > desired) { |
| 2362 | gParcelGlobalAllocSize -= (mDataCapacity - desired); |
| 2363 | } else { |
| 2364 | gParcelGlobalAllocSize += (desired - mDataCapacity); |
| 2365 | } |
| 2366 | |
Colin Cross | 83ec65e | 2015-12-08 17:15:50 -0800 | [diff] [blame] | 2367 | if (!mData) { |
| 2368 | gParcelGlobalAllocCount++; |
| 2369 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2370 | mData = data; |
| 2371 | mDataCapacity = desired; |
| 2372 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2373 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2374 | mDataSize = mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2375 | ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize); |
| 2376 | ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos); |
| 2377 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2378 | free(mObjects); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2379 | mObjects = nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2380 | mObjectsSize = mObjectsCapacity = 0; |
| 2381 | mNextObjectHint = 0; |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 2382 | mObjectsSorted = false; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2383 | mHasFds = false; |
| 2384 | mFdsKnown = true; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 2385 | mAllowFds = true; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2386 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2387 | return NO_ERROR; |
| 2388 | } |
| 2389 | |
| 2390 | status_t Parcel::continueWrite(size_t desired) |
| 2391 | { |
Nick Kralevich | b6b1423 | 2015-04-02 09:36:02 -0700 | [diff] [blame] | 2392 | if (desired > INT32_MAX) { |
| 2393 | // don't accept size_t values which may have come from an |
| 2394 | // inadvertent conversion from a negative int. |
| 2395 | return BAD_VALUE; |
| 2396 | } |
| 2397 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2398 | // If shrinking, first adjust for any objects that appear |
| 2399 | // after the new data size. |
| 2400 | size_t objectsSize = mObjectsSize; |
| 2401 | if (desired < mDataSize) { |
| 2402 | if (desired == 0) { |
| 2403 | objectsSize = 0; |
| 2404 | } else { |
| 2405 | while (objectsSize > 0) { |
Michael Wachenschwanz | a654163 | 2017-05-18 22:08:32 +0000 | [diff] [blame] | 2406 | if (mObjects[objectsSize-1] < desired) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2407 | break; |
| 2408 | objectsSize--; |
| 2409 | } |
| 2410 | } |
| 2411 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2412 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2413 | if (mOwner) { |
| 2414 | // If the size is going to zero, just release the owner's data. |
| 2415 | if (desired == 0) { |
| 2416 | freeData(); |
| 2417 | return NO_ERROR; |
| 2418 | } |
| 2419 | |
| 2420 | // If there is a different owner, we need to take |
| 2421 | // posession. |
| 2422 | uint8_t* data = (uint8_t*)malloc(desired); |
| 2423 | if (!data) { |
| 2424 | mError = NO_MEMORY; |
| 2425 | return NO_MEMORY; |
| 2426 | } |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2427 | binder_size_t* objects = nullptr; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2428 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2429 | if (objectsSize) { |
Nick Kralevich | e9881a3 | 2015-04-28 16:21:30 -0700 | [diff] [blame] | 2430 | objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2431 | if (!objects) { |
Hyejin Kim | 3f727c0 | 2013-03-09 11:28:54 +0900 | [diff] [blame] | 2432 | free(data); |
| 2433 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2434 | mError = NO_MEMORY; |
| 2435 | return NO_MEMORY; |
| 2436 | } |
| 2437 | |
| 2438 | // Little hack to only acquire references on objects |
| 2439 | // we will be keeping. |
| 2440 | size_t oldObjectsSize = mObjectsSize; |
| 2441 | mObjectsSize = objectsSize; |
| 2442 | acquireObjects(); |
| 2443 | mObjectsSize = oldObjectsSize; |
| 2444 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2445 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2446 | if (mData) { |
| 2447 | memcpy(data, mData, mDataSize < desired ? mDataSize : desired); |
| 2448 | } |
| 2449 | if (objects && mObjects) { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 2450 | memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2451 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2452 | //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); |
Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 2453 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2454 | mOwner = nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2455 | |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2456 | LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2457 | gParcelGlobalAllocSize += desired; |
| 2458 | gParcelGlobalAllocCount++; |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2459 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2460 | mData = data; |
| 2461 | mObjects = objects; |
| 2462 | mDataSize = (mDataSize < desired) ? mDataSize : desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2463 | ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2464 | mDataCapacity = desired; |
| 2465 | mObjectsSize = mObjectsCapacity = objectsSize; |
| 2466 | mNextObjectHint = 0; |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 2467 | mObjectsSorted = false; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2468 | |
| 2469 | } else if (mData) { |
| 2470 | if (objectsSize < mObjectsSize) { |
| 2471 | // Need to release refs on any objects we are dropping. |
| 2472 | const sp<ProcessState> proc(ProcessState::self()); |
| 2473 | for (size_t i=objectsSize; i<mObjectsSize; i++) { |
| 2474 | const flat_binder_object* flat |
| 2475 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
Christopher Ferris | dbaa22a | 2017-07-27 10:38:45 -0700 | [diff] [blame] | 2476 | if (flat->hdr.type == BINDER_TYPE_FD) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2477 | // will need to rescan because we may have lopped off the only FDs |
| 2478 | mFdsKnown = false; |
| 2479 | } |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 2480 | release_object(proc, *flat, this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2481 | } |
Michael Wachenschwanz | 6af27a8 | 2019-06-03 17:24:51 -0700 | [diff] [blame] | 2482 | |
| 2483 | if (objectsSize == 0) { |
| 2484 | free(mObjects); |
| 2485 | mObjects = nullptr; |
Michael Wachenschwanz | daf29a6 | 2019-10-15 11:49:22 -0700 | [diff] [blame] | 2486 | mObjectsCapacity = 0; |
Michael Wachenschwanz | 6af27a8 | 2019-06-03 17:24:51 -0700 | [diff] [blame] | 2487 | } else { |
| 2488 | binder_size_t* objects = |
| 2489 | (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t)); |
| 2490 | if (objects) { |
| 2491 | mObjects = objects; |
Michael Wachenschwanz | daf29a6 | 2019-10-15 11:49:22 -0700 | [diff] [blame] | 2492 | mObjectsCapacity = objectsSize; |
Michael Wachenschwanz | 6af27a8 | 2019-06-03 17:24:51 -0700 | [diff] [blame] | 2493 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2494 | } |
| 2495 | mObjectsSize = objectsSize; |
| 2496 | mNextObjectHint = 0; |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 2497 | mObjectsSorted = false; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | // We own the data, so we can just do a realloc(). |
| 2501 | if (desired > mDataCapacity) { |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 2502 | uint8_t* data = reallocZeroFree(mData, mDataCapacity, desired, mDeallocZero); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2503 | if (data) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2504 | LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity, |
| 2505 | desired); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2506 | gParcelGlobalAllocSize += desired; |
| 2507 | gParcelGlobalAllocSize -= mDataCapacity; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2508 | mData = data; |
| 2509 | mDataCapacity = desired; |
Ganesh Mahendran | ade8989 | 2017-09-28 16:56:03 +0800 | [diff] [blame] | 2510 | } else { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2511 | mError = NO_MEMORY; |
| 2512 | return NO_MEMORY; |
| 2513 | } |
| 2514 | } else { |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 2515 | if (mDataSize > desired) { |
| 2516 | mDataSize = desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2517 | ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 2518 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2519 | if (mDataPos > desired) { |
| 2520 | mDataPos = desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2521 | ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2522 | } |
| 2523 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2524 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2525 | } else { |
| 2526 | // This is the first data. Easy! |
| 2527 | uint8_t* data = (uint8_t*)malloc(desired); |
| 2528 | if (!data) { |
| 2529 | mError = NO_MEMORY; |
| 2530 | return NO_MEMORY; |
| 2531 | } |
Hyejin Kim | 3f727c0 | 2013-03-09 11:28:54 +0900 | [diff] [blame] | 2532 | |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2533 | if(!(mDataCapacity == 0 && mObjects == nullptr |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2534 | && mObjectsCapacity == 0)) { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 2535 | ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2536 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2537 | |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2538 | LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2539 | gParcelGlobalAllocSize += desired; |
| 2540 | gParcelGlobalAllocCount++; |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2541 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2542 | mData = data; |
| 2543 | mDataSize = mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2544 | ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); |
| 2545 | ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2546 | mDataCapacity = desired; |
| 2547 | } |
| 2548 | |
| 2549 | return NO_ERROR; |
| 2550 | } |
| 2551 | |
| 2552 | void Parcel::initState() |
| 2553 | { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 2554 | LOG_ALLOC("Parcel %p: initState", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2555 | mError = NO_ERROR; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2556 | mData = nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2557 | mDataSize = 0; |
| 2558 | mDataCapacity = 0; |
| 2559 | mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 2560 | ALOGV("initState Setting data size of %p to %zu", this, mDataSize); |
| 2561 | ALOGV("initState Setting data pos of %p to %zu", this, mDataPos); |
Steven Moreland | c993906 | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 2562 | mSession = nullptr; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2563 | mObjects = nullptr; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2564 | mObjectsSize = 0; |
| 2565 | mObjectsCapacity = 0; |
| 2566 | mNextObjectHint = 0; |
Michael Wachenschwanz | c517681 | 2017-11-17 18:25:05 -0800 | [diff] [blame] | 2567 | mObjectsSorted = false; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2568 | mHasFds = false; |
| 2569 | mFdsKnown = true; |
Steven Moreland | 6e5a775 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 2570 | mAllowFds = true; |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 2571 | mDeallocZero = false; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2572 | mOwner = nullptr; |
Olivier Gaillard | dc848a0 | 2019-01-30 17:10:44 +0000 | [diff] [blame] | 2573 | mWorkSourceRequestHeaderPosition = 0; |
| 2574 | mRequestHeaderPresent = false; |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 2575 | |
| 2576 | // racing multiple init leads only to multiple identical write |
| 2577 | if (gMaxFds == 0) { |
| 2578 | struct rlimit result; |
| 2579 | if (!getrlimit(RLIMIT_NOFILE, &result)) { |
| 2580 | gMaxFds = (size_t)result.rlim_cur; |
Christopher Tate | bf14e94 | 2016-03-25 14:16:24 -0700 | [diff] [blame] | 2581 | //ALOGI("parcel fd limit set to %zu", gMaxFds); |
Christopher Tate | e4e0ae8 | 2016-03-24 16:03:44 -0700 | [diff] [blame] | 2582 | } else { |
| 2583 | ALOGW("Unable to getrlimit: %s", strerror(errno)); |
| 2584 | gMaxFds = 1024; |
| 2585 | } |
| 2586 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Bernardo Rufino | bbbd88d | 2021-10-15 14:54:30 +0100 | [diff] [blame] | 2589 | void Parcel::scanForFds() const { |
| 2590 | status_t status = hasFileDescriptorsInRange(0, dataSize(), &mHasFds); |
| 2591 | ALOGE_IF(status != NO_ERROR, "Error %d calling hasFileDescriptorsInRange()", status); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2592 | mFdsKnown = true; |
| 2593 | } |
| 2594 | |
Dan Sandler | aa5c234 | 2015-04-10 10:08:45 -0400 | [diff] [blame] | 2595 | size_t Parcel::getBlobAshmemSize() const |
| 2596 | { |
Adrian Roos | 6bb3114 | 2015-10-22 16:46:12 -0700 | [diff] [blame] | 2597 | // This used to return the size of all blobs that were written to ashmem, now we're returning |
| 2598 | // the ashmem currently referenced by this Parcel, which should be equivalent. |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 2599 | // TODO(b/202029388): Remove method once ABI can be changed. |
| 2600 | return getOpenAshmemSize(); |
Dan Sandler | aa5c234 | 2015-04-10 10:08:45 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
Adrian Roos | cbf3726 | 2015-10-22 16:12:53 -0700 | [diff] [blame] | 2603 | size_t Parcel::getOpenAshmemSize() const |
| 2604 | { |
Steven Moreland | c673f1f | 2021-10-07 18:23:35 -0700 | [diff] [blame] | 2605 | size_t openAshmemSize = 0; |
| 2606 | for (size_t i = 0; i < mObjectsSize; i++) { |
| 2607 | const flat_binder_object* flat = |
| 2608 | reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); |
| 2609 | |
| 2610 | // cookie is compared against zero for historical reasons |
| 2611 | // > obj.cookie = takeOwnership ? 1 : 0; |
| 2612 | if (flat->hdr.type == BINDER_TYPE_FD && flat->cookie != 0 && ashmem_valid(flat->handle)) { |
| 2613 | int size = ashmem_get_size_region(flat->handle); |
| 2614 | if (__builtin_add_overflow(openAshmemSize, size, &openAshmemSize)) { |
| 2615 | ALOGE("Overflow when computing ashmem size."); |
| 2616 | return SIZE_MAX; |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | return openAshmemSize; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | // --- Parcel::Blob --- |
| 2624 | |
| 2625 | Parcel::Blob::Blob() : |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2626 | mFd(-1), mData(nullptr), mSize(0), mMutable(false) { |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | Parcel::Blob::~Blob() { |
| 2630 | release(); |
| 2631 | } |
| 2632 | |
| 2633 | void Parcel::Blob::release() { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2634 | if (mFd != -1 && mData) { |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2635 | ::munmap(mData, mSize); |
| 2636 | } |
| 2637 | clear(); |
| 2638 | } |
| 2639 | |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2640 | void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) { |
| 2641 | mFd = fd; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2642 | mData = data; |
| 2643 | mSize = size; |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2644 | mMutable = isMutable; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | void Parcel::Blob::clear() { |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2648 | mFd = -1; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 2649 | mData = nullptr; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2650 | mSize = 0; |
Jeff Brown | 13b1604 | 2014-11-11 16:44:25 -0800 | [diff] [blame] | 2651 | mMutable = false; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 2654 | } // namespace android |