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