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