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