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