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 "hw-Parcel"
18 //#define LOG_NDEBUG 0
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <pthread.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/resource.h>
31 #include <unistd.h>
32
33 #include <hwbinder/Binder.h>
34 #include <hwbinder/BpHwBinder.h>
35 #include <hwbinder/IPCThreadState.h>
36 #include <hwbinder/Parcel.h>
37 #include <hwbinder/ProcessState.h>
38 #include <hwbinder/TextOutput.h>
39 #include <hwbinder/binder_kernel.h>
40
41 #include <cutils/ashmem.h>
42 #include <utils/Debug.h>
43 #include <utils/Log.h>
44 #include <utils/misc.h>
45 #include <utils/String8.h>
46 #include <utils/String16.h>
47
48 #include <private/binder/binder_module.h>
49 #include <hwbinder/Static.h>
50
51 #ifndef INT32_MAX
52 #define INT32_MAX ((int32_t)(2147483647))
53 #endif
54
55 #define LOG_REFS(...)
56 //#define LOG_REFS(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
57 #define LOG_ALLOC(...)
58 //#define LOG_ALLOC(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
59 #define LOG_BUFFER(...)
60 // #define LOG_BUFFER(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
61
62 // ---------------------------------------------------------------------------
63
64 // This macro should never be used at runtime, as a too large value
65 // of s could cause an integer overflow. Instead, you should always
66 // use the wrapper function pad_size()
67 #define PAD_SIZE_UNSAFE(s) (((s)+3)&~3)
68
pad_size(size_t s)69 static size_t pad_size(size_t s) {
70 if (s > (SIZE_T_MAX - 3)) {
71 abort();
72 }
73 return PAD_SIZE_UNSAFE(s);
74 }
75
76 // Note: must be kept in sync with android/os/StrictMode.java's PENALTY_GATHER
77 #define STRICT_MODE_PENALTY_GATHER (0x40 << 16)
78
79 // XXX This can be made public if we want to provide
80 // support for typed data.
81 struct small_flat_data
82 {
83 uint32_t type;
84 uint32_t data;
85 };
86
87 namespace android {
88 namespace hardware {
89
90 static pthread_mutex_t gParcelGlobalAllocSizeLock = PTHREAD_MUTEX_INITIALIZER;
91 static size_t gParcelGlobalAllocSize = 0;
92 static size_t gParcelGlobalAllocCount = 0;
93
94 static size_t gMaxFds = 0;
95
96 static const size_t PARCEL_REF_CAP = 1024;
97
acquire_binder_object(const sp<ProcessState> & proc,const flat_binder_object & obj,const void * who)98 void acquire_binder_object(const sp<ProcessState>& proc,
99 const flat_binder_object& obj, const void* who)
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);
105 reinterpret_cast<IBinder*>(obj.cookie)->incStrong(who);
106 }
107 return;
108 case BINDER_TYPE_WEAK_BINDER:
109 if (obj.binder)
110 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->incWeak(who);
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 }
126
127 ALOGD("Invalid object type 0x%08x", obj.type);
128 }
129
acquire_object(const sp<ProcessState> & proc,const binder_object_header & obj,const void * who)130 void acquire_object(const sp<ProcessState>& proc, const binder_object_header& obj,
131 const void *who) {
132 switch (obj.type) {
133 case BINDER_TYPE_BINDER:
134 case BINDER_TYPE_WEAK_BINDER:
135 case BINDER_TYPE_HANDLE:
136 case BINDER_TYPE_WEAK_HANDLE: {
137 const flat_binder_object& fbo = reinterpret_cast<const flat_binder_object&>(obj);
138 acquire_binder_object(proc, fbo, who);
139 break;
140 }
141 }
142 }
143
release_object(const sp<ProcessState> & proc,const flat_binder_object & obj,const void * who)144 void release_object(const sp<ProcessState>& proc,
145 const flat_binder_object& obj, const void* who)
146 {
147 switch (obj.type) {
148 case BINDER_TYPE_BINDER:
149 if (obj.binder) {
150 LOG_REFS("Parcel %p releasing reference on local %p", who, obj.cookie);
151 reinterpret_cast<IBinder*>(obj.cookie)->decStrong(who);
152 }
153 return;
154 case BINDER_TYPE_WEAK_BINDER:
155 if (obj.binder)
156 reinterpret_cast<RefBase::weakref_type*>(obj.binder)->decWeak(who);
157 return;
158 case BINDER_TYPE_HANDLE: {
159 const sp<IBinder> b = proc->getStrongProxyForHandle(obj.handle);
160 if (b != NULL) {
161 LOG_REFS("Parcel %p releasing reference on remote %p", who, b.get());
162 b->decStrong(who);
163 }
164 return;
165 }
166 case BINDER_TYPE_WEAK_HANDLE: {
167 const wp<IBinder> b = proc->getWeakProxyForHandle(obj.handle);
168 if (b != NULL) b.get_refs()->decWeak(who);
169 return;
170 }
171 case BINDER_TYPE_FD: {
172 if (obj.cookie != 0) { // owned
173 close(obj.handle);
174 }
175 return;
176 }
177 case BINDER_TYPE_PTR: {
178 // The relevant buffer is part of the transaction buffer and will be freed that way
179 return;
180 }
181 case BINDER_TYPE_FDA: {
182 // The enclosed file descriptors are closed in the kernel
183 return;
184 }
185 }
186
187 ALOGE("Invalid object type 0x%08x", obj.type);
188 }
189
finish_flatten_binder(const sp<IBinder> &,const flat_binder_object & flat,Parcel * out)190 inline static status_t finish_flatten_binder(
191 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
192 {
193 return out->writeObject(flat);
194 }
195
flatten_binder(const sp<ProcessState> &,const sp<IBinder> & binder,Parcel * out)196 status_t flatten_binder(const sp<ProcessState>& /*proc*/,
197 const sp<IBinder>& binder, Parcel* out)
198 {
199 flat_binder_object obj = {};
200
201 if (binder != NULL) {
202 BHwBinder *local = binder->localBinder();
203 if (!local) {
204 BpHwBinder *proxy = binder->remoteBinder();
205 if (proxy == NULL) {
206 ALOGE("null proxy");
207 }
208 const int32_t handle = proxy ? proxy->handle() : 0;
209 obj.type = BINDER_TYPE_HANDLE;
210 obj.flags = FLAT_BINDER_FLAG_ACCEPTS_FDS;
211 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
212 obj.handle = handle;
213 obj.cookie = 0;
214 } else {
215 // Get policy and convert it
216 int policy = local->getMinSchedulingPolicy();
217 int priority = local->getMinSchedulingPriority();
218
219 obj.flags = priority & FLAT_BINDER_FLAG_PRIORITY_MASK;
220 obj.flags |= FLAT_BINDER_FLAG_ACCEPTS_FDS | FLAT_BINDER_FLAG_INHERIT_RT;
221 obj.flags |= (policy & 3) << FLAT_BINDER_FLAG_SCHEDPOLICY_SHIFT;
222 if (local->isRequestingSid()) {
223 obj.flags |= FLAT_BINDER_FLAG_TXN_SECURITY_CTX;
224 }
225 obj.type = BINDER_TYPE_BINDER;
226 obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
227 obj.cookie = reinterpret_cast<uintptr_t>(local);
228 }
229 } else {
230 obj.type = BINDER_TYPE_BINDER;
231 obj.binder = 0;
232 obj.cookie = 0;
233 }
234
235 return finish_flatten_binder(binder, obj, out);
236 }
237
flatten_binder(const sp<ProcessState> &,const wp<IBinder> & binder,Parcel * out)238 status_t flatten_binder(const sp<ProcessState>& /*proc*/,
239 const wp<IBinder>& binder, Parcel* out)
240 {
241 flat_binder_object obj = {};
242
243 obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
244 if (binder != NULL) {
245 sp<IBinder> real = binder.promote();
246 if (real != NULL) {
247 IBinder *local = real->localBinder();
248 if (!local) {
249 BpHwBinder *proxy = real->remoteBinder();
250 if (proxy == NULL) {
251 ALOGE("null proxy");
252 }
253 const int32_t handle = proxy ? proxy->handle() : 0;
254 obj.type = BINDER_TYPE_WEAK_HANDLE;
255 obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
256 obj.handle = handle;
257 obj.cookie = 0;
258 } else {
259 obj.type = BINDER_TYPE_WEAK_BINDER;
260 obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
261 obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
262 }
263 return finish_flatten_binder(real, obj, out);
264 }
265
266 // XXX How to deal? In order to flatten the given binder,
267 // we need to probe it for information, which requires a primary
268 // reference... but we don't have one.
269 //
270 // The OpenBinder implementation uses a dynamic_cast<> here,
271 // but we can't do that with the different reference counting
272 // implementation we are using.
273 ALOGE("Unable to unflatten Binder weak reference!");
274 obj.type = BINDER_TYPE_BINDER;
275 obj.binder = 0;
276 obj.cookie = 0;
277 return finish_flatten_binder(NULL, obj, out);
278
279 } else {
280 obj.type = BINDER_TYPE_BINDER;
281 obj.binder = 0;
282 obj.cookie = 0;
283 return finish_flatten_binder(NULL, obj, out);
284 }
285 }
286
finish_unflatten_binder(BpHwBinder *,const flat_binder_object &,const Parcel &)287 inline static status_t finish_unflatten_binder(
288 BpHwBinder* /*proxy*/, const flat_binder_object& /*flat*/,
289 const Parcel& /*in*/)
290 {
291 return NO_ERROR;
292 }
293
unflatten_binder(const sp<ProcessState> & proc,const Parcel & in,sp<IBinder> * out)294 status_t unflatten_binder(const sp<ProcessState>& proc,
295 const Parcel& in, sp<IBinder>* out)
296 {
297 const flat_binder_object* flat = in.readObject<flat_binder_object>();
298
299 if (flat) {
300 switch (flat->type) {
301 case BINDER_TYPE_BINDER:
302 *out = reinterpret_cast<IBinder*>(flat->cookie);
303 return finish_unflatten_binder(NULL, *flat, in);
304 case BINDER_TYPE_HANDLE:
305 *out = proc->getStrongProxyForHandle(flat->handle);
306 return finish_unflatten_binder(
307 static_cast<BpHwBinder*>(out->get()), *flat, in);
308 }
309 }
310 return BAD_TYPE;
311 }
312
unflatten_binder(const sp<ProcessState> & proc,const Parcel & in,wp<IBinder> * out)313 status_t unflatten_binder(const sp<ProcessState>& proc,
314 const Parcel& in, wp<IBinder>* out)
315 {
316 const flat_binder_object* flat = in.readObject<flat_binder_object>();
317
318 if (flat) {
319 switch (flat->type) {
320 case BINDER_TYPE_BINDER:
321 *out = reinterpret_cast<IBinder*>(flat->cookie);
322 return finish_unflatten_binder(NULL, *flat, in);
323 case BINDER_TYPE_WEAK_BINDER:
324 if (flat->binder != 0) {
325 out->set_object_and_refs(
326 reinterpret_cast<IBinder*>(flat->cookie),
327 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
328 } else {
329 *out = NULL;
330 }
331 return finish_unflatten_binder(NULL, *flat, in);
332 case BINDER_TYPE_HANDLE:
333 case BINDER_TYPE_WEAK_HANDLE:
334 *out = proc->getWeakProxyForHandle(flat->handle);
335 return finish_unflatten_binder(
336 static_cast<BpHwBinder*>(out->unsafe_get()), *flat, in);
337 }
338 }
339 return BAD_TYPE;
340 }
341
342 /*
343 * Return true iff:
344 * 1. obj is indeed a binder_buffer_object (type is BINDER_TYPE_PTR), and
345 * 2. obj does NOT have the flag BINDER_BUFFER_REF (it is not a reference, but
346 * an actual buffer.)
347 */
isBuffer(const binder_buffer_object & obj)348 static inline bool isBuffer(const binder_buffer_object& obj) {
349 return obj.hdr.type == BINDER_TYPE_PTR
350 && (obj.flags & BINDER_BUFFER_REF) == 0;
351 }
352
353 // ---------------------------------------------------------------------------
354
Parcel()355 Parcel::Parcel()
356 {
357 LOG_ALLOC("Parcel %p: constructing", this);
358 initState();
359 }
360
~Parcel()361 Parcel::~Parcel()
362 {
363 freeDataNoInit();
364 LOG_ALLOC("Parcel %p: destroyed", this);
365 }
366
getGlobalAllocSize()367 size_t Parcel::getGlobalAllocSize() {
368 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
369 size_t size = gParcelGlobalAllocSize;
370 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
371 return size;
372 }
373
getGlobalAllocCount()374 size_t Parcel::getGlobalAllocCount() {
375 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
376 size_t count = gParcelGlobalAllocCount;
377 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
378 return count;
379 }
380
data() const381 const uint8_t* Parcel::data() const
382 {
383 return mData;
384 }
385
dataSize() const386 size_t Parcel::dataSize() const
387 {
388 return (mDataSize > mDataPos ? mDataSize : mDataPos);
389 }
390
dataAvail() const391 size_t Parcel::dataAvail() const
392 {
393 size_t result = dataSize() - dataPosition();
394 if (result > INT32_MAX) {
395 abort();
396 }
397 return result;
398 }
399
dataPosition() const400 size_t Parcel::dataPosition() const
401 {
402 return mDataPos;
403 }
404
dataCapacity() const405 size_t Parcel::dataCapacity() const
406 {
407 return mDataCapacity;
408 }
409
setDataSize(size_t size)410 status_t Parcel::setDataSize(size_t size)
411 {
412 if (size > INT32_MAX) {
413 // don't accept size_t values which may have come from an
414 // inadvertent conversion from a negative int.
415 return BAD_VALUE;
416 }
417
418 status_t err;
419 err = continueWrite(size);
420 if (err == NO_ERROR) {
421 mDataSize = size;
422 ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
423 }
424 return err;
425 }
426
setDataPosition(size_t pos) const427 void Parcel::setDataPosition(size_t pos) const
428 {
429 if (pos > INT32_MAX) {
430 // don't accept size_t values which may have come from an
431 // inadvertent conversion from a negative int.
432 abort();
433 }
434
435 mDataPos = pos;
436 mNextObjectHint = 0;
437 }
438
setDataCapacity(size_t size)439 status_t Parcel::setDataCapacity(size_t size)
440 {
441 if (size > INT32_MAX) {
442 // don't accept size_t values which may have come from an
443 // inadvertent conversion from a negative int.
444 return BAD_VALUE;
445 }
446
447 if (size > mDataCapacity) return continueWrite(size);
448 return NO_ERROR;
449 }
450
setData(const uint8_t * buffer,size_t len)451 status_t Parcel::setData(const uint8_t* buffer, size_t len)
452 {
453 if (len > INT32_MAX) {
454 // don't accept size_t values which may have come from an
455 // inadvertent conversion from a negative int.
456 return BAD_VALUE;
457 }
458
459 status_t err = restartWrite(len);
460 if (err == NO_ERROR) {
461 memcpy(const_cast<uint8_t*>(data()), buffer, len);
462 mDataSize = len;
463 mFdsKnown = false;
464 }
465 return err;
466 }
467
468 // Write RPC headers. (previously just the interface token)
writeInterfaceToken(const char * interface)469 status_t Parcel::writeInterfaceToken(const char* interface)
470 {
471 // currently the interface identification token is just its name as a string
472 return writeCString(interface);
473 }
474
enforceInterface(const char * interface) const475 bool Parcel::enforceInterface(const char* interface) const
476 {
477 const char* str = readCString();
478 if (strcmp(str, interface) == 0) {
479 return true;
480 } else {
481 ALOGW("**** enforceInterface() expected '%s' but read '%s'",
482 String8(interface).string(), String8(str).string());
483 return false;
484 }
485 }
486
objects() const487 const binder_size_t* Parcel::objects() const
488 {
489 return mObjects;
490 }
491
objectsCount() const492 size_t Parcel::objectsCount() const
493 {
494 return mObjectsSize;
495 }
496
errorCheck() const497 status_t Parcel::errorCheck() const
498 {
499 return mError;
500 }
501
setError(status_t err)502 void Parcel::setError(status_t err)
503 {
504 mError = err;
505 }
506
finishWrite(size_t len)507 status_t Parcel::finishWrite(size_t len)
508 {
509 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
515 //printf("Finish write of %d\n", len);
516 mDataPos += len;
517 ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
518 if (mDataPos > mDataSize) {
519 mDataSize = mDataPos;
520 ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
521 }
522 //printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
523 return NO_ERROR;
524 }
525
writeUnpadded(const void * data,size_t len)526 status_t Parcel::writeUnpadded(const void* data, size_t len)
527 {
528 if (len > INT32_MAX) {
529 // don't accept size_t values which may have come from an
530 // inadvertent conversion from a negative int.
531 return BAD_VALUE;
532 }
533
534 size_t end = mDataPos + len;
535 if (end < mDataPos) {
536 // integer overflow
537 return BAD_VALUE;
538 }
539
540 if (end <= mDataCapacity) {
541 restart_write:
542 memcpy(mData+mDataPos, data, len);
543 return finishWrite(len);
544 }
545
546 status_t err = growData(len);
547 if (err == NO_ERROR) goto restart_write;
548 return err;
549 }
550
write(const void * data,size_t len)551 status_t Parcel::write(const void* data, size_t len)
552 {
553 if (len > INT32_MAX) {
554 // don't accept size_t values which may have come from an
555 // inadvertent conversion from a negative int.
556 return BAD_VALUE;
557 }
558
559 void* const d = writeInplace(len);
560 if (d) {
561 memcpy(d, data, len);
562 return NO_ERROR;
563 }
564 return mError;
565 }
566
writeInplace(size_t len)567 void* Parcel::writeInplace(size_t len)
568 {
569 if (len > INT32_MAX) {
570 // don't accept size_t values which may have come from an
571 // inadvertent conversion from a negative int.
572 return NULL;
573 }
574
575 const size_t padded = pad_size(len);
576
577 // sanity check for integer overflow
578 if (mDataPos+padded < mDataPos) {
579 return NULL;
580 }
581
582 if ((mDataPos+padded) <= mDataCapacity) {
583 restart_write:
584 //printf("Writing %ld bytes, padded to %ld\n", len, padded);
585 uint8_t* const data = mData+mDataPos;
586
587 // Need to pad at end?
588 if (padded != len) {
589 #if BYTE_ORDER == BIG_ENDIAN
590 static const uint32_t mask[4] = {
591 0x00000000, 0xffffff00, 0xffff0000, 0xff000000
592 };
593 #endif
594 #if BYTE_ORDER == LITTLE_ENDIAN
595 static const uint32_t mask[4] = {
596 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
597 };
598 #endif
599 //printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
600 // *reinterpret_cast<void**>(data+padded-4));
601 *reinterpret_cast<uint32_t*>(data+padded-4) &= mask[padded-len];
602 }
603
604 finishWrite(padded);
605 return data;
606 }
607
608 status_t err = growData(padded);
609 if (err == NO_ERROR) goto restart_write;
610 return NULL;
611 }
612
writeInt8(int8_t val)613 status_t Parcel::writeInt8(int8_t val)
614 {
615 return write(&val, sizeof(val));
616 }
617
writeUint8(uint8_t val)618 status_t Parcel::writeUint8(uint8_t val)
619 {
620 return write(&val, sizeof(val));
621 }
622
writeInt16(int16_t val)623 status_t Parcel::writeInt16(int16_t val)
624 {
625 return write(&val, sizeof(val));
626 }
627
writeUint16(uint16_t val)628 status_t Parcel::writeUint16(uint16_t val)
629 {
630 return write(&val, sizeof(val));
631 }
632
writeInt32(int32_t val)633 status_t Parcel::writeInt32(int32_t val)
634 {
635 return writeAligned(val);
636 }
637
writeUint32(uint32_t val)638 status_t Parcel::writeUint32(uint32_t val)
639 {
640 return writeAligned(val);
641 }
642
writeBool(bool val)643 status_t Parcel::writeBool(bool val)
644 {
645 return writeInt8(int8_t(val));
646 }
writeInt64(int64_t val)647 status_t Parcel::writeInt64(int64_t val)
648 {
649 return writeAligned(val);
650 }
651
writeUint64(uint64_t val)652 status_t Parcel::writeUint64(uint64_t val)
653 {
654 return writeAligned(val);
655 }
656
writePointer(uintptr_t val)657 status_t Parcel::writePointer(uintptr_t val)
658 {
659 return writeAligned<binder_uintptr_t>(val);
660 }
661
writeFloat(float val)662 status_t Parcel::writeFloat(float val)
663 {
664 return writeAligned(val);
665 }
666
667 #if defined(__mips__) && defined(__mips_hard_float)
668
writeDouble(double val)669 status_t Parcel::writeDouble(double val)
670 {
671 union {
672 double d;
673 unsigned long long ll;
674 } u;
675 u.d = val;
676 return writeAligned(u.ll);
677 }
678
679 #else
680
writeDouble(double val)681 status_t Parcel::writeDouble(double val)
682 {
683 return writeAligned(val);
684 }
685
686 #endif
687
writeCString(const char * str)688 status_t Parcel::writeCString(const char* str)
689 {
690 return write(str, strlen(str)+1);
691 }
writeString16(const std::unique_ptr<String16> & str)692 status_t Parcel::writeString16(const std::unique_ptr<String16>& str)
693 {
694 if (!str) {
695 return writeInt32(-1);
696 }
697
698 return writeString16(*str);
699 }
700
writeString16(const String16 & str)701 status_t Parcel::writeString16(const String16& str)
702 {
703 return writeString16(str.string(), str.size());
704 }
705
writeString16(const char16_t * str,size_t len)706 status_t Parcel::writeString16(const char16_t* str, size_t len)
707 {
708 if (str == NULL) return writeInt32(-1);
709
710 status_t err = writeInt32(len);
711 if (err == NO_ERROR) {
712 len *= sizeof(char16_t);
713 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
714 if (data) {
715 memcpy(data, str, len);
716 *reinterpret_cast<char16_t*>(data+len) = 0;
717 return NO_ERROR;
718 }
719 err = mError;
720 }
721 return err;
722 }
writeStrongBinder(const sp<IBinder> & val)723 status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
724 {
725 return flatten_binder(ProcessState::self(), val, this);
726 }
727
writeWeakBinder(const wp<IBinder> & val)728 status_t Parcel::writeWeakBinder(const wp<IBinder>& val)
729 {
730 return flatten_binder(ProcessState::self(), val, this);
731 }
732
733 template <typename T>
writeObject(const T & val)734 status_t Parcel::writeObject(const T& val)
735 {
736 const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
737 const bool enoughObjects = mObjectsSize < mObjectsCapacity;
738 if (enoughData && enoughObjects) {
739 restart_write:
740 *reinterpret_cast<T*>(mData+mDataPos) = val;
741
742 const binder_object_header* hdr = reinterpret_cast<binder_object_header*>(mData+mDataPos);
743 switch (hdr->type) {
744 case BINDER_TYPE_BINDER:
745 case BINDER_TYPE_WEAK_BINDER:
746 case BINDER_TYPE_HANDLE:
747 case BINDER_TYPE_WEAK_HANDLE: {
748 const flat_binder_object *fbo = reinterpret_cast<const flat_binder_object*>(hdr);
749 if (fbo->binder != 0) {
750 mObjects[mObjectsSize++] = mDataPos;
751 acquire_binder_object(ProcessState::self(), *fbo, this);
752 }
753 break;
754 }
755 case BINDER_TYPE_FD: {
756 const binder_fd_object *fd_obj = reinterpret_cast<const binder_fd_object*>(hdr);
757 // remember if it's a file descriptor
758 if (!mAllowFds) {
759 // fail before modifying our object index
760 return FDS_NOT_ALLOWED;
761 }
762 mHasFds = mFdsKnown = true;
763 mObjects[mObjectsSize++] = mDataPos;
764 break;
765 }
766 case BINDER_TYPE_FDA:
767 mObjects[mObjectsSize++] = mDataPos;
768 break;
769 case BINDER_TYPE_PTR: {
770 const binder_buffer_object *buffer_obj = reinterpret_cast<
771 const binder_buffer_object*>(hdr);
772 if ((void *)buffer_obj->buffer != nullptr) {
773 mObjects[mObjectsSize++] = mDataPos;
774 }
775 break;
776 }
777 default: {
778 ALOGE("writeObject: unknown type %d", hdr->type);
779 break;
780 }
781 }
782 return finishWrite(sizeof(val));
783 }
784
785 if (!enoughData) {
786 const status_t err = growData(sizeof(val));
787 if (err != NO_ERROR) return err;
788 }
789 if (!enoughObjects) {
790 size_t newSize = ((mObjectsSize+2)*3)/2;
791 if (newSize * sizeof(binder_size_t) < mObjectsSize) return NO_MEMORY; // overflow
792 binder_size_t* objects = (binder_size_t*)realloc(mObjects, newSize*sizeof(binder_size_t));
793 if (objects == NULL) return NO_MEMORY;
794 mObjects = objects;
795 mObjectsCapacity = newSize;
796 }
797
798 goto restart_write;
799 }
800
801 template status_t Parcel::writeObject<flat_binder_object>(const flat_binder_object& val);
802 template status_t Parcel::writeObject<binder_fd_object>(const binder_fd_object& val);
803 template status_t Parcel::writeObject<binder_buffer_object>(const binder_buffer_object& val);
804 template status_t Parcel::writeObject<binder_fd_array_object>(const binder_fd_array_object& val);
805
806
807 // TODO merge duplicated code in writeEmbeddedBuffer, writeEmbeddedReference, and writeEmbeddedNullReference
808 // TODO merge duplicated code in writeBuffer, writeReference, and writeNullReference
809
validateBufferChild(size_t child_buffer_handle,size_t child_offset) const810 bool Parcel::validateBufferChild(size_t child_buffer_handle,
811 size_t child_offset) const {
812 if (child_buffer_handle >= mObjectsSize)
813 return false;
814 binder_buffer_object *child = reinterpret_cast<binder_buffer_object*>
815 (mData + mObjects[child_buffer_handle]);
816 if (!isBuffer(*child) || child_offset > child->length) {
817 // Parent object not a buffer, or not large enough
818 LOG_BUFFER("writeEmbeddedReference found wierd child. "
819 "child_offset = %zu, child->length = %zu",
820 child_offset, (size_t)child->length);
821 return false;
822 }
823 return true;
824 }
825
validateBufferParent(size_t parent_buffer_handle,size_t parent_offset) const826 bool Parcel::validateBufferParent(size_t parent_buffer_handle,
827 size_t parent_offset) const {
828 if (parent_buffer_handle >= mObjectsSize)
829 return false;
830 binder_buffer_object *parent = reinterpret_cast<binder_buffer_object*>
831 (mData + mObjects[parent_buffer_handle]);
832 if (!isBuffer(*parent) ||
833 sizeof(binder_uintptr_t) > parent->length ||
834 parent_offset > parent->length - sizeof(binder_uintptr_t)) {
835 // Parent object not a buffer, or not large enough
836 return false;
837 }
838 return true;
839 }
writeEmbeddedBuffer(const void * buffer,size_t length,size_t * handle,size_t parent_buffer_handle,size_t parent_offset)840 status_t Parcel::writeEmbeddedBuffer(
841 const void *buffer, size_t length, size_t *handle,
842 size_t parent_buffer_handle, size_t parent_offset) {
843 LOG_BUFFER("writeEmbeddedBuffer(%p, %zu, parent = (%zu, %zu)) -> %zu",
844 buffer, length, parent_buffer_handle,
845 parent_offset, mObjectsSize);
846 if(!validateBufferParent(parent_buffer_handle, parent_offset))
847 return BAD_VALUE;
848 binder_buffer_object obj = {
849 .hdr = { .type = BINDER_TYPE_PTR },
850 .buffer = reinterpret_cast<binder_uintptr_t>(buffer),
851 .length = length,
852 .flags = BINDER_BUFFER_HAS_PARENT,
853 .parent = parent_buffer_handle,
854 .parent_offset = parent_offset,
855 };
856 if (handle != nullptr) {
857 // We use an index into mObjects as a handle
858 *handle = mObjectsSize;
859 }
860 return writeObject(obj);
861 }
862
writeBuffer(const void * buffer,size_t length,size_t * handle)863 status_t Parcel::writeBuffer(const void *buffer, size_t length, size_t *handle)
864 {
865 LOG_BUFFER("writeBuffer(%p, %zu) -> %zu",
866 buffer, length, mObjectsSize);
867 binder_buffer_object obj {
868 .hdr = { .type = BINDER_TYPE_PTR },
869 .buffer = reinterpret_cast<binder_uintptr_t>(buffer),
870 .length = length,
871 .flags = 0,
872 };
873 if (handle != nullptr) {
874 // We use an index into mObjects as a handle
875 *handle = mObjectsSize;
876 }
877 return writeObject(obj);
878 }
879
incrementNumReferences()880 status_t Parcel::incrementNumReferences() {
881 ++mNumRef;
882 LOG_BUFFER("incrementNumReferences: %zu", mNumRef);
883 return mNumRef <= PARCEL_REF_CAP ? OK : NO_MEMORY;
884 }
885
writeReference(size_t * handle,size_t child_buffer_handle,size_t child_offset)886 status_t Parcel::writeReference(size_t *handle,
887 size_t child_buffer_handle, size_t child_offset) {
888 LOG_BUFFER("writeReference(child = (%zu, %zu)) -> %zu",
889 child_buffer_handle, child_offset,
890 mObjectsSize);
891 status_t status = incrementNumReferences();
892 if (status != OK)
893 return status;
894 if (!validateBufferChild(child_buffer_handle, child_offset))
895 return BAD_VALUE;
896 binder_buffer_object obj {
897 .hdr = { .type = BINDER_TYPE_PTR },
898 .flags = BINDER_BUFFER_REF,
899 // The current binder.h does not have child and child_offset names yet.
900 // Use the buffer and length parameters.
901 .child = child_buffer_handle,
902 .child_offset = child_offset,
903 };
904 if (handle != nullptr)
905 // We use an index into mObjects as a handle
906 *handle = mObjectsSize;
907 return writeObject(obj);
908 }
909
910 /* Write an object that describes a pointer from parent to child.
911 * Output the handle of that object in the size_t *handle variable. */
writeEmbeddedReference(size_t * handle,size_t child_buffer_handle,size_t child_offset,size_t parent_buffer_handle,size_t parent_offset)912 status_t Parcel::writeEmbeddedReference(size_t *handle,
913 size_t child_buffer_handle, size_t child_offset,
914 size_t parent_buffer_handle, size_t parent_offset) {
915 LOG_BUFFER("writeEmbeddedReference(child = (%zu, %zu), parent = (%zu, %zu)) -> %zu",
916 child_buffer_handle, child_offset,
917 parent_buffer_handle, parent_offset,
918 mObjectsSize);
919 status_t status = incrementNumReferences();
920 if (status != OK)
921 return status;
922 // The current binder.h does not have child and child_offset names yet.
923 // Use the buffer and length parameters.
924 if (!validateBufferChild(child_buffer_handle, child_offset))
925 return BAD_VALUE;
926 binder_buffer_object obj {
927 .hdr = { .type = BINDER_TYPE_PTR },
928 .flags = BINDER_BUFFER_REF | BINDER_BUFFER_HAS_PARENT,
929 .child = child_buffer_handle,
930 .child_offset = child_offset,
931 .parent = parent_buffer_handle,
932 .parent_offset = parent_offset,
933 };
934 if (handle != nullptr) {
935 // We use an index into mObjects as a handle
936 *handle = mObjectsSize;
937 }
938 return writeObject(obj);
939 }
940
writeNullReference(size_t * handle)941 status_t Parcel::writeNullReference(size_t * handle) {
942 LOG_BUFFER("writeNullReference -> %zu", mObjectsSize);
943 status_t status = incrementNumReferences();
944 if (status != OK)
945 return status;
946
947 binder_buffer_object obj {
948 .hdr = { .type = BINDER_TYPE_PTR },
949 .flags = BINDER_BUFFER_REF,
950 };
951
952 if (handle != nullptr)
953 // We use an index into mObjects as a handle
954 *handle = mObjectsSize;
955 return writeObject(obj);
956 }
957
writeEmbeddedNullReference(size_t * handle,size_t parent_buffer_handle,size_t parent_offset)958 status_t Parcel::writeEmbeddedNullReference(size_t * handle,
959 size_t parent_buffer_handle, size_t parent_offset) {
960 LOG_BUFFER("writeEmbeddedNullReference(parent = (%zu, %zu)) -> %zu",
961 parent_buffer_handle,
962 parent_offset,
963 mObjectsSize);
964 status_t status = incrementNumReferences();
965 if (status != OK)
966 return status;
967 if(!validateBufferParent(parent_buffer_handle, parent_offset))
968 return BAD_VALUE;
969 binder_buffer_object obj {
970 .hdr = { .type = BINDER_TYPE_PTR, },
971 .flags = BINDER_BUFFER_REF | BINDER_BUFFER_HAS_PARENT,
972 .parent = parent_buffer_handle,
973 .parent_offset = parent_offset,
974 };
975 if (handle != nullptr) {
976 // We use an index into mObjects as a handle
977 *handle = mObjectsSize;
978 }
979 return writeObject(obj);
980 }
981
clearCache() const982 void Parcel::clearCache() const {
983 LOG_BUFFER("clearing cache.");
984 mBufCachePos = 0;
985 mBufCache.clear();
986 }
987
updateCache() const988 void Parcel::updateCache() const {
989 if(mBufCachePos == mObjectsSize)
990 return;
991 LOG_BUFFER("updating cache from %zu to %zu", mBufCachePos, mObjectsSize);
992 for(size_t i = mBufCachePos; i < mObjectsSize; i++) {
993 binder_size_t dataPos = mObjects[i];
994 binder_buffer_object *obj =
995 reinterpret_cast<binder_buffer_object*>(mData+dataPos);
996 if(!isBuffer(*obj))
997 continue;
998 BufferInfo ifo;
999 ifo.index = i;
1000 ifo.buffer = obj->buffer;
1001 ifo.bufend = obj->buffer + obj->length;
1002 mBufCache.push_back(ifo);
1003 }
1004 mBufCachePos = mObjectsSize;
1005 }
1006
1007 /* O(n) (n=#buffers) to find a buffer that contains the given addr */
findBuffer(const void * ptr,size_t length,bool * found,size_t * handle,size_t * offset) const1008 status_t Parcel::findBuffer(const void *ptr, size_t length, bool *found,
1009 size_t *handle, size_t *offset) const {
1010 if(found == nullptr)
1011 return UNKNOWN_ERROR;
1012 updateCache();
1013 binder_uintptr_t ptrVal = reinterpret_cast<binder_uintptr_t>(ptr);
1014 // true if the pointer is in some buffer, but the length is too big
1015 // so that ptr + length doesn't fit into the buffer.
1016 bool suspectRejectBadPointer = false;
1017 LOG_BUFFER("findBuffer examining %zu objects.", mObjectsSize);
1018 for(auto entry = mBufCache.rbegin(); entry != mBufCache.rend(); ++entry ) {
1019 if(entry->buffer <= ptrVal && ptrVal < entry->bufend) {
1020 // might have found it.
1021 if(ptrVal + length <= entry->bufend) {
1022 *found = true;
1023 if(handle != nullptr) *handle = entry->index;
1024 if(offset != nullptr) *offset = ptrVal - entry->buffer;
1025 LOG_BUFFER(" findBuffer has a match at %zu!", entry->index);
1026 return OK;
1027 } else {
1028 suspectRejectBadPointer = true;
1029 }
1030 }
1031 }
1032 LOG_BUFFER("findBuffer did not find for ptr = %p.", ptr);
1033 *found = false;
1034 return suspectRejectBadPointer ? BAD_VALUE : OK;
1035 }
1036
1037 /* findBuffer with the assumption that ptr = .buffer (so it points to top
1038 * of the buffer, aka offset 0).
1039 * */
quickFindBuffer(const void * ptr,size_t * handle) const1040 status_t Parcel::quickFindBuffer(const void *ptr, size_t *handle) const {
1041 updateCache();
1042 binder_uintptr_t ptrVal = reinterpret_cast<binder_uintptr_t>(ptr);
1043 LOG_BUFFER("quickFindBuffer examining %zu objects.", mObjectsSize);
1044 for(auto entry = mBufCache.rbegin(); entry != mBufCache.rend(); ++entry ) {
1045 if(entry->buffer == ptrVal) {
1046 if(handle != nullptr) *handle = entry->index;
1047 return OK;
1048 }
1049 }
1050 LOG_BUFFER("quickFindBuffer did not find for ptr = %p.", ptr);
1051 return NO_INIT;
1052 }
1053
writeNativeHandleNoDup(const native_handle_t * handle,bool embedded,size_t parent_buffer_handle,size_t parent_offset)1054 status_t Parcel::writeNativeHandleNoDup(const native_handle_t *handle,
1055 bool embedded,
1056 size_t parent_buffer_handle,
1057 size_t parent_offset)
1058 {
1059 size_t buffer_handle;
1060 status_t status = OK;
1061 uint32_t flags = 0;
1062
1063 if (handle == nullptr) {
1064 status = writeUint64(0);
1065 return status;
1066 }
1067
1068 size_t native_handle_size = sizeof(native_handle_t)
1069 + handle->numFds * sizeof(int) + handle->numInts * sizeof(int);
1070 writeUint64(native_handle_size);
1071
1072 if (embedded) {
1073 status = writeEmbeddedBuffer((void*) handle,
1074 native_handle_size, &buffer_handle,
1075 parent_buffer_handle, parent_offset);
1076 } else {
1077 status = writeBuffer((void*) handle, native_handle_size, &buffer_handle);
1078 }
1079
1080 if (status != OK) {
1081 return status;
1082 }
1083
1084 struct binder_fd_array_object fd_array {
1085 .hdr = { .type = BINDER_TYPE_FDA },
1086 .num_fds = static_cast<binder_size_t>(handle->numFds),
1087 .parent = buffer_handle,
1088 .parent_offset = offsetof(native_handle_t, data),
1089 };
1090
1091 return writeObject(fd_array);
1092 }
1093
writeNativeHandleNoDup(const native_handle_t * handle)1094 status_t Parcel::writeNativeHandleNoDup(const native_handle_t *handle)
1095 {
1096 return writeNativeHandleNoDup(handle, false /* embedded */);
1097 }
1098
writeEmbeddedNativeHandle(const native_handle_t * handle,size_t parent_buffer_handle,size_t parent_offset)1099 status_t Parcel::writeEmbeddedNativeHandle(const native_handle_t *handle,
1100 size_t parent_buffer_handle,
1101 size_t parent_offset)
1102 {
1103 return writeNativeHandleNoDup(handle, true /* embedded */,
1104 parent_buffer_handle, parent_offset);
1105 }
1106
remove(size_t,size_t)1107 void Parcel::remove(size_t /*start*/, size_t /*amt*/)
1108 {
1109 LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
1110 }
1111
read(void * outData,size_t len) const1112 status_t Parcel::read(void* outData, size_t len) const
1113 {
1114 if (len > INT32_MAX) {
1115 // don't accept size_t values which may have come from an
1116 // inadvertent conversion from a negative int.
1117 return BAD_VALUE;
1118 }
1119
1120 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1121 && len <= pad_size(len)) {
1122 memcpy(outData, mData+mDataPos, len);
1123 mDataPos += pad_size(len);
1124 ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
1125 return NO_ERROR;
1126 }
1127 return NOT_ENOUGH_DATA;
1128 }
1129
readInplace(size_t len) const1130 const void* Parcel::readInplace(size_t len) const
1131 {
1132 if (len > INT32_MAX) {
1133 // don't accept size_t values which may have come from an
1134 // inadvertent conversion from a negative int.
1135 return NULL;
1136 }
1137
1138 if ((mDataPos+pad_size(len)) >= mDataPos && (mDataPos+pad_size(len)) <= mDataSize
1139 && len <= pad_size(len)) {
1140 const void* data = mData+mDataPos;
1141 mDataPos += pad_size(len);
1142 ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
1143 return data;
1144 }
1145 return NULL;
1146 }
1147
1148 template<class T>
readAligned(T * pArg) const1149 status_t Parcel::readAligned(T *pArg) const {
1150 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
1151
1152 if ((mDataPos+sizeof(T)) <= mDataSize) {
1153 const void* data = mData+mDataPos;
1154 mDataPos += sizeof(T);
1155 *pArg = *reinterpret_cast<const T*>(data);
1156 return NO_ERROR;
1157 } else {
1158 return NOT_ENOUGH_DATA;
1159 }
1160 }
1161
1162 template<class T>
readAligned() const1163 T Parcel::readAligned() const {
1164 T result;
1165 if (readAligned(&result) != NO_ERROR) {
1166 result = 0;
1167 }
1168
1169 return result;
1170 }
1171
1172 template<class T>
writeAligned(T val)1173 status_t Parcel::writeAligned(T val) {
1174 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));
1175
1176 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
1177 restart_write:
1178 *reinterpret_cast<T*>(mData+mDataPos) = val;
1179 return finishWrite(sizeof(val));
1180 }
1181
1182 status_t err = growData(sizeof(val));
1183 if (err == NO_ERROR) goto restart_write;
1184 return err;
1185 }
1186
readInt8(int8_t * pArg) const1187 status_t Parcel::readInt8(int8_t *pArg) const
1188 {
1189 return read(pArg, sizeof(*pArg));
1190 }
1191
readUint8(uint8_t * pArg) const1192 status_t Parcel::readUint8(uint8_t *pArg) const
1193 {
1194 return read(pArg, sizeof(*pArg));
1195 }
1196
readInt16(int16_t * pArg) const1197 status_t Parcel::readInt16(int16_t *pArg) const
1198 {
1199 return read(pArg, sizeof(*pArg));
1200 }
1201
readUint16(uint16_t * pArg) const1202 status_t Parcel::readUint16(uint16_t *pArg) const
1203 {
1204 return read(pArg, sizeof(*pArg));
1205 }
1206
readInt32(int32_t * pArg) const1207 status_t Parcel::readInt32(int32_t *pArg) const
1208 {
1209 return readAligned(pArg);
1210 }
1211
readInt32() const1212 int32_t Parcel::readInt32() const
1213 {
1214 return readAligned<int32_t>();
1215 }
1216
readUint32(uint32_t * pArg) const1217 status_t Parcel::readUint32(uint32_t *pArg) const
1218 {
1219 return readAligned(pArg);
1220 }
1221
readUint32() const1222 uint32_t Parcel::readUint32() const
1223 {
1224 return readAligned<uint32_t>();
1225 }
1226
readInt64(int64_t * pArg) const1227 status_t Parcel::readInt64(int64_t *pArg) const
1228 {
1229 return readAligned(pArg);
1230 }
1231
readInt64() const1232 int64_t Parcel::readInt64() const
1233 {
1234 return readAligned<int64_t>();
1235 }
1236
readUint64(uint64_t * pArg) const1237 status_t Parcel::readUint64(uint64_t *pArg) const
1238 {
1239 return readAligned(pArg);
1240 }
1241
readUint64() const1242 uint64_t Parcel::readUint64() const
1243 {
1244 return readAligned<uint64_t>();
1245 }
1246
readPointer(uintptr_t * pArg) const1247 status_t Parcel::readPointer(uintptr_t *pArg) const
1248 {
1249 status_t ret;
1250 binder_uintptr_t ptr;
1251 ret = readAligned(&ptr);
1252 if (!ret)
1253 *pArg = ptr;
1254 return ret;
1255 }
1256
readPointer() const1257 uintptr_t Parcel::readPointer() const
1258 {
1259 return readAligned<binder_uintptr_t>();
1260 }
1261
1262
readFloat(float * pArg) const1263 status_t Parcel::readFloat(float *pArg) const
1264 {
1265 return readAligned(pArg);
1266 }
1267
1268
readFloat() const1269 float Parcel::readFloat() const
1270 {
1271 return readAligned<float>();
1272 }
1273
1274 #if defined(__mips__) && defined(__mips_hard_float)
1275
readDouble(double * pArg) const1276 status_t Parcel::readDouble(double *pArg) const
1277 {
1278 union {
1279 double d;
1280 unsigned long long ll;
1281 } u;
1282 u.d = 0;
1283 status_t status;
1284 status = readAligned(&u.ll);
1285 *pArg = u.d;
1286 return status;
1287 }
1288
readDouble() const1289 double Parcel::readDouble() const
1290 {
1291 union {
1292 double d;
1293 unsigned long long ll;
1294 } u;
1295 u.ll = readAligned<unsigned long long>();
1296 return u.d;
1297 }
1298
1299 #else
1300
readDouble(double * pArg) const1301 status_t Parcel::readDouble(double *pArg) const
1302 {
1303 return readAligned(pArg);
1304 }
1305
readDouble() const1306 double Parcel::readDouble() const
1307 {
1308 return readAligned<double>();
1309 }
1310
1311 #endif
1312
readBool(bool * pArg) const1313 status_t Parcel::readBool(bool *pArg) const
1314 {
1315 int8_t tmp;
1316 status_t ret = readInt8(&tmp);
1317 *pArg = (tmp != 0);
1318 return ret;
1319 }
1320
readBool() const1321 bool Parcel::readBool() const
1322 {
1323 int8_t tmp;
1324 status_t err = readInt8(&tmp);
1325
1326 if (err != OK) {
1327 return 0;
1328 }
1329
1330 return tmp != 0;
1331 }
1332
readCString() const1333 const char* Parcel::readCString() const
1334 {
1335 if (mDataPos < mDataSize) {
1336 const size_t avail = mDataSize-mDataPos;
1337 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
1338 // is the string's trailing NUL within the parcel's valid bounds?
1339 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
1340 if (eos) {
1341 const size_t len = eos - str;
1342 mDataPos += pad_size(len+1);
1343 ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
1344 return str;
1345 }
1346 }
1347 return NULL;
1348 }
readString16() const1349 String16 Parcel::readString16() const
1350 {
1351 size_t len;
1352 const char16_t* str = readString16Inplace(&len);
1353 if (str) return String16(str, len);
1354 ALOGE("Reading a NULL string not supported here.");
1355 return String16();
1356 }
1357
readString16(std::unique_ptr<String16> * pArg) const1358 status_t Parcel::readString16(std::unique_ptr<String16>* pArg) const
1359 {
1360 const int32_t start = dataPosition();
1361 int32_t size;
1362 status_t status = readInt32(&size);
1363 pArg->reset();
1364
1365 if (status != OK || size < 0) {
1366 return status;
1367 }
1368
1369 setDataPosition(start);
1370 pArg->reset(new (std::nothrow) String16());
1371
1372 status = readString16(pArg->get());
1373
1374 if (status != OK) {
1375 pArg->reset();
1376 }
1377
1378 return status;
1379 }
1380
readString16(String16 * pArg) const1381 status_t Parcel::readString16(String16* pArg) const
1382 {
1383 size_t len;
1384 const char16_t* str = readString16Inplace(&len);
1385 if (str) {
1386 pArg->setTo(str, len);
1387 return 0;
1388 } else {
1389 *pArg = String16();
1390 return UNEXPECTED_NULL;
1391 }
1392 }
1393
readString16Inplace(size_t * outLen) const1394 const char16_t* Parcel::readString16Inplace(size_t* outLen) const
1395 {
1396 int32_t size = readInt32();
1397 // watch for potential int overflow from size+1
1398 if (size >= 0 && size < INT32_MAX) {
1399 *outLen = size;
1400 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
1401 if (str != NULL) {
1402 return str;
1403 }
1404 }
1405 *outLen = 0;
1406 return NULL;
1407 }
readStrongBinder(sp<IBinder> * val) const1408 status_t Parcel::readStrongBinder(sp<IBinder>* val) const
1409 {
1410 status_t status = readNullableStrongBinder(val);
1411 if (status == OK && !val->get()) {
1412 status = UNEXPECTED_NULL;
1413 }
1414 return status;
1415 }
1416
readNullableStrongBinder(sp<IBinder> * val) const1417 status_t Parcel::readNullableStrongBinder(sp<IBinder>* val) const
1418 {
1419 return unflatten_binder(ProcessState::self(), *this, val);
1420 }
1421
readStrongBinder() const1422 sp<IBinder> Parcel::readStrongBinder() const
1423 {
1424 sp<IBinder> val;
1425 // Note that a lot of code in Android reads binders by hand with this
1426 // method, and that code has historically been ok with getting nullptr
1427 // back (while ignoring error codes).
1428 readNullableStrongBinder(&val);
1429 return val;
1430 }
1431
readWeakBinder() const1432 wp<IBinder> Parcel::readWeakBinder() const
1433 {
1434 wp<IBinder> val;
1435 unflatten_binder(ProcessState::self(), *this, &val);
1436 return val;
1437 }
1438
1439 template<typename T>
readObject(size_t * objects_offset) const1440 const T* Parcel::readObject(size_t *objects_offset) const
1441 {
1442 const size_t DPOS = mDataPos;
1443 if (objects_offset != nullptr) {
1444 *objects_offset = 0;
1445 }
1446
1447 if ((DPOS+sizeof(T)) <= mDataSize) {
1448 const T* obj = reinterpret_cast<const T*>(mData+DPOS);
1449 mDataPos = DPOS + sizeof(T);
1450 const binder_object_header *hdr = reinterpret_cast<const binder_object_header*>(obj);
1451 switch (hdr->type) {
1452 case BINDER_TYPE_BINDER:
1453 case BINDER_TYPE_WEAK_BINDER:
1454 case BINDER_TYPE_HANDLE:
1455 case BINDER_TYPE_WEAK_HANDLE: {
1456 const flat_binder_object *flat_obj =
1457 reinterpret_cast<const flat_binder_object*>(hdr);
1458 if (flat_obj->cookie == 0 && flat_obj->binder == 0) {
1459 // When transferring a NULL binder object, we don't write it into
1460 // the object list, so we don't want to check for it when
1461 // reading.
1462 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
1463 return obj;
1464 }
1465 break;
1466 }
1467 case BINDER_TYPE_FD:
1468 case BINDER_TYPE_FDA:
1469 // fd (-arrays) must always appear in the meta-data list (eg touched by the kernel)
1470 break;
1471 case BINDER_TYPE_PTR: {
1472 const binder_buffer_object *buffer_obj =
1473 reinterpret_cast<const binder_buffer_object*>(hdr);
1474 if ((void *)buffer_obj->buffer == nullptr) {
1475 // null pointers can be returned directly - they're not written in the
1476 // object list. All non-null buffers must appear in the objects list.
1477 return obj;
1478 }
1479 break;
1480 }
1481 }
1482 // Ensure that this object is valid...
1483 binder_size_t* const OBJS = mObjects;
1484 const size_t N = mObjectsSize;
1485 size_t opos = mNextObjectHint;
1486
1487 if (N > 0) {
1488 ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
1489 this, DPOS, opos);
1490
1491 // Start at the current hint position, looking for an object at
1492 // the current data position.
1493 if (opos < N) {
1494 while (opos < (N-1) && OBJS[opos] < DPOS) {
1495 opos++;
1496 }
1497 } else {
1498 opos = N-1;
1499 }
1500 if (OBJS[opos] == DPOS) {
1501 // Found it!
1502 ALOGV("Parcel %p found obj %zu at index %zu with forward search",
1503 this, DPOS, opos);
1504 mNextObjectHint = opos+1;
1505 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
1506 if (objects_offset != nullptr) {
1507 *objects_offset = opos;
1508 }
1509 return obj;
1510 }
1511
1512 // Look backwards for it...
1513 while (opos > 0 && OBJS[opos] > DPOS) {
1514 opos--;
1515 }
1516 if (OBJS[opos] == DPOS) {
1517 // Found it!
1518 ALOGV("Parcel %p found obj %zu at index %zu with backward search",
1519 this, DPOS, opos);
1520 mNextObjectHint = opos+1;
1521 ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
1522 if (objects_offset != nullptr) {
1523 *objects_offset = opos;
1524 }
1525 return obj;
1526 }
1527 }
1528 ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
1529 this, DPOS);
1530 }
1531 return NULL;
1532 }
1533
1534 template const flat_binder_object* Parcel::readObject<flat_binder_object>(size_t *objects_offset) const;
1535
1536 template const binder_fd_object* Parcel::readObject<binder_fd_object>(size_t *objects_offset) const;
1537
1538 template const binder_buffer_object* Parcel::readObject<binder_buffer_object>(size_t *objects_offset) const;
1539
1540 template const binder_fd_array_object* Parcel::readObject<binder_fd_array_object>(size_t *objects_offset) const;
1541
verifyBufferObject(const binder_buffer_object * buffer_obj,size_t size,uint32_t flags,size_t parent,size_t parentOffset) const1542 bool Parcel::verifyBufferObject(const binder_buffer_object *buffer_obj,
1543 size_t size, uint32_t flags, size_t parent,
1544 size_t parentOffset) const {
1545 if (buffer_obj->length != size) {
1546 ALOGE("Buffer length %" PRIu64 " does not match expected size %zu.",
1547 static_cast<uint64_t>(buffer_obj->length), size);
1548 return false;
1549 }
1550
1551 if (buffer_obj->flags != flags) {
1552 ALOGE("Buffer flags 0x%02X do not match expected flags 0x%02X.", buffer_obj->flags, flags);
1553 return false;
1554 }
1555
1556 if (flags & BINDER_BUFFER_HAS_PARENT) {
1557 if (buffer_obj->parent != parent) {
1558 ALOGE("Buffer parent %" PRIu64 " does not match expected parent %zu.",
1559 static_cast<uint64_t>(buffer_obj->parent), parent);
1560 return false;
1561 }
1562 if (buffer_obj->parent_offset != parentOffset) {
1563 ALOGE("Buffer parent offset %" PRIu64 " does not match expected offset %zu.",
1564 static_cast<uint64_t>(buffer_obj->parent_offset), parentOffset);
1565 return false;
1566 }
1567 }
1568
1569 return true;
1570 }
1571
readBuffer(size_t buffer_size,size_t * buffer_handle,uint32_t flags,size_t parent,size_t parentOffset,const void ** buffer_out) const1572 status_t Parcel::readBuffer(size_t buffer_size, size_t *buffer_handle,
1573 uint32_t flags, size_t parent, size_t parentOffset,
1574 const void **buffer_out) const {
1575
1576 status_t status = OK;
1577
1578 const binder_buffer_object* buffer_obj = readObject<binder_buffer_object>(buffer_handle);
1579
1580 if (buffer_obj == nullptr || !isBuffer(*buffer_obj)) {
1581 return BAD_VALUE;
1582 }
1583
1584 if (!verifyBufferObject(buffer_obj, buffer_size, flags, parent, parentOffset)) {
1585 return BAD_VALUE;
1586 }
1587
1588 // in read side, always use .buffer and .length.
1589 *buffer_out = reinterpret_cast<void*>(buffer_obj->buffer);
1590
1591 return OK;
1592 }
1593
readNullableBuffer(size_t buffer_size,size_t * buffer_handle,const void ** buffer_out) const1594 status_t Parcel::readNullableBuffer(size_t buffer_size, size_t *buffer_handle,
1595 const void **buffer_out) const
1596 {
1597 return readBuffer(buffer_size, buffer_handle,
1598 0 /* flags */, 0 /* parent */, 0 /* parentOffset */,
1599 buffer_out);
1600 }
1601
readBuffer(size_t buffer_size,size_t * buffer_handle,const void ** buffer_out) const1602 status_t Parcel::readBuffer(size_t buffer_size, size_t *buffer_handle,
1603 const void **buffer_out) const
1604 {
1605 status_t status = readNullableBuffer(buffer_size, buffer_handle, buffer_out);
1606 if (status == OK && *buffer_out == nullptr) {
1607 return UNEXPECTED_NULL;
1608 }
1609 return status;
1610 }
1611
1612
readEmbeddedBuffer(size_t buffer_size,size_t * buffer_handle,size_t parent_buffer_handle,size_t parent_offset,const void ** buffer_out) const1613 status_t Parcel::readEmbeddedBuffer(size_t buffer_size,
1614 size_t *buffer_handle,
1615 size_t parent_buffer_handle,
1616 size_t parent_offset,
1617 const void **buffer_out) const
1618 {
1619 status_t status = readNullableEmbeddedBuffer(buffer_size, buffer_handle,
1620 parent_buffer_handle,
1621 parent_offset, buffer_out);
1622 if (status == OK && *buffer_out == nullptr) {
1623 return UNEXPECTED_NULL;
1624 }
1625 return status;
1626 }
1627
readNullableEmbeddedBuffer(size_t buffer_size,size_t * buffer_handle,size_t parent_buffer_handle,size_t parent_offset,const void ** buffer_out) const1628 status_t Parcel::readNullableEmbeddedBuffer(size_t buffer_size,
1629 size_t *buffer_handle,
1630 size_t parent_buffer_handle,
1631 size_t parent_offset,
1632 const void **buffer_out) const
1633 {
1634 return readBuffer(buffer_size, buffer_handle, BINDER_BUFFER_HAS_PARENT,
1635 parent_buffer_handle, parent_offset, buffer_out);
1636 }
1637
1638 // isRef if corresponds to a writeReference call, else corresponds to a writeBuffer call.
1639 // see ::android::hardware::writeReferenceToParcel for details.
readReference(void const ** bufptr,size_t * buffer_handle,bool * isRef) const1640 status_t Parcel::readReference(void const* *bufptr,
1641 size_t *buffer_handle, bool *isRef) const
1642 {
1643 LOG_BUFFER("readReference");
1644 const binder_buffer_object* buffer_obj = readObject<binder_buffer_object>();
1645 LOG_BUFFER(" readReference: buf = %p, len = %zu, flags = %x",
1646 (void*)buffer_obj->buffer, (size_t)buffer_obj->length,
1647 (int)buffer_obj->flags);
1648 // TODO need verification here
1649 if (buffer_obj && buffer_obj->hdr.type == BINDER_TYPE_PTR) {
1650 if (buffer_handle != nullptr) {
1651 *buffer_handle = 0; // TODO fix this, as readBuffer would do
1652 }
1653 if(isRef != nullptr) {
1654 *isRef = (buffer_obj->flags & BINDER_BUFFER_REF) != 0;
1655 LOG_BUFFER(" readReference: isRef = %d", *isRef);
1656 }
1657 // in read side, always use .buffer and .length.
1658 if(bufptr != nullptr) {
1659 *bufptr = (void*)buffer_obj->buffer;
1660 }
1661 return OK;
1662 }
1663
1664 return BAD_VALUE;
1665 }
1666
1667 // isRef if corresponds to a writeEmbeddedReference call, else corresponds to a writeEmbeddedBuffer call.
1668 // see ::android::hardware::writeEmbeddedReferenceToParcel for details.
readEmbeddedReference(void const ** bufptr,size_t * buffer_handle,size_t,size_t,bool * isRef) const1669 status_t Parcel::readEmbeddedReference(void const* *bufptr,
1670 size_t *buffer_handle,
1671 size_t /* parent_buffer_handle */,
1672 size_t /* parent_offset */,
1673 bool *isRef) const
1674 {
1675 // TODO verify parent and offset
1676 LOG_BUFFER("readEmbeddedReference");
1677 return (readReference(bufptr, buffer_handle, isRef));
1678 }
1679
readEmbeddedNativeHandle(size_t parent_buffer_handle,size_t parent_offset,const native_handle_t ** handle) const1680 status_t Parcel::readEmbeddedNativeHandle(size_t parent_buffer_handle,
1681 size_t parent_offset,
1682 const native_handle_t **handle) const
1683 {
1684 status_t status = readNullableEmbeddedNativeHandle(parent_buffer_handle, parent_offset, handle);
1685 if (status == OK && *handle == nullptr) {
1686 return UNEXPECTED_NULL;
1687 }
1688 return status;
1689 }
1690
readNullableNativeHandleNoDup(const native_handle_t ** handle,bool embedded,size_t parent_buffer_handle,size_t parent_offset) const1691 status_t Parcel::readNullableNativeHandleNoDup(const native_handle_t **handle,
1692 bool embedded,
1693 size_t parent_buffer_handle,
1694 size_t parent_offset) const
1695 {
1696 status_t status;
1697 uint64_t nativeHandleSize;
1698 size_t fdaParent;
1699
1700 status = readUint64(&nativeHandleSize);
1701 if (status != OK || nativeHandleSize == 0) {
1702 *handle = nullptr;
1703 return status;
1704 }
1705
1706 if (nativeHandleSize < sizeof(native_handle_t)) {
1707 ALOGE("Received a native_handle_t size that was too small.");
1708 return BAD_VALUE;
1709 }
1710
1711 if (embedded) {
1712 status = readNullableEmbeddedBuffer(nativeHandleSize, &fdaParent,
1713 parent_buffer_handle, parent_offset,
1714 reinterpret_cast<const void**>(handle));
1715 } else {
1716 status = readNullableBuffer(nativeHandleSize, &fdaParent,
1717 reinterpret_cast<const void**>(handle));
1718 }
1719
1720 if (status != OK) {
1721 return status;
1722 }
1723
1724 int numFds = (*handle)->numFds;
1725 int numInts = (*handle)->numInts;
1726
1727 if (numFds < 0 || numFds > NATIVE_HANDLE_MAX_FDS) {
1728 ALOGE("Received native_handle with invalid number of fds.");
1729 return BAD_VALUE;
1730 }
1731
1732 if (numInts < 0 || numInts > NATIVE_HANDLE_MAX_INTS) {
1733 ALOGE("Received native_handle with invalid number of ints.");
1734 return BAD_VALUE;
1735 }
1736
1737 if (nativeHandleSize != (sizeof(native_handle_t) + ((numFds + numInts) * sizeof(int)))) {
1738 ALOGE("Size of native_handle doesn't match.");
1739 return BAD_VALUE;
1740 }
1741
1742 const binder_fd_array_object* fd_array_obj = readObject<binder_fd_array_object>();
1743
1744 if (fd_array_obj == nullptr || fd_array_obj->hdr.type != BINDER_TYPE_FDA) {
1745 ALOGE("Can't find file-descriptor array object.");
1746 return BAD_VALUE;
1747 }
1748
1749 if (static_cast<int>(fd_array_obj->num_fds) != numFds) {
1750 ALOGE("Number of native handles does not match.");
1751 return BAD_VALUE;
1752 }
1753
1754 if (fd_array_obj->parent != fdaParent) {
1755 ALOGE("Parent handle of file-descriptor array not correct.");
1756 return BAD_VALUE;
1757 }
1758
1759 if (fd_array_obj->parent_offset != offsetof(native_handle_t, data)) {
1760 ALOGE("FD array object not properly offset in parent.");
1761 return BAD_VALUE;
1762 }
1763
1764 return OK;
1765 }
1766
readNullableEmbeddedNativeHandle(size_t parent_buffer_handle,size_t parent_offset,const native_handle_t ** handle) const1767 status_t Parcel::readNullableEmbeddedNativeHandle(size_t parent_buffer_handle,
1768 size_t parent_offset,
1769 const native_handle_t **handle) const
1770 {
1771 return readNullableNativeHandleNoDup(handle, true /* embedded */, parent_buffer_handle,
1772 parent_offset);
1773 }
1774
readNativeHandleNoDup(const native_handle_t ** handle) const1775 status_t Parcel::readNativeHandleNoDup(const native_handle_t **handle) const
1776 {
1777 status_t status = readNullableNativeHandleNoDup(handle);
1778 if (status == OK && *handle == nullptr) {
1779 return UNEXPECTED_NULL;
1780 }
1781 return status;
1782 }
1783
readNullableNativeHandleNoDup(const native_handle_t ** handle) const1784 status_t Parcel::readNullableNativeHandleNoDup(const native_handle_t **handle) const
1785 {
1786 return readNullableNativeHandleNoDup(handle, false /* embedded */);
1787 }
1788
closeFileDescriptors()1789 void Parcel::closeFileDescriptors()
1790 {
1791 size_t i = mObjectsSize;
1792 if (i > 0) {
1793 //ALOGI("Closing file descriptors for %zu objects...", i);
1794 }
1795 while (i > 0) {
1796 i--;
1797 const flat_binder_object* flat
1798 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
1799 if (flat->type == BINDER_TYPE_FD) {
1800 //ALOGI("Closing fd: %ld", flat->handle);
1801 close(flat->handle);
1802 }
1803 }
1804 }
1805
ipcData() const1806 uintptr_t Parcel::ipcData() const
1807 {
1808 return reinterpret_cast<uintptr_t>(mData);
1809 }
1810
ipcDataSize() const1811 size_t Parcel::ipcDataSize() const
1812 {
1813 return mDataSize > mDataPos ? mDataSize : mDataPos;
1814 }
1815
ipcObjects() const1816 uintptr_t Parcel::ipcObjects() const
1817 {
1818 return reinterpret_cast<uintptr_t>(mObjects);
1819 }
1820
ipcObjectsCount() const1821 size_t Parcel::ipcObjectsCount() const
1822 {
1823 return mObjectsSize;
1824 }
1825
1826 #define BUFFER_ALIGNMENT_BYTES 8
ipcBufferSize() const1827 size_t Parcel::ipcBufferSize() const
1828 {
1829 size_t totalBuffersSize = 0;
1830 // Add size for BINDER_TYPE_PTR
1831 size_t i = mObjectsSize;
1832 while (i > 0) {
1833 i--;
1834 const binder_buffer_object* buffer
1835 = reinterpret_cast<binder_buffer_object*>(mData+mObjects[i]);
1836 if (isBuffer(*buffer)) {
1837 /* The binder kernel driver requires each buffer to be 8-byte
1838 * aligned */
1839 size_t alignedSize = (buffer->length + (BUFFER_ALIGNMENT_BYTES - 1))
1840 & ~(BUFFER_ALIGNMENT_BYTES - 1);
1841 if (alignedSize > SIZE_MAX - totalBuffersSize) {
1842 ALOGE("ipcBuffersSize(): invalid buffer sizes.");
1843 return 0;
1844 }
1845 totalBuffersSize += alignedSize;
1846 }
1847 }
1848 return totalBuffersSize;
1849 }
1850
ipcSetDataReference(const uint8_t * data,size_t dataSize,const binder_size_t * objects,size_t objectsCount,release_func relFunc,void * relCookie)1851 void Parcel::ipcSetDataReference(const uint8_t* data, size_t dataSize,
1852 const binder_size_t* objects, size_t objectsCount, release_func relFunc, void* relCookie)
1853 {
1854 binder_size_t minOffset = 0;
1855 freeDataNoInit();
1856 mError = NO_ERROR;
1857 mData = const_cast<uint8_t*>(data);
1858 mDataSize = mDataCapacity = dataSize;
1859 //ALOGI("setDataReference Setting data size of %p to %lu (pid=%d)", this, mDataSize, getpid());
1860 mDataPos = 0;
1861 ALOGV("setDataReference Setting data pos of %p to %zu", this, mDataPos);
1862 mObjects = const_cast<binder_size_t*>(objects);
1863 mObjectsSize = mObjectsCapacity = objectsCount;
1864 mNextObjectHint = 0;
1865 clearCache();
1866 mNumRef = 0;
1867 mOwner = relFunc;
1868 mOwnerCookie = relCookie;
1869 for (size_t i = 0; i < mObjectsSize; i++) {
1870 binder_size_t offset = mObjects[i];
1871 if (offset < minOffset) {
1872 ALOGE("%s: bad object offset %" PRIu64 " < %" PRIu64 "\n",
1873 __func__, (uint64_t)offset, (uint64_t)minOffset);
1874 mObjectsSize = 0;
1875 break;
1876 }
1877 minOffset = offset + sizeof(flat_binder_object);
1878 }
1879 scanForFds();
1880 }
1881
print(TextOutput & to,uint32_t) const1882 void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
1883 {
1884 to << "Parcel(";
1885
1886 if (errorCheck() != NO_ERROR) {
1887 const status_t err = errorCheck();
1888 to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
1889 } else if (dataSize() > 0) {
1890 const uint8_t* DATA = data();
1891 to << indent << HexDump(DATA, dataSize()) << dedent;
1892 const binder_size_t* OBJS = objects();
1893 const size_t N = objectsCount();
1894 for (size_t i=0; i<N; i++) {
1895 const flat_binder_object* flat
1896 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
1897 if (flat->type == BINDER_TYPE_PTR) {
1898 const binder_buffer_object* buffer
1899 = reinterpret_cast<const binder_buffer_object*>(DATA+OBJS[i]);
1900 if(isBuffer(*buffer)) {
1901 HexDump bufferDump((const uint8_t*)buffer->buffer, (size_t)buffer->length);
1902 bufferDump.setSingleLineCutoff(0);
1903 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << " (buffer size " << buffer->length << "):";
1904 to << indent << bufferDump << dedent;
1905 } else {
1906 to << endl << "Object #" << i << " @ " << (void*)OBJS[i];
1907 }
1908 } else {
1909 to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
1910 << TypeCode(flat->type & 0x7f7f7f00)
1911 << " = " << flat->binder;
1912 }
1913 }
1914 } else {
1915 to << "NULL";
1916 }
1917
1918 to << ")";
1919 }
1920
releaseObjects()1921 void Parcel::releaseObjects()
1922 {
1923 const sp<ProcessState> proc(ProcessState::self());
1924 size_t i = mObjectsSize;
1925 uint8_t* const data = mData;
1926 binder_size_t* const objects = mObjects;
1927 while (i > 0) {
1928 i--;
1929 const flat_binder_object* flat
1930 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
1931 release_object(proc, *flat, this);
1932 }
1933 }
1934
acquireObjects()1935 void Parcel::acquireObjects()
1936 {
1937 const sp<ProcessState> proc(ProcessState::self());
1938 size_t i = mObjectsSize;
1939 uint8_t* const data = mData;
1940 binder_size_t* const objects = mObjects;
1941 while (i > 0) {
1942 i--;
1943 const binder_object_header* flat
1944 = reinterpret_cast<binder_object_header*>(data+objects[i]);
1945 acquire_object(proc, *flat, this);
1946 }
1947 }
1948
freeData()1949 void Parcel::freeData()
1950 {
1951 freeDataNoInit();
1952 initState();
1953 }
1954
freeDataNoInit()1955 void Parcel::freeDataNoInit()
1956 {
1957 if (mOwner) {
1958 LOG_ALLOC("Parcel %p: freeing other owner data", this);
1959 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
1960 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
1961 } else {
1962 LOG_ALLOC("Parcel %p: freeing allocated data", this);
1963 releaseObjects();
1964 if (mData) {
1965 LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
1966 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
1967 if (mDataCapacity <= gParcelGlobalAllocSize) {
1968 gParcelGlobalAllocSize = gParcelGlobalAllocSize - mDataCapacity;
1969 } else {
1970 gParcelGlobalAllocSize = 0;
1971 }
1972 if (gParcelGlobalAllocCount > 0) {
1973 gParcelGlobalAllocCount--;
1974 }
1975 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
1976 free(mData);
1977 }
1978 if (mObjects) free(mObjects);
1979 }
1980 }
1981
growData(size_t len)1982 status_t Parcel::growData(size_t len)
1983 {
1984 if (len > INT32_MAX) {
1985 // don't accept size_t values which may have come from an
1986 // inadvertent conversion from a negative int.
1987 return BAD_VALUE;
1988 }
1989
1990 size_t newSize = ((mDataSize+len)*3)/2;
1991 return (newSize <= mDataSize)
1992 ? (status_t) NO_MEMORY
1993 : continueWrite(newSize);
1994 }
1995
restartWrite(size_t desired)1996 status_t Parcel::restartWrite(size_t desired)
1997 {
1998 if (desired > INT32_MAX) {
1999 // don't accept size_t values which may have come from an
2000 // inadvertent conversion from a negative int.
2001 return BAD_VALUE;
2002 }
2003
2004 if (mOwner) {
2005 freeData();
2006 return continueWrite(desired);
2007 }
2008
2009 uint8_t* data = (uint8_t*)realloc(mData, desired);
2010 if (!data && desired > mDataCapacity) {
2011 mError = NO_MEMORY;
2012 return NO_MEMORY;
2013 }
2014
2015 releaseObjects();
2016
2017 if (data) {
2018 LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
2019 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2020 gParcelGlobalAllocSize += desired;
2021 gParcelGlobalAllocSize -= mDataCapacity;
2022 if (!mData) {
2023 gParcelGlobalAllocCount++;
2024 }
2025 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2026 mData = data;
2027 mDataCapacity = desired;
2028 }
2029
2030 mDataSize = mDataPos = 0;
2031 ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
2032 ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);
2033
2034 free(mObjects);
2035 mObjects = NULL;
2036 mObjectsSize = mObjectsCapacity = 0;
2037 mNextObjectHint = 0;
2038 mHasFds = false;
2039 clearCache();
2040 mNumRef = 0;
2041 mFdsKnown = true;
2042 mAllowFds = true;
2043
2044 return NO_ERROR;
2045 }
2046
continueWrite(size_t desired)2047 status_t Parcel::continueWrite(size_t desired)
2048 {
2049 if (desired > INT32_MAX) {
2050 // don't accept size_t values which may have come from an
2051 // inadvertent conversion from a negative int.
2052 return BAD_VALUE;
2053 }
2054
2055 // If shrinking, first adjust for any objects that appear
2056 // after the new data size.
2057 size_t objectsSize = mObjectsSize;
2058 if (desired < mDataSize) {
2059 if (desired == 0) {
2060 objectsSize = 0;
2061 } else {
2062 while (objectsSize > 0) {
2063 if (mObjects[objectsSize-1] < desired)
2064 break;
2065 objectsSize--;
2066 }
2067 }
2068 }
2069
2070 if (mOwner) {
2071 // If the size is going to zero, just release the owner's data.
2072 if (desired == 0) {
2073 freeData();
2074 return NO_ERROR;
2075 }
2076
2077 // If there is a different owner, we need to take
2078 // posession.
2079 uint8_t* data = (uint8_t*)malloc(desired);
2080 if (!data) {
2081 mError = NO_MEMORY;
2082 return NO_MEMORY;
2083 }
2084 binder_size_t* objects = NULL;
2085
2086 if (objectsSize) {
2087 objects = (binder_size_t*)calloc(objectsSize, sizeof(binder_size_t));
2088 if (!objects) {
2089 free(data);
2090
2091 mError = NO_MEMORY;
2092 return NO_MEMORY;
2093 }
2094
2095 // Little hack to only acquire references on objects
2096 // we will be keeping.
2097 size_t oldObjectsSize = mObjectsSize;
2098 mObjectsSize = objectsSize;
2099 acquireObjects();
2100 mObjectsSize = oldObjectsSize;
2101 }
2102
2103 if (mData) {
2104 memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
2105 }
2106 if (objects && mObjects) {
2107 memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
2108 }
2109 //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
2110 mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
2111 mOwner = NULL;
2112
2113 LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
2114 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2115 gParcelGlobalAllocSize += desired;
2116 gParcelGlobalAllocCount++;
2117 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2118
2119 mData = data;
2120 mObjects = objects;
2121 mDataSize = (mDataSize < desired) ? mDataSize : desired;
2122 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2123 mDataCapacity = desired;
2124 mObjectsSize = mObjectsCapacity = objectsSize;
2125 mNextObjectHint = 0;
2126
2127 clearCache();
2128 } else if (mData) {
2129 if (objectsSize < mObjectsSize) {
2130 // Need to release refs on any objects we are dropping.
2131 const sp<ProcessState> proc(ProcessState::self());
2132 for (size_t i=objectsSize; i<mObjectsSize; i++) {
2133 const flat_binder_object* flat
2134 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
2135 if (flat->type == BINDER_TYPE_FD) {
2136 // will need to rescan because we may have lopped off the only FDs
2137 mFdsKnown = false;
2138 }
2139 release_object(proc, *flat, this);
2140 }
2141 binder_size_t* objects =
2142 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
2143 if (objects) {
2144 mObjects = objects;
2145 }
2146 mObjectsSize = objectsSize;
2147 mNextObjectHint = 0;
2148
2149 clearCache();
2150 }
2151
2152 // We own the data, so we can just do a realloc().
2153 if (desired > mDataCapacity) {
2154 uint8_t* data = (uint8_t*)realloc(mData, desired);
2155 if (data) {
2156 LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
2157 desired);
2158 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2159 gParcelGlobalAllocSize += desired;
2160 gParcelGlobalAllocSize -= mDataCapacity;
2161 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2162 mData = data;
2163 mDataCapacity = desired;
2164 } else if (desired > mDataCapacity) {
2165 mError = NO_MEMORY;
2166 return NO_MEMORY;
2167 }
2168 } else {
2169 if (mDataSize > desired) {
2170 mDataSize = desired;
2171 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2172 }
2173 if (mDataPos > desired) {
2174 mDataPos = desired;
2175 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
2176 }
2177 }
2178
2179 } else {
2180 // This is the first data. Easy!
2181 uint8_t* data = (uint8_t*)malloc(desired);
2182 if (!data) {
2183 mError = NO_MEMORY;
2184 return NO_MEMORY;
2185 }
2186
2187 if(!(mDataCapacity == 0 && mObjects == NULL
2188 && mObjectsCapacity == 0)) {
2189 ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
2190 }
2191
2192 LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
2193 pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
2194 gParcelGlobalAllocSize += desired;
2195 gParcelGlobalAllocCount++;
2196 pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
2197
2198 mData = data;
2199 mDataSize = mDataPos = 0;
2200 ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
2201 ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
2202 mDataCapacity = desired;
2203 }
2204
2205 return NO_ERROR;
2206 }
2207
initState()2208 void Parcel::initState()
2209 {
2210 LOG_ALLOC("Parcel %p: initState", this);
2211 mError = NO_ERROR;
2212 mData = 0;
2213 mDataSize = 0;
2214 mDataCapacity = 0;
2215 mDataPos = 0;
2216 ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
2217 ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
2218 mObjects = NULL;
2219 mObjectsSize = 0;
2220 mObjectsCapacity = 0;
2221 mNextObjectHint = 0;
2222 mHasFds = false;
2223 mFdsKnown = true;
2224 mAllowFds = true;
2225 mOwner = NULL;
2226 clearCache();
2227 mNumRef = 0;
2228
2229 // racing multiple init leads only to multiple identical write
2230 if (gMaxFds == 0) {
2231 struct rlimit result;
2232 if (!getrlimit(RLIMIT_NOFILE, &result)) {
2233 gMaxFds = (size_t)result.rlim_cur;
2234 //ALOGI("parcel fd limit set to %zu", gMaxFds);
2235 } else {
2236 ALOGW("Unable to getrlimit: %s", strerror(errno));
2237 gMaxFds = 1024;
2238 }
2239 }
2240 }
2241
scanForFds() const2242 void Parcel::scanForFds() const
2243 {
2244 bool hasFds = false;
2245 for (size_t i=0; i<mObjectsSize; i++) {
2246 const flat_binder_object* flat
2247 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
2248 if (flat->type == BINDER_TYPE_FD) {
2249 hasFds = true;
2250 break;
2251 }
2252 }
2253 mHasFds = hasFds;
2254 mFdsKnown = true;
2255 }
2256
2257 }; // namespace hardware
2258 }; // namespace android
2259