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