Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include <stdint.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <memory> |
| 24 | |
Martin Stjernholm | 9062bb4 | 2019-10-04 01:28:43 +0100 | [diff] [blame] | 25 | #define LOG_TAG "unwind" |
| 26 | #include <log/log.h> |
| 27 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> |
Martin Stjernholm | b49289b | 2018-12-14 15:26:32 +0000 | [diff] [blame] | 29 | #include <art_api/dex_file_support.h> |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 30 | |
| 31 | #include <unwindstack/MapInfo.h> |
| 32 | #include <unwindstack/Memory.h> |
| 33 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 34 | #include "DexFile.h" |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 35 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 36 | namespace unwindstack { |
| 37 | |
Martin Stjernholm | 9062bb4 | 2019-10-04 01:28:43 +0100 | [diff] [blame] | 38 | static bool CheckDexSupport() { |
| 39 | if (std::string err_msg; !art_api::dex::TryLoadLibdexfileExternal(&err_msg)) { |
| 40 | ALOGW("Failed to initialize DEX file support: %s", err_msg.c_str()); |
| 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | static bool HasDexSupport() { |
| 47 | static bool has_dex_support = CheckDexSupport(); |
| 48 | return has_dex_support; |
| 49 | } |
| 50 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 51 | std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory, |
| 52 | MapInfo* info) { |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame] | 53 | if (UNLIKELY(!HasDexSupport())) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | size_t max_size = info->end - dex_file_offset_in_memory; |
| 58 | if (memory->IsLocal()) { |
| 59 | size_t size = max_size; |
| 60 | |
| 61 | std::string err_msg; |
| 62 | std::unique_ptr<art_api::dex::DexFile> art_dex_file = DexFile::OpenFromMemory( |
| 63 | reinterpret_cast<void const*>(dex_file_offset_in_memory), &size, info->name, &err_msg); |
| 64 | if (art_dex_file != nullptr && size <= max_size) { |
| 65 | return std::unique_ptr<DexFile>(new DexFile(art_dex_file)); |
| 66 | } |
| 67 | } |
| 68 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 69 | if (!info->name.empty()) { |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 70 | std::unique_ptr<DexFile> dex_file = |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 71 | DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name); |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 72 | if (dex_file) { |
| 73 | return dex_file; |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame] | 76 | return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name, max_size); |
Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 77 | } |
| 78 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 79 | bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name, |
| 80 | uint64_t* method_offset) { |
Martin Stjernholm | b49289b | 2018-12-14 15:26:32 +0000 | [diff] [blame] | 81 | art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 82 | if (method_info.offset == 0) { |
Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 83 | return false; |
| 84 | } |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 85 | *method_name = method_info.name; |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 86 | *method_offset = dex_offset - method_info.offset; |
| 87 | return true; |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 90 | std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file, |
| 91 | const std::string& file) { |
Martin Stjernholm | 9062bb4 | 2019-10-04 01:28:43 +0100 | [diff] [blame] | 92 | if (UNLIKELY(!HasDexSupport())) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 96 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC))); |
| 97 | if (fd == -1) { |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 98 | return nullptr; |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 101 | std::string error_msg; |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 102 | std::unique_ptr<art_api::dex::DexFile> art_dex_file = |
| 103 | OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg); |
| 104 | if (art_dex_file == nullptr) { |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 108 | return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(art_dex_file)); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 111 | std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory, |
| 112 | Memory* memory, |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame] | 113 | const std::string& name, |
| 114 | size_t max_size) { |
Martin Stjernholm | 9062bb4 | 2019-10-04 01:28:43 +0100 | [diff] [blame] | 115 | if (UNLIKELY(!HasDexSupport())) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 119 | std::vector<uint8_t> backing_memory; |
Martin Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 120 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 121 | for (size_t size = 0;;) { |
| 122 | std::string error_msg; |
| 123 | std::unique_ptr<art_api::dex::DexFile> art_dex_file = |
| 124 | OpenFromMemory(backing_memory.data(), &size, name, &error_msg); |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame] | 125 | if (size > max_size) { |
| 126 | return nullptr; |
| 127 | } |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 128 | |
| 129 | if (art_dex_file != nullptr) { |
| 130 | return std::unique_ptr<DexFileFromMemory>( |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 131 | new DexFileFromMemory(art_dex_file, std::move(backing_memory))); |
Martin Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 132 | } |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 133 | |
| 134 | if (!error_msg.empty()) { |
| 135 | return nullptr; |
Martin Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 136 | } |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 137 | |
| 138 | backing_memory.resize(size); |
| 139 | if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(), |
| 140 | backing_memory.size())) { |
| 141 | return nullptr; |
| 142 | } |
Martin Stjernholm | cacf5bf | 2018-12-19 00:09:41 +0000 | [diff] [blame] | 143 | } |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 144 | } |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 145 | |
| 146 | } // namespace unwindstack |