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 
18 #include <map>
19 #include <mutex>
20 #include <string>
21 
22 #include <AshmemMemory.h>
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>
28 #include <log/log.h>
29 
30 using android::sp;
31 using android::hidl::memory::V1_0::IMemory;
32 using android::hidl::memory::V1_0::IMapper;
33 
34 namespace android {
35 namespace hardware {
36 
37 static std::map<std::string, sp<IMapper>> gMappersByName;
38 static std::mutex gMutex;
39 static std::once_flag gOnceFlagLog;
40 
createAshmemMemory(const hidl_memory & mem)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 }
66 
getMapperService(const std::string & name)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 
mapMemory(const hidl_memory & memory)81 sp<IMemory> mapMemory(const hidl_memory& memory) {
82 
83     sp<IMapper> mapper = getMapperService(memory.name());
84 
85     if (mapper == nullptr) {
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         }
94     }
95 
96     if (mapper->isRemote()) {
97         LOG(ERROR) << "IMapper must be a passthrough service.";
98         return nullptr;
99     }
100 
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 
110     Return<sp<IMemory>> ret = mapper->mapMemory(memory);
111 
112     if (!ret.isOk()) {
113         LOG(ERROR) << "hidl_memory map returned transport error.";
114         return nullptr;
115     }
116 
117     return ret;
118 }
119 
120 }  // namespace hardware
121 }  // namespace android
122