Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 1 | /* |
| 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 Coenen | e5f1e5a | 2017-05-30 17:09:50 -0700 | [diff] [blame] | 18 | #include <map> |
| 19 | #include <mutex> |
| 20 | #include <string> |
| 21 | |
Devin Moore | 96424c6 | 2023-11-13 22:35:19 +0000 | [diff] [blame] | 22 | #include <AshmemMemory.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 23 | #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 Hong | 694bd8d | 2018-05-11 14:19:42 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 29 | |
| 30 | using android::sp; |
| 31 | using android::hidl::memory::V1_0::IMemory; |
Martijn Coenen | e5f1e5a | 2017-05-30 17:09:50 -0700 | [diff] [blame] | 32 | using android::hidl::memory::V1_0::IMapper; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | |
Martijn Coenen | e5f1e5a | 2017-05-30 17:09:50 -0700 | [diff] [blame] | 37 | static std::map<std::string, sp<IMapper>> gMappersByName; |
| 38 | static std::mutex gMutex; |
Devin Moore | 96424c6 | 2023-11-13 22:35:19 +0000 | [diff] [blame] | 39 | static std::once_flag gOnceFlagLog; |
| 40 | |
| 41 | static 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 Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 66 | |
Martijn Coenen | e5f1e5a | 2017-05-30 17:09:50 -0700 | [diff] [blame] | 67 | static 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 | |
| 81 | sp<IMemory> mapMemory(const hidl_memory& memory) { |
| 82 | |
| 83 | sp<IMapper> mapper = getMapperService(memory.name()); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 84 | |
| 85 | if (mapper == nullptr) { |
Devin Moore | 96424c6 | 2023-11-13 22:35:19 +0000 | [diff] [blame] | 86 | 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 Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (mapper->isRemote()) { |
Steven Moreland | 10de9ef | 2017-05-17 09:26:24 -0700 | [diff] [blame] | 97 | LOG(ERROR) << "IMapper must be a passthrough service."; |
| 98 | return nullptr; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Yifan Hong | 694bd8d | 2018-05-11 14:19:42 -0700 | [diff] [blame] | 101 | // 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 Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 110 | Return<sp<IMemory>> ret = mapper->mapMemory(memory); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 111 | |
| 112 | if (!ret.isOk()) { |
Steven Moreland | 10de9ef | 2017-05-17 09:26:24 -0700 | [diff] [blame] | 113 | LOG(ERROR) << "hidl_memory map returned transport error."; |
| 114 | return nullptr; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 117 | return ret; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace hardware |
Martijn Coenen | d272cb9 | 2017-01-02 15:20:38 +0100 | [diff] [blame] | 121 | } // namespace android |