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