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