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 DFXLOG_ERROR("%s : Failed to get fd.", __func__);
57 return false;
58 }
59
60 size_t fileSize = static_cast<size_t>(GetFileSize(fd));
61 if ((fileSize == 0) || (offset >= static_cast<uint64_t>(fileSize))) {
62 DFXLOG_ERROR("%s : invalid fileSize(%zu)", __func__, fileSize);
63 return false;
64 }
65
66 offset_ = offset & static_cast<uint64_t>ALIGN_BYTES(getpagesize());
67 uint64_t alignedOffset = offset & ALIGN_MASK(getpagesize());
68 if (alignedOffset > static_cast<uint64_t>(fileSize) ||
69 offset > static_cast<uint64_t>(fileSize)) {
70 DFXLOG_ERROR(
71 "%s : invalid alignedOffset(%" PRIu64 ") or offset(%" PRIu64 ") : file size(%zu)",
72 __func__, alignedOffset, offset, fileSize);
73 return false;
74 }
75
76 size_ = fileSize - alignedOffset;
77 uint64_t maxSize;
78 if (!__builtin_add_overflow(size, offset_, &maxSize) && maxSize < size_) {
79 size_ = maxSize;
80 }
81 // (tomys): Some symbol seek & fetch libraries like fake_dlfcn search libs base address
82 // by matching privilege 'r--p' and 'r-xp' in proc maps. So we mapped our target library
83 // by 'rw-p' privilege flags to avoid disturbing these libraries.
84 void* map = mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, alignedOffset);
85 if (map == MAP_FAILED) {
86 DFXLOG_ERROR("%s : Failed to mmap", __func__);
87 return false;
88 }
89
90 data_ = &reinterpret_cast<uint8_t*>(map)[offset_];
91 size_ -= offset_;
92 return true;
93 }
94
Read(uint64_t addr,void * dst,size_t size)95 size_t DfxMemoryFile::Read(uint64_t addr, void* dst, size_t size)
96 {
97 if (addr >= size_) {
98 return 0;
99 }
100 return MemoryCopy(dst, data_, size, addr, size_);
101 }
102 } // namespace HiviewDFX
103 } // namespace OHOS
104