1 /*
2  * Copyright (C) 2018 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 #include <errno.h>
18 #include <sys/mman.h>
19 #include <limits>
20 
21 #include <cutils/ashmem.h>
22 #include <log/log.h>
23 #include <ui/BufferHubMetadata.h>
24 
25 namespace android {
26 
27 namespace {
28 
29 static const int kAshmemProt = PROT_READ | PROT_WRITE;
30 
31 } // namespace
32 
33 using BufferHubDefs::kMetadataHeaderSize;
34 using BufferHubDefs::MetadataHeader;
35 
36 /* static */
create(size_t userMetadataSize)37 BufferHubMetadata BufferHubMetadata::create(size_t userMetadataSize) {
38     // The size the of metadata buffer is used as the "width" parameter during allocation. Thus it
39     // cannot overflow uint32_t.
40     if (userMetadataSize >= (std::numeric_limits<uint32_t>::max() - kMetadataHeaderSize)) {
41         ALOGE("BufferHubMetadata::Create: metadata size too big: %zu.", userMetadataSize);
42         return {};
43     }
44 
45     const size_t metadataSize = userMetadataSize + kMetadataHeaderSize;
46     int fd = ashmem_create_region(/*name=*/"BufferHubMetadata", metadataSize);
47     if (fd < 0) {
48         ALOGE("BufferHubMetadata::Create: failed to create ashmem region.");
49         return {};
50     }
51 
52     // Hand over the ownership of the fd to a unique_fd immediately after the successful
53     // return of ashmem_create_region. The ashmemFd is going to own the fd and to prevent fd
54     // leaks during error handling.
55     unique_fd ashmemFd{fd};
56 
57     if (ashmem_set_prot_region(ashmemFd.get(), kAshmemProt) != 0) {
58         ALOGE("BufferHubMetadata::Create: failed to set protect region.");
59         return {};
60     }
61 
62     return BufferHubMetadata::import(std::move(ashmemFd));
63 }
64 
65 /* static */
import(unique_fd ashmemFd)66 BufferHubMetadata BufferHubMetadata::import(unique_fd ashmemFd) {
67     if (!ashmem_valid(ashmemFd.get())) {
68         ALOGE("BufferHubMetadata::Import: invalid ashmem fd.");
69         return {};
70     }
71 
72     size_t metadataSize = static_cast<size_t>(ashmem_get_size_region(ashmemFd.get()));
73     size_t userMetadataSize = metadataSize - kMetadataHeaderSize;
74 
75     // Note that here the buffer state is mapped from shared memory as an atomic object. The
76     // std::atomic's constructor will not be called so that the original value stored in the memory
77     // region can be preserved.
78     auto metadataHeader = static_cast<MetadataHeader*>(mmap(nullptr, metadataSize, kAshmemProt,
79                                                             MAP_SHARED, ashmemFd.get(),
80                                                             /*offset=*/0));
81     if (metadataHeader == nullptr) {
82         ALOGE("BufferHubMetadata::Import: failed to map region.");
83         return {};
84     }
85 
86     return BufferHubMetadata(userMetadataSize, std::move(ashmemFd), metadataHeader);
87 }
88 
BufferHubMetadata(size_t userMetadataSize,unique_fd ashmemFd,MetadataHeader * metadataHeader)89 BufferHubMetadata::BufferHubMetadata(size_t userMetadataSize, unique_fd ashmemFd,
90                                      MetadataHeader* metadataHeader)
91       : mUserMetadataSize(userMetadataSize),
92         mAshmemFd(std::move(ashmemFd)),
93         mMetadataHeader(metadataHeader) {}
94 
~BufferHubMetadata()95 BufferHubMetadata::~BufferHubMetadata() {
96     if (mMetadataHeader != nullptr) {
97         int ret = munmap(mMetadataHeader, metadataSize());
98         ALOGE_IF(ret != 0,
99                  "BufferHubMetadata::~BufferHubMetadata: failed to unmap ashmem, error=%d.", errno);
100         mMetadataHeader = nullptr;
101     }
102 }
103 
104 } // namespace android
105