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