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