blob: 3fcc3549b6a807e739c399e952d2ea6aeec2976b [file] [log] [blame]
Martijn Coenen30791002016-12-01 15:40:46 +01001/*
2 * Copyright (C) 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#define LOG_TAG "libhidlmemory"
17
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070018#include <map>
19#include <mutex>
20#include <string>
21
Devin Moore96424c62023-11-13 22:35:19 +000022#include <AshmemMemory.h>
Martijn Coenen30791002016-12-01 15:40:46 +010023#include <hidlmemory/mapping.h>
24
25#include <android-base/logging.h>
26#include <android/hidl/memory/1.0/IMapper.h>
27#include <hidl/HidlSupport.h>
Yifan Hong694bd8d2018-05-11 14:19:42 -070028#include <log/log.h>
Martijn Coenen30791002016-12-01 15:40:46 +010029
30using android::sp;
31using android::hidl::memory::V1_0::IMemory;
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070032using android::hidl::memory::V1_0::IMapper;
Martijn Coenen30791002016-12-01 15:40:46 +010033
34namespace android {
35namespace hardware {
36
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070037static std::map<std::string, sp<IMapper>> gMappersByName;
38static std::mutex gMutex;
Devin Moore96424c62023-11-13 22:35:19 +000039static std::once_flag gOnceFlagLog;
40
41static sp<IMemory> createAshmemMemory(const hidl_memory& mem) {
42 if (mem.handle()->numFds == 0) {
43 return nullptr;
44 }
45
46 // If ashmem service runs in 32-bit (size_t is uint32_t) and a 64-bit
47 // client process requests a memory > 2^32 bytes, the size would be
48 // converted to a 32-bit number in mmap. mmap could succeed but the
49 // mapped memory's actual size would be smaller than the reported size.
50 if (mem.size() > SIZE_MAX) {
51 ALOGE("Cannot map %" PRIu64 " bytes of memory because it is too large.", mem.size());
52 android_errorWriteLog(0x534e4554, "79376389");
53 return nullptr;
54 }
55
56 int fd = mem.handle()->data[0];
57 void* data = mmap(0, mem.size(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
58 if (data == MAP_FAILED) {
59 // mmap never maps at address zero without MAP_FIXED, so we can avoid
60 // exposing clients to MAP_FAILED.
61 return nullptr;
62 }
63
64 return new impl::AshmemMemory(mem, data);
65}
Martijn Coenen30791002016-12-01 15:40:46 +010066
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070067static inline sp<IMapper> getMapperService(const std::string& name) {
68 std::unique_lock<std::mutex> _lock(gMutex);
69 auto iter = gMappersByName.find(name);
70 if (iter != gMappersByName.end()) {
71 return iter->second;
72 }
73
74 sp<IMapper> mapper = IMapper::getService(name, true /* getStub */);
75 if (mapper != nullptr) {
76 gMappersByName[name] = mapper;
77 }
78 return mapper;
79}
80
81sp<IMemory> mapMemory(const hidl_memory& memory) {
82
83 sp<IMapper> mapper = getMapperService(memory.name());
Martijn Coenen30791002016-12-01 15:40:46 +010084
85 if (mapper == nullptr) {
Devin Moore96424c62023-11-13 22:35:19 +000086 if (memory.name() == "ashmem") {
87 std::call_once(gOnceFlagLog,
88 [&]() { LOG(INFO) << "Using libhidlmemory mapper for ashmem."; });
89 return createAshmemMemory(memory);
90 } else {
91 LOG(ERROR) << "Could not fetch mapper for " << memory.name() << " shared memory";
92 return nullptr;
93 }
Martijn Coenen30791002016-12-01 15:40:46 +010094 }
95
96 if (mapper->isRemote()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070097 LOG(ERROR) << "IMapper must be a passthrough service.";
98 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010099 }
100
Yifan Hong694bd8d2018-05-11 14:19:42 -0700101 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
102 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
103 // but the mapped memory's actual size will be smaller than the reported size.
104 if (memory.size() > SIZE_MAX) {
105 LOG(ERROR) << "Cannot map " << memory.size() << " bytes of memory because it is too large.";
106 android_errorWriteLog(0x534e4554, "79376389");
107 return nullptr;
108 }
109
Martijn Coenend272cb92017-01-02 15:20:38 +0100110 Return<sp<IMemory>> ret = mapper->mapMemory(memory);
Martijn Coenen30791002016-12-01 15:40:46 +0100111
112 if (!ret.isOk()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -0700113 LOG(ERROR) << "hidl_memory map returned transport error.";
114 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +0100115 }
116
Martijn Coenend272cb92017-01-02 15:20:38 +0100117 return ret;
Martijn Coenen30791002016-12-01 15:40:46 +0100118}
119
120} // namespace hardware
Martijn Coenend272cb92017-01-02 15:20:38 +0100121} // namespace android