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> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 26 | #include <binder/TextOutput.h> |
| 27 | |
Jun Jiang | abf8a2c | 2014-04-29 14:22:10 +0800 | [diff] [blame] | 28 | #include <errno.h> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 29 | #include <utils/Debug.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | #include <utils/String8.h> |
| 32 | #include <utils/String16.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | #include <utils/misc.h> |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 34 | #include <utils/Flattenable.h> |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 35 | #include <cutils/ashmem.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 37 | #include <private/binder/binder_module.h> |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 38 | #include <private/binder/Static.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 40 | #include <inttypes.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <stdint.h> |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 44 | #include <sys/mman.h> |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | |
| 46 | #ifndef INT32_MAX |
| 47 | #define INT32_MAX ((int32_t)(2147483647)) |
| 48 | #endif |
| 49 | |
| 50 | #define LOG_REFS(...) |
Steve Block | 9f76015 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 51 | //#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 52 | #define LOG_ALLOC(...) |
| 53 | //#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | // --------------------------------------------------------------------------- |
| 56 | |
| 57 | #define PAD_SIZE(s) (((s)+3)&~3) |
| 58 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 59 | // 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] | 60 | #define STRICT_MODE_PENALTY_GATHER (0x40 << 16) |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 61 | |
Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 62 | // Note: must be kept in sync with android/os/Parcel.java's EX_HAS_REPLY_HEADER |
| 63 | #define EX_HAS_REPLY_HEADER -128 |
| 64 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 65 | // Maximum size of a blob to transfer in-place. |
| 66 | static const size_t IN_PLACE_BLOB_LIMIT = 40 * 1024; |
| 67 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | // XXX This can be made public if we want to provide |
| 69 | // support for typed data. |
| 70 | struct small_flat_data |
| 71 | { |
| 72 | uint32_t type; |
| 73 | uint32_t data; |
| 74 | }; |
| 75 | |
| 76 | namespace android { |
| 77 | |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 78 | static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER; |
| 79 | static size_t gParcelGlobalAllocSize = 0; |
| 80 | static size_t gParcelGlobalAllocCount = 0; |
| 81 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | void acquire_object(const sp<ProcessState>& proc, |
| 83 | const flat_binder_object& obj, const void* who) |
| 84 | { |
| 85 | switch (obj.type) { |
| 86 | case BINDER_TYPE_BINDER: |
| 87 | if (obj.binder) { |
| 88 | 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] | 89 | reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | } |
| 91 | return; |
| 92 | case BINDER_TYPE_WEAK_BINDER: |
| 93 | if (obj.binder) |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 94 | reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | return; |
| 96 | case BINDER_TYPE_HANDLE: { |
| 97 | const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); |
| 98 | if (b != NULL) { |
| 99 | LOG_REFS("Parcel %p acquiring reference on remote %p", who, b.get()); |
| 100 | b->incStrong(who); |
| 101 | } |
| 102 | return; |
| 103 | } |
| 104 | case BINDER_TYPE_WEAK_HANDLE: { |
| 105 | const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); |
| 106 | if (b != NULL) b.get_refs()->incWeak(who); |
| 107 | return; |
| 108 | } |
| 109 | case BINDER_TYPE_FD: { |
| 110 | // intentionally blank -- nothing to do to acquire this, but we do |
| 111 | // recognize it as a legitimate object type. |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 116 | ALOGD("Invalid object type 0x%08x", obj.type); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void release_object(const sp<ProcessState>& proc, |
| 120 | const flat_binder_object& obj, const void* who) |
| 121 | { |
| 122 | switch (obj.type) { |
| 123 | case BINDER_TYPE_BINDER: |
| 124 | if (obj.binder) { |
| 125 | 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] | 126 | reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | } |
| 128 | return; |
| 129 | case BINDER_TYPE_WEAK_BINDER: |
| 130 | if (obj.binder) |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 131 | reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | return; |
| 133 | case BINDER_TYPE_HANDLE: { |
| 134 | const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle); |
| 135 | if (b != NULL) { |
| 136 | LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get()); |
| 137 | b->decStrong(who); |
| 138 | } |
| 139 | return; |
| 140 | } |
| 141 | case BINDER_TYPE_WEAK_HANDLE: { |
| 142 | const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle); |
| 143 | if (b != NULL) b.get_refs()->decWeak(who); |
| 144 | return; |
| 145 | } |
| 146 | case BINDER_TYPE_FD: { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 147 | if (obj.cookie != 0) close(obj.handle); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | } |
| 151 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 152 | ALOGE("Invalid object type 0x%08x", obj.type); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | inline static status_t finish_flatten_binder( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 156 | 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] | 157 | { |
| 158 | return out->writeObject(flat, false); |
| 159 | } |
| 160 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 161 | status_t flatten_binder(const sp<ProcessState>& /*proc*/, |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | const sp<IBinder>& binder, Parcel* out) |
| 163 | { |
| 164 | flat_binder_object obj; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 165 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 166 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
| 167 | if (binder != NULL) { |
| 168 | IBinder *local = binder->localBinder(); |
| 169 | if (!local) { |
| 170 | BpBinder *proxy = binder->remoteBinder(); |
| 171 | if (proxy == NULL) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 172 | ALOGE("null proxy"); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | } |
| 174 | const int32_t handle = proxy ? proxy->handle() : 0; |
| 175 | obj.type = BINDER_TYPE_HANDLE; |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 176 | 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] | 177 | obj.handle = handle; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 178 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | } else { |
| 180 | obj.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 181 | obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs()); |
| 182 | obj.cookie = reinterpret_cast<uintptr_t>(local); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | } |
| 184 | } else { |
| 185 | obj.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 186 | obj.binder = 0; |
| 187 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 189 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | return finish_flatten_binder(binder, obj, out); |
| 191 | } |
| 192 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 193 | status_t flatten_binder(const sp<ProcessState>& /*proc*/, |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | const wp<IBinder>& binder, Parcel* out) |
| 195 | { |
| 196 | flat_binder_object obj; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
| 199 | if (binder != NULL) { |
| 200 | sp<IBinder> real = binder.promote(); |
| 201 | if (real != NULL) { |
| 202 | IBinder *local = real->localBinder(); |
| 203 | if (!local) { |
| 204 | BpBinder *proxy = real->remoteBinder(); |
| 205 | if (proxy == NULL) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 206 | ALOGE("null proxy"); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 207 | } |
| 208 | const int32_t handle = proxy ? proxy->handle() : 0; |
| 209 | obj.type = BINDER_TYPE_WEAK_HANDLE; |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 210 | 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] | 211 | obj.handle = handle; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 212 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | } else { |
| 214 | obj.type = BINDER_TYPE_WEAK_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 215 | obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs()); |
| 216 | obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | } |
| 218 | return finish_flatten_binder(real, obj, out); |
| 219 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 220 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | // XXX How to deal? In order to flatten the given binder, |
| 222 | // we need to probe it for information, which requires a primary |
| 223 | // reference... but we don't have one. |
| 224 | // |
| 225 | // The OpenBinder implementation uses a dynamic_cast<> here, |
| 226 | // but we can't do that with the different reference counting |
| 227 | // implementation we are using. |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 228 | ALOGE("Unable to unflatten Binder weak reference!"); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | obj.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 230 | obj.binder = 0; |
| 231 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 232 | return finish_flatten_binder(NULL, obj, out); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 233 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | } else { |
| 235 | obj.type = BINDER_TYPE_BINDER; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 236 | obj.binder = 0; |
| 237 | obj.cookie = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | return finish_flatten_binder(NULL, obj, out); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | inline static status_t finish_unflatten_binder( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 243 | BpBinder* /*proxy*/, const flat_binder_object& /*flat*/, |
| 244 | const Parcel& /*in*/) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 245 | { |
| 246 | return NO_ERROR; |
| 247 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 248 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 249 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 250 | const Parcel& in, sp<IBinder>* out) |
| 251 | { |
| 252 | const flat_binder_object* flat = in.readObject(false); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 253 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | if (flat) { |
| 255 | switch (flat->type) { |
| 256 | case BINDER_TYPE_BINDER: |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 257 | *out = reinterpret_cast<IBinder*>(flat->cookie); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | return finish_unflatten_binder(NULL, *flat, in); |
| 259 | case BINDER_TYPE_HANDLE: |
| 260 | *out = proc->getStrongProxyForHandle(flat->handle); |
| 261 | return finish_unflatten_binder( |
| 262 | static_cast<BpBinder*>(out->get()), *flat, in); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 263 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | } |
| 265 | return BAD_TYPE; |
| 266 | } |
| 267 | |
| 268 | status_t unflatten_binder(const sp<ProcessState>& proc, |
| 269 | const Parcel& in, wp<IBinder>* out) |
| 270 | { |
| 271 | const flat_binder_object* flat = in.readObject(false); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 272 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | if (flat) { |
| 274 | switch (flat->type) { |
| 275 | case BINDER_TYPE_BINDER: |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 276 | *out = reinterpret_cast<IBinder*>(flat->cookie); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 277 | return finish_unflatten_binder(NULL, *flat, in); |
| 278 | case BINDER_TYPE_WEAK_BINDER: |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 279 | if (flat->binder != 0) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 280 | out->set_object_and_refs( |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 281 | reinterpret_cast<IBinder*>(flat->cookie), |
| 282 | reinterpret_cast<RefBase::weakref_type*>(flat->binder)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | } else { |
| 284 | *out = NULL; |
| 285 | } |
| 286 | return finish_unflatten_binder(NULL, *flat, in); |
| 287 | case BINDER_TYPE_HANDLE: |
| 288 | case BINDER_TYPE_WEAK_HANDLE: |
| 289 | *out = proc->getWeakProxyForHandle(flat->handle); |
| 290 | return finish_unflatten_binder( |
| 291 | static_cast<BpBinder*>(out->unsafe_get()), *flat, in); |
| 292 | } |
| 293 | } |
| 294 | return BAD_TYPE; |
| 295 | } |
| 296 | |
| 297 | // --------------------------------------------------------------------------- |
| 298 | |
| 299 | Parcel::Parcel() |
| 300 | { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 301 | LOG_ALLOC("Parcel %p: constructing", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 302 | initState(); |
| 303 | } |
| 304 | |
| 305 | Parcel::~Parcel() |
| 306 | { |
| 307 | freeDataNoInit(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 308 | LOG_ALLOC("Parcel %p: destroyed", this); |
| 309 | } |
| 310 | |
| 311 | size_t Parcel::getGlobalAllocSize() { |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 312 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
| 313 | size_t size = gParcelGlobalAllocSize; |
| 314 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
| 315 | return size; |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | size_t Parcel::getGlobalAllocCount() { |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 319 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
| 320 | size_t count = gParcelGlobalAllocCount; |
| 321 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
| 322 | return count; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | const uint8_t* Parcel::data() const |
| 326 | { |
| 327 | return mData; |
| 328 | } |
| 329 | |
| 330 | size_t Parcel::dataSize() const |
| 331 | { |
| 332 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 333 | } |
| 334 | |
| 335 | size_t Parcel::dataAvail() const |
| 336 | { |
| 337 | // TODO: decide what to do about the possibility that this can |
| 338 | // report an available-data size that exceeds a Java int's max |
| 339 | // positive value, causing havoc. Fortunately this will only |
| 340 | // happen if someone constructs a Parcel containing more than two |
| 341 | // gigabytes of data, which on typical phone hardware is simply |
| 342 | // not possible. |
| 343 | return dataSize() - dataPosition(); |
| 344 | } |
| 345 | |
| 346 | size_t Parcel::dataPosition() const |
| 347 | { |
| 348 | return mDataPos; |
| 349 | } |
| 350 | |
| 351 | size_t Parcel::dataCapacity() const |
| 352 | { |
| 353 | return mDataCapacity; |
| 354 | } |
| 355 | |
| 356 | status_t Parcel::setDataSize(size_t size) |
| 357 | { |
| 358 | status_t err; |
| 359 | err = continueWrite(size); |
| 360 | if (err == NO_ERROR) { |
| 361 | mDataSize = size; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 362 | 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] | 363 | } |
| 364 | return err; |
| 365 | } |
| 366 | |
| 367 | void Parcel::setDataPosition(size_t pos) const |
| 368 | { |
| 369 | mDataPos = pos; |
| 370 | mNextObjectHint = 0; |
| 371 | } |
| 372 | |
| 373 | status_t Parcel::setDataCapacity(size_t size) |
| 374 | { |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 375 | if (size > mDataCapacity) return continueWrite(size); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 376 | return NO_ERROR; |
| 377 | } |
| 378 | |
| 379 | status_t Parcel::setData(const uint8_t* buffer, size_t len) |
| 380 | { |
| 381 | status_t err = restartWrite(len); |
| 382 | if (err == NO_ERROR) { |
| 383 | memcpy(const_cast<uint8_t*>(data()), buffer, len); |
| 384 | mDataSize = len; |
| 385 | mFdsKnown = false; |
| 386 | } |
| 387 | return err; |
| 388 | } |
| 389 | |
Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 390 | 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] | 391 | { |
| 392 | const sp<ProcessState> proc(ProcessState::self()); |
| 393 | status_t err; |
Andreas Huber | 51faf46 | 2011-04-13 10:21:56 -0700 | [diff] [blame] | 394 | const uint8_t *data = parcel->mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 395 | const binder_size_t *objects = parcel->mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | size_t size = parcel->mObjectsSize; |
| 397 | int startPos = mDataPos; |
| 398 | int firstIndex = -1, lastIndex = -2; |
| 399 | |
| 400 | if (len == 0) { |
| 401 | return NO_ERROR; |
| 402 | } |
| 403 | |
| 404 | // range checks against the source parcel size |
| 405 | if ((offset > parcel->mDataSize) |
| 406 | || (len > parcel->mDataSize) |
| 407 | || (offset + len > parcel->mDataSize)) { |
| 408 | return BAD_VALUE; |
| 409 | } |
| 410 | |
| 411 | // Count objects in range |
| 412 | for (int i = 0; i < (int) size; i++) { |
| 413 | size_t off = objects[i]; |
| 414 | if ((off >= offset) && (off < offset + len)) { |
| 415 | if (firstIndex == -1) { |
| 416 | firstIndex = i; |
| 417 | } |
| 418 | lastIndex = i; |
| 419 | } |
| 420 | } |
| 421 | int numObjects = lastIndex - firstIndex + 1; |
| 422 | |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 423 | if ((mDataSize+len) > mDataCapacity) { |
| 424 | // grow data |
| 425 | err = growData(len); |
| 426 | if (err != NO_ERROR) { |
| 427 | return err; |
| 428 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // append data |
| 432 | memcpy(mData + mDataPos, data + offset, len); |
| 433 | mDataPos += len; |
| 434 | mDataSize += len; |
| 435 | |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 436 | err = NO_ERROR; |
| 437 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 438 | if (numObjects > 0) { |
| 439 | // grow objects |
| 440 | if (mObjectsCapacity < mObjectsSize + numObjects) { |
| 441 | int newSize = ((mObjectsSize + numObjects)*3)/2; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 442 | binder_size_t *objects = |
| 443 | (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t)); |
| 444 | if (objects == (binder_size_t*)0) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 445 | return NO_MEMORY; |
| 446 | } |
| 447 | mObjects = objects; |
| 448 | mObjectsCapacity = newSize; |
| 449 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 450 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 451 | // append and acquire objects |
| 452 | int idx = mObjectsSize; |
| 453 | for (int i = firstIndex; i <= lastIndex; i++) { |
| 454 | size_t off = objects[i] - offset + startPos; |
| 455 | mObjects[idx++] = off; |
| 456 | mObjectsSize++; |
| 457 | |
Dianne Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 458 | flat_binder_object* flat |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | = reinterpret_cast<flat_binder_object*>(mData + off); |
| 460 | acquire_object(proc, *flat, this); |
| 461 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 462 | if (flat->type == BINDER_TYPE_FD) { |
Dianne Hackborn | 8af0f82 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 463 | // If this is a file descriptor, we need to dup it so the |
| 464 | // new Parcel now owns its own fd, and can declare that we |
| 465 | // officially know we have fds. |
| 466 | flat->handle = dup(flat->handle); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 467 | flat->cookie = 1; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 468 | mHasFds = mFdsKnown = true; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 469 | if (!mAllowFds) { |
| 470 | err = FDS_NOT_ALLOWED; |
| 471 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 476 | return err; |
| 477 | } |
| 478 | |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 479 | bool Parcel::pushAllowFds(bool allowFds) |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 480 | { |
| 481 | const bool origValue = mAllowFds; |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 482 | if (!allowFds) { |
| 483 | mAllowFds = false; |
| 484 | } |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 485 | return origValue; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Dianne Hackborn | 7746cc3 | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 488 | void Parcel::restoreAllowFds(bool lastValue) |
| 489 | { |
| 490 | mAllowFds = lastValue; |
| 491 | } |
| 492 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | bool Parcel::hasFileDescriptors() const |
| 494 | { |
| 495 | if (!mFdsKnown) { |
| 496 | scanForFds(); |
| 497 | } |
| 498 | return mHasFds; |
| 499 | } |
| 500 | |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 501 | // Write RPC headers. (previously just the interface token) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 502 | status_t Parcel::writeInterfaceToken(const String16& interface) |
| 503 | { |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 504 | writeInt32(IPCThreadState::self()->getStrictModePolicy() | |
| 505 | STRICT_MODE_PENALTY_GATHER); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 506 | // currently the interface identification token is just its name as a string |
| 507 | return writeString16(interface); |
| 508 | } |
| 509 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 510 | bool Parcel::checkInterface(IBinder* binder) const |
| 511 | { |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 512 | return enforceInterface(binder->getInterfaceDescriptor()); |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 515 | bool Parcel::enforceInterface(const String16& interface, |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 516 | IPCThreadState* threadState) const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | { |
Brad Fitzpatrick | 70081a1 | 2010-07-27 09:49:11 -0700 | [diff] [blame] | 518 | int32_t strictPolicy = readInt32(); |
| 519 | if (threadState == NULL) { |
| 520 | threadState = IPCThreadState::self(); |
Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 521 | } |
Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 522 | if ((threadState->getLastTransactionBinderFlags() & |
| 523 | IBinder::FLAG_ONEWAY) != 0) { |
| 524 | // For one-way calls, the callee is running entirely |
| 525 | // disconnected from the caller, so disable StrictMode entirely. |
| 526 | // Not only does disk/network usage not impact the caller, but |
| 527 | // there's no way to commuicate back any violations anyway. |
| 528 | threadState->setStrictModePolicy(0); |
| 529 | } else { |
| 530 | threadState->setStrictModePolicy(strictPolicy); |
| 531 | } |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 532 | const String16 str(readString16()); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 533 | if (str == interface) { |
| 534 | return true; |
| 535 | } else { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 536 | ALOGW("**** enforceInterface() expected '%s' but read '%s'", |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 537 | String8(interface).string(), String8(str).string()); |
| 538 | return false; |
| 539 | } |
Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 540 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 541 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 542 | const binder_size_t* Parcel::objects() const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 543 | { |
| 544 | return mObjects; |
| 545 | } |
| 546 | |
| 547 | size_t Parcel::objectsCount() const |
| 548 | { |
| 549 | return mObjectsSize; |
| 550 | } |
| 551 | |
| 552 | status_t Parcel::errorCheck() const |
| 553 | { |
| 554 | return mError; |
| 555 | } |
| 556 | |
| 557 | void Parcel::setError(status_t err) |
| 558 | { |
| 559 | mError = err; |
| 560 | } |
| 561 | |
| 562 | status_t Parcel::finishWrite(size_t len) |
| 563 | { |
| 564 | //printf("Finish write of %d\n", len); |
| 565 | mDataPos += len; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 566 | 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] | 567 | if (mDataPos > mDataSize) { |
| 568 | mDataSize = mDataPos; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 569 | 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] | 570 | } |
| 571 | //printf("New pos=%d, size=%d\n", mDataPos, mDataSize); |
| 572 | return NO_ERROR; |
| 573 | } |
| 574 | |
| 575 | status_t Parcel::writeUnpadded(const void* data, size_t len) |
| 576 | { |
| 577 | size_t end = mDataPos + len; |
| 578 | if (end < mDataPos) { |
| 579 | // integer overflow |
| 580 | return BAD_VALUE; |
| 581 | } |
| 582 | |
| 583 | if (end <= mDataCapacity) { |
| 584 | restart_write: |
| 585 | memcpy(mData+mDataPos, data, len); |
| 586 | return finishWrite(len); |
| 587 | } |
| 588 | |
| 589 | status_t err = growData(len); |
| 590 | if (err == NO_ERROR) goto restart_write; |
| 591 | return err; |
| 592 | } |
| 593 | |
| 594 | status_t Parcel::write(const void* data, size_t len) |
| 595 | { |
| 596 | void* const d = writeInplace(len); |
| 597 | if (d) { |
| 598 | memcpy(d, data, len); |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | return mError; |
| 602 | } |
| 603 | |
| 604 | void* Parcel::writeInplace(size_t len) |
| 605 | { |
| 606 | const size_t padded = PAD_SIZE(len); |
| 607 | |
| 608 | // sanity check for integer overflow |
| 609 | if (mDataPos+padded < mDataPos) { |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | if ((mDataPos+padded) <= mDataCapacity) { |
| 614 | restart_write: |
| 615 | //printf("Writing %ld bytes, padded to %ld\n", len, padded); |
| 616 | uint8_t* const data = mData+mDataPos; |
| 617 | |
| 618 | // Need to pad at end? |
| 619 | if (padded != len) { |
| 620 | #if BYTE_ORDER == BIG_ENDIAN |
| 621 | static const uint32_t mask[4] = { |
| 622 | 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 |
| 623 | }; |
| 624 | #endif |
| 625 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 626 | static const uint32_t mask[4] = { |
| 627 | 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff |
| 628 | }; |
| 629 | #endif |
| 630 | //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len], |
| 631 | // *reinterpret_cast<void**>(data+padded-4)); |
| 632 | *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len]; |
| 633 | } |
| 634 | |
| 635 | finishWrite(padded); |
| 636 | return data; |
| 637 | } |
| 638 | |
| 639 | status_t err = growData(padded); |
| 640 | if (err == NO_ERROR) goto restart_write; |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | status_t Parcel::writeInt32(int32_t val) |
| 645 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 646 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 647 | } |
Dan Stoza | 41a0f2f | 2014-12-01 10:01:10 -0800 | [diff] [blame] | 648 | |
| 649 | status_t Parcel::writeUint32(uint32_t val) |
| 650 | { |
| 651 | return writeAligned(val); |
| 652 | } |
| 653 | |
Marco Nelissen | 5c0106e | 2013-10-16 10:57:51 -0700 | [diff] [blame] | 654 | status_t Parcel::writeInt32Array(size_t len, const int32_t *val) { |
| 655 | if (!val) { |
| 656 | return writeAligned(-1); |
| 657 | } |
| 658 | status_t ret = writeAligned(len); |
| 659 | if (ret == NO_ERROR) { |
| 660 | ret = write(val, len * sizeof(*val)); |
| 661 | } |
| 662 | return ret; |
| 663 | } |
Marco Nelissen | f0190bf | 2014-03-13 14:17:40 -0700 | [diff] [blame] | 664 | status_t Parcel::writeByteArray(size_t len, const uint8_t *val) { |
| 665 | if (!val) { |
| 666 | return writeAligned(-1); |
| 667 | } |
| 668 | status_t ret = writeAligned(len); |
| 669 | if (ret == NO_ERROR) { |
| 670 | ret = write(val, len * sizeof(*val)); |
| 671 | } |
| 672 | return ret; |
| 673 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 674 | |
| 675 | status_t Parcel::writeInt64(int64_t val) |
| 676 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 677 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Ronghua Wu | 2d13afd | 2015-03-16 11:11:07 -0700 | [diff] [blame^] | 680 | status_t Parcel::writeUint64(uint64_t val) |
| 681 | { |
| 682 | return writeAligned(val); |
| 683 | } |
| 684 | |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 685 | status_t Parcel::writePointer(uintptr_t val) |
| 686 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 687 | return writeAligned<binder_uintptr_t>(val); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 688 | } |
| 689 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 690 | status_t Parcel::writeFloat(float val) |
| 691 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 692 | return writeAligned(val); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 695 | #if defined(__mips__) && defined(__mips_hard_float) |
| 696 | |
| 697 | status_t Parcel::writeDouble(double val) |
| 698 | { |
| 699 | union { |
| 700 | double d; |
| 701 | unsigned long long ll; |
| 702 | } u; |
| 703 | u.d = val; |
| 704 | return writeAligned(u.ll); |
| 705 | } |
| 706 | |
| 707 | #else |
| 708 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 709 | status_t Parcel::writeDouble(double val) |
| 710 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 711 | return writeAligned(val); |
| 712 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 713 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 714 | #endif |
| 715 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 716 | status_t Parcel::writeCString(const char* str) |
| 717 | { |
| 718 | return write(str, strlen(str)+1); |
| 719 | } |
| 720 | |
| 721 | status_t Parcel::writeString8(const String8& str) |
| 722 | { |
| 723 | status_t err = writeInt32(str.bytes()); |
Pravat Dalbehera | d1dff8d | 2010-12-15 08:40:00 +0100 | [diff] [blame] | 724 | // only write string if its length is more than zero characters, |
| 725 | // as readString8 will only read if the length field is non-zero. |
| 726 | // this is slightly different from how writeString16 works. |
| 727 | if (str.bytes() > 0 && err == NO_ERROR) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 728 | err = write(str.string(), str.bytes()+1); |
| 729 | } |
| 730 | return err; |
| 731 | } |
| 732 | |
| 733 | status_t Parcel::writeString16(const String16& str) |
| 734 | { |
| 735 | return writeString16(str.string(), str.size()); |
| 736 | } |
| 737 | |
| 738 | status_t Parcel::writeString16(const char16_t* str, size_t len) |
| 739 | { |
| 740 | if (str == NULL) return writeInt32(-1); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 741 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 742 | status_t err = writeInt32(len); |
| 743 | if (err == NO_ERROR) { |
| 744 | len *= sizeof(char16_t); |
| 745 | uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t)); |
| 746 | if (data) { |
| 747 | memcpy(data, str, len); |
| 748 | *reinterpret_cast<char16_t*>(data+len) = 0; |
| 749 | return NO_ERROR; |
| 750 | } |
| 751 | err = mError; |
| 752 | } |
| 753 | return err; |
| 754 | } |
| 755 | |
| 756 | status_t Parcel::writeStrongBinder(const sp<IBinder>& val) |
| 757 | { |
| 758 | return flatten_binder(ProcessState::self(), val, this); |
| 759 | } |
| 760 | |
| 761 | status_t Parcel::writeWeakBinder(const wp<IBinder>& val) |
| 762 | { |
| 763 | return flatten_binder(ProcessState::self(), val, this); |
| 764 | } |
| 765 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 766 | status_t Parcel::writeNativeHandle(const native_handle* handle) |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 767 | { |
Mathias Agopian | 1d0a95b | 2009-07-31 16:12:13 -0700 | [diff] [blame] | 768 | if (!handle || handle->version != sizeof(native_handle)) |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 769 | return BAD_TYPE; |
| 770 | |
| 771 | status_t err; |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 772 | err = writeInt32(handle->numFds); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 773 | if (err != NO_ERROR) return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 775 | err = writeInt32(handle->numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 776 | if (err != NO_ERROR) return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 778 | for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++) |
| 779 | err = writeDupFileDescriptor(handle->data[i]); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | |
| 781 | if (err != NO_ERROR) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 782 | ALOGD("write native handle, write dup fd failed"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | return err; |
| 784 | } |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 785 | 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] | 786 | return err; |
| 787 | } |
| 788 | |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 789 | status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 790 | { |
| 791 | flat_binder_object obj; |
| 792 | obj.type = BINDER_TYPE_FD; |
| 793 | obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS; |
Arve Hjønnevåg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 794 | 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] | 795 | obj.handle = fd; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 796 | obj.cookie = takeOwnership ? 1 : 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 797 | return writeObject(obj, true); |
| 798 | } |
| 799 | |
| 800 | status_t Parcel::writeDupFileDescriptor(int fd) |
| 801 | { |
Jeff Brown | d341c71 | 2011-11-04 20:19:33 -0700 | [diff] [blame] | 802 | int dupFd = dup(fd); |
| 803 | if (dupFd < 0) { |
| 804 | return -errno; |
| 805 | } |
| 806 | status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); |
| 807 | if (err) { |
| 808 | close(dupFd); |
| 809 | } |
| 810 | return err; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Mike Lockwood | cbe36fe | 2013-09-05 08:41:06 -0700 | [diff] [blame] | 813 | // WARNING: This method must stay in sync with |
| 814 | // Parcelable.Creator<ParcelFileDescriptor> CREATOR |
| 815 | // in frameworks/base/core/java/android/os/ParcelFileDescriptor.java |
| 816 | status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) { |
| 817 | status_t status; |
| 818 | |
| 819 | if (fd < 0) { |
| 820 | status = writeInt32(0); // ParcelFileDescriptor is null |
| 821 | if (status) return status; |
| 822 | } else { |
| 823 | status = writeInt32(1); // ParcelFileDescriptor is not null |
| 824 | if (status) return status; |
| 825 | status = writeDupFileDescriptor(fd); |
| 826 | if (status) return status; |
| 827 | if (commChannel < 0) { |
| 828 | status = writeInt32(0); // commChannel is null |
| 829 | if (status) return status; |
| 830 | } else { |
| 831 | status = writeInt32(1); // commChannel is not null |
| 832 | if (status) return status; |
| 833 | status = writeDupFileDescriptor(commChannel); |
| 834 | } |
| 835 | } |
| 836 | return status; |
| 837 | } |
| 838 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 839 | status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob) |
| 840 | { |
| 841 | status_t status; |
| 842 | |
| 843 | if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 844 | ALOGV("writeBlob: write in place"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 845 | status = writeInt32(0); |
| 846 | if (status) return status; |
| 847 | |
| 848 | void* ptr = writeInplace(len); |
| 849 | if (!ptr) return NO_MEMORY; |
| 850 | |
| 851 | outBlob->init(false /*mapped*/, ptr, len); |
| 852 | return NO_ERROR; |
| 853 | } |
| 854 | |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 855 | ALOGV("writeBlob: write to ashmem"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 856 | int fd = ashmem_create_region("Parcel Blob", len); |
| 857 | if (fd < 0) return NO_MEMORY; |
| 858 | |
| 859 | int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
| 860 | if (result < 0) { |
Jeff Brown | ec4e006 | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 861 | status = result; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 862 | } else { |
| 863 | void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 864 | if (ptr == MAP_FAILED) { |
| 865 | status = -errno; |
| 866 | } else { |
| 867 | result = ashmem_set_prot_region(fd, PROT_READ); |
| 868 | if (result < 0) { |
Jeff Brown | ec4e006 | 2011-10-10 14:50:10 -0700 | [diff] [blame] | 869 | status = result; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 870 | } else { |
| 871 | status = writeInt32(1); |
| 872 | if (!status) { |
Jeff Brown | 93ff1f9 | 2011-11-04 19:01:44 -0700 | [diff] [blame] | 873 | status = writeFileDescriptor(fd, true /*takeOwnership*/); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 874 | if (!status) { |
| 875 | outBlob->init(true /*mapped*/, ptr, len); |
| 876 | return NO_ERROR; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | ::munmap(ptr, len); |
| 882 | } |
| 883 | ::close(fd); |
| 884 | return status; |
| 885 | } |
| 886 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 887 | status_t Parcel::write(const FlattenableHelperInterface& val) |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 888 | { |
| 889 | status_t err; |
| 890 | |
| 891 | // size if needed |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 892 | const size_t len = val.getFlattenedSize(); |
| 893 | const size_t fd_count = val.getFdCount(); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 894 | |
| 895 | err = this->writeInt32(len); |
| 896 | if (err) return err; |
| 897 | |
| 898 | err = this->writeInt32(fd_count); |
| 899 | if (err) return err; |
| 900 | |
| 901 | // payload |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 902 | void* const buf = this->writeInplace(PAD_SIZE(len)); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 903 | if (buf == NULL) |
| 904 | return BAD_VALUE; |
| 905 | |
| 906 | int* fds = NULL; |
| 907 | if (fd_count) { |
| 908 | fds = new int[fd_count]; |
| 909 | } |
| 910 | |
| 911 | err = val.flatten(buf, len, fds, fd_count); |
| 912 | for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { |
| 913 | err = this->writeDupFileDescriptor( fds[i] ); |
| 914 | } |
| 915 | |
| 916 | if (fd_count) { |
| 917 | delete [] fds; |
| 918 | } |
| 919 | |
| 920 | return err; |
| 921 | } |
| 922 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 923 | status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData) |
| 924 | { |
| 925 | const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity; |
| 926 | const bool enoughObjects = mObjectsSize < mObjectsCapacity; |
| 927 | if (enoughData && enoughObjects) { |
| 928 | restart_write: |
| 929 | *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 930 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 931 | // Need to write meta-data? |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 932 | if (nullMetaData || val.binder != 0) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 933 | mObjects[mObjectsSize] = mDataPos; |
| 934 | acquire_object(ProcessState::self(), val, this); |
| 935 | mObjectsSize++; |
| 936 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 937 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 938 | // remember if it's a file descriptor |
| 939 | if (val.type == BINDER_TYPE_FD) { |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 940 | if (!mAllowFds) { |
| 941 | return FDS_NOT_ALLOWED; |
| 942 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 943 | mHasFds = mFdsKnown = true; |
| 944 | } |
| 945 | |
| 946 | return finishWrite(sizeof(flat_binder_object)); |
| 947 | } |
| 948 | |
| 949 | if (!enoughData) { |
| 950 | const status_t err = growData(sizeof(val)); |
| 951 | if (err != NO_ERROR) return err; |
| 952 | } |
| 953 | if (!enoughObjects) { |
| 954 | size_t newSize = ((mObjectsSize+2)*3)/2; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 955 | 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] | 956 | if (objects == NULL) return NO_MEMORY; |
| 957 | mObjects = objects; |
| 958 | mObjectsCapacity = newSize; |
| 959 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 960 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 961 | goto restart_write; |
| 962 | } |
| 963 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 964 | status_t Parcel::writeNoException() |
| 965 | { |
| 966 | return writeInt32(0); |
| 967 | } |
| 968 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 969 | void Parcel::remove(size_t /*start*/, size_t /*amt*/) |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 970 | { |
| 971 | LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!"); |
| 972 | } |
| 973 | |
| 974 | status_t Parcel::read(void* outData, size_t len) const |
| 975 | { |
Kenny Root | 5b61ad2 | 2014-03-17 13:18:16 -0700 | [diff] [blame] | 976 | if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize |
| 977 | && len <= PAD_SIZE(len)) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 978 | memcpy(outData, mData+mDataPos, len); |
| 979 | mDataPos += PAD_SIZE(len); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 980 | 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] | 981 | return NO_ERROR; |
| 982 | } |
| 983 | return NOT_ENOUGH_DATA; |
| 984 | } |
| 985 | |
| 986 | const void* Parcel::readInplace(size_t len) const |
| 987 | { |
Kenny Root | 5b61ad2 | 2014-03-17 13:18:16 -0700 | [diff] [blame] | 988 | if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize |
| 989 | && len <= PAD_SIZE(len)) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 990 | const void* data = mData+mDataPos; |
| 991 | mDataPos += PAD_SIZE(len); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 992 | 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] | 993 | return data; |
| 994 | } |
| 995 | return NULL; |
| 996 | } |
| 997 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 998 | template<class T> |
| 999 | status_t Parcel::readAligned(T *pArg) const { |
| 1000 | COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); |
| 1001 | |
| 1002 | if ((mDataPos+sizeof(T)) <= mDataSize) { |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1003 | const void* data = mData+mDataPos; |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1004 | mDataPos += sizeof(T); |
| 1005 | *pArg = *reinterpret_cast<const T*>(data); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1006 | return NO_ERROR; |
| 1007 | } else { |
| 1008 | return NOT_ENOUGH_DATA; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1012 | template<class T> |
| 1013 | T Parcel::readAligned() const { |
| 1014 | T result; |
| 1015 | if (readAligned(&result) != NO_ERROR) { |
| 1016 | result = 0; |
| 1017 | } |
| 1018 | |
| 1019 | return result; |
| 1020 | } |
| 1021 | |
| 1022 | template<class T> |
| 1023 | status_t Parcel::writeAligned(T val) { |
| 1024 | COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T)); |
| 1025 | |
| 1026 | if ((mDataPos+sizeof(val)) <= mDataCapacity) { |
| 1027 | restart_write: |
| 1028 | *reinterpret_cast<T*>(mData+mDataPos) = val; |
| 1029 | return finishWrite(sizeof(val)); |
| 1030 | } |
| 1031 | |
| 1032 | status_t err = growData(sizeof(val)); |
| 1033 | if (err == NO_ERROR) goto restart_write; |
| 1034 | return err; |
| 1035 | } |
| 1036 | |
| 1037 | status_t Parcel::readInt32(int32_t *pArg) const |
| 1038 | { |
| 1039 | return readAligned(pArg); |
| 1040 | } |
| 1041 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1042 | int32_t Parcel::readInt32() const |
| 1043 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1044 | return readAligned<int32_t>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Dan Stoza | 41a0f2f | 2014-12-01 10:01:10 -0800 | [diff] [blame] | 1047 | status_t Parcel::readUint32(uint32_t *pArg) const |
| 1048 | { |
| 1049 | return readAligned(pArg); |
| 1050 | } |
| 1051 | |
| 1052 | uint32_t Parcel::readUint32() const |
| 1053 | { |
| 1054 | return readAligned<uint32_t>(); |
| 1055 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1056 | |
| 1057 | status_t Parcel::readInt64(int64_t *pArg) const |
| 1058 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1059 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | int64_t Parcel::readInt64() const |
| 1064 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1065 | return readAligned<int64_t>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Ronghua Wu | 2d13afd | 2015-03-16 11:11:07 -0700 | [diff] [blame^] | 1068 | status_t Parcel::readUint64(uint64_t *pArg) const |
| 1069 | { |
| 1070 | return readAligned(pArg); |
| 1071 | } |
| 1072 | |
| 1073 | uint64_t Parcel::readUint64() const |
| 1074 | { |
| 1075 | return readAligned<uint64_t>(); |
| 1076 | } |
| 1077 | |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1078 | status_t Parcel::readPointer(uintptr_t *pArg) const |
| 1079 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1080 | status_t ret; |
| 1081 | binder_uintptr_t ptr; |
| 1082 | ret = readAligned(&ptr); |
| 1083 | if (!ret) |
| 1084 | *pArg = ptr; |
| 1085 | return ret; |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | uintptr_t Parcel::readPointer() const |
| 1089 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1090 | return readAligned<binder_uintptr_t>(); |
Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1094 | status_t Parcel::readFloat(float *pArg) const |
| 1095 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1096 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | float Parcel::readFloat() const |
| 1101 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1102 | return readAligned<float>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1105 | #if defined(__mips__) && defined(__mips_hard_float) |
| 1106 | |
| 1107 | status_t Parcel::readDouble(double *pArg) const |
| 1108 | { |
| 1109 | union { |
| 1110 | double d; |
| 1111 | unsigned long long ll; |
| 1112 | } u; |
Narayan Kamath | 2c68d38 | 2014-06-04 15:04:29 +0100 | [diff] [blame] | 1113 | u.d = 0; |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1114 | status_t status; |
| 1115 | status = readAligned(&u.ll); |
| 1116 | *pArg = u.d; |
| 1117 | return status; |
| 1118 | } |
| 1119 | |
| 1120 | double Parcel::readDouble() const |
| 1121 | { |
| 1122 | union { |
| 1123 | double d; |
| 1124 | unsigned long long ll; |
| 1125 | } u; |
| 1126 | u.ll = readAligned<unsigned long long>(); |
| 1127 | return u.d; |
| 1128 | } |
| 1129 | |
| 1130 | #else |
| 1131 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1132 | status_t Parcel::readDouble(double *pArg) const |
| 1133 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1134 | return readAligned(pArg); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1137 | double Parcel::readDouble() const |
| 1138 | { |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1139 | return readAligned<double>(); |
| 1140 | } |
| 1141 | |
Douglas Leung | cc1a4bb | 2013-01-11 15:00:55 -0800 | [diff] [blame] | 1142 | #endif |
| 1143 | |
Andreas Huber | 84a6d04 | 2009-08-17 13:33:27 -0700 | [diff] [blame] | 1144 | status_t Parcel::readIntPtr(intptr_t *pArg) const |
| 1145 | { |
| 1146 | return readAligned(pArg); |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | intptr_t Parcel::readIntPtr() const |
| 1151 | { |
| 1152 | return readAligned<intptr_t>(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | const char* Parcel::readCString() const |
| 1157 | { |
| 1158 | const size_t avail = mDataSize-mDataPos; |
| 1159 | if (avail > 0) { |
| 1160 | const char* str = reinterpret_cast<const char*>(mData+mDataPos); |
| 1161 | // is the string's trailing NUL within the parcel's valid bounds? |
| 1162 | const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail)); |
| 1163 | if (eos) { |
| 1164 | const size_t len = eos - str; |
| 1165 | mDataPos += PAD_SIZE(len+1); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1166 | 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] | 1167 | return str; |
| 1168 | } |
| 1169 | } |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | |
| 1173 | String8 Parcel::readString8() const |
| 1174 | { |
| 1175 | int32_t size = readInt32(); |
| 1176 | // watch for potential int overflow adding 1 for trailing NUL |
| 1177 | if (size > 0 && size < INT32_MAX) { |
| 1178 | const char* str = (const char*)readInplace(size+1); |
| 1179 | if (str) return String8(str, size); |
| 1180 | } |
| 1181 | return String8(); |
| 1182 | } |
| 1183 | |
| 1184 | String16 Parcel::readString16() const |
| 1185 | { |
| 1186 | size_t len; |
| 1187 | const char16_t* str = readString16Inplace(&len); |
| 1188 | if (str) return String16(str, len); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1189 | ALOGE("Reading a NULL string not supported here."); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1190 | return String16(); |
| 1191 | } |
| 1192 | |
| 1193 | const char16_t* Parcel::readString16Inplace(size_t* outLen) const |
| 1194 | { |
| 1195 | int32_t size = readInt32(); |
| 1196 | // watch for potential int overflow from size+1 |
| 1197 | if (size >= 0 && size < INT32_MAX) { |
| 1198 | *outLen = size; |
| 1199 | const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t)); |
| 1200 | if (str != NULL) { |
| 1201 | return str; |
| 1202 | } |
| 1203 | } |
| 1204 | *outLen = 0; |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
| 1208 | sp<IBinder> Parcel::readStrongBinder() const |
| 1209 | { |
| 1210 | sp<IBinder> val; |
| 1211 | unflatten_binder(ProcessState::self(), *this, &val); |
| 1212 | return val; |
| 1213 | } |
| 1214 | |
| 1215 | wp<IBinder> Parcel::readWeakBinder() const |
| 1216 | { |
| 1217 | wp<IBinder> val; |
| 1218 | unflatten_binder(ProcessState::self(), *this, &val); |
| 1219 | return val; |
| 1220 | } |
| 1221 | |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1222 | int32_t Parcel::readExceptionCode() const |
| 1223 | { |
| 1224 | int32_t exception_code = readAligned<int32_t>(); |
Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1225 | if (exception_code == EX_HAS_REPLY_HEADER) { |
Magnus Strandberg | 1ba2457 | 2011-05-03 15:44:00 +0200 | [diff] [blame] | 1226 | int32_t header_start = dataPosition(); |
Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1227 | int32_t header_size = readAligned<int32_t>(); |
| 1228 | // Skip over fat responses headers. Not used (or propagated) in |
| 1229 | // native code |
Magnus Strandberg | 1ba2457 | 2011-05-03 15:44:00 +0200 | [diff] [blame] | 1230 | setDataPosition(header_start + header_size); |
Brad Fitzpatrick | d36f4a5 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1231 | // And fat response headers are currently only used when there are no |
| 1232 | // exceptions, so return no error: |
| 1233 | return 0; |
| 1234 | } |
Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 1235 | return exception_code; |
| 1236 | } |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1237 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1238 | native_handle* Parcel::readNativeHandle() const |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1239 | { |
| 1240 | int numFds, numInts; |
| 1241 | status_t err; |
| 1242 | err = readInt32(&numFds); |
| 1243 | if (err != NO_ERROR) return 0; |
| 1244 | err = readInt32(&numInts); |
| 1245 | if (err != NO_ERROR) return 0; |
| 1246 | |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1247 | native_handle* h = native_handle_create(numFds, numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1248 | for (int i=0 ; err==NO_ERROR && i<numFds ; i++) { |
Rebecca Schultz Zavin | 360211f | 2009-02-13 16:34:38 -0800 | [diff] [blame] | 1249 | h->data[i] = dup(readFileDescriptor()); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1250 | if (h->data[i] < 0) err = BAD_VALUE; |
| 1251 | } |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1252 | err = read(h->data + numFds, sizeof(int)*numInts); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1253 | if (err != NO_ERROR) { |
Mathias Agopian | a47f02a | 2009-05-21 16:29:38 -0700 | [diff] [blame] | 1254 | native_handle_close(h); |
| 1255 | native_handle_delete(h); |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1256 | h = 0; |
| 1257 | } |
| 1258 | return h; |
| 1259 | } |
| 1260 | |
| 1261 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1262 | int Parcel::readFileDescriptor() const |
| 1263 | { |
| 1264 | const flat_binder_object* flat = readObject(true); |
| 1265 | if (flat) { |
| 1266 | switch (flat->type) { |
| 1267 | case BINDER_TYPE_FD: |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1268 | //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] | 1269 | return flat->handle; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1270 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1271 | } |
| 1272 | return BAD_TYPE; |
| 1273 | } |
| 1274 | |
Mike Lockwood | cbe36fe | 2013-09-05 08:41:06 -0700 | [diff] [blame] | 1275 | // WARNING: This method must stay in sync with writeToParcel() |
| 1276 | // in frameworks/base/core/java/android/os/ParcelFileDescriptor.java |
| 1277 | int Parcel::readParcelFileDescriptor(int& outCommChannel) const { |
| 1278 | int fd; |
| 1279 | outCommChannel = -1; |
| 1280 | |
| 1281 | if (readInt32() == 0) { |
| 1282 | fd = -1; |
| 1283 | } else { |
| 1284 | fd = readFileDescriptor(); |
| 1285 | if (fd >= 0 && readInt32() != 0) { |
| 1286 | outCommChannel = readFileDescriptor(); |
| 1287 | } |
| 1288 | } |
| 1289 | return fd; |
| 1290 | } |
| 1291 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1292 | status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const |
| 1293 | { |
| 1294 | int32_t useAshmem; |
| 1295 | status_t status = readInt32(&useAshmem); |
| 1296 | if (status) return status; |
| 1297 | |
| 1298 | if (!useAshmem) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1299 | ALOGV("readBlob: read in place"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1300 | const void* ptr = readInplace(len); |
| 1301 | if (!ptr) return BAD_VALUE; |
| 1302 | |
| 1303 | outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len); |
| 1304 | return NO_ERROR; |
| 1305 | } |
| 1306 | |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1307 | ALOGV("readBlob: read from ashmem"); |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1308 | int fd = readFileDescriptor(); |
| 1309 | if (fd == int(BAD_TYPE)) return BAD_VALUE; |
| 1310 | |
| 1311 | void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); |
Narayan Kamath | 9ea0975 | 2014-10-08 17:35:45 +0100 | [diff] [blame] | 1312 | if (ptr == MAP_FAILED) return NO_MEMORY; |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1313 | |
| 1314 | outBlob->init(true /*mapped*/, ptr, len); |
| 1315 | return NO_ERROR; |
| 1316 | } |
| 1317 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 1318 | status_t Parcel::read(FlattenableHelperInterface& val) const |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1319 | { |
| 1320 | // size |
| 1321 | const size_t len = this->readInt32(); |
| 1322 | const size_t fd_count = this->readInt32(); |
| 1323 | |
| 1324 | // payload |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 1325 | void const* const buf = this->readInplace(PAD_SIZE(len)); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1326 | if (buf == NULL) |
| 1327 | return BAD_VALUE; |
| 1328 | |
| 1329 | int* fds = NULL; |
| 1330 | if (fd_count) { |
| 1331 | fds = new int[fd_count]; |
| 1332 | } |
| 1333 | |
| 1334 | status_t err = NO_ERROR; |
| 1335 | for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) { |
Jesse Hall | fee9904 | 2014-11-04 08:36:31 -0800 | [diff] [blame] | 1336 | fds[i] = dup(this->readFileDescriptor()); |
Jun Jiang | abf8a2c | 2014-04-29 14:22:10 +0800 | [diff] [blame] | 1337 | if (fds[i] < 0) { |
| 1338 | err = BAD_VALUE; |
Jesse Hall | fee9904 | 2014-11-04 08:36:31 -0800 | [diff] [blame] | 1339 | ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s", |
| 1340 | i, fds[i], fd_count, strerror(errno)); |
Jun Jiang | abf8a2c | 2014-04-29 14:22:10 +0800 | [diff] [blame] | 1341 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (err == NO_ERROR) { |
| 1345 | err = val.unflatten(buf, len, fds, fd_count); |
| 1346 | } |
| 1347 | |
| 1348 | if (fd_count) { |
| 1349 | delete [] fds; |
| 1350 | } |
| 1351 | |
| 1352 | return err; |
| 1353 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1354 | const flat_binder_object* Parcel::readObject(bool nullMetaData) const |
| 1355 | { |
| 1356 | const size_t DPOS = mDataPos; |
| 1357 | if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) { |
| 1358 | const flat_binder_object* obj |
| 1359 | = reinterpret_cast<const flat_binder_object*>(mData+DPOS); |
| 1360 | mDataPos = DPOS + sizeof(flat_binder_object); |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1361 | if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) { |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 1362 | // 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] | 1363 | // the object list, so we don't want to check for it when |
| 1364 | // reading. |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1365 | 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] | 1366 | return obj; |
| 1367 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1368 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1369 | // Ensure that this object is valid... |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1370 | binder_size_t* const OBJS = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1371 | const size_t N = mObjectsSize; |
| 1372 | size_t opos = mNextObjectHint; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1373 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1374 | if (N > 0) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1375 | 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] | 1376 | this, DPOS, opos); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1377 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1378 | // Start at the current hint position, looking for an object at |
| 1379 | // the current data position. |
| 1380 | if (opos < N) { |
| 1381 | while (opos < (N-1) && OBJS[opos] < DPOS) { |
| 1382 | opos++; |
| 1383 | } |
| 1384 | } else { |
| 1385 | opos = N-1; |
| 1386 | } |
| 1387 | if (OBJS[opos] == DPOS) { |
| 1388 | // Found it! |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1389 | 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] | 1390 | this, DPOS, opos); |
| 1391 | mNextObjectHint = opos+1; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1392 | 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] | 1393 | return obj; |
| 1394 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1395 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1396 | // Look backwards for it... |
| 1397 | while (opos > 0 && OBJS[opos] > DPOS) { |
| 1398 | opos--; |
| 1399 | } |
| 1400 | if (OBJS[opos] == DPOS) { |
| 1401 | // Found it! |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1402 | 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] | 1403 | this, DPOS, opos); |
| 1404 | mNextObjectHint = opos+1; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1405 | 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] | 1406 | return obj; |
| 1407 | } |
| 1408 | } |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1409 | 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] | 1410 | this, DPOS); |
| 1411 | } |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | |
| 1415 | void Parcel::closeFileDescriptors() |
| 1416 | { |
| 1417 | size_t i = mObjectsSize; |
| 1418 | if (i > 0) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1419 | //ALOGI("Closing file descriptors for %zu objects...", i); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1420 | } |
| 1421 | while (i > 0) { |
| 1422 | i--; |
| 1423 | const flat_binder_object* flat |
| 1424 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
| 1425 | if (flat->type == BINDER_TYPE_FD) { |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1426 | //ALOGI("Closing fd: %ld", flat->handle); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1427 | close(flat->handle); |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1432 | uintptr_t Parcel::ipcData() const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1433 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1434 | return reinterpret_cast<uintptr_t>(mData); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | size_t Parcel::ipcDataSize() const |
| 1438 | { |
| 1439 | return (mDataSize > mDataPos ? mDataSize : mDataPos); |
| 1440 | } |
| 1441 | |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1442 | uintptr_t Parcel::ipcObjects() const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1443 | { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1444 | return reinterpret_cast<uintptr_t>(mObjects); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | size_t Parcel::ipcObjectsCount() const |
| 1448 | { |
| 1449 | return mObjectsSize; |
| 1450 | } |
| 1451 | |
| 1452 | void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize, |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1453 | 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] | 1454 | { |
Arve Hjønnevåg | 6f28611 | 2014-02-19 20:42:13 -0800 | [diff] [blame] | 1455 | binder_size_t minOffset = 0; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1456 | freeDataNoInit(); |
| 1457 | mError = NO_ERROR; |
| 1458 | mData = const_cast<uint8_t*>(data); |
| 1459 | mDataSize = mDataCapacity = dataSize; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1460 | //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] | 1461 | mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1462 | 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] | 1463 | mObjects = const_cast<binder_size_t*>(objects); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1464 | mObjectsSize = mObjectsCapacity = objectsCount; |
| 1465 | mNextObjectHint = 0; |
| 1466 | mOwner = relFunc; |
| 1467 | mOwnerCookie = relCookie; |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 1468 | for (size_t i = 0; i < mObjectsSize; i++) { |
Arve Hjønnevåg | 6f28611 | 2014-02-19 20:42:13 -0800 | [diff] [blame] | 1469 | binder_size_t offset = mObjects[i]; |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 1470 | if (offset < minOffset) { |
Dan Albert | 3bdc5b8 | 2014-11-20 11:50:23 -0800 | [diff] [blame] | 1471 | ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n", |
Arve Hjønnevåg | 6f28611 | 2014-02-19 20:42:13 -0800 | [diff] [blame] | 1472 | __func__, (uint64_t)offset, (uint64_t)minOffset); |
Arve Hjønnevåg | f50b9ea | 2014-02-13 19:22:08 -0800 | [diff] [blame] | 1473 | mObjectsSize = 0; |
| 1474 | break; |
| 1475 | } |
| 1476 | minOffset = offset + sizeof(flat_binder_object); |
| 1477 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1478 | scanForFds(); |
| 1479 | } |
| 1480 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1481 | void Parcel::print(TextOutput& to, uint32_t /*flags*/) const |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1482 | { |
| 1483 | to << "Parcel("; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1484 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1485 | if (errorCheck() != NO_ERROR) { |
| 1486 | const status_t err = errorCheck(); |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1487 | to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\""; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1488 | } else if (dataSize() > 0) { |
| 1489 | const uint8_t* DATA = data(); |
| 1490 | to << indent << HexDump(DATA, dataSize()) << dedent; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1491 | const binder_size_t* OBJS = objects(); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1492 | const size_t N = objectsCount(); |
| 1493 | for (size_t i=0; i<N; i++) { |
| 1494 | const flat_binder_object* flat |
| 1495 | = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]); |
| 1496 | to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": " |
| 1497 | << TypeCode(flat->type & 0x7f7f7f00) |
| 1498 | << " = " << flat->binder; |
| 1499 | } |
| 1500 | } else { |
| 1501 | to << "NULL"; |
| 1502 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1503 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1504 | to << ")"; |
| 1505 | } |
| 1506 | |
| 1507 | void Parcel::releaseObjects() |
| 1508 | { |
| 1509 | const sp<ProcessState> proc(ProcessState::self()); |
| 1510 | size_t i = mObjectsSize; |
| 1511 | uint8_t* const data = mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1512 | binder_size_t* const objects = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1513 | while (i > 0) { |
| 1514 | i--; |
| 1515 | const flat_binder_object* flat |
| 1516 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
| 1517 | release_object(proc, *flat, this); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | void Parcel::acquireObjects() |
| 1522 | { |
| 1523 | const sp<ProcessState> proc(ProcessState::self()); |
| 1524 | size_t i = mObjectsSize; |
| 1525 | uint8_t* const data = mData; |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1526 | binder_size_t* const objects = mObjects; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1527 | while (i > 0) { |
| 1528 | i--; |
| 1529 | const flat_binder_object* flat |
| 1530 | = reinterpret_cast<flat_binder_object*>(data+objects[i]); |
| 1531 | acquire_object(proc, *flat, this); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | void Parcel::freeData() |
| 1536 | { |
| 1537 | freeDataNoInit(); |
| 1538 | initState(); |
| 1539 | } |
| 1540 | |
| 1541 | void Parcel::freeDataNoInit() |
| 1542 | { |
| 1543 | if (mOwner) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1544 | LOG_ALLOC("Parcel %p: freeing other owner data", this); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1545 | //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] | 1546 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); |
| 1547 | } else { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1548 | LOG_ALLOC("Parcel %p: freeing allocated data", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1549 | releaseObjects(); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1550 | if (mData) { |
| 1551 | LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity); |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1552 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1553 | gParcelGlobalAllocSize -= mDataCapacity; |
| 1554 | gParcelGlobalAllocCount--; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1555 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1556 | free(mData); |
| 1557 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1558 | if (mObjects) free(mObjects); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | status_t Parcel::growData(size_t len) |
| 1563 | { |
| 1564 | size_t newSize = ((mDataSize+len)*3)/2; |
| 1565 | return (newSize <= mDataSize) |
| 1566 | ? (status_t) NO_MEMORY |
| 1567 | : continueWrite(newSize); |
| 1568 | } |
| 1569 | |
| 1570 | status_t Parcel::restartWrite(size_t desired) |
| 1571 | { |
| 1572 | if (mOwner) { |
| 1573 | freeData(); |
| 1574 | return continueWrite(desired); |
| 1575 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1576 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1577 | uint8_t* data = (uint8_t*)realloc(mData, desired); |
| 1578 | if (!data && desired > mDataCapacity) { |
| 1579 | mError = NO_MEMORY; |
| 1580 | return NO_MEMORY; |
| 1581 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1582 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1583 | releaseObjects(); |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1584 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1585 | if (data) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1586 | 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] | 1587 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1588 | gParcelGlobalAllocSize += desired; |
| 1589 | gParcelGlobalAllocSize -= mDataCapacity; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1590 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1591 | mData = data; |
| 1592 | mDataCapacity = desired; |
| 1593 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1594 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1595 | mDataSize = mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1596 | ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize); |
| 1597 | ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos); |
| 1598 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1599 | free(mObjects); |
| 1600 | mObjects = NULL; |
| 1601 | mObjectsSize = mObjectsCapacity = 0; |
| 1602 | mNextObjectHint = 0; |
| 1603 | mHasFds = false; |
| 1604 | mFdsKnown = true; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1605 | mAllowFds = true; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1606 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1607 | return NO_ERROR; |
| 1608 | } |
| 1609 | |
| 1610 | status_t Parcel::continueWrite(size_t desired) |
| 1611 | { |
| 1612 | // If shrinking, first adjust for any objects that appear |
| 1613 | // after the new data size. |
| 1614 | size_t objectsSize = mObjectsSize; |
| 1615 | if (desired < mDataSize) { |
| 1616 | if (desired == 0) { |
| 1617 | objectsSize = 0; |
| 1618 | } else { |
| 1619 | while (objectsSize > 0) { |
| 1620 | if (mObjects[objectsSize-1] < desired) |
| 1621 | break; |
| 1622 | objectsSize--; |
| 1623 | } |
| 1624 | } |
| 1625 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1626 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1627 | if (mOwner) { |
| 1628 | // If the size is going to zero, just release the owner's data. |
| 1629 | if (desired == 0) { |
| 1630 | freeData(); |
| 1631 | return NO_ERROR; |
| 1632 | } |
| 1633 | |
| 1634 | // If there is a different owner, we need to take |
| 1635 | // posession. |
| 1636 | uint8_t* data = (uint8_t*)malloc(desired); |
| 1637 | if (!data) { |
| 1638 | mError = NO_MEMORY; |
| 1639 | return NO_MEMORY; |
| 1640 | } |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1641 | binder_size_t* objects = NULL; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1642 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1643 | if (objectsSize) { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1644 | objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1645 | if (!objects) { |
Hyejin Kim | 3f727c0 | 2013-03-09 11:28:54 +0900 | [diff] [blame] | 1646 | free(data); |
| 1647 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1648 | mError = NO_MEMORY; |
| 1649 | return NO_MEMORY; |
| 1650 | } |
| 1651 | |
| 1652 | // Little hack to only acquire references on objects |
| 1653 | // we will be keeping. |
| 1654 | size_t oldObjectsSize = mObjectsSize; |
| 1655 | mObjectsSize = objectsSize; |
| 1656 | acquireObjects(); |
| 1657 | mObjectsSize = oldObjectsSize; |
| 1658 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1659 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1660 | if (mData) { |
| 1661 | memcpy(data, mData, mDataSize < desired ? mDataSize : desired); |
| 1662 | } |
| 1663 | if (objects && mObjects) { |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1664 | memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t)); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1665 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1666 | //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] | 1667 | mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); |
| 1668 | mOwner = NULL; |
| 1669 | |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1670 | LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired); |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1671 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1672 | gParcelGlobalAllocSize += desired; |
| 1673 | gParcelGlobalAllocCount++; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1674 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1675 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1676 | mData = data; |
| 1677 | mObjects = objects; |
| 1678 | mDataSize = (mDataSize < desired) ? mDataSize : desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1679 | 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] | 1680 | mDataCapacity = desired; |
| 1681 | mObjectsSize = mObjectsCapacity = objectsSize; |
| 1682 | mNextObjectHint = 0; |
| 1683 | |
| 1684 | } else if (mData) { |
| 1685 | if (objectsSize < mObjectsSize) { |
| 1686 | // Need to release refs on any objects we are dropping. |
| 1687 | const sp<ProcessState> proc(ProcessState::self()); |
| 1688 | for (size_t i=objectsSize; i<mObjectsSize; i++) { |
| 1689 | const flat_binder_object* flat |
| 1690 | = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]); |
| 1691 | if (flat->type == BINDER_TYPE_FD) { |
| 1692 | // will need to rescan because we may have lopped off the only FDs |
| 1693 | mFdsKnown = false; |
| 1694 | } |
| 1695 | release_object(proc, *flat, this); |
| 1696 | } |
Arve Hjønnevåg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1697 | binder_size_t* objects = |
| 1698 | (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] | 1699 | if (objects) { |
| 1700 | mObjects = objects; |
| 1701 | } |
| 1702 | mObjectsSize = objectsSize; |
| 1703 | mNextObjectHint = 0; |
| 1704 | } |
| 1705 | |
| 1706 | // We own the data, so we can just do a realloc(). |
| 1707 | if (desired > mDataCapacity) { |
| 1708 | uint8_t* data = (uint8_t*)realloc(mData, desired); |
| 1709 | if (data) { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1710 | LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity, |
| 1711 | desired); |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1712 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1713 | gParcelGlobalAllocSize += desired; |
| 1714 | gParcelGlobalAllocSize -= mDataCapacity; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1715 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1716 | mData = data; |
| 1717 | mDataCapacity = desired; |
| 1718 | } else if (desired > mDataCapacity) { |
| 1719 | mError = NO_MEMORY; |
| 1720 | return NO_MEMORY; |
| 1721 | } |
| 1722 | } else { |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1723 | if (mDataSize > desired) { |
| 1724 | mDataSize = desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1725 | ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); |
Dianne Hackborn | 97e2bcd | 2011-04-13 18:15:56 -0700 | [diff] [blame] | 1726 | } |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1727 | if (mDataPos > desired) { |
| 1728 | mDataPos = desired; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1729 | 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] | 1730 | } |
| 1731 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1732 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1733 | } else { |
| 1734 | // This is the first data. Easy! |
| 1735 | uint8_t* data = (uint8_t*)malloc(desired); |
| 1736 | if (!data) { |
| 1737 | mError = NO_MEMORY; |
| 1738 | return NO_MEMORY; |
| 1739 | } |
Hyejin Kim | 3f727c0 | 2013-03-09 11:28:54 +0900 | [diff] [blame] | 1740 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1741 | if(!(mDataCapacity == 0 && mObjects == NULL |
| 1742 | && mObjectsCapacity == 0)) { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1743 | 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] | 1744 | } |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1745 | |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1746 | LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired); |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1747 | pthread_mutex_lock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1748 | gParcelGlobalAllocSize += desired; |
| 1749 | gParcelGlobalAllocCount++; |
Dianne Hackborn | a4cff88 | 2014-11-13 17:07:40 -0800 | [diff] [blame] | 1750 | pthread_mutex_unlock(&gParcelGlobalAllocSizeLock); |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1751 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1752 | mData = data; |
| 1753 | mDataSize = mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1754 | ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); |
| 1755 | 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] | 1756 | mDataCapacity = desired; |
| 1757 | } |
| 1758 | |
| 1759 | return NO_ERROR; |
| 1760 | } |
| 1761 | |
| 1762 | void Parcel::initState() |
| 1763 | { |
Dianne Hackborn | 7e790af | 2014-11-11 12:22:53 -0800 | [diff] [blame] | 1764 | LOG_ALLOC("Parcel %p: initState", this); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1765 | mError = NO_ERROR; |
| 1766 | mData = 0; |
| 1767 | mDataSize = 0; |
| 1768 | mDataCapacity = 0; |
| 1769 | mDataPos = 0; |
Mark Salyzyn | d4ecccf | 2014-05-30 16:35:57 -0700 | [diff] [blame] | 1770 | ALOGV("initState Setting data size of %p to %zu", this, mDataSize); |
| 1771 | 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] | 1772 | mObjects = NULL; |
| 1773 | mObjectsSize = 0; |
| 1774 | mObjectsCapacity = 0; |
| 1775 | mNextObjectHint = 0; |
| 1776 | mHasFds = false; |
| 1777 | mFdsKnown = true; |
Dianne Hackborn | 8938ed2 | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1778 | mAllowFds = true; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1779 | mOwner = NULL; |
| 1780 | } |
| 1781 | |
| 1782 | void Parcel::scanForFds() const |
| 1783 | { |
| 1784 | bool hasFds = false; |
| 1785 | for (size_t i=0; i<mObjectsSize; i++) { |
| 1786 | const flat_binder_object* flat |
| 1787 | = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]); |
| 1788 | if (flat->type == BINDER_TYPE_FD) { |
| 1789 | hasFds = true; |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | mHasFds = hasFds; |
| 1794 | mFdsKnown = true; |
| 1795 | } |
| 1796 | |
Jeff Brown | 5707dbf | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 1797 | // --- Parcel::Blob --- |
| 1798 | |
| 1799 | Parcel::Blob::Blob() : |
| 1800 | mMapped(false), mData(NULL), mSize(0) { |
| 1801 | } |
| 1802 | |
| 1803 | Parcel::Blob::~Blob() { |
| 1804 | release(); |
| 1805 | } |
| 1806 | |
| 1807 | void Parcel::Blob::release() { |
| 1808 | if (mMapped && mData) { |
| 1809 | ::munmap(mData, mSize); |
| 1810 | } |
| 1811 | clear(); |
| 1812 | } |
| 1813 | |
| 1814 | void Parcel::Blob::init(bool mapped, void* data, size_t size) { |
| 1815 | mMapped = mapped; |
| 1816 | mData = data; |
| 1817 | mSize = size; |
| 1818 | } |
| 1819 | |
| 1820 | void Parcel::Blob::clear() { |
| 1821 | mMapped = false; |
| 1822 | mData = NULL; |
| 1823 | mSize = 0; |
| 1824 | } |
| 1825 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1826 | }; // namespace android |