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