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