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