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