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