Commit message: None
CVE: CVE-2015-3845
CWE: CWE-264
Vulnerability description: The Parcel::appendFrom function in libs/binder/Parcel.cpp in Binder in Android before 5.1.1 LMY48M does not consider parcel boundaries during identification of binder objects in an append operation, which allows attackers to obtain a different application's privileges via a crafted application, aka internal bug 17312693.

Project: Android

void Parcel::acquireObjects()
{
 const sp<ProcessState> proc(ProcessState::self());
 size_t i = mObjectsSize;
 uint8_t* const data = mData;
 binder_size_t* const objects = mObjects;
 while (i > 0) {
        i--;
 const flat_binder_object* flat
 = reinterpret_cast<flat_binder_object*>(data+objects[i]);
        acquire_object(proc, *flat, this);
 }
}

Project: Android

bool Parcel::checkInterface(IBinder* binder) const
{
 return enforceInterface(binder->getInterfaceDescriptor());
}

Project: Android

void Parcel::Blob::clear() {
    mMapped = false;
    mData = NULL;
    mSize = 0;
}

Project: Android

void Parcel::closeFileDescriptors()
{
 size_t i = mObjectsSize;
 if (i > 0) {
 }
 while (i > 0) {
        i--;
 const flat_binder_object* flat
 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
 if (flat->type == BINDER_TYPE_FD) {
            close(flat->handle);
 }
 }
}

Project: Android

status_t Parcel::continueWrite(size_t desired)
{
 size_t objectsSize = mObjectsSize;
 if (desired < mDataSize) {
 if (desired == 0) {
            objectsSize = 0;
 } else {
 while (objectsSize > 0) {
 if (mObjects[objectsSize-1] < desired)
 break;
                objectsSize--;
 }
 }
 }

 if (mOwner) {
 if (desired == 0) {
            freeData();
 return NO_ERROR;
 }

 uint8_t* data = (uint8_t*)malloc(desired);
 if (!data) {
            mError = NO_MEMORY;
 return NO_MEMORY;
 }
 binder_size_t* objects = NULL;

 if (objectsSize) {
            objects = (binder_size_t*)malloc(objectsSize*sizeof(binder_size_t));
 if (!objects) {
                free(data);

                mError = NO_MEMORY;
 return NO_MEMORY;
 }

 size_t oldObjectsSize = mObjectsSize;
            mObjectsSize = objectsSize;
            acquireObjects();
            mObjectsSize = oldObjectsSize;
 }

 if (mData) {
            memcpy(data, mData, mDataSize < desired ? mDataSize : desired);
 }
 if (objects && mObjects) {
            memcpy(objects, mObjects, objectsSize*sizeof(binder_size_t));
 }
        mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
        mOwner = NULL;

        LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
        pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
        gParcelGlobalAllocSize += desired;
        gParcelGlobalAllocCount++;
        pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);

        mData = data;
        mObjects = objects;
        mDataSize = (mDataSize < desired) ? mDataSize : desired;
        ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
        mDataCapacity = desired;
        mObjectsSize = mObjectsCapacity = objectsSize;
        mNextObjectHint = 0;

 } else if (mData) {
 if (objectsSize < mObjectsSize) {
 const sp<ProcessState> proc(ProcessState::self());
 for (size_t i=objectsSize; i<mObjectsSize; i++) {
 const flat_binder_object* flat
 = reinterpret_cast<flat_binder_object*>(mData+mObjects[i]);
 if (flat->type == BINDER_TYPE_FD) {
                    mFdsKnown = false;
 }
                release_object(proc, *flat, this);
 }
 binder_size_t* objects =
 (binder_size_t*)realloc(mObjects, objectsSize*sizeof(binder_size_t));
 if (objects) {
                mObjects = objects;
 }
            mObjectsSize = objectsSize;
            mNextObjectHint = 0;
 }

 if (desired > mDataCapacity) {
 uint8_t* data = (uint8_t*)realloc(mData, desired);
 if (data) {
                LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
                        desired);
                pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
                gParcelGlobalAllocSize += desired;
                gParcelGlobalAllocSize -= mDataCapacity;
                pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
                mData = data;
                mDataCapacity = desired;
 } else if (desired > mDataCapacity) {
                mError = NO_MEMORY;
 return NO_MEMORY;
 }
 } else {
 if (mDataSize > desired) {
                mDataSize = desired;
                ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
 }
 if (mDataPos > desired) {
                mDataPos = desired;
                ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
 }
 }

 } else {
 uint8_t* data = (uint8_t*)malloc(desired);
 if (!data) {
            mError = NO_MEMORY;
 return NO_MEMORY;
 }

 if(!(mDataCapacity == 0 && mObjects == NULL
 && mObjectsCapacity == 0)) {
            ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
 }

        LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
        pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
        gParcelGlobalAllocSize += desired;
        gParcelGlobalAllocCount++;
        pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);

        mData = data;
        mDataSize = mDataPos = 0;
        ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
        ALOGV("continueWrite Setting data pos of %p to %zu", this, mDataPos);
        mDataCapacity = desired;
 }

 return NO_ERROR;
}

Project: Android

const uint8_t* Parcel::data() const
{
 return mData;
}

Project: Android

size_t Parcel::dataCapacity() const
{
 return mDataCapacity;
}

Project: Android

size_t Parcel::dataPosition() const
{
 return mDataPos;
}

Project: Android

size_t Parcel::dataSize() const
{
 return (mDataSize > mDataPos ? mDataSize : mDataPos);
}

Project: Android

bool Parcel::enforceInterface(const String16& interface,
 IPCThreadState* threadState) const
{
 int32_t strictPolicy = readInt32();
 if (threadState == NULL) {
        threadState = IPCThreadState::self();
 }
 if ((threadState->getLastTransactionBinderFlags() &
 IBinder::FLAG_ONEWAY) != 0) {
      threadState->setStrictModePolicy(0);
 } else {
      threadState->setStrictModePolicy(strictPolicy);
 }
 const String16 str(readString16());
 if (str == interface) {
 return true;
 } else {
        ALOGW("**** enforceInterface() expected '%s' but read '%s'",
 String8(interface).string(), String8(str).string());
 return false;
 }
}

Project: Android

status_t Parcel::errorCheck() const
{
 return mError;
}

Project: Android

status_t Parcel::finishWrite(size_t len)
{
    mDataPos += len;
    ALOGV("finishWrite Setting data pos of %p to %zu", this, mDataPos);
 if (mDataPos > mDataSize) {
        mDataSize = mDataPos;
        ALOGV("finishWrite Setting data size of %p to %zu", this, mDataSize);
 }
 return NO_ERROR;
}

Project: Android

inline static status_t finish_flatten_binder(
 const sp<IBinder>& /*binder*/, const flat_binder_object& flat, Parcel* out)
{
 return out->writeObject(flat, false);
}

Project: Android

inline static status_t finish_unflatten_binder(
 BpBinder* /*proxy*/, const flat_binder_object& /*flat*/,
 const Parcel& /*in*/)
{
 return NO_ERROR;
}

Project: Android

status_t flatten_binder(const sp<ProcessState>& /*proc*/,
 const sp<IBinder>& binder, Parcel* out)
{
    flat_binder_object obj;

    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
 if (binder != NULL) {
 IBinder *local = binder->localBinder();
 if (!local) {
 BpBinder *proxy = binder->remoteBinder();
 if (proxy == NULL) {
                ALOGE("null proxy");
 }
 const int32_t handle = proxy ? proxy->handle() : 0;
            obj.type = BINDER_TYPE_HANDLE;
            obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
            obj.handle = handle;
            obj.cookie = 0;
 } else {
            obj.type = BINDER_TYPE_BINDER;
            obj.binder = reinterpret_cast<uintptr_t>(local->getWeakRefs());
            obj.cookie = reinterpret_cast<uintptr_t>(local);
 }
 } else {
        obj.type = BINDER_TYPE_BINDER;
        obj.binder = 0;
        obj.cookie = 0;
 }

 return finish_flatten_binder(binder, obj, out);
}

Project: Android

status_t flatten_binder(const sp<ProcessState>& /*proc*/,
 const wp<IBinder>& binder, Parcel* out)
{
    flat_binder_object obj;

    obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
 if (binder != NULL) {
        sp<IBinder> real = binder.promote();
 if (real != NULL) {
 IBinder *local = real->localBinder();
 if (!local) {
 BpBinder *proxy = real->remoteBinder();
 if (proxy == NULL) {
                    ALOGE("null proxy");
 }
 const int32_t handle = proxy ? proxy->handle() : 0;
                obj.type = BINDER_TYPE_WEAK_HANDLE;
                obj.binder = 0; /* Don't pass uninitialized stack data to a remote process */
                obj.handle = handle;
                obj.cookie = 0;
 } else {
                obj.type = BINDER_TYPE_WEAK_BINDER;
                obj.binder = reinterpret_cast<uintptr_t>(binder.get_refs());
                obj.cookie = reinterpret_cast<uintptr_t>(binder.unsafe_get());
 }
 return finish_flatten_binder(real, obj, out);
 }

        ALOGE("Unable to unflatten Binder weak reference!");
        obj.type = BINDER_TYPE_BINDER;
        obj.binder = 0;
        obj.cookie = 0;
 return finish_flatten_binder(NULL, obj, out);

 } else {
        obj.type = BINDER_TYPE_BINDER;
        obj.binder = 0;
        obj.cookie = 0;
 return finish_flatten_binder(NULL, obj, out);
 }
}

Project: Android

void Parcel::freeData()
{
    freeDataNoInit();
    initState();
}

Project: Android

void Parcel::freeDataNoInit()
{
 if (mOwner) {
        LOG_ALLOC("Parcel %p: freeing other owner data", this);
        mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
 } else {
        LOG_ALLOC("Parcel %p: freeing allocated data", this);
        releaseObjects();
 if (mData) {
            LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
            pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
            gParcelGlobalAllocSize -= mDataCapacity;
            gParcelGlobalAllocCount--;
            pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
            free(mData);
 }
 if (mObjects) free(mObjects);
 }
}

Project: Android

size_t Parcel::getGlobalAllocCount() {
    pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
 size_t count = gParcelGlobalAllocCount;
    pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
 return count;
}

Project: Android

size_t Parcel::getGlobalAllocSize() {
    pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
 size_t size = gParcelGlobalAllocSize;
    pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
 return size;
}

Project: Android

status_t Parcel::growData(size_t len)
{
 size_t newSize = ((mDataSize+len)*3)/2;
 return (newSize <= mDataSize)
 ? (status_t) NO_MEMORY
 : continueWrite(newSize);
}

Project: Android

bool Parcel::hasFileDescriptors() const
{
 if (!mFdsKnown) {
        scanForFds();
 }
 return mHasFds;
}

Project: Android

void Parcel::Blob::init(bool mapped, void* data, size_t size) {
    mMapped = mapped;
    mData = data;
    mSize = size;
}

Project: Android

void Parcel::initState()
{
    LOG_ALLOC("Parcel %p: initState", this);
    mError = NO_ERROR;
    mData = 0;
    mDataSize = 0;
    mDataCapacity = 0;
    mDataPos = 0;
    ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
    ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
    mObjects = NULL;
    mObjectsSize = 0;
    mObjectsCapacity = 0;
    mNextObjectHint = 0;
    mHasFds = false;
    mFdsKnown = true;
    mAllowFds = true;
    mOwner = NULL;
}

Project: Android

uintptr_t Parcel::ipcData() const
{
 return reinterpret_cast<uintptr_t>(mData);
}

Project: Android

size_t Parcel::ipcDataSize() const
{
 return (mDataSize > mDataPos ? mDataSize : mDataPos);
}

Project: Android

uintptr_t Parcel::ipcObjects() const
{
 return reinterpret_cast<uintptr_t>(mObjects);
}

Project: Android

size_t Parcel::ipcObjectsCount() const
{
 return mObjectsSize;
}

Project: Android

const binder_size_t* Parcel::objects() const
{
 return mObjects;
}

Project: Android

size_t Parcel::objectsCount() const
{
 return mObjectsSize;
}

Project: Android

void Parcel::print(TextOutput& to, uint32_t /*flags*/) const
{
    to << "Parcel(";

 if (errorCheck() != NO_ERROR) {
 const status_t err = errorCheck();
        to << "Error: " << (void*)(intptr_t)err << " \"" << strerror(-err) << "\"";
 } else if (dataSize() > 0) {
 const uint8_t* DATA = data();
        to << indent << HexDump(DATA, dataSize()) << dedent;
 const binder_size_t* OBJS = objects();
 const size_t N = objectsCount();
 for (size_t i=0; i<N; i++) {
 const flat_binder_object* flat
 = reinterpret_cast<const flat_binder_object*>(DATA+OBJS[i]);
            to << endl << "Object #" << i << " @ " << (void*)OBJS[i] << ": "
 << TypeCode(flat->type & 0x7f7f7f00)
 << " = " << flat->binder;
 }
 } else {
        to << "NULL";
 }

    to << ")";
}

Project: Android

bool Parcel::pushAllowFds(bool allowFds)
{
 const bool origValue = mAllowFds;
 if (!allowFds) {
        mAllowFds = false;
 }
 return origValue;
}

Project: Android

status_t Parcel::read(void* outData, size_t len) const
{
 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
 && len <= PAD_SIZE(len)) {
        memcpy(outData, mData+mDataPos, len);
        mDataPos += PAD_SIZE(len);
        ALOGV("read Setting data pos of %p to %zu", this, mDataPos);
 return NO_ERROR;
 }
 return NOT_ENOUGH_DATA;
}

Project: Android

status_t Parcel::read(FlattenableHelperInterface& val) const
{
 const size_t len = this->readInt32();
 const size_t fd_count = this->readInt32();

 void const* const buf = this->readInplace(PAD_SIZE(len));
 if (buf == NULL)
 return BAD_VALUE;

 int* fds = NULL;
 if (fd_count) {
        fds = new int[fd_count];
 }

 status_t err = NO_ERROR;
 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
        fds[i] = dup(this->readFileDescriptor());
 if (fds[i] < 0) {
            err = BAD_VALUE;
            ALOGE("dup() failed in Parcel::read, i is %zu, fds[i] is %d, fd_count is %zu, error: %s",
                i, fds[i], fd_count, strerror(errno));
 }
 }

 if (err == NO_ERROR) {
        err = val.unflatten(buf, len, fds, fd_count);
 }

 if (fd_count) {
 delete [] fds;
 }

 return err;
}

Project: Android

status_t Parcel::readAligned(T *pArg) const {
    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));

 if ((mDataPos+sizeof(T)) <= mDataSize) {
 const void* data = mData+mDataPos;
        mDataPos += sizeof(T);
 *pArg = *reinterpret_cast<const T*>(data);
 return NO_ERROR;
 } else {
 return NOT_ENOUGH_DATA;
 }
}

Project: Android

T Parcel::readAligned() const {
    T result;
 if (readAligned(&result) != NO_ERROR) {
        result = 0;
 }

 return result;
}

Project: Android

status_t Parcel::readBlob(size_t len, ReadableBlob* outBlob) const
{
 int32_t useAshmem;
 status_t status = readInt32(&useAshmem);
 if (status) return status;

 if (!useAshmem) {
        ALOGV("readBlob: read in place");
 const void* ptr = readInplace(len);
 if (!ptr) return BAD_VALUE;

        outBlob->init(false /*mapped*/, const_cast<void*>(ptr), len);
 return NO_ERROR;
 }

    ALOGV("readBlob: read from ashmem");
 int fd = readFileDescriptor();
 if (fd == int(BAD_TYPE)) return BAD_VALUE;

 void* ptr = ::mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
 if (ptr == MAP_FAILED) return NO_MEMORY;

    outBlob->init(true /*mapped*/, ptr, len);
 return NO_ERROR;
}

Project: Android

const char* Parcel::readCString() const
{
 const size_t avail = mDataSize-mDataPos;
 if (avail > 0) {
 const char* str = reinterpret_cast<const char*>(mData+mDataPos);
 const char* eos = reinterpret_cast<const char*>(memchr(str, 0, avail));
 if (eos) {
 const size_t len = eos - str;
            mDataPos += PAD_SIZE(len+1);
            ALOGV("readCString Setting data pos of %p to %zu", this, mDataPos);
 return str;
 }
 }
 return NULL;
}

Project: Android

status_t Parcel::readDouble(double *pArg) const
{
 union {
 double d;
 unsigned long long ll;
 } u;
    u.d = 0;
 status_t status;
    status = readAligned(&u.ll);
 *pArg = u.d;
 return status;
}

Project: Android

double Parcel::readDouble() const
{
 union {
 double d;
 unsigned long long ll;
 } u;
    u.ll = readAligned<unsigned long long>();
 return u.d;
}

Project: Android

status_t Parcel::readDouble(double *pArg) const
{
 return readAligned(pArg);
}

Project: Android

double Parcel::readDouble() const
{
 return readAligned<double>();
}

Project: Android

int32_t Parcel::readExceptionCode() const
{
 int32_t exception_code = readAligned<int32_t>();
 if (exception_code == EX_HAS_REPLY_HEADER) {
 int32_t header_start = dataPosition();
 int32_t header_size = readAligned<int32_t>();
    setDataPosition(header_start + header_size);
 return 0;
 }
 return exception_code;
}

Project: Android

int Parcel::readFileDescriptor() const
{
 const flat_binder_object* flat = readObject(true);
 if (flat) {
 switch (flat->type) {
 case BINDER_TYPE_FD:
 return flat->handle;
 }
 }
 return BAD_TYPE;
}

Project: Android

status_t Parcel::readFloat(float *pArg) const
{
 return readAligned(pArg);
}

Project: Android

float Parcel::readFloat() const
{
 return readAligned<float>();
}

Project: Android

const void* Parcel::readInplace(size_t len) const
{
 if ((mDataPos+PAD_SIZE(len)) >= mDataPos && (mDataPos+PAD_SIZE(len)) <= mDataSize
 && len <= PAD_SIZE(len)) {
 const void* data = mData+mDataPos;
        mDataPos += PAD_SIZE(len);
        ALOGV("readInplace Setting data pos of %p to %zu", this, mDataPos);
 return data;
 }
 return NULL;
}

Project: Android

status_t Parcel::readInt32(int32_t *pArg) const
{
 return readAligned(pArg);
}

Project: Android

status_t Parcel::readIntPtr(intptr_t *pArg) const
{
 return readAligned(pArg);
}

Project: Android

intptr_t Parcel::readIntPtr() const
{
 return readAligned<intptr_t>();
}

Project: Android

const flat_binder_object* Parcel::readObject(bool nullMetaData) const
{
 const size_t DPOS = mDataPos;
 if ((DPOS+sizeof(flat_binder_object)) <= mDataSize) {
 const flat_binder_object* obj
 = reinterpret_cast<const flat_binder_object*>(mData+DPOS);
        mDataPos = DPOS + sizeof(flat_binder_object);
 if (!nullMetaData && (obj->cookie == 0 && obj->binder == 0)) {
            ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
 return obj;
 }

 binder_size_t* const OBJS = mObjects;
 const size_t N = mObjectsSize;
 size_t opos = mNextObjectHint;

 if (N > 0) {
            ALOGV("Parcel %p looking for obj at %zu, hint=%zu",
 this, DPOS, opos);

 if (opos < N) {
 while (opos < (N-1) && OBJS[opos] < DPOS) {
                    opos++;
 }
 } else {
                opos = N-1;
 }
 if (OBJS[opos] == DPOS) {
                ALOGV("Parcel %p found obj %zu at index %zu with forward search",
 this, DPOS, opos);
                mNextObjectHint = opos+1;
                ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
 return obj;
 }

 while (opos > 0 && OBJS[opos] > DPOS) {
                opos--;
 }
 if (OBJS[opos] == DPOS) {
                ALOGV("Parcel %p found obj %zu at index %zu with backward search",
 this, DPOS, opos);
                mNextObjectHint = opos+1;
                ALOGV("readObject Setting data pos of %p to %zu", this, mDataPos);
 return obj;
 }
 }
        ALOGW("Attempt to read object from Parcel %p at offset %zu that is not in the object list",
 this, DPOS);
 }
 return NULL;
}

Project: Android

int Parcel::readParcelFileDescriptor(int& outCommChannel) const {
 int fd;
    outCommChannel = -1;

 if (readInt32() == 0) {
        fd = -1;
 } else {
        fd = readFileDescriptor();
 if (fd >= 0 && readInt32() != 0) {
            outCommChannel = readFileDescriptor();
 }
 }
 return fd;
}

Project: Android

status_t Parcel::readPointer(uintptr_t *pArg) const
{
 status_t ret;
 binder_uintptr_t ptr;
    ret = readAligned(&ptr);
 if (!ret)
 *pArg = ptr;
 return ret;
}

Project: Android

uintptr_t Parcel::readPointer() const
{
 return readAligned<binder_uintptr_t>();
}

Project: Android

String16 Parcel::readString16() const
{
 size_t len;
 const char16_t* str = readString16Inplace(&len);
 if (str) return String16(str, len);
    ALOGE("Reading a NULL string not supported here.");
 return String16();
}

Project: Android

const char16_t* Parcel::readString16Inplace(size_t* outLen) const
{
 int32_t size = readInt32();
 if (size >= 0 && size < INT32_MAX) {
 *outLen = size;
 const char16_t* str = (const char16_t*)readInplace((size+1)*sizeof(char16_t));
 if (str != NULL) {
 return str;
 }
 }
 *outLen = 0;
 return NULL;
}

Project: Android

String8 Parcel::readString8() const
{
 int32_t size = readInt32();
 if (size > 0 && size < INT32_MAX) {
 const char* str = (const char*)readInplace(size+1);
 if (str) return String8(str, size);
 }
 return String8();
}

Project: Android

sp<IBinder> Parcel::readStrongBinder() const
{
    sp<IBinder> val;
    unflatten_binder(ProcessState::self(), *this, &val);
 return val;
}

Project: Android

wp<IBinder> Parcel::readWeakBinder() const
{
    wp<IBinder> val;
    unflatten_binder(ProcessState::self(), *this, &val);
 return val;
}

Project: Android

void Parcel::Blob::release() {
 if (mMapped && mData) {
 ::munmap(mData, mSize);
 }
    clear();
}

Project: Android

void Parcel::remove(size_t /*start*/, size_t /*amt*/)
{
    LOG_ALWAYS_FATAL("Parcel::remove() not yet implemented!");
}

Project: Android

status_t Parcel::restartWrite(size_t desired)
{
 if (mOwner) {
        freeData();
 return continueWrite(desired);
 }

 uint8_t* data = (uint8_t*)realloc(mData, desired);
 if (!data && desired > mDataCapacity) {
        mError = NO_MEMORY;
 return NO_MEMORY;
 }

    releaseObjects();

 if (data) {
        LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
        pthread_mutex_lock(&gParcelGlobalAllocSizeLock);
        gParcelGlobalAllocSize += desired;
        gParcelGlobalAllocSize -= mDataCapacity;
        pthread_mutex_unlock(&gParcelGlobalAllocSizeLock);
        mData = data;
        mDataCapacity = desired;
 }

    mDataSize = mDataPos = 0;
    ALOGV("restartWrite Setting data size of %p to %zu", this, mDataSize);
    ALOGV("restartWrite Setting data pos of %p to %zu", this, mDataPos);

    free(mObjects);
    mObjects = NULL;
    mObjectsSize = mObjectsCapacity = 0;
    mNextObjectHint = 0;
    mHasFds = false;
    mFdsKnown = true;
    mAllowFds = true;

 return NO_ERROR;
}

Project: Android

void Parcel::restoreAllowFds(bool lastValue)
{
    mAllowFds = lastValue;
}

Project: Android

void Parcel::scanForFds() const
{
 bool hasFds = false;
 for (size_t i=0; i<mObjectsSize; i++) {
 const flat_binder_object* flat
 = reinterpret_cast<const flat_binder_object*>(mData + mObjects[i]);
 if (flat->type == BINDER_TYPE_FD) {
            hasFds = true;
 break;
 }
 }
    mHasFds = hasFds;
    mFdsKnown = true;
}

Project: Android

status_t Parcel::setData(const uint8_t* buffer, size_t len)
{
 status_t err = restartWrite(len);
 if (err == NO_ERROR) {
        memcpy(const_cast<uint8_t*>(data()), buffer, len);
        mDataSize = len;
        mFdsKnown = false;
 }
 return err;
}

Project: Android

status_t Parcel::setDataCapacity(size_t size)
{
 if (size > mDataCapacity) return continueWrite(size);
 return NO_ERROR;
}

Project: Android

void Parcel::setDataPosition(size_t pos) const
{
    mDataPos = pos;
    mNextObjectHint = 0;
}

Project: Android

status_t Parcel::setDataSize(size_t size)
{
 status_t err;
    err = continueWrite(size);
 if (err == NO_ERROR) {
        mDataSize = size;
        ALOGV("setDataSize Setting data size of %p to %zu", this, mDataSize);
 }
 return err;
}

Project: Android

void Parcel::setError(status_t err)
{
    mError = err;
}

Project: Android

status_t unflatten_binder(const sp<ProcessState>& proc,
 const Parcel& in, sp<IBinder>* out)
{
 const flat_binder_object* flat = in.readObject(false);

 if (flat) {
 switch (flat->type) {
 case BINDER_TYPE_BINDER:
 *out = reinterpret_cast<IBinder*>(flat->cookie);
 return finish_unflatten_binder(NULL, *flat, in);
 case BINDER_TYPE_HANDLE:
 *out = proc->getStrongProxyForHandle(flat->handle);
 return finish_unflatten_binder(
 static_cast<BpBinder*>(out->get()), *flat, in);
 }
 }
 return BAD_TYPE;
}

Project: Android

status_t unflatten_binder(const sp<ProcessState>& proc,
 const Parcel& in, wp<IBinder>* out)
{
 const flat_binder_object* flat = in.readObject(false);

 if (flat) {
 switch (flat->type) {
 case BINDER_TYPE_BINDER:
 *out = reinterpret_cast<IBinder*>(flat->cookie);
 return finish_unflatten_binder(NULL, *flat, in);
 case BINDER_TYPE_WEAK_BINDER:
 if (flat->binder != 0) {
                    out->set_object_and_refs(
 reinterpret_cast<IBinder*>(flat->cookie),
 reinterpret_cast<RefBase::weakref_type*>(flat->binder));
 } else {
 *out = NULL;
 }
 return finish_unflatten_binder(NULL, *flat, in);
 case BINDER_TYPE_HANDLE:
 case BINDER_TYPE_WEAK_HANDLE:
 *out = proc->getWeakProxyForHandle(flat->handle);
 return finish_unflatten_binder(
 static_cast<BpBinder*>(out->unsafe_get()), *flat, in);
 }
 }
 return BAD_TYPE;
}

Project: Android

status_t Parcel::write(const void* data, size_t len)
{
 void* const d = writeInplace(len);
 if (d) {
        memcpy(d, data, len);
 return NO_ERROR;
 }
 return mError;
}

Project: Android

status_t Parcel::write(const FlattenableHelperInterface& val)
{
 status_t err;

 const size_t len = val.getFlattenedSize();
 const size_t fd_count = val.getFdCount();

    err = this->writeInt32(len);
 if (err) return err;

    err = this->writeInt32(fd_count);
 if (err) return err;

 void* const buf = this->writeInplace(PAD_SIZE(len));
 if (buf == NULL)
 return BAD_VALUE;

 int* fds = NULL;
 if (fd_count) {
        fds = new int[fd_count];
 }

    err = val.flatten(buf, len, fds, fd_count);
 for (size_t i=0 ; i<fd_count && err==NO_ERROR ; i++) {
        err = this->writeDupFileDescriptor( fds[i] );
 }

 if (fd_count) {
 delete [] fds;
 }

 return err;
}

Project: Android

status_t Parcel::writeAligned(T val) {
    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));

 if ((mDataPos+sizeof(val)) <= mDataCapacity) {
restart_write:
 *reinterpret_cast<T*>(mData+mDataPos) = val;
 return finishWrite(sizeof(val));
 }

 status_t err = growData(sizeof(val));
 if (err == NO_ERROR) goto restart_write;
 return err;
}

Project: Android

status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
{
 status_t status;

 if (!mAllowFds || len <= IN_PLACE_BLOB_LIMIT) {
        ALOGV("writeBlob: write in place");
        status = writeInt32(0);
 if (status) return status;

 void* ptr = writeInplace(len);
 if (!ptr) return NO_MEMORY;

        outBlob->init(false /*mapped*/, ptr, len);
 return NO_ERROR;
 }

    ALOGV("writeBlob: write to ashmem");
 int fd = ashmem_create_region("Parcel Blob", len);
 if (fd < 0) return NO_MEMORY;

 int result = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
 if (result < 0) {
        status = result;
 } else {
 void* ptr = ::mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 if (ptr == MAP_FAILED) {
            status = -errno;
 } else {
            result = ashmem_set_prot_region(fd, PROT_READ);
 if (result < 0) {
                status = result;
 } else {
                status = writeInt32(1);
 if (!status) {
                    status = writeFileDescriptor(fd, true /*takeOwnership*/);
 if (!status) {
                        outBlob->init(true /*mapped*/, ptr, len);
 return NO_ERROR;
 }
 }
 }
 }
 ::munmap(ptr, len);
 }
 ::close(fd);
 return status;
}

Project: Android

status_t Parcel::writeByteArray(size_t len, const uint8_t *val) {
 if (!val) {
 return writeAligned(-1);
 }
 status_t ret = writeAligned(len);
 if (ret == NO_ERROR) {
        ret = write(val, len * sizeof(*val));
 }
 return ret;
}

Project: Android

status_t Parcel::writeCString(const char* str)
{
 return write(str, strlen(str)+1);
}

Project: Android

status_t Parcel::writeDouble(double val)
{
 return writeAligned(val);
}

Project: Android

status_t Parcel::writeDupFileDescriptor(int fd)
{
 int dupFd = dup(fd);
 if (dupFd < 0) {
 return -errno;
 }
 status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/);
 if (err) {
        close(dupFd);
 }
 return err;
}

Project: Android

status_t Parcel::writeFloat(float val)
{
 return writeAligned(val);
}

Project: Android

status_t Parcel::writeInt32(int32_t val)
{
 return writeAligned(val);
}

Project: Android

status_t Parcel::writeInt32Array(size_t len, const int32_t *val) {
 if (!val) {
 return writeAligned(-1);
 }
 status_t ret = writeAligned(len);
 if (ret == NO_ERROR) {
        ret = write(val, len * sizeof(*val));
 }
 return ret;
}

Project: Android

status_t Parcel::writeInt64(int64_t val)
{
 return writeAligned(val);
}

Project: Android

status_t Parcel::writeInterfaceToken(const String16& interface)
{
    writeInt32(IPCThreadState::self()->getStrictModePolicy() |
               STRICT_MODE_PENALTY_GATHER);
 return writeString16(interface);
}

Project: Android

status_t Parcel::writeNativeHandle(const native_handle* handle)
{
 if (!handle || handle->version != sizeof(native_handle))
 return BAD_TYPE;

 status_t err;
    err = writeInt32(handle->numFds);
 if (err != NO_ERROR) return err;

    err = writeInt32(handle->numInts);
 if (err != NO_ERROR) return err;

 for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
        err = writeDupFileDescriptor(handle->data[i]);

 if (err != NO_ERROR) {
        ALOGD("write native handle, write dup fd failed");
 return err;
 }
    err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
 return err;
}

Project: Android

status_t Parcel::writeNoException()
{
 return writeInt32(0);
}

Project: Android

status_t Parcel::writeParcelFileDescriptor(int fd, int commChannel) {
 status_t status;

 if (fd < 0) {
        status = writeInt32(0); // ParcelFileDescriptor is null
 if (status) return status;
 } else {
        status = writeInt32(1); // ParcelFileDescriptor is not null
 if (status) return status;
        status = writeDupFileDescriptor(fd);
 if (status) return status;
 if (commChannel < 0) {
            status = writeInt32(0); // commChannel is null
 if (status) return status;
 } else {
            status = writeInt32(1); // commChannel is not null
 if (status) return status;
            status = writeDupFileDescriptor(commChannel);
 }
 }
 return status;
}

Project: Android

status_t Parcel::writePointer(uintptr_t val)
{
 return writeAligned<binder_uintptr_t>(val);
}

Project: Android

status_t Parcel::writeString16(const char16_t* str, size_t len)
{
 if (str == NULL) return writeInt32(-1);

 status_t err = writeInt32(len);
 if (err == NO_ERROR) {
        len *= sizeof(char16_t);
 uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
 if (data) {
            memcpy(data, str, len);
 *reinterpret_cast<char16_t*>(data+len) = 0;
 return NO_ERROR;
 }
        err = mError;
 }
 return err;
}

Project: Android

status_t Parcel::writeString8(const String8& str)
{
 status_t err = writeInt32(str.bytes());
 if (str.bytes() > 0 && err == NO_ERROR) {
        err = write(str.string(), str.bytes()+1);
 }
 return err;
}

Project: Android

status_t Parcel::writeStrongBinder(const sp<IBinder>& val)
{
 return flatten_binder(ProcessState::self(), val, this);
}

Project: Android

status_t Parcel::writeUnpadded(const void* data, size_t len)
{
 size_t end = mDataPos + len;
 if (end < mDataPos) {
 return BAD_VALUE;
 }

 if (end <= mDataCapacity) {
restart_write:
        memcpy(mData+mDataPos, data, len);
 return finishWrite(len);
 }

 status_t err = growData(len);
 if (err == NO_ERROR) goto restart_write;
 return err;
}

Project: Android

Parcel::Blob::~Blob() {
    release();
}

Project: Android

Parcel::~Parcel()
{
    freeDataNoInit();
    LOG_ALLOC("Parcel %p: destroyed", this);
}