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