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