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