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