1/*
2 * Copyright 2016 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 "SharedMemoryParcelable"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <stdio.h>
23
24#include <sys/mman.h>
25#include <aaudio/AAudio.h>
26
27#include <android-base/unique_fd.h>
28#include <binder/Parcelable.h>
29#include <utility/AAudioUtilities.h>
30
31#include "binding/SharedMemoryParcelable.h"
32
33using android::base::unique_fd;
34using android::NO_ERROR;
35using android::status_t;
36using android::Parcel;
37using android::Parcelable;
38
39using namespace aaudio;
40
41SharedMemoryParcelable::SharedMemoryParcelable() {}
42SharedMemoryParcelable::~SharedMemoryParcelable() {};
43
44void SharedMemoryParcelable::setup(const unique_fd& fd, int32_t sizeInBytes) {
45    mFd.reset(dup(fd.get())); // store a duplicate fd
46    ALOGV("setup(%d -> %d, %d) this = %p\n", fd.get(), mFd.get(), sizeInBytes, this);
47    mSizeInBytes = sizeInBytes;
48}
49
50status_t SharedMemoryParcelable::writeToParcel(Parcel* parcel) const {
51    status_t status = AAudioConvert_aaudioToAndroidStatus(validate());
52    if (status != NO_ERROR) return status;
53
54    status = parcel->writeInt32(mSizeInBytes);
55    if (status != NO_ERROR) return status;
56    if (mSizeInBytes > 0) {
57        ALOGV("writeToParcel() mFd = %d, this = %p\n", mFd.get(), this);
58        status = parcel->writeUniqueFileDescriptor(mFd);
59        ALOGE_IF(status != NO_ERROR, "SharedMemoryParcelable writeDupFileDescriptor failed : %d",
60                 status);
61    }
62    return status;
63}
64
65status_t SharedMemoryParcelable::readFromParcel(const Parcel* parcel) {
66    status_t status = parcel->readInt32(&mSizeInBytes);
67    if (status != NO_ERROR) goto error;
68
69    if (mSizeInBytes > 0) {
70        // The Parcel owns the file descriptor and will close it later.
71        unique_fd mmapFd;
72        status = parcel->readUniqueFileDescriptor(&mmapFd);
73        if (status != NO_ERROR) {
74            ALOGE("readFromParcel() readUniqueFileDescriptor() failed : %d", status);
75            goto error;
76        }
77
78        // Resolve the memory now while we still have the FD from the Parcel.
79        // Closing the FD will not affect the shared memory once mmap() has been called.
80        aaudio_result_t result = resolveSharedMemory(mmapFd);
81        status = AAudioConvert_aaudioToAndroidStatus(result);
82        if (status != NO_ERROR) goto error;
83    }
84
85    return AAudioConvert_aaudioToAndroidStatus(validate());
86
87error:
88    return status;
89}
90
91aaudio_result_t SharedMemoryParcelable::close() {
92    if (mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) {
93        int err = munmap(mResolvedAddress, mSizeInBytes);
94        if (err < 0) {
95            ALOGE("close() munmap() failed %d", err);
96            return AAudioConvert_androidToAAudioResult(err);
97        }
98        mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
99    }
100    return AAUDIO_OK;
101}
102
103aaudio_result_t SharedMemoryParcelable::resolveSharedMemory(const unique_fd& fd) {
104    mResolvedAddress = (uint8_t *) mmap(0, mSizeInBytes, PROT_READ | PROT_WRITE,
105                                        MAP_SHARED, fd.get(), 0);
106    if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) {
107        ALOGE("mmap() failed for fd = %d, errno = %s", fd.get(), strerror(errno));
108        return AAUDIO_ERROR_INTERNAL;
109    }
110    return AAUDIO_OK;
111}
112
113aaudio_result_t SharedMemoryParcelable::resolve(int32_t offsetInBytes, int32_t sizeInBytes,
114                                              void **regionAddressPtr) {
115    if (offsetInBytes < 0) {
116        ALOGE("illegal offsetInBytes = %d", offsetInBytes);
117        return AAUDIO_ERROR_OUT_OF_RANGE;
118    } else if ((offsetInBytes + sizeInBytes) > mSizeInBytes) {
119        ALOGE("out of range, offsetInBytes = %d, "
120                      "sizeInBytes = %d, mSizeInBytes = %d",
121              offsetInBytes, sizeInBytes, mSizeInBytes);
122        return AAUDIO_ERROR_OUT_OF_RANGE;
123    }
124
125    aaudio_result_t result = AAUDIO_OK;
126
127    if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) {
128        if (mFd.get() != -1) {
129            result = resolveSharedMemory(mFd);
130        } else {
131            ALOGE("has no file descriptor for shared memory.");
132            result = AAUDIO_ERROR_INTERNAL;
133        }
134    }
135
136    if (result == AAUDIO_OK && mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) {
137        *regionAddressPtr = mResolvedAddress + offsetInBytes;
138        ALOGV("mResolvedAddress = %p", mResolvedAddress);
139        ALOGV("offset by %d, *regionAddressPtr = %p", offsetInBytes, *regionAddressPtr);
140    }
141    return result;
142}
143
144int32_t SharedMemoryParcelable::getSizeInBytes() {
145    return mSizeInBytes;
146}
147
148aaudio_result_t SharedMemoryParcelable::validate() const {
149    if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
150        ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
151        return AAUDIO_ERROR_OUT_OF_RANGE;
152    }
153    return AAUDIO_OK;
154}
155
156void SharedMemoryParcelable::dump() {
157    ALOGD("mFd = %d", mFd.get());
158    ALOGD("mSizeInBytes = %d", mSizeInBytes);
159    ALOGD("mResolvedAddress = %p", mResolvedAddress);
160}
161