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