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