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