• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dfx_memory_file.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include "dfx_define.h"
26 #include "dfx_log.h"
27 #include "dfx_util.h"
28 #include "unique_fd.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
CreateFileMemory(const std::string & path,uint64_t offset)32 std::shared_ptr<DfxMemoryFile> DfxMemoryFile::CreateFileMemory(const std::string& path, uint64_t offset)
33 {
34     auto memory = std::make_shared<DfxMemoryFile>();
35     if (memory->Init(path, offset)) {
36         return memory;
37     }
38     return nullptr;
39 }
40 
Clear()41 void DfxMemoryFile::Clear()
42 {
43     if (data_ != nullptr) {
44         munmap(&data_[-offset_], size_ + offset_);
45         data_ = nullptr;
46     }
47 }
48 
Init(const std::string & file,uint64_t offset,uint64_t size)49 bool DfxMemoryFile::Init(const std::string& file, uint64_t offset, uint64_t size)
50 {
51     Clear(); // Clear out any previous data if it exists.
52 
53     OHOS::UniqueFd ufd = OHOS::UniqueFd(OHOS_TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
54     int fd = ufd.Get();
55     if (fd == -1) {
56         return false;
57     }
58 
59     size_t fileSize = static_cast<size_t>(GetFileSize(fd));
60     if ((fileSize == 0) || (offset >= static_cast<uint64_t>(fileSize))) {
61         return false;
62     }
63 
64     offset_ = offset & static_cast<uint64_t>ALIGN_BYTES(getpagesize());
65     uint64_t alignedOffset = offset & ALIGN_MASK(getpagesize());
66     if (alignedOffset > static_cast<uint64_t>(fileSize) ||
67         offset > static_cast<uint64_t>(fileSize)) {
68         return false;
69     }
70 
71     size_ = fileSize - alignedOffset;
72     uint64_t maxSize;
73     if (!__builtin_add_overflow(size, offset_, &maxSize) && maxSize < size_) {
74         size_ = maxSize;
75     }
76     // (tomys): Some symbol seek & fetch libraries like fake_dlfcn search libs base address
77     // by matching privilege 'r--p' and 'r-xp' in proc maps. So we mapped our target library
78     // by 'rw-p' privilege flags to avoid disturbing these libraries.
79     void* map = mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, alignedOffset);
80     if (map == MAP_FAILED) {
81         return false;
82     }
83 
84     data_ = &reinterpret_cast<uint8_t*>(map)[offset_];
85     size_ -= offset_;
86     return true;
87 }
88 
Read(uint64_t addr,void * dst,size_t size)89 size_t DfxMemoryFile::Read(uint64_t addr, void* dst, size_t size)
90 {
91     if (addr >= size_) {
92         return 0;
93     }
94     return MemoryCopy(dst, data_, size, addr, size_);
95 }
96 } // namespace HiviewDFX
97 } // namespace OHOS
98