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