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