• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 using android::base::unique_fd;
34 using android::NO_ERROR;
35 using android::status_t;
36 using android::Parcel;
37 using android::Parcelable;
38 
39 using namespace aaudio;
40 
SharedMemoryParcelable()41 SharedMemoryParcelable::SharedMemoryParcelable() {}
~SharedMemoryParcelable()42 SharedMemoryParcelable::~SharedMemoryParcelable() {};
43 
setup(const unique_fd & fd,int32_t sizeInBytes)44 void SharedMemoryParcelable::setup(const unique_fd& fd, int32_t sizeInBytes) {
45     mFd.reset(dup(fd.get())); // store a duplicate fd
46     ALOGV("setup(fd = %d -> %d, size = %d) this = %p\n", fd.get(), mFd.get(), sizeInBytes, this);
47     mSizeInBytes = sizeInBytes;
48 }
49 
writeToParcel(Parcel * parcel) const50 status_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 
readFromParcel(const Parcel * parcel)65 status_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 
87 error:
88     return status;
89 }
90 
close()91 aaudio_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 
resolveSharedMemory(const unique_fd & fd)103 aaudio_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, nBytes = %d, errno = %s",
108               fd.get(), mSizeInBytes, strerror(errno));
109         return AAUDIO_ERROR_INTERNAL;
110     }
111     return AAUDIO_OK;
112 }
113 
resolve(int32_t offsetInBytes,int32_t sizeInBytes,void ** regionAddressPtr)114 aaudio_result_t SharedMemoryParcelable::resolve(int32_t offsetInBytes, int32_t sizeInBytes,
115                                               void **regionAddressPtr) {
116     if (offsetInBytes < 0) {
117         ALOGE("illegal offsetInBytes = %d", offsetInBytes);
118         return AAUDIO_ERROR_OUT_OF_RANGE;
119     } else if ((offsetInBytes + sizeInBytes) > mSizeInBytes) {
120         ALOGE("out of range, offsetInBytes = %d, "
121                       "sizeInBytes = %d, mSizeInBytes = %d",
122               offsetInBytes, sizeInBytes, mSizeInBytes);
123         return AAUDIO_ERROR_OUT_OF_RANGE;
124     }
125 
126     aaudio_result_t result = AAUDIO_OK;
127 
128     if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) {
129         if (mFd.get() != -1) {
130             result = resolveSharedMemory(mFd);
131         } else {
132             ALOGE("has no file descriptor for shared memory.");
133             result = AAUDIO_ERROR_INTERNAL;
134         }
135     }
136 
137     if (result == AAUDIO_OK && mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) {
138         *regionAddressPtr = mResolvedAddress + offsetInBytes;
139         ALOGV("mResolvedAddress = %p", mResolvedAddress);
140         ALOGV("offset by %d, *regionAddressPtr = %p", offsetInBytes, *regionAddressPtr);
141     }
142     return result;
143 }
144 
getSizeInBytes()145 int32_t SharedMemoryParcelable::getSizeInBytes() {
146     return mSizeInBytes;
147 }
148 
validate() const149 aaudio_result_t SharedMemoryParcelable::validate() const {
150     if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
151         ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
152         return AAUDIO_ERROR_OUT_OF_RANGE;
153     }
154     return AAUDIO_OK;
155 }
156 
dump()157 void SharedMemoryParcelable::dump() {
158     ALOGD("mFd = %d", mFd.get());
159     ALOGD("mSizeInBytes = %d", mSizeInBytes);
160 }
161