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