1 /*
2  * Copyright (C) 2017 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 "Memory"
18 
19 #include "Memory.h"
20 
21 #include "HalInterfaces.h"
22 #include "Utils.h"
23 
24 namespace android {
25 namespace nn {
26 
create(uint32_t size)27 int Memory::create(uint32_t size) {
28     mHidlMemory = allocateSharedMemory(size);
29     mMemory = mapMemory(mHidlMemory);
30     if (mMemory == nullptr) {
31         LOG(ERROR) << "Memory::create failed";
32         return ANEURALNETWORKS_OUT_OF_MEMORY;
33     }
34     return ANEURALNETWORKS_NO_ERROR;
35 }
36 
validateSize(uint32_t offset,uint32_t length) const37 bool Memory::validateSize(uint32_t offset, uint32_t length) const {
38     if (offset + length > mHidlMemory.size()) {
39         LOG(ERROR) << "Request size larger than the memory size.";
40         return false;
41     } else {
42         return true;
43     }
44 }
45 
~MemoryFd()46 MemoryFd::~MemoryFd() {
47     // Unmap the memory.
48     if (mMapping) {
49         munmap(mMapping, mHidlMemory.size());
50     }
51     // Delete the native_handle.
52     if (mHandle) {
53         int fd = mHandle->data[0];
54         if (fd != -1) {
55             close(fd);
56         }
57         native_handle_delete(mHandle);
58     }
59 }
60 
set(size_t size,int prot,int fd,size_t offset)61 int MemoryFd::set(size_t size, int prot, int fd, size_t offset) {
62     if (fd < 0) {
63         LOG(ERROR) << "ANeuralNetworksMemory_createFromFd invalid fd " << fd;
64         return ANEURALNETWORKS_UNEXPECTED_NULL;
65     }
66     if (size == 0 || fd < 0) {
67         LOG(ERROR) << "Invalid size or fd";
68         return ANEURALNETWORKS_BAD_DATA;
69     }
70     int dupfd = dup(fd);
71     if (dupfd == -1) {
72         LOG(ERROR) << "Failed to dup the fd";
73         return ANEURALNETWORKS_UNEXPECTED_NULL;
74     }
75 
76     if (mMapping) {
77         if (munmap(mMapping, mHidlMemory.size()) != 0) {
78             LOG(ERROR) << "Failed to remove the existing mapping";
79             // This is not actually fatal.
80         }
81         mMapping = nullptr;
82     }
83     if (mHandle) {
84         native_handle_delete(mHandle);
85     }
86     mHandle = native_handle_create(1, 3);
87     if (mHandle == nullptr) {
88         LOG(ERROR) << "Failed to create native_handle";
89         return ANEURALNETWORKS_UNEXPECTED_NULL;
90     }
91     mHandle->data[0] = dupfd;
92     mHandle->data[1] = prot;
93     mHandle->data[2] = (int32_t)(uint32_t)(offset & 0xffffffff);
94 #if defined(__LP64__)
95     mHandle->data[3] = (int32_t)(uint32_t)(offset >> 32);
96 #else
97     mHandle->data[3] = 0;
98 #endif
99     mHidlMemory = hidl_memory("mmap_fd", mHandle, size);
100     return ANEURALNETWORKS_NO_ERROR;
101 }
102 
getPointer(uint8_t ** buffer) const103 int MemoryFd::getPointer(uint8_t** buffer) const {
104     if (mMapping) {
105         *buffer = mMapping;
106         return ANEURALNETWORKS_NO_ERROR;
107     }
108 
109     if (mHandle == nullptr) {
110         LOG(ERROR) << "Memory not initialized";
111         return ANEURALNETWORKS_UNEXPECTED_NULL;
112     }
113 
114     int fd = mHandle->data[0];
115     int prot = mHandle->data[1];
116     size_t offset = getSizeFromInts(mHandle->data[2], mHandle->data[3]);
117     void* data = mmap(nullptr, mHidlMemory.size(), prot, MAP_SHARED, fd, offset);
118     if (data == MAP_FAILED) {
119         LOG(ERROR) << "MemoryFd::getPointer(): Can't mmap the file descriptor.";
120         return ANEURALNETWORKS_UNMAPPABLE;
121     } else {
122         mMapping = *buffer = static_cast<uint8_t*>(data);
123         return ANEURALNETWORKS_NO_ERROR;
124     }
125 }
126 
add(const Memory * memory)127 uint32_t MemoryTracker::add(const Memory* memory) {
128     VLOG(MODEL) << __func__ << "(" << SHOW_IF_DEBUG(memory) << ")";
129     // See if we already have this memory. If so,
130     // return its index.
131     auto i = mKnown.find(memory);
132     if (i != mKnown.end()) {
133         return i->second;
134     }
135     VLOG(MODEL) << "It's new";
136     // It's a new one.  Save it an assign an index to it.
137     size_t next = mKnown.size();
138     if (next > 0xFFFFFFFF) {
139         LOG(ERROR) << "ANeuralNetworks more than 2^32 memories.";
140         return ANEURALNETWORKS_BAD_DATA;
141     }
142     uint32_t idx = static_cast<uint32_t>(next);
143     mKnown[memory] = idx;
144     mMemories.push_back(memory);
145     return idx;
146 }
147 
148 } // namespace nn
149 } // namespace android
150