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