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