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