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