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