blob: c4817510ce276fe8e4bf717c938e697311270f51 [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Parcel"
18//#define LOG_NDEBUG 0
19
Mark Salyzyndfc272d2016-01-27 08:02:48 -080020#include <errno.h>
21#include <inttypes.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/mman.h>
Mark Salyzyn71379b72016-01-27 08:02:48 -080026#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070029
Mathias Agopian16475702009-05-19 19:08:10 -070030#include <binder/Binder.h>
31#include <binder/BpBinder.h>
Mark Salyzyndfc272d2016-01-27 08:02:48 -080032#include <binder/IPCThreadState.h>
33#include <binder/Parcel.h>
Mathias Agopian16475702009-05-19 19:08:10 -070034#include <binder/ProcessState.h>
Christopher Wiley75cdfa92015-11-09 15:06:15 -080035#include <binder/Status.h>
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070036#include <binder/TextOutput.h>
37
Mark Salyzyndfc272d2016-01-27 08:02:48 -080038#include <cutils/ashmem.h>
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070039#include <utils/Debug.h>
Mark Salyzyndfc272d2016-01-27 08:02:48 -080040#include <utils/Flattenable.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070041#include <utils/Log.h>
Mark Salyzyndfc272d2016-01-27 08:02:48 -080042#include <utils/misc.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070043#include <utils/String8.h>
44#include <utils/String16.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070045
46#include <private/binder/binder_module.h>
Dianne Hackborn492acff2014-11-11 12:22:53 -080047#include <private/binder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070048
Mathias Agopian7922fa22009-05-18 15:08:03 -070049#ifndef INT32_MAX
50#define INT32_MAX ((int32_t)(2147483647))
51#endif
52
53#define LOG_REFS(...)
Mark Salyzynf9afaf52016-01-27 08:02:48 -080054//#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Dianne Hackborn492acff2014-11-11 12:22:53 -080055#define LOG_ALLOC(...)
Mark Salyzynf9afaf52016-01-27 08:02:48 -080056//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
Mathias Agopian7922fa22009-05-18 15:08:03 -070057
58// ---------------------------------------------------------------------------
59
Nick Kralevich15961372015-04-02 09:36:02 -070060// This macro should never be used at runtime, as a too large value
61// of s could cause an integer overflow. Instead, you should always
62// use the wrapper function pad_size()
63#define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
64
65static size_t pad_size(size_t s) {
66 if (s > (SIZE_T_MAX - 3)) {
67 abort();
68 }
69 return PAD_SIZE_UNSAFE(s);
70}
Mathias Agopian7922fa22009-05-18 15:08:03 -070071
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -070072// Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
Jeff Sharkeyf51ee4b2014-12-18 10:26:57 -080073#define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -070074
Mathias Agopian7922fa22009-05-18 15:08:03 -070075// XXX This can be made public if we want to provide
76// support for typed data.
77struct small_flat_data
78{
79 uint32_t type;
80 uint32_t data;
81};
82
83namespace android {
84
Dianne Hackborn353d79a2014-11-13 17:07:40 -080085static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
86static size_t gParcelGlobalAllocSize = 0;
87static size_t gParcelGlobalAllocCount = 0;
88
Jeff Browna091b542014-11-11 16:44:25 -080089// Maximum size of a blob to transfer in-place.
90static const size_t BLOB_INPLACE_LIMIT = 16 * 1024;
91
92enum {
93 BLOB_INPLACE = 0,
94 BLOB_ASHMEM_IMMUTABLE = 1,
95 BLOB_ASHMEM_MUTABLE = 2,
96};
97
Mathias Agopian7922fa22009-05-18 15:08:03 -070098void acquire_object(const sp<ProcessState>& proc,
Adrian Roos95e4f022015-10-22 16:12:53 -070099 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700100{
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åga5440702014-01-28 20:12:59 -0800105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700111 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: {
Mark Salyzyn71379b72016-01-27 08:02:48 -0800126 if ((obj.cookie != 0) && (outAshmemSize != NULL)) {
127 struct stat st;
128 int ret = fstat(obj.handle, &st);
129 if (!ret && S_ISCHR(st.st_mode)) {
Adrian Roos1b5149e2015-10-22 16:46:12 -0700130 // If we own an ashmem fd, keep track of how much memory it refers to.
131 int size = ashmem_get_size_region(obj.handle);
132 if (size > 0) {
133 *outAshmemSize += size;
134 }
Adrian Roos95e4f022015-10-22 16:12:53 -0700135 }
136 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700137 return;
138 }
139 }
140
Colin Crossf0487982014-02-05 17:42:44 -0800141 ALOGD("Invalid object type 0x%08x", obj.type);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700142}
143
Adrian Roos1b5149e2015-10-22 16:46:12 -0700144void acquire_object(const sp<ProcessState>& proc,
145 const flat_binder_object& obj, const void* who)
146{
147 acquire_object(proc, obj, who, NULL);
148}
149
150static void release_object(const sp<ProcessState>& proc,
Adrian Roos95e4f022015-10-22 16:12:53 -0700151 const flat_binder_object& obj, const void* who, size_t* outAshmemSize)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700152{
153 switch (obj.type) {
154 case BINDER_TYPE_BINDER:
155 if (obj.binder) {
156 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800157 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700158 }
159 return;
160 case BINDER_TYPE_WEAK_BINDER:
161 if (obj.binder)
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800162 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700163 return;
164 case BINDER_TYPE_HANDLE: {
165 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
166 if (b != NULL) {
167 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
168 b->decStrong(who);
169 }
170 return;
171 }
172 case BINDER_TYPE_WEAK_HANDLE: {
173 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
174 if (b != NULL) b.get_refs()->decWeak(who);
175 return;
176 }
177 case BINDER_TYPE_FD: {
Mark Salyzynd40445a2016-01-27 08:02:48 -0800178 if (obj.cookie != 0) { // owned
179 if (outAshmemSize != NULL) {
Mark Salyzyn71379b72016-01-27 08:02:48 -0800180 struct stat st;
181 int ret = fstat(obj.handle, &st);
182 if (!ret && S_ISCHR(st.st_mode)) {
183 int size = ashmem_get_size_region(obj.handle);
184 if (size > 0) {
185 *outAshmemSize -= size;
186 }
Adrian Roos1b5149e2015-10-22 16:46:12 -0700187 }
Adrian Roos1b5149e2015-10-22 16:46:12 -0700188 }
Mark Salyzynd40445a2016-01-27 08:02:48 -0800189
190 close(obj.handle);
Adrian Roos95e4f022015-10-22 16:12:53 -0700191 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700192 return;
193 }
194 }
195
Colin Crossf0487982014-02-05 17:42:44 -0800196 ALOGE("Invalid object type 0x%08x", obj.type);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700197}
198
Adrian Roos1b5149e2015-10-22 16:46:12 -0700199void release_object(const sp<ProcessState>& proc,
200 const flat_binder_object& obj, const void* who)
201{
202 release_object(proc, obj, who, NULL);
203}
204
Mathias Agopian7922fa22009-05-18 15:08:03 -0700205inline static status_t finish_flatten_binder(
Colin Crossf0487982014-02-05 17:42:44 -0800206 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700207{
208 return out->writeObject(flat, false);
209}
210
Colin Crossf0487982014-02-05 17:42:44 -0800211status_t flatten_binder(const sp<ProcessState>& /*proc*/,
Mathias Agopian7922fa22009-05-18 15:08:03 -0700212 const sp<IBinder>& binder, Parcel* out)
213{
214 flat_binder_object obj;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700215
Mathias Agopian7922fa22009-05-18 15:08:03 -0700216 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
217 if (binder != NULL) {
218 IBinder *local = binder->localBinder();
219 if (!local) {
220 BpBinder *proxy = binder->remoteBinder();
221 if (proxy == NULL) {
Steve Blockeafc6212012-01-06 19:20:56 +0000222 ALOGE("null proxy");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700223 }
224 const int32_t handle = proxy ? proxy->handle() : 0;
225 obj.type = BINDER_TYPE_HANDLE;
Arve Hjønnevågea8e05d2014-02-18 21:10:29 -0800226 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Mathias Agopian7922fa22009-05-18 15:08:03 -0700227 obj.handle = handle;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800228 obj.cookie = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700229 } else {
230 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800231 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
232 obj.cookie = reinterpret_cast<uintptr_t>(local);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700233 }
234 } else {
235 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800236 obj.binder = 0;
237 obj.cookie = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700238 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700239
Mathias Agopian7922fa22009-05-18 15:08:03 -0700240 return finish_flatten_binder(binder, obj, out);
241}
242
Colin Crossf0487982014-02-05 17:42:44 -0800243status_t flatten_binder(const sp<ProcessState>& /*proc*/,
Mathias Agopian7922fa22009-05-18 15:08:03 -0700244 const wp<IBinder>& binder, Parcel* out)
245{
246 flat_binder_object obj;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700247
Mathias Agopian7922fa22009-05-18 15:08:03 -0700248 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
249 if (binder != NULL) {
250 sp<IBinder> real = binder.promote();
251 if (real != NULL) {
252 IBinder *local = real->localBinder();
253 if (!local) {
254 BpBinder *proxy = real->remoteBinder();
255 if (proxy == NULL) {
Steve Blockeafc6212012-01-06 19:20:56 +0000256 ALOGE("null proxy");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700257 }
258 const int32_t handle = proxy ? proxy->handle() : 0;
259 obj.type = BINDER_TYPE_WEAK_HANDLE;
Arve Hjønnevågea8e05d2014-02-18 21:10:29 -0800260 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Mathias Agopian7922fa22009-05-18 15:08:03 -0700261 obj.handle = handle;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800262 obj.cookie = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700263 } else {
264 obj.type = BINDER_TYPE_WEAK_BINDER;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800265 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
266 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700267 }
268 return finish_flatten_binder(real, obj, out);
269 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700270
Mathias Agopian7922fa22009-05-18 15:08:03 -0700271 // XXX How to deal? In order to flatten the given binder,
272 // we need to probe it for information, which requires a primary
273 // reference... but we don't have one.
274 //
275 // The OpenBinder implementation uses a dynamic_cast<> here,
276 // but we can't do that with the different reference counting
277 // implementation we are using.
Steve Blockeafc6212012-01-06 19:20:56 +0000278 ALOGE("Unable to unflatten Binder weak reference!");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700279 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800280 obj.binder = 0;
281 obj.cookie = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700282 return finish_flatten_binder(NULL, obj, out);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700283
Mathias Agopian7922fa22009-05-18 15:08:03 -0700284 } else {
285 obj.type = BINDER_TYPE_BINDER;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800286 obj.binder = 0;
287 obj.cookie = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700288 return finish_flatten_binder(NULL, obj, out);
289 }
290}
291
292inline static status_t finish_unflatten_binder(
Colin Crossf0487982014-02-05 17:42:44 -0800293 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
294 const Parcel& /*in*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700295{
296 return NO_ERROR;
297}
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700298
Mathias Agopian7922fa22009-05-18 15:08:03 -0700299status_t unflatten_binder(const sp<ProcessState>& proc,
300 const Parcel& in, sp<IBinder>* out)
301{
302 const flat_binder_object* flat = in.readObject(false);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700303
Mathias Agopian7922fa22009-05-18 15:08:03 -0700304 if (flat) {
305 switch (flat->type) {
306 case BINDER_TYPE_BINDER:
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800307 *out = reinterpret_cast<IBinder*>(flat->cookie);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700308 return finish_unflatten_binder(NULL, *flat, in);
309 case BINDER_TYPE_HANDLE:
310 *out = proc->getStrongProxyForHandle(flat->handle);
311 return finish_unflatten_binder(
312 static_cast<BpBinder*>(out->get()), *flat, in);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700313 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700314 }
315 return BAD_TYPE;
316}
317
318status_t unflatten_binder(const sp<ProcessState>& proc,
319 const Parcel& in, wp<IBinder>* out)
320{
321 const flat_binder_object* flat = in.readObject(false);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700322
Mathias Agopian7922fa22009-05-18 15:08:03 -0700323 if (flat) {
324 switch (flat->type) {
325 case BINDER_TYPE_BINDER:
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800326 *out = reinterpret_cast<IBinder*>(flat->cookie);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700327 return finish_unflatten_binder(NULL, *flat, in);
328 case BINDER_TYPE_WEAK_BINDER:
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800329 if (flat->binder != 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700330 out->set_object_and_refs(
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800331 reinterpret_cast<IBinder*>(flat->cookie),
332 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700333 } else {
334 *out = NULL;
335 }
336 return finish_unflatten_binder(NULL, *flat, in);
337 case BINDER_TYPE_HANDLE:
338 case BINDER_TYPE_WEAK_HANDLE:
339 *out = proc->getWeakProxyForHandle(flat->handle);
340 return finish_unflatten_binder(
341 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
342 }
343 }
344 return BAD_TYPE;
345}
346
Christopher Wiley02b3c172015-11-10 09:44:30 -0800347namespace {
348
349template<typename T>
350status_t readTypedVector(std::vector<T>* val, const Parcel* p,
351 status_t(Parcel::*read_func)(T*) const) {
352 val->clear();
353
354 int32_t size;
355 status_t status = p->readInt32(&size);
356
357 if (status != OK) {
358 return status;
359 }
360
361 if (size < 0) {
362 return UNEXPECTED_NULL;
363 }
364
365 val->resize(size);
366
367 for (auto& v: *val) {
368 status = (p->*read_func)(&v);
369
370 if (status != OK) {
371 return status;
372 }
373 }
374
375 return OK;
376}
377
378} // namespace
379
Mathias Agopian7922fa22009-05-18 15:08:03 -0700380// ---------------------------------------------------------------------------
381
382Parcel::Parcel()
383{
Dianne Hackborn492acff2014-11-11 12:22:53 -0800384 LOG_ALLOC("Parcel %p: constructing", this);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700385 initState();
386}
387
388Parcel::~Parcel()
389{
390 freeDataNoInit();
Dianne Hackborn492acff2014-11-11 12:22:53 -0800391 LOG_ALLOC("Parcel %p: destroyed", this);
392}
393
394size_t Parcel::getGlobalAllocSize() {
Dianne Hackborn353d79a2014-11-13 17:07:40 -0800395 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
396 size_t size = gParcelGlobalAllocSize;
397 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
398 return size;
Dianne Hackborn492acff2014-11-11 12:22:53 -0800399}
400
401size_t Parcel::getGlobalAllocCount() {
Dianne Hackborn353d79a2014-11-13 17:07:40 -0800402 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
403 size_t count = gParcelGlobalAllocCount;
404 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
405 return count;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700406}
407
408const uint8_t* Parcel::data() const
409{
410 return mData;
411}
412
413size_t Parcel::dataSize() const
414{
415 return (mDataSize > mDataPos ? mDataSize : mDataPos);
416}
417
418size_t Parcel::dataAvail() const
419{
Nick Kralevich37bcab02015-09-16 09:49:15 -0700420 size_t result = dataSize() - dataPosition();
421 if (result > INT32_MAX) {
422 abort();
423 }
424 return result;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700425}
426
427size_t Parcel::dataPosition() const
428{
429 return mDataPos;
430}
431
432size_t Parcel::dataCapacity() const
433{
434 return mDataCapacity;
435}
436
437status_t Parcel::setDataSize(size_t size)
438{
Nick Kralevich15961372015-04-02 09:36:02 -0700439 if (size > INT32_MAX) {
440 // don't accept size_t values which may have come from an
441 // inadvertent conversion from a negative int.
442 return BAD_VALUE;
443 }
444
Mathias Agopian7922fa22009-05-18 15:08:03 -0700445 status_t err;
446 err = continueWrite(size);
447 if (err == NO_ERROR) {
448 mDataSize = size;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700449 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700450 }
451 return err;
452}
453
454void Parcel::setDataPosition(size_t pos) const
455{
Nick Kralevich15961372015-04-02 09:36:02 -0700456 if (pos > INT32_MAX) {
457 // don't accept size_t values which may have come from an
458 // inadvertent conversion from a negative int.
459 abort();
460 }
461
Mathias Agopian7922fa22009-05-18 15:08:03 -0700462 mDataPos = pos;
463 mNextObjectHint = 0;
464}
465
466status_t Parcel::setDataCapacity(size_t size)
467{
Nick Kralevich15961372015-04-02 09:36:02 -0700468 if (size > INT32_MAX) {
469 // don't accept size_t values which may have come from an
470 // inadvertent conversion from a negative int.
471 return BAD_VALUE;
472 }
473
Dianne Hackborndb8c4942011-04-13 18:15:56 -0700474 if (size > mDataCapacity) return continueWrite(size);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700475 return NO_ERROR;
476}
477
478status_t Parcel::setData(const uint8_t* buffer, size_t len)
479{
Nick Kralevich15961372015-04-02 09:36:02 -0700480 if (len > INT32_MAX) {
481 // don't accept size_t values which may have come from an
482 // inadvertent conversion from a negative int.
483 return BAD_VALUE;
484 }
485
Mathias Agopian7922fa22009-05-18 15:08:03 -0700486 status_t err = restartWrite(len);
487 if (err == NO_ERROR) {
488 memcpy(const_cast<uint8_t*>(data()), buffer, len);
489 mDataSize = len;
490 mFdsKnown = false;
491 }
492 return err;
493}
494
Andreas Huber6cb95082011-04-13 10:21:56 -0700495status_t Parcel::appendFrom(const Parcel *parcel, size_t offset, size_t len)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700496{
497 const sp<ProcessState> proc(ProcessState::self());
498 status_t err;
Andreas Huber6cb95082011-04-13 10:21:56 -0700499 const uint8_t *data = parcel->mData;
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800500 const binder_size_t *objects = parcel->mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700501 size_t size = parcel->mObjectsSize;
502 int startPos = mDataPos;
503 int firstIndex = -1, lastIndex = -2;
504
505 if (len == 0) {
506 return NO_ERROR;
507 }
508
Nick Kralevich15961372015-04-02 09:36:02 -0700509 if (len > INT32_MAX) {
510 // don't accept size_t values which may have come from an
511 // inadvertent conversion from a negative int.
512 return BAD_VALUE;
513 }
514
Mathias Agopian7922fa22009-05-18 15:08:03 -0700515 // range checks against the source parcel size
516 if ((offset > parcel->mDataSize)
517 || (len > parcel->mDataSize)
518 || (offset + len > parcel->mDataSize)) {
519 return BAD_VALUE;
520 }
521
522 // Count objects in range
523 for (int i = 0; i < (int) size; i++) {
524 size_t off = objects[i];
Christopher Tatebc76b9a2015-05-27 17:53:02 -0700525 if ((off >= offset) && (off + sizeof(flat_binder_object) <= offset + len)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700526 if (firstIndex == -1) {
527 firstIndex = i;
528 }
529 lastIndex = i;
530 }
531 }
532 int numObjects = lastIndex - firstIndex + 1;
533
Dianne Hackborndb8c4942011-04-13 18:15:56 -0700534 if ((mDataSize+len) > mDataCapacity) {
535 // grow data
536 err = growData(len);
537 if (err != NO_ERROR) {
538 return err;
539 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700540 }
541
542 // append data
543 memcpy(mData + mDataPos, data + offset, len);
544 mDataPos += len;
545 mDataSize += len;
546
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400547 err = NO_ERROR;
548
Mathias Agopian7922fa22009-05-18 15:08:03 -0700549 if (numObjects > 0) {
550 // grow objects
551 if (mObjectsCapacity < mObjectsSize + numObjects) {
Christopher Tateabaa7622015-06-08 14:45:14 -0700552 size_t newSize = ((mObjectsSize + numObjects)*3)/2;
553 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800554 binder_size_t *objects =
555 (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
556 if (objects == (binder_size_t*)0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700557 return NO_MEMORY;
558 }
559 mObjects = objects;
560 mObjectsCapacity = newSize;
561 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700562
Mathias Agopian7922fa22009-05-18 15:08:03 -0700563 // append and acquire objects
564 int idx = mObjectsSize;
565 for (int i = firstIndex; i <= lastIndex; i++) {
566 size_t off = objects[i] - offset + startPos;
567 mObjects[idx++] = off;
568 mObjectsSize++;
569
Android (Google) Code Review74feb4c2009-05-22 14:53:18 -0700570 flat_binder_object* flat
Mathias Agopian7922fa22009-05-18 15:08:03 -0700571 = reinterpret_cast<flat_binder_object*>(mData + off);
Adrian Roos95e4f022015-10-22 16:12:53 -0700572 acquire_object(proc, *flat, this, &mOpenAshmemSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700573
Mathias Agopian7922fa22009-05-18 15:08:03 -0700574 if (flat->type == BINDER_TYPE_FD) {
Android (Google) Code Review74feb4c2009-05-22 14:53:18 -0700575 // If this is a file descriptor, we need to dup it so the
576 // new Parcel now owns its own fd, and can declare that we
577 // officially know we have fds.
578 flat->handle = dup(flat->handle);
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800579 flat->cookie = 1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700580 mHasFds = mFdsKnown = true;
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400581 if (!mAllowFds) {
582 err = FDS_NOT_ALLOWED;
583 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700584 }
585 }
586 }
587
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400588 return err;
589}
590
Jeff Browna091b542014-11-11 16:44:25 -0800591bool Parcel::allowFds() const
592{
593 return mAllowFds;
594}
595
Dianne Hackbornb0652d92011-10-03 21:09:35 -0700596bool Parcel::pushAllowFds(bool allowFds)
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400597{
598 const bool origValue = mAllowFds;
Dianne Hackbornb0652d92011-10-03 21:09:35 -0700599 if (!allowFds) {
600 mAllowFds = false;
601 }
Dianne Hackbornabc3cf42011-09-28 23:19:47 -0400602 return origValue;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700603}
604
Dianne Hackbornb0652d92011-10-03 21:09:35 -0700605void Parcel::restoreAllowFds(bool lastValue)
606{
607 mAllowFds = lastValue;
608}
609
Mathias Agopian7922fa22009-05-18 15:08:03 -0700610bool Parcel::hasFileDescriptors() const
611{
612 if (!mFdsKnown) {
613 scanForFds();
614 }
615 return mHasFds;
616}
617
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700618// Write RPC headers. (previously just the interface token)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700619status_t Parcel::writeInterfaceToken(const String16& interface)
620{
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -0700621 writeInt32(IPCThreadState::self()->getStrictModePolicy() |
622 STRICT_MODE_PENALTY_GATHER);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700623 // currently the interface identification token is just its name as a string
624 return writeString16(interface);
625}
626
Mathias Agopiana6286c32009-05-22 19:00:22 -0700627bool Parcel::checkInterface(IBinder* binder) const
628{
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700629 return enforceInterface(binder->getInterfaceDescriptor());
Mathias Agopiana6286c32009-05-22 19:00:22 -0700630}
631
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -0700632bool Parcel::enforceInterface(const String16& interface,
Brad Fitzpatrickc1f5bf82010-07-27 09:49:11 -0700633 IPCThreadState* threadState) const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700634{
Brad Fitzpatrickc1f5bf82010-07-27 09:49:11 -0700635 int32_t strictPolicy = readInt32();
636 if (threadState == NULL) {
637 threadState = IPCThreadState::self();
Brad Fitzpatrick3f4ef592010-07-07 16:06:39 -0700638 }
Brad Fitzpatrick24f8bca2010-08-30 16:01:16 -0700639 if ((threadState->getLastTransactionBinderFlags() &
640 IBinder::FLAG_ONEWAY) != 0) {
641 // For one-way calls, the callee is running entirely
642 // disconnected from the caller, so disable StrictMode entirely.
643 // Not only does disk/network usage not impact the caller, but
644 // there's no way to commuicate back any violations anyway.
645 threadState->setStrictModePolicy(0);
646 } else {
647 threadState->setStrictModePolicy(strictPolicy);
648 }
Mathias Agopiana6286c32009-05-22 19:00:22 -0700649 const String16 str(readString16());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700650 if (str == interface) {
651 return true;
652 } else {
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700653 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700654 String8(interface).string(), String8(str).string());
655 return false;
656 }
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700657}
Mathias Agopian7922fa22009-05-18 15:08:03 -0700658
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800659const binder_size_t* Parcel::objects() const
Mathias Agopian7922fa22009-05-18 15:08:03 -0700660{
661 return mObjects;
662}
663
664size_t Parcel::objectsCount() const
665{
666 return mObjectsSize;
667}
668
669status_t Parcel::errorCheck() const
670{
671 return mError;
672}
673
674void Parcel::setError(status_t err)
675{
676 mError = err;
677}
678
679status_t Parcel::finishWrite(size_t len)
680{
Nick Kralevich15961372015-04-02 09:36:02 -0700681 if (len > INT32_MAX) {
682 // don't accept size_t values which may have come from an
683 // inadvertent conversion from a negative int.
684 return BAD_VALUE;
685 }
686
Mathias Agopian7922fa22009-05-18 15:08:03 -0700687 //printf("Finish write of %d\n", len);
688 mDataPos += len;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700689 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700690 if (mDataPos > mDataSize) {
691 mDataSize = mDataPos;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -0700692 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700693 }
694 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
695 return NO_ERROR;
696}
697
698status_t Parcel::writeUnpadded(const void* data, size_t len)
699{
Nick Kralevich15961372015-04-02 09:36:02 -0700700 if (len > INT32_MAX) {
701 // don't accept size_t values which may have come from an
702 // inadvertent conversion from a negative int.
703 return BAD_VALUE;
704 }
705
Mathias Agopian7922fa22009-05-18 15:08:03 -0700706 size_t end = mDataPos + len;
707 if (end < mDataPos) {
708 // integer overflow
709 return BAD_VALUE;
710 }
711
712 if (end <= mDataCapacity) {
713restart_write:
714 memcpy(mData+mDataPos, data, len);
715 return finishWrite(len);
716 }
717
718 status_t err = growData(len);
719 if (err == NO_ERROR) goto restart_write;
720 return err;
721}
722
723status_t Parcel::write(const void* data, size_t len)
724{
Nick Kralevich15961372015-04-02 09:36:02 -0700725 if (len > INT32_MAX) {
726 // don't accept size_t values which may have come from an
727 // inadvertent conversion from a negative int.
728 return BAD_VALUE;
729 }
730
Mathias Agopian7922fa22009-05-18 15:08:03 -0700731 void* const d = writeInplace(len);
732 if (d) {
733 memcpy(d, data, len);
734 return NO_ERROR;
735 }
736 return mError;
737}
738
739void* Parcel::writeInplace(size_t len)
740{
Nick Kralevich15961372015-04-02 09:36:02 -0700741 if (len > INT32_MAX) {
742 // don't accept size_t values which may have come from an
743 // inadvertent conversion from a negative int.
744 return NULL;
745 }
746
747 const size_t padded = pad_size(len);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700748
749 // sanity check for integer overflow
750 if (mDataPos+padded < mDataPos) {
751 return NULL;
752 }
753
754 if ((mDataPos+padded) <= mDataCapacity) {
755restart_write:
756 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
757 uint8_t* const data = mData+mDataPos;
758
759 // Need to pad at end?
760 if (padded != len) {
761#if BYTE_ORDER == BIG_ENDIAN
762 static const uint32_t mask[4] = {
763 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
764 };
765#endif
766#if BYTE_ORDER == LITTLE_ENDIAN
767 static const uint32_t mask[4] = {
768 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
769 };
770#endif
771 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
772 // *reinterpret_cast<void**>(data+padded-4));
773 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
774 }
775
776 finishWrite(padded);
777 return data;
778 }
779
780 status_t err = growData(padded);
781 if (err == NO_ERROR) goto restart_write;
782 return NULL;
783}
784
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800785namespace {
786
787template<typename T, typename U>
788status_t unsafeWriteTypedVector(const std::vector<T>& val, Parcel* p,
789 status_t(Parcel::*write_func)(U)) {
790 if (val.size() > std::numeric_limits<int32_t>::max()) {
791 return BAD_VALUE;
792 }
793
794 status_t status = p->writeInt32(val.size());
795
796 if (status != OK) {
797 return status;
798 }
799
800 for (const auto& item : val) {
801 status = (p->*write_func)(item);
802
803 if (status != OK) {
804 return status;
805 }
806 }
807
808 return OK;
809}
810
811template<typename T>
812status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
813 status_t(Parcel::*write_func)(const T&)) {
814 return unsafeWriteTypedVector(val, p, write_func);
815}
816
817template<typename T>
818status_t writeTypedVector(const std::vector<T>& val, Parcel* p,
819 status_t(Parcel::*write_func)(T)) {
820 return unsafeWriteTypedVector(val, p, write_func);
821}
822
823} // namespace
824
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700825status_t Parcel::writeByteVector(const std::vector<int8_t>& val)
826{
Christopher Wiley42f8fd72015-10-31 13:22:15 -0700827 status_t status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700828 if (val.size() > std::numeric_limits<int32_t>::max()) {
Christopher Wiley42f8fd72015-10-31 13:22:15 -0700829 status = BAD_VALUE;
830 return status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700831 }
832
Christopher Wiley42f8fd72015-10-31 13:22:15 -0700833 status = writeInt32(val.size());
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700834 if (status != OK) {
835 return status;
836 }
837
Christopher Wiley42f8fd72015-10-31 13:22:15 -0700838 void* data = writeInplace(val.size());
839 if (!data) {
840 status = BAD_VALUE;
841 return status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700842 }
843
Christopher Wiley42f8fd72015-10-31 13:22:15 -0700844 memcpy(data, val.data(), val.size());
845 return status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700846}
847
848status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
849{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800850 return writeTypedVector(val, this, &Parcel::writeInt32);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700851}
852
853status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
854{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800855 return writeTypedVector(val, this, &Parcel::writeInt64);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700856}
857
858status_t Parcel::writeFloatVector(const std::vector<float>& val)
859{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800860 return writeTypedVector(val, this, &Parcel::writeFloat);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700861}
862
863status_t Parcel::writeDoubleVector(const std::vector<double>& val)
864{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800865 return writeTypedVector(val, this, &Parcel::writeDouble);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700866}
867
868status_t Parcel::writeBoolVector(const std::vector<bool>& val)
869{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800870 return writeTypedVector(val, this, &Parcel::writeBool);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700871}
872
873status_t Parcel::writeCharVector(const std::vector<char16_t>& val)
874{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800875 return writeTypedVector(val, this, &Parcel::writeChar);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700876}
877
878status_t Parcel::writeString16Vector(const std::vector<String16>& val)
879{
Casey Dahlin4b0e7152015-11-13 13:46:29 -0800880 return writeTypedVector(val, this, &Parcel::writeString16);
Casey Dahlina32ee4e2015-10-19 18:12:18 -0700881}
882
Mathias Agopian7922fa22009-05-18 15:08:03 -0700883status_t Parcel::writeInt32(int32_t val)
884{
Andreas Huberf79b77e2009-08-17 13:33:27 -0700885 return writeAligned(val);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700886}
Dan Stozaff4c1b72014-12-01 10:01:10 -0800887
888status_t Parcel::writeUint32(uint32_t val)
889{
890 return writeAligned(val);
891}
892
Marco Nelissen2d11f222013-10-16 10:57:51 -0700893status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
Nick Kralevich15961372015-04-02 09:36:02 -0700894 if (len > INT32_MAX) {
895 // don't accept size_t values which may have come from an
896 // inadvertent conversion from a negative int.
897 return BAD_VALUE;
898 }
899
Marco Nelissen2d11f222013-10-16 10:57:51 -0700900 if (!val) {
Chad Brubaker055f0002015-06-30 14:03:55 -0700901 return writeInt32(-1);
Marco Nelissen2d11f222013-10-16 10:57:51 -0700902 }
Chad Brubaker055f0002015-06-30 14:03:55 -0700903 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen2d11f222013-10-16 10:57:51 -0700904 if (ret == NO_ERROR) {
905 ret = write(val, len * sizeof(*val));
906 }
907 return ret;
908}
Marco Nelissen0b8b0f82014-03-13 14:17:40 -0700909status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
Nick Kralevich15961372015-04-02 09:36:02 -0700910 if (len > INT32_MAX) {
911 // don't accept size_t values which may have come from an
912 // inadvertent conversion from a negative int.
913 return BAD_VALUE;
914 }
915
Marco Nelissen0b8b0f82014-03-13 14:17:40 -0700916 if (!val) {
Chad Brubaker055f0002015-06-30 14:03:55 -0700917 return writeInt32(-1);
Marco Nelissen0b8b0f82014-03-13 14:17:40 -0700918 }
Chad Brubaker055f0002015-06-30 14:03:55 -0700919 status_t ret = writeInt32(static_cast<uint32_t>(len));
Marco Nelissen0b8b0f82014-03-13 14:17:40 -0700920 if (ret == NO_ERROR) {
921 ret = write(val, len * sizeof(*val));
922 }
923 return ret;
924}
Mathias Agopian7922fa22009-05-18 15:08:03 -0700925
Casey Dahlind563b272015-10-15 15:44:59 -0700926status_t Parcel::writeBool(bool val)
927{
928 return writeInt32(int32_t(val));
929}
930
931status_t Parcel::writeChar(char16_t val)
932{
933 return writeInt32(int32_t(val));
934}
935
936status_t Parcel::writeByte(int8_t val)
937{
938 return writeInt32(int32_t(val));
939}
940
Mathias Agopian7922fa22009-05-18 15:08:03 -0700941status_t Parcel::writeInt64(int64_t val)
942{
Andreas Huberf79b77e2009-08-17 13:33:27 -0700943 return writeAligned(val);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700944}
945
Ronghua Wu5c2eeca2015-03-16 11:11:07 -0700946status_t Parcel::writeUint64(uint64_t val)
947{
948 return writeAligned(val);
949}
950
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000951status_t Parcel::writePointer(uintptr_t val)
952{
Arve Hjønnevåga5440702014-01-28 20:12:59 -0800953 return writeAligned<binder_uintptr_t>(val);
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +0000954}
955
Mathias Agopian7922fa22009-05-18 15:08:03 -0700956status_t Parcel::writeFloat(float val)
957{
Andreas Huberf79b77e2009-08-17 13:33:27 -0700958 return writeAligned(val);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700959}
960
Douglas Leung2ff5c072013-01-11 15:00:55 -0800961#if defined(__mips__) && defined(__mips_hard_float)
962
963status_t Parcel::writeDouble(double val)
964{
965 union {
966 double d;
967 unsigned long long ll;
968 } u;
969 u.d = val;
970 return writeAligned(u.ll);
971}
972
973#else
974
Mathias Agopian7922fa22009-05-18 15:08:03 -0700975status_t Parcel::writeDouble(double val)
976{
Andreas Huberf79b77e2009-08-17 13:33:27 -0700977 return writeAligned(val);
978}
Mathias Agopian7922fa22009-05-18 15:08:03 -0700979
Douglas Leung2ff5c072013-01-11 15:00:55 -0800980#endif
981
Mathias Agopian7922fa22009-05-18 15:08:03 -0700982status_t Parcel::writeCString(const char* str)
983{
984 return write(str, strlen(str)+1);
985}
986
987status_t Parcel::writeString8(const String8& str)
988{
989 status_t err = writeInt32(str.bytes());
Pravat Dalbehera58e68132010-12-15 08:40:00 +0100990 // only write string if its length is more than zero characters,
991 // as readString8 will only read if the length field is non-zero.
992 // this is slightly different from how writeString16 works.
993 if (str.bytes() > 0 && err == NO_ERROR) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700994 err = write(str.string(), str.bytes()+1);
995 }
996 return err;
997}
998
999status_t Parcel::writeString16(const String16& str)
1000{
1001 return writeString16(str.string(), str.size());
1002}
1003
1004status_t Parcel::writeString16(const char16_t* str, size_t len)
1005{
1006 if (str == NULL) return writeInt32(-1);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001007
Mathias Agopian7922fa22009-05-18 15:08:03 -07001008 status_t err = writeInt32(len);
1009 if (err == NO_ERROR) {
1010 len *= sizeof(char16_t);
1011 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
1012 if (data) {
1013 memcpy(data, str, len);
1014 *reinterpret_cast<char16_t*>(data+len) = 0;
1015 return NO_ERROR;
1016 }
1017 err = mError;
1018 }
1019 return err;
1020}
1021
1022status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
1023{
1024 return flatten_binder(ProcessState::self(), val, this);
1025}
1026
Casey Dahlin278e7ee2015-11-03 13:50:37 -08001027status_t Parcel::writeStrongBinderVector(const std::vector<sp<IBinder>>& val)
1028{
Casey Dahlin4b0e7152015-11-13 13:46:29 -08001029 return writeTypedVector(val, this, &Parcel::writeStrongBinder);
Casey Dahlin278e7ee2015-11-03 13:50:37 -08001030}
1031
1032status_t Parcel::readStrongBinderVector(std::vector<sp<IBinder>>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001033 return readTypedVector(val, this, &Parcel::readStrongBinder);
Casey Dahlin278e7ee2015-11-03 13:50:37 -08001034}
1035
Mathias Agopian7922fa22009-05-18 15:08:03 -07001036status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
1037{
1038 return flatten_binder(ProcessState::self(), val, this);
1039}
1040
Mathias Agopian559e4e92009-05-21 16:29:38 -07001041status_t Parcel::writeNativeHandle(const native_handle* handle)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001042{
Mathias Agopianaaac89e2009-07-31 16:12:13 -07001043 if (!handle || handle->version != sizeof(native_handle))
Mathias Agopian7922fa22009-05-18 15:08:03 -07001044 return BAD_TYPE;
1045
1046 status_t err;
Mathias Agopian559e4e92009-05-21 16:29:38 -07001047 err = writeInt32(handle->numFds);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001048 if (err != NO_ERROR) return err;
1049
Mathias Agopian559e4e92009-05-21 16:29:38 -07001050 err = writeInt32(handle->numInts);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001051 if (err != NO_ERROR) return err;
1052
Mathias Agopian559e4e92009-05-21 16:29:38 -07001053 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
1054 err = writeDupFileDescriptor(handle->data[i]);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001055
1056 if (err != NO_ERROR) {
Steve Block52aa6df2011-12-20 16:23:08 +00001057 ALOGD("write native handle, write dup fd failed");
Mathias Agopian7922fa22009-05-18 15:08:03 -07001058 return err;
1059 }
Mathias Agopian559e4e92009-05-21 16:29:38 -07001060 err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001061 return err;
1062}
1063
Jeff Brown5b601192011-11-04 19:01:44 -07001064status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001065{
1066 flat_binder_object obj;
1067 obj.type = BINDER_TYPE_FD;
1068 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
Arve Hjønnevågea8e05d2014-02-18 21:10:29 -08001069 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
Mathias Agopian7922fa22009-05-18 15:08:03 -07001070 obj.handle = fd;
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001071 obj.cookie = takeOwnership ? 1 : 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001072 return writeObject(obj, true);
1073}
1074
1075status_t Parcel::writeDupFileDescriptor(int fd)
1076{
Jeff Brownc407adf2011-11-04 20:19:33 -07001077 int dupFd = dup(fd);
1078 if (dupFd < 0) {
1079 return -errno;
1080 }
1081 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
1082 if (err) {
1083 close(dupFd);
1084 }
1085 return err;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001086}
1087
Jeff Browna091b542014-11-11 16:44:25 -08001088status_t Parcel::writeBlob(size_t len, bool mutableCopy, WritableBlob* outBlob)
Jeff Brown8a11ee52011-09-23 21:17:56 -07001089{
Nick Kralevich15961372015-04-02 09:36:02 -07001090 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
Jeff Browna091b542014-11-11 16:44:25 -08001096 status_t status;
1097 if (!mAllowFds || len <= BLOB_INPLACE_LIMIT) {
Steve Block48f4e152011-10-20 11:56:00 +01001098 ALOGV("writeBlob: write in place");
Jeff Browna091b542014-11-11 16:44:25 -08001099 status = writeInt32(BLOB_INPLACE);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001100 if (status) return status;
1101
1102 void* ptr = writeInplace(len);
1103 if (!ptr) return NO_MEMORY;
1104
Jeff Browna091b542014-11-11 16:44:25 -08001105 outBlob->init(-1, ptr, len, false);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001106 return NO_ERROR;
1107 }
1108
Steve Block48f4e152011-10-20 11:56:00 +01001109 ALOGV("writeBlob: write to ashmem");
Jeff Brown8a11ee52011-09-23 21:17:56 -07001110 int fd = ashmem_create_region("Parcel Blob", len);
1111 if (fd < 0) return NO_MEMORY;
1112
1113 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
1114 if (result < 0) {
Jeff Brown0db491f2011-10-10 14:50:10 -07001115 status = result;
Jeff Brown8a11ee52011-09-23 21:17:56 -07001116 } else {
1117 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1118 if (ptr == MAP_FAILED) {
1119 status = -errno;
1120 } else {
Jeff Browna091b542014-11-11 16:44:25 -08001121 if (!mutableCopy) {
1122 result = ashmem_set_prot_region(fd, PROT_READ);
1123 }
Jeff Brown8a11ee52011-09-23 21:17:56 -07001124 if (result < 0) {
Jeff Brown0db491f2011-10-10 14:50:10 -07001125 status = result;
Jeff Brown8a11ee52011-09-23 21:17:56 -07001126 } else {
Jeff Browna091b542014-11-11 16:44:25 -08001127 status = writeInt32(mutableCopy ? BLOB_ASHMEM_MUTABLE : BLOB_ASHMEM_IMMUTABLE);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001128 if (!status) {
Jeff Brown5b601192011-11-04 19:01:44 -07001129 status = writeFileDescriptor(fd, true /*takeOwnership*/);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001130 if (!status) {
Jeff Browna091b542014-11-11 16:44:25 -08001131 outBlob->init(fd, ptr, len, mutableCopy);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001132 return NO_ERROR;
1133 }
1134 }
1135 }
1136 }
1137 ::munmap(ptr, len);
1138 }
1139 ::close(fd);
1140 return status;
1141}
1142
Jeff Browna091b542014-11-11 16:44:25 -08001143status_t Parcel::writeDupImmutableBlobFileDescriptor(int fd)
1144{
1145 // Must match up with what's done in writeBlob.
1146 if (!mAllowFds) return FDS_NOT_ALLOWED;
1147 status_t status = writeInt32(BLOB_ASHMEM_IMMUTABLE);
1148 if (status) return status;
1149 return writeDupFileDescriptor(fd);
1150}
1151
Mathias Agopian0d8e2e42013-07-29 21:24:40 -07001152status_t Parcel::write(const FlattenableHelperInterface& val)
Mathias Agopian31bc6982010-02-11 17:30:52 -08001153{
1154 status_t err;
1155
1156 // size if needed
Mathias Agopian0d8e2e42013-07-29 21:24:40 -07001157 const size_t len = val.getFlattenedSize();
1158 const size_t fd_count = val.getFdCount();
Mathias Agopian31bc6982010-02-11 17:30:52 -08001159
Nick Kralevich15961372015-04-02 09:36:02 -07001160 if ((len > INT32_MAX) || (fd_count > INT32_MAX)) {
1161 // don't accept size_t values which may have come from an
1162 // inadvertent conversion from a negative int.
1163 return BAD_VALUE;
1164 }
1165
Mathias Agopian31bc6982010-02-11 17:30:52 -08001166 err = this->writeInt32(len);
1167 if (err) return err;
1168
1169 err = this->writeInt32(fd_count);
1170 if (err) return err;
1171
1172 // payload
Nick Kralevich15961372015-04-02 09:36:02 -07001173 void* const buf = this->writeInplace(pad_size(len));
Mathias Agopian31bc6982010-02-11 17:30:52 -08001174 if (buf == NULL)
1175 return BAD_VALUE;
1176
1177 int* fds = NULL;
1178 if (fd_count) {
1179 fds = new int[fd_count];
1180 }
1181
1182 err = val.flatten(buf, len, fds, fd_count);
1183 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
1184 err = this->writeDupFileDescriptor( fds[i] );
1185 }
1186
1187 if (fd_count) {
1188 delete [] fds;
1189 }
1190
1191 return err;
1192}
1193
Mathias Agopian7922fa22009-05-18 15:08:03 -07001194status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
1195{
1196 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
1197 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
1198 if (enoughData && enoughObjects) {
1199restart_write:
1200 *reinterpret_cast<flat_binder_object*>(mData+mDataPos) = val;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001201
Christopher Tate78183cc2015-06-03 18:44:15 -07001202 // remember if it's a file descriptor
1203 if (val.type == BINDER_TYPE_FD) {
1204 if (!mAllowFds) {
1205 // fail before modifying our object index
1206 return FDS_NOT_ALLOWED;
1207 }
1208 mHasFds = mFdsKnown = true;
1209 }
1210
Mathias Agopian7922fa22009-05-18 15:08:03 -07001211 // Need to write meta-data?
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001212 if (nullMetaData || val.binder != 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001213 mObjects[mObjectsSize] = mDataPos;
Adrian Roos95e4f022015-10-22 16:12:53 -07001214 acquire_object(ProcessState::self(), val, this, &mOpenAshmemSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001215 mObjectsSize++;
1216 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001217
Mathias Agopian7922fa22009-05-18 15:08:03 -07001218 return finishWrite(sizeof(flat_binder_object));
1219 }
1220
1221 if (!enoughData) {
1222 const status_t err = growData(sizeof(val));
1223 if (err != NO_ERROR) return err;
1224 }
1225 if (!enoughObjects) {
1226 size_t newSize = ((mObjectsSize+2)*3)/2;
Christopher Tateabaa7622015-06-08 14:45:14 -07001227 if (newSize < mObjectsSize) return NO_MEMORY; // overflow
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001228 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
Mathias Agopian7922fa22009-05-18 15:08:03 -07001229 if (objects == NULL) return NO_MEMORY;
1230 mObjects = objects;
1231 mObjectsCapacity = newSize;
1232 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001233
Mathias Agopian7922fa22009-05-18 15:08:03 -07001234 goto restart_write;
1235}
1236
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -07001237status_t Parcel::writeNoException()
1238{
Christopher Wiley75cdfa92015-11-09 15:06:15 -08001239 binder::Status status;
1240 return status.writeToParcel(this);
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -07001241}
1242
Colin Crossf0487982014-02-05 17:42:44 -08001243void Parcel::remove(size_t /*start*/, size_t /*amt*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001244{
1245 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1246}
1247
1248status_t Parcel::read(void* outData, size_t len) const
1249{
Nick Kralevich15961372015-04-02 09:36:02 -07001250 if (len > INT32_MAX) {
1251 // don't accept size_t values which may have come from an
1252 // inadvertent conversion from a negative int.
1253 return BAD_VALUE;
1254 }
1255
1256 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1257 && len <= pad_size(len)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001258 memcpy(outData, mData+mDataPos, len);
Nick Kralevich15961372015-04-02 09:36:02 -07001259 mDataPos += pad_size(len);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001260 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001261 return NO_ERROR;
1262 }
1263 return NOT_ENOUGH_DATA;
1264}
1265
1266const void* Parcel::readInplace(size_t len) const
1267{
Nick Kralevich15961372015-04-02 09:36:02 -07001268 if (len > INT32_MAX) {
1269 // don't accept size_t values which may have come from an
1270 // inadvertent conversion from a negative int.
1271 return NULL;
1272 }
1273
1274 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1275 && len <= pad_size(len)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001276 const void* data = mData+mDataPos;
Nick Kralevich15961372015-04-02 09:36:02 -07001277 mDataPos += pad_size(len);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001278 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001279 return data;
1280 }
1281 return NULL;
1282}
1283
Andreas Huberf79b77e2009-08-17 13:33:27 -07001284template<class T>
1285status_t Parcel::readAligned(T *pArg) const {
Nick Kralevich15961372015-04-02 09:36:02 -07001286 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huberf79b77e2009-08-17 13:33:27 -07001287
1288 if ((mDataPos+sizeof(T)) <= mDataSize) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001289 const void* data = mData+mDataPos;
Andreas Huberf79b77e2009-08-17 13:33:27 -07001290 mDataPos += sizeof(T);
1291 *pArg = *reinterpret_cast<const T*>(data);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001292 return NO_ERROR;
1293 } else {
1294 return NOT_ENOUGH_DATA;
1295 }
1296}
1297
Andreas Huberf79b77e2009-08-17 13:33:27 -07001298template<class T>
1299T Parcel::readAligned() const {
1300 T result;
1301 if (readAligned(&result) != NO_ERROR) {
1302 result = 0;
1303 }
1304
1305 return result;
1306}
1307
1308template<class T>
1309status_t Parcel::writeAligned(T val) {
Nick Kralevich15961372015-04-02 09:36:02 -07001310 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
Andreas Huberf79b77e2009-08-17 13:33:27 -07001311
1312 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1313restart_write:
1314 *reinterpret_cast<T*>(mData+mDataPos) = val;
1315 return finishWrite(sizeof(val));
1316 }
1317
1318 status_t err = growData(sizeof(val));
1319 if (err == NO_ERROR) goto restart_write;
1320 return err;
1321}
1322
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001323status_t Parcel::readByteVector(std::vector<int8_t>* val) const {
1324 val->clear();
1325
1326 int32_t size;
1327 status_t status = readInt32(&size);
1328
1329 if (status != OK) {
1330 return status;
1331 }
1332
Christopher Wiley02b3c172015-11-10 09:44:30 -08001333 if (size < 0) {
1334 status = UNEXPECTED_NULL;
1335 return status;
1336 }
1337 if (size_t(size) > dataAvail()) {
Christopher Wiley42f8fd72015-10-31 13:22:15 -07001338 status = BAD_VALUE;
1339 return status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001340 }
Christopher Wiley02b3c172015-11-10 09:44:30 -08001341
Christopher Wiley42f8fd72015-10-31 13:22:15 -07001342 const void* data = readInplace(size);
1343 if (!data) {
1344 status = BAD_VALUE;
1345 return status;
1346 }
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001347 val->resize(size);
Christopher Wiley42f8fd72015-10-31 13:22:15 -07001348 memcpy(val->data(), data, size);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001349
Christopher Wiley42f8fd72015-10-31 13:22:15 -07001350 return status;
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001351}
1352
1353status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001354 return readTypedVector(val, this, &Parcel::readInt32);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001355}
1356
1357status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001358 return readTypedVector(val, this, &Parcel::readInt64);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001359}
1360
1361status_t Parcel::readFloatVector(std::vector<float>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001362 return readTypedVector(val, this, &Parcel::readFloat);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001363}
1364
1365status_t Parcel::readDoubleVector(std::vector<double>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001366 return readTypedVector(val, this, &Parcel::readDouble);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001367}
1368
1369status_t Parcel::readBoolVector(std::vector<bool>* val) const {
1370 val->clear();
1371
1372 int32_t size;
1373 status_t status = readInt32(&size);
1374
1375 if (status != OK) {
1376 return status;
1377 }
1378
1379 if (size < 0) {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001380 return UNEXPECTED_NULL;
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001381 }
1382
1383 val->resize(size);
1384
1385 /* C++ bool handling means a vector of bools isn't necessarily addressable
1386 * (we might use individual bits)
1387 */
Christopher Wiley424d6602015-10-27 16:33:47 -07001388 bool data;
1389 for (int32_t i = 0; i < size; ++i) {
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001390 status = readBool(&data);
1391 (*val)[i] = data;
1392
1393 if (status != OK) {
1394 return status;
1395 }
1396 }
1397
1398 return OK;
1399}
1400
1401status_t Parcel::readCharVector(std::vector<char16_t>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001402 return readTypedVector(val, this, &Parcel::readChar);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001403}
1404
1405status_t Parcel::readString16Vector(std::vector<String16>* val) const {
Christopher Wiley02b3c172015-11-10 09:44:30 -08001406 return readTypedVector(val, this, &Parcel::readString16);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001407}
1408
1409
Andreas Huberf79b77e2009-08-17 13:33:27 -07001410status_t Parcel::readInt32(int32_t *pArg) const
1411{
1412 return readAligned(pArg);
1413}
1414
Mathias Agopian7922fa22009-05-18 15:08:03 -07001415int32_t Parcel::readInt32() const
1416{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001417 return readAligned<int32_t>();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001418}
1419
Dan Stozaff4c1b72014-12-01 10:01:10 -08001420status_t Parcel::readUint32(uint32_t *pArg) const
1421{
1422 return readAligned(pArg);
1423}
1424
1425uint32_t Parcel::readUint32() const
1426{
1427 return readAligned<uint32_t>();
1428}
Mathias Agopian7922fa22009-05-18 15:08:03 -07001429
1430status_t Parcel::readInt64(int64_t *pArg) const
1431{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001432 return readAligned(pArg);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001433}
1434
1435
1436int64_t Parcel::readInt64() const
1437{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001438 return readAligned<int64_t>();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001439}
1440
Ronghua Wu5c2eeca2015-03-16 11:11:07 -07001441status_t Parcel::readUint64(uint64_t *pArg) const
1442{
1443 return readAligned(pArg);
1444}
1445
1446uint64_t Parcel::readUint64() const
1447{
1448 return readAligned<uint64_t>();
1449}
1450
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001451status_t Parcel::readPointer(uintptr_t *pArg) const
1452{
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001453 status_t ret;
1454 binder_uintptr_t ptr;
1455 ret = readAligned(&ptr);
1456 if (!ret)
1457 *pArg = ptr;
1458 return ret;
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001459}
1460
1461uintptr_t Parcel::readPointer() const
1462{
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001463 return readAligned<binder_uintptr_t>();
Serban Constantinescu4ca5baf2013-11-05 16:53:55 +00001464}
1465
1466
Mathias Agopian7922fa22009-05-18 15:08:03 -07001467status_t Parcel::readFloat(float *pArg) const
1468{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001469 return readAligned(pArg);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001470}
1471
1472
1473float Parcel::readFloat() const
1474{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001475 return readAligned<float>();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001476}
1477
Douglas Leung2ff5c072013-01-11 15:00:55 -08001478#if defined(__mips__) && defined(__mips_hard_float)
1479
1480status_t Parcel::readDouble(double *pArg) const
1481{
1482 union {
1483 double d;
1484 unsigned long long ll;
1485 } u;
Narayan Kamath9a0a2012014-06-04 15:04:29 +01001486 u.d = 0;
Douglas Leung2ff5c072013-01-11 15:00:55 -08001487 status_t status;
1488 status = readAligned(&u.ll);
1489 *pArg = u.d;
1490 return status;
1491}
1492
1493double Parcel::readDouble() const
1494{
1495 union {
1496 double d;
1497 unsigned long long ll;
1498 } u;
1499 u.ll = readAligned<unsigned long long>();
1500 return u.d;
1501}
1502
1503#else
1504
Mathias Agopian7922fa22009-05-18 15:08:03 -07001505status_t Parcel::readDouble(double *pArg) const
1506{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001507 return readAligned(pArg);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001508}
1509
Mathias Agopian7922fa22009-05-18 15:08:03 -07001510double Parcel::readDouble() const
1511{
Andreas Huberf79b77e2009-08-17 13:33:27 -07001512 return readAligned<double>();
1513}
1514
Douglas Leung2ff5c072013-01-11 15:00:55 -08001515#endif
1516
Andreas Huberf79b77e2009-08-17 13:33:27 -07001517status_t Parcel::readIntPtr(intptr_t *pArg) const
1518{
1519 return readAligned(pArg);
1520}
1521
1522
1523intptr_t Parcel::readIntPtr() const
1524{
1525 return readAligned<intptr_t>();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001526}
1527
Casey Dahlind563b272015-10-15 15:44:59 -07001528status_t Parcel::readBool(bool *pArg) const
1529{
1530 int32_t tmp;
1531 status_t ret = readInt32(&tmp);
1532 *pArg = (tmp != 0);
1533 return ret;
1534}
1535
1536bool Parcel::readBool() const
1537{
1538 return readInt32() != 0;
1539}
1540
1541status_t Parcel::readChar(char16_t *pArg) const
1542{
1543 int32_t tmp;
1544 status_t ret = readInt32(&tmp);
1545 *pArg = char16_t(tmp);
1546 return ret;
1547}
1548
1549char16_t Parcel::readChar() const
1550{
1551 return char16_t(readInt32());
1552}
1553
1554status_t Parcel::readByte(int8_t *pArg) const
1555{
1556 int32_t tmp;
1557 status_t ret = readInt32(&tmp);
1558 *pArg = int8_t(tmp);
1559 return ret;
1560}
1561
1562int8_t Parcel::readByte() const
1563{
1564 return int8_t(readInt32());
1565}
Mathias Agopian7922fa22009-05-18 15:08:03 -07001566
1567const char* Parcel::readCString() const
1568{
1569 const size_t avail = mDataSize-mDataPos;
1570 if (avail > 0) {
1571 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1572 // is the string's trailing NUL within the parcel's valid bounds?
1573 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1574 if (eos) {
1575 const size_t len = eos - str;
Nick Kralevich15961372015-04-02 09:36:02 -07001576 mDataPos += pad_size(len+1);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001577 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001578 return str;
1579 }
1580 }
1581 return NULL;
1582}
1583
1584String8 Parcel::readString8() const
1585{
1586 int32_t size = readInt32();
1587 // watch for potential int overflow adding 1 for trailing NUL
1588 if (size > 0 && size < INT32_MAX) {
1589 const char* str = (const char*)readInplace(size+1);
1590 if (str) return String8(str, size);
1591 }
1592 return String8();
1593}
1594
1595String16 Parcel::readString16() const
1596{
1597 size_t len;
1598 const char16_t* str = readString16Inplace(&len);
1599 if (str) return String16(str, len);
Steve Blockeafc6212012-01-06 19:20:56 +00001600 ALOGE("Reading a NULL string not supported here.");
Mathias Agopian7922fa22009-05-18 15:08:03 -07001601 return String16();
1602}
1603
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001604status_t Parcel::readString16(String16* pArg) const
1605{
1606 size_t len;
1607 const char16_t* str = readString16Inplace(&len);
1608 if (str) {
Casey Dahlinee3efac2015-10-20 16:26:23 -07001609 pArg->setTo(str, len);
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001610 return 0;
1611 } else {
1612 *pArg = String16();
Christopher Wiley02b3c172015-11-10 09:44:30 -08001613 return UNEXPECTED_NULL;
Casey Dahlina32ee4e2015-10-19 18:12:18 -07001614 }
1615}
1616
Mathias Agopian7922fa22009-05-18 15:08:03 -07001617const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1618{
1619 int32_t size = readInt32();
1620 // watch for potential int overflow from size+1
1621 if (size >= 0 && size < INT32_MAX) {
1622 *outLen = size;
1623 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1624 if (str != NULL) {
1625 return str;
1626 }
1627 }
1628 *outLen = 0;
1629 return NULL;
1630}
1631
Casey Dahlin9575e152015-10-27 18:33:56 -07001632status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1633{
1634 return unflatten_binder(ProcessState::self(), *this, val);
1635}
1636
Mathias Agopian7922fa22009-05-18 15:08:03 -07001637sp<IBinder> Parcel::readStrongBinder() const
1638{
1639 sp<IBinder> val;
Casey Dahlin9575e152015-10-27 18:33:56 -07001640 readStrongBinder(&val);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001641 return val;
1642}
1643
1644wp<IBinder> Parcel::readWeakBinder() const
1645{
1646 wp<IBinder> val;
1647 unflatten_binder(ProcessState::self(), *this, &val);
1648 return val;
1649}
1650
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -07001651int32_t Parcel::readExceptionCode() const
1652{
Christopher Wiley75cdfa92015-11-09 15:06:15 -08001653 binder::Status status;
1654 status.readFromParcel(*this);
1655 return status.exceptionCode();
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -07001656}
Mathias Agopian7922fa22009-05-18 15:08:03 -07001657
Mathias Agopian559e4e92009-05-21 16:29:38 -07001658native_handle* Parcel::readNativeHandle() const
Mathias Agopian7922fa22009-05-18 15:08:03 -07001659{
1660 int numFds, numInts;
1661 status_t err;
1662 err = readInt32(&numFds);
1663 if (err != NO_ERROR) return 0;
1664 err = readInt32(&numInts);
1665 if (err != NO_ERROR) return 0;
1666
Mathias Agopian559e4e92009-05-21 16:29:38 -07001667 native_handle* h = native_handle_create(numFds, numInts);
Adam Lesinski5eb54672015-05-12 17:35:48 -07001668 if (!h) {
1669 return 0;
1670 }
1671
Mathias Agopian7922fa22009-05-18 15:08:03 -07001672 for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
1673 h->data[i] = dup(readFileDescriptor());
1674 if (h->data[i] < 0) err = BAD_VALUE;
1675 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001676 err = read(h->data + numFds, sizeof(int)*numInts);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001677 if (err != NO_ERROR) {
Mathias Agopian559e4e92009-05-21 16:29:38 -07001678 native_handle_close(h);
1679 native_handle_delete(h);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001680 h = 0;
1681 }
1682 return h;
1683}
1684
1685
1686int Parcel::readFileDescriptor() const
1687{
1688 const flat_binder_object* flat = readObject(true);
1689 if (flat) {
1690 switch (flat->type) {
1691 case BINDER_TYPE_FD:
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001692 //ALOGI("Returning file descriptor %ld from parcel %p", flat->handle, this);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001693 return flat->handle;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001694 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001695 }
1696 return BAD_TYPE;
1697}
1698
Jeff Brown8a11ee52011-09-23 21:17:56 -07001699status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
1700{
Jeff Browna091b542014-11-11 16:44:25 -08001701 int32_t blobType;
1702 status_t status = readInt32(&blobType);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001703 if (status) return status;
1704
Jeff Browna091b542014-11-11 16:44:25 -08001705 if (blobType == BLOB_INPLACE) {
Steve Block48f4e152011-10-20 11:56:00 +01001706 ALOGV("readBlob: read in place");
Jeff Brown8a11ee52011-09-23 21:17:56 -07001707 const void* ptr = readInplace(len);
1708 if (!ptr) return BAD_VALUE;
1709
Jeff Browna091b542014-11-11 16:44:25 -08001710 outBlob->init(-1, const_cast<void*>(ptr), len, false);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001711 return NO_ERROR;
1712 }
1713
Steve Block48f4e152011-10-20 11:56:00 +01001714 ALOGV("readBlob: read from ashmem");
Jeff Browna091b542014-11-11 16:44:25 -08001715 bool isMutable = (blobType == BLOB_ASHMEM_MUTABLE);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001716 int fd = readFileDescriptor();
1717 if (fd == int(BAD_TYPE)) return BAD_VALUE;
1718
Jeff Browna091b542014-11-11 16:44:25 -08001719 void* ptr = ::mmap(NULL, len, isMutable ? PROT_READ | PROT_WRITE : PROT_READ,
1720 MAP_SHARED, fd, 0);
Narayan Kamathcfedf7d2014-10-08 17:35:45 +01001721 if (ptr == MAP_FAILED) return NO_MEMORY;
Jeff Brown8a11ee52011-09-23 21:17:56 -07001722
Jeff Browna091b542014-11-11 16:44:25 -08001723 outBlob->init(fd, ptr, len, isMutable);
Jeff Brown8a11ee52011-09-23 21:17:56 -07001724 return NO_ERROR;
1725}
1726
Mathias Agopian0d8e2e42013-07-29 21:24:40 -07001727status_t Parcel::read(FlattenableHelperInterface& val) const
Mathias Agopian31bc6982010-02-11 17:30:52 -08001728{
1729 // size
1730 const size_t len = this->readInt32();
1731 const size_t fd_count = this->readInt32();
1732
Nick Kralevich15961372015-04-02 09:36:02 -07001733 if (len > INT32_MAX) {
1734 // don't accept size_t values which may have come from an
1735 // inadvertent conversion from a negative int.
1736 return BAD_VALUE;
1737 }
1738
Mathias Agopian31bc6982010-02-11 17:30:52 -08001739 // payload
Nick Kralevich15961372015-04-02 09:36:02 -07001740 void const* const buf = this->readInplace(pad_size(len));
Mathias Agopian31bc6982010-02-11 17:30:52 -08001741 if (buf == NULL)
1742 return BAD_VALUE;
1743
1744 int* fds = NULL;
1745 if (fd_count) {
1746 fds = new int[fd_count];
1747 }
1748
1749 status_t err = NO_ERROR;
1750 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
Jesse Hall5004b552014-11-04 08:36:31 -08001751 fds[i] = dup(this->readFileDescriptor());
Jun Jiang79997662014-04-29 14:22:10 +08001752 if (fds[i] < 0) {
1753 err = BAD_VALUE;
Jesse Hall5004b552014-11-04 08:36:31 -08001754 ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
1755 i, fds[i], fd_count, strerror(errno));
Jun Jiang79997662014-04-29 14:22:10 +08001756 }
Mathias Agopian31bc6982010-02-11 17:30:52 -08001757 }
1758
1759 if (err == NO_ERROR) {
1760 err = val.unflatten(buf, len, fds, fd_count);
1761 }
1762
1763 if (fd_count) {
1764 delete [] fds;
1765 }
1766
1767 return err;
1768}
Mathias Agopian7922fa22009-05-18 15:08:03 -07001769const flat_binder_object* Parcel::readObject(bool nullMetaData) const
1770{
1771 const size_t DPOS = mDataPos;
1772 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
1773 const flat_binder_object* obj
1774 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
1775 mDataPos = DPOS + sizeof(flat_binder_object);
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001776 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
Mathias Agopian7922fa22009-05-18 15:08:03 -07001777 // When transferring a NULL object, we don't write it into
1778 // the object list, so we don't want to check for it when
1779 // reading.
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001780 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001781 return obj;
1782 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001783
Mathias Agopian7922fa22009-05-18 15:08:03 -07001784 // Ensure that this object is valid...
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001785 binder_size_t* const OBJS = mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001786 const size_t N = mObjectsSize;
1787 size_t opos = mNextObjectHint;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001788
Mathias Agopian7922fa22009-05-18 15:08:03 -07001789 if (N > 0) {
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001790 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001791 this, DPOS, opos);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001792
Mathias Agopian7922fa22009-05-18 15:08:03 -07001793 // Start at the current hint position, looking for an object at
1794 // the current data position.
1795 if (opos < N) {
1796 while (opos < (N-1) && OBJS[opos] < DPOS) {
1797 opos++;
1798 }
1799 } else {
1800 opos = N-1;
1801 }
1802 if (OBJS[opos] == DPOS) {
1803 // Found it!
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001804 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001805 this, DPOS, opos);
1806 mNextObjectHint = opos+1;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001807 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001808 return obj;
1809 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001810
Mathias Agopian7922fa22009-05-18 15:08:03 -07001811 // Look backwards for it...
1812 while (opos > 0 && OBJS[opos] > DPOS) {
1813 opos--;
1814 }
1815 if (OBJS[opos] == DPOS) {
1816 // Found it!
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001817 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001818 this, DPOS, opos);
1819 mNextObjectHint = opos+1;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001820 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001821 return obj;
1822 }
1823 }
Colin Crossf0487982014-02-05 17:42:44 -08001824 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
Mathias Agopian7922fa22009-05-18 15:08:03 -07001825 this, DPOS);
1826 }
1827 return NULL;
1828}
1829
1830void Parcel::closeFileDescriptors()
1831{
1832 size_t i = mObjectsSize;
1833 if (i > 0) {
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001834 //ALOGI("Closing file descriptors for %zu objects...", i);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001835 }
1836 while (i > 0) {
1837 i--;
1838 const flat_binder_object* flat
1839 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1840 if (flat->type == BINDER_TYPE_FD) {
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001841 //ALOGI("Closing fd: %ld", flat->handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001842 close(flat->handle);
1843 }
1844 }
1845}
1846
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001847uintptr_t Parcel::ipcData() const
Mathias Agopian7922fa22009-05-18 15:08:03 -07001848{
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001849 return reinterpret_cast<uintptr_t>(mData);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001850}
1851
1852size_t Parcel::ipcDataSize() const
1853{
1854 return (mDataSize > mDataPos ? mDataSize : mDataPos);
1855}
1856
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001857uintptr_t Parcel::ipcObjects() const
Mathias Agopian7922fa22009-05-18 15:08:03 -07001858{
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001859 return reinterpret_cast<uintptr_t>(mObjects);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001860}
1861
1862size_t Parcel::ipcObjectsCount() const
1863{
1864 return mObjectsSize;
1865}
1866
1867void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001868 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
Mathias Agopian7922fa22009-05-18 15:08:03 -07001869{
Arve Hjønnevågef1cda82014-02-19 20:42:13 -08001870 binder_size_t minOffset = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001871 freeDataNoInit();
1872 mError = NO_ERROR;
1873 mData = const_cast<uint8_t*>(data);
1874 mDataSize = mDataCapacity = dataSize;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001875 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
Mathias Agopian7922fa22009-05-18 15:08:03 -07001876 mDataPos = 0;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001877 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001878 mObjects = const_cast<binder_size_t*>(objects);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001879 mObjectsSize = mObjectsCapacity = objectsCount;
1880 mNextObjectHint = 0;
1881 mOwner = relFunc;
1882 mOwnerCookie = relCookie;
Arve Hjønnevågb51680b2014-02-13 19:22:08 -08001883 for (size_t i = 0; i < mObjectsSize; i++) {
Arve Hjønnevågef1cda82014-02-19 20:42:13 -08001884 binder_size_t offset = mObjects[i];
Arve Hjønnevågb51680b2014-02-13 19:22:08 -08001885 if (offset < minOffset) {
Dan Albertd29ab952014-11-20 11:50:23 -08001886 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
Arve Hjønnevågef1cda82014-02-19 20:42:13 -08001887 __func__, (uint64_t)offset, (uint64_t)minOffset);
Arve Hjønnevågb51680b2014-02-13 19:22:08 -08001888 mObjectsSize = 0;
1889 break;
1890 }
1891 minOffset = offset + sizeof(flat_binder_object);
1892 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001893 scanForFds();
1894}
1895
Colin Crossf0487982014-02-05 17:42:44 -08001896void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
Mathias Agopian7922fa22009-05-18 15:08:03 -07001897{
1898 to << "Parcel(";
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001899
Mathias Agopian7922fa22009-05-18 15:08:03 -07001900 if (errorCheck() != NO_ERROR) {
1901 const status_t err = errorCheck();
Colin Crossf0487982014-02-05 17:42:44 -08001902 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
Mathias Agopian7922fa22009-05-18 15:08:03 -07001903 } else if (dataSize() > 0) {
1904 const uint8_t* DATA = data();
1905 to << indent << HexDump(DATA, dataSize()) << dedent;
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001906 const binder_size_t* OBJS = objects();
Mathias Agopian7922fa22009-05-18 15:08:03 -07001907 const size_t N = objectsCount();
1908 for (size_t i=0; i<N; i++) {
1909 const flat_binder_object* flat
1910 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1911 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1912 << TypeCode(flat->type & 0x7f7f7f00)
1913 << " = " << flat->binder;
1914 }
1915 } else {
1916 to << "NULL";
1917 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001918
Mathias Agopian7922fa22009-05-18 15:08:03 -07001919 to << ")";
1920}
1921
1922void Parcel::releaseObjects()
1923{
1924 const sp<ProcessState> proc(ProcessState::self());
1925 size_t i = mObjectsSize;
1926 uint8_t* const data = mData;
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001927 binder_size_t* const objects = mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001928 while (i > 0) {
1929 i--;
1930 const flat_binder_object* flat
1931 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Roos95e4f022015-10-22 16:12:53 -07001932 release_object(proc, *flat, this, &mOpenAshmemSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001933 }
1934}
1935
1936void Parcel::acquireObjects()
1937{
1938 const sp<ProcessState> proc(ProcessState::self());
1939 size_t i = mObjectsSize;
1940 uint8_t* const data = mData;
Arve Hjønnevåga5440702014-01-28 20:12:59 -08001941 binder_size_t* const objects = mObjects;
Mathias Agopian7922fa22009-05-18 15:08:03 -07001942 while (i > 0) {
1943 i--;
1944 const flat_binder_object* flat
1945 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
Adrian Roos95e4f022015-10-22 16:12:53 -07001946 acquire_object(proc, *flat, this, &mOpenAshmemSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001947 }
1948}
1949
1950void Parcel::freeData()
1951{
1952 freeDataNoInit();
1953 initState();
1954}
1955
1956void Parcel::freeDataNoInit()
1957{
1958 if (mOwner) {
Dianne Hackborn492acff2014-11-11 12:22:53 -08001959 LOG_ALLOC("Parcel %p: freeing other owner data", this);
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07001960 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Mathias Agopian7922fa22009-05-18 15:08:03 -07001961 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1962 } else {
Dianne Hackborn492acff2014-11-11 12:22:53 -08001963 LOG_ALLOC("Parcel %p: freeing allocated data", this);
Mathias Agopian7922fa22009-05-18 15:08:03 -07001964 releaseObjects();
Dianne Hackborn492acff2014-11-11 12:22:53 -08001965 if (mData) {
1966 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
Dianne Hackborn353d79a2014-11-13 17:07:40 -08001967 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dan Austin8e5e47f2015-09-10 13:46:02 -07001968 if (mDataCapacity <= gParcelGlobalAllocSize) {
1969 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1970 } else {
1971 gParcelGlobalAllocSize = 0;
1972 }
1973 if (gParcelGlobalAllocCount > 0) {
1974 gParcelGlobalAllocCount--;
1975 }
Dianne Hackborn353d79a2014-11-13 17:07:40 -08001976 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08001977 free(mData);
1978 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07001979 if (mObjects) free(mObjects);
1980 }
1981}
1982
1983status_t Parcel::growData(size_t len)
1984{
Nick Kralevich15961372015-04-02 09:36:02 -07001985 if (len > INT32_MAX) {
1986 // don't accept size_t values which may have come from an
1987 // inadvertent conversion from a negative int.
1988 return BAD_VALUE;
1989 }
1990
Mathias Agopian7922fa22009-05-18 15:08:03 -07001991 size_t newSize = ((mDataSize+len)*3)/2;
1992 return (newSize <= mDataSize)
1993 ? (status_t) NO_MEMORY
1994 : continueWrite(newSize);
1995}
1996
1997status_t Parcel::restartWrite(size_t desired)
1998{
Nick Kralevich15961372015-04-02 09:36:02 -07001999 if (desired > INT32_MAX) {
2000 // don't accept size_t values which may have come from an
2001 // inadvertent conversion from a negative int.
2002 return BAD_VALUE;
2003 }
2004
Mathias Agopian7922fa22009-05-18 15:08:03 -07002005 if (mOwner) {
2006 freeData();
2007 return continueWrite(desired);
2008 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002009
Mathias Agopian7922fa22009-05-18 15:08:03 -07002010 uint8_t* data = (uint8_t*)realloc(mData, desired);
2011 if (!data && desired > mDataCapacity) {
2012 mError = NO_MEMORY;
2013 return NO_MEMORY;
2014 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002015
Mathias Agopian7922fa22009-05-18 15:08:03 -07002016 releaseObjects();
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002017
Mathias Agopian7922fa22009-05-18 15:08:03 -07002018 if (data) {
Dianne Hackborn492acff2014-11-11 12:22:53 -08002019 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002020 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002021 gParcelGlobalAllocSize += desired;
2022 gParcelGlobalAllocSize -= mDataCapacity;
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002023 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002024 mData = data;
2025 mDataCapacity = desired;
2026 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002027
Mathias Agopian7922fa22009-05-18 15:08:03 -07002028 mDataSize = mDataPos = 0;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002029 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2030 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2031
Mathias Agopian7922fa22009-05-18 15:08:03 -07002032 free(mObjects);
2033 mObjects = NULL;
2034 mObjectsSize = mObjectsCapacity = 0;
2035 mNextObjectHint = 0;
2036 mHasFds = false;
2037 mFdsKnown = true;
Dianne Hackbornabc3cf42011-09-28 23:19:47 -04002038 mAllowFds = true;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002039
Mathias Agopian7922fa22009-05-18 15:08:03 -07002040 return NO_ERROR;
2041}
2042
2043status_t Parcel::continueWrite(size_t desired)
2044{
Nick Kralevich15961372015-04-02 09:36:02 -07002045 if (desired > INT32_MAX) {
2046 // don't accept size_t values which may have come from an
2047 // inadvertent conversion from a negative int.
2048 return BAD_VALUE;
2049 }
2050
Mathias Agopian7922fa22009-05-18 15:08:03 -07002051 // If shrinking, first adjust for any objects that appear
2052 // after the new data size.
2053 size_t objectsSize = mObjectsSize;
2054 if (desired < mDataSize) {
2055 if (desired == 0) {
2056 objectsSize = 0;
2057 } else {
2058 while (objectsSize > 0) {
2059 if (mObjects[objectsSize-1] < desired)
2060 break;
2061 objectsSize--;
2062 }
2063 }
2064 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002065
Mathias Agopian7922fa22009-05-18 15:08:03 -07002066 if (mOwner) {
2067 // If the size is going to zero, just release the owner's data.
2068 if (desired == 0) {
2069 freeData();
2070 return NO_ERROR;
2071 }
2072
2073 // If there is a different owner, we need to take
2074 // posession.
2075 uint8_t* data = (uint8_t*)malloc(desired);
2076 if (!data) {
2077 mError = NO_MEMORY;
2078 return NO_MEMORY;
2079 }
Arve Hjønnevåga5440702014-01-28 20:12:59 -08002080 binder_size_t* objects = NULL;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002081
Mathias Agopian7922fa22009-05-18 15:08:03 -07002082 if (objectsSize) {
Nick Kralevich73f41502015-04-28 16:21:30 -07002083 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
Mathias Agopian7922fa22009-05-18 15:08:03 -07002084 if (!objects) {
Hyejin Kimbdfee452013-03-09 11:28:54 +09002085 free(data);
2086
Mathias Agopian7922fa22009-05-18 15:08:03 -07002087 mError = NO_MEMORY;
2088 return NO_MEMORY;
2089 }
2090
2091 // Little hack to only acquire references on objects
2092 // we will be keeping.
2093 size_t oldObjectsSize = mObjectsSize;
2094 mObjectsSize = objectsSize;
2095 acquireObjects();
2096 mObjectsSize = oldObjectsSize;
2097 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002098
Mathias Agopian7922fa22009-05-18 15:08:03 -07002099 if (mData) {
2100 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2101 }
2102 if (objects && mObjects) {
Arve Hjønnevåga5440702014-01-28 20:12:59 -08002103 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
Mathias Agopian7922fa22009-05-18 15:08:03 -07002104 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002105 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
Mathias Agopian7922fa22009-05-18 15:08:03 -07002106 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2107 mOwner = NULL;
2108
Dianne Hackborn492acff2014-11-11 12:22:53 -08002109 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002110 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002111 gParcelGlobalAllocSize += desired;
2112 gParcelGlobalAllocCount++;
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002113 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002114
Mathias Agopian7922fa22009-05-18 15:08:03 -07002115 mData = data;
2116 mObjects = objects;
2117 mDataSize = (mDataSize < desired) ? mDataSize : desired;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002118 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002119 mDataCapacity = desired;
2120 mObjectsSize = mObjectsCapacity = objectsSize;
2121 mNextObjectHint = 0;
2122
2123 } else if (mData) {
2124 if (objectsSize < mObjectsSize) {
2125 // Need to release refs on any objects we are dropping.
2126 const sp<ProcessState> proc(ProcessState::self());
2127 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2128 const flat_binder_object* flat
2129 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2130 if (flat->type == BINDER_TYPE_FD) {
2131 // will need to rescan because we may have lopped off the only FDs
2132 mFdsKnown = false;
2133 }
Adrian Roos95e4f022015-10-22 16:12:53 -07002134 release_object(proc, *flat, this, &mOpenAshmemSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002135 }
Arve Hjønnevåga5440702014-01-28 20:12:59 -08002136 binder_size_t* objects =
2137 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
Mathias Agopian7922fa22009-05-18 15:08:03 -07002138 if (objects) {
2139 mObjects = objects;
2140 }
2141 mObjectsSize = objectsSize;
2142 mNextObjectHint = 0;
2143 }
2144
2145 // We own the data, so we can just do a realloc().
2146 if (desired > mDataCapacity) {
2147 uint8_t* data = (uint8_t*)realloc(mData, desired);
2148 if (data) {
Dianne Hackborn492acff2014-11-11 12:22:53 -08002149 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2150 desired);
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002151 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002152 gParcelGlobalAllocSize += desired;
2153 gParcelGlobalAllocSize -= mDataCapacity;
Dan Austin8e5e47f2015-09-10 13:46:02 -07002154 gParcelGlobalAllocCount++;
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002155 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002156 mData = data;
2157 mDataCapacity = desired;
2158 } else if (desired > mDataCapacity) {
2159 mError = NO_MEMORY;
2160 return NO_MEMORY;
2161 }
2162 } else {
Dianne Hackborndb8c4942011-04-13 18:15:56 -07002163 if (mDataSize > desired) {
2164 mDataSize = desired;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002165 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
Dianne Hackborndb8c4942011-04-13 18:15:56 -07002166 }
Mathias Agopian7922fa22009-05-18 15:08:03 -07002167 if (mDataPos > desired) {
2168 mDataPos = desired;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002169 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002170 }
2171 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002172
Mathias Agopian7922fa22009-05-18 15:08:03 -07002173 } else {
2174 // This is the first data. Easy!
2175 uint8_t* data = (uint8_t*)malloc(desired);
2176 if (!data) {
2177 mError = NO_MEMORY;
2178 return NO_MEMORY;
2179 }
Hyejin Kimbdfee452013-03-09 11:28:54 +09002180
Mathias Agopian7922fa22009-05-18 15:08:03 -07002181 if(!(mDataCapacity == 0 && mObjects == NULL
2182 && mObjectsCapacity == 0)) {
Colin Crossf0487982014-02-05 17:42:44 -08002183 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002184 }
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002185
Dianne Hackborn492acff2014-11-11 12:22:53 -08002186 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002187 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002188 gParcelGlobalAllocSize += desired;
2189 gParcelGlobalAllocCount++;
Dianne Hackborn353d79a2014-11-13 17:07:40 -08002190 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
Dianne Hackborn492acff2014-11-11 12:22:53 -08002191
Mathias Agopian7922fa22009-05-18 15:08:03 -07002192 mData = data;
2193 mDataSize = mDataPos = 0;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002194 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2195 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002196 mDataCapacity = desired;
2197 }
2198
2199 return NO_ERROR;
2200}
2201
2202void Parcel::initState()
2203{
Dianne Hackborn492acff2014-11-11 12:22:53 -08002204 LOG_ALLOC("Parcel %p: initState", this);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002205 mError = NO_ERROR;
2206 mData = 0;
2207 mDataSize = 0;
2208 mDataCapacity = 0;
2209 mDataPos = 0;
Mark Salyzyn386eb9e2014-05-30 16:35:57 -07002210 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2211 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
Mathias Agopian7922fa22009-05-18 15:08:03 -07002212 mObjects = NULL;
2213 mObjectsSize = 0;
2214 mObjectsCapacity = 0;
2215 mNextObjectHint = 0;
2216 mHasFds = false;
2217 mFdsKnown = true;
Dianne Hackbornabc3cf42011-09-28 23:19:47 -04002218 mAllowFds = true;
Mathias Agopian7922fa22009-05-18 15:08:03 -07002219 mOwner = NULL;
Adrian Roos95e4f022015-10-22 16:12:53 -07002220 mOpenAshmemSize = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -07002221}
2222
2223void Parcel::scanForFds() const
2224{
2225 bool hasFds = false;
2226 for (size_t i=0; i<mObjectsSize; i++) {
2227 const flat_binder_object* flat
2228 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2229 if (flat->type == BINDER_TYPE_FD) {
2230 hasFds = true;
2231 break;
2232 }
2233 }
2234 mHasFds = hasFds;
2235 mFdsKnown = true;
2236}
2237
Dan Sandler532036b2015-04-10 10:08:45 -04002238size_t Parcel::getBlobAshmemSize() const
2239{
Adrian Roos1b5149e2015-10-22 16:46:12 -07002240 // This used to return the size of all blobs that were written to ashmem, now we're returning
2241 // the ashmem currently referenced by this Parcel, which should be equivalent.
2242 // TODO: Remove method once ABI can be changed.
2243 return mOpenAshmemSize;
Dan Sandler532036b2015-04-10 10:08:45 -04002244}
2245
Adrian Roos95e4f022015-10-22 16:12:53 -07002246size_t Parcel::getOpenAshmemSize() const
2247{
2248 return mOpenAshmemSize;
2249}
2250
Jeff Brown8a11ee52011-09-23 21:17:56 -07002251// --- Parcel::Blob ---
2252
2253Parcel::Blob::Blob() :
Jeff Browna091b542014-11-11 16:44:25 -08002254 mFd(-1), mData(NULL), mSize(0), mMutable(false) {
Jeff Brown8a11ee52011-09-23 21:17:56 -07002255}
2256
2257Parcel::Blob::~Blob() {
2258 release();
2259}
2260
2261void Parcel::Blob::release() {
Jeff Browna091b542014-11-11 16:44:25 -08002262 if (mFd != -1 && mData) {
Jeff Brown8a11ee52011-09-23 21:17:56 -07002263 ::munmap(mData, mSize);
2264 }
2265 clear();
2266}
2267
Jeff Browna091b542014-11-11 16:44:25 -08002268void Parcel::Blob::init(int fd, void* data, size_t size, bool isMutable) {
2269 mFd = fd;
Jeff Brown8a11ee52011-09-23 21:17:56 -07002270 mData = data;
2271 mSize = size;
Jeff Browna091b542014-11-11 16:44:25 -08002272 mMutable = isMutable;
Jeff Brown8a11ee52011-09-23 21:17:56 -07002273}
2274
2275void Parcel::Blob::clear() {
Jeff Browna091b542014-11-11 16:44:25 -08002276 mFd = -1;
Jeff Brown8a11ee52011-09-23 21:17:56 -07002277 mData = NULL;
2278 mSize = 0;
Jeff Browna091b542014-11-11 16:44:25 -08002279 mMutable = false;
Jeff Brown8a11ee52011-09-23 21:17:56 -07002280}
2281
Mathias Agopian7922fa22009-05-18 15:08:03 -07002282}; // namespace android