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