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