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