1 /*
2 * Copyright (C) 2011 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 "oat_quick_method_header.h"
18
19 #include <optional>
20
21 #ifndef __APPLE__
22 #include <link.h> // for dl_iterate_phdr.
23 #endif
24
25 #include "arch/instruction_set.h"
26 #include "art_method.h"
27 #include "dex/dex_file_types.h"
28 #include "interpreter/mterp/nterp.h"
29 #include "nterp_helpers.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "stack_map.h"
32 #include "thread.h"
33
34 namespace art HIDDEN {
35
ToDexPc(ArtMethod ** frame,const uintptr_t pc,bool abort_on_failure) const36 uint32_t OatQuickMethodHeader::ToDexPc(ArtMethod** frame,
37 const uintptr_t pc,
38 bool abort_on_failure) const {
39 ArtMethod* method = *frame;
40 const void* entry_point = GetEntryPoint();
41 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
42 if (method->IsNative()) {
43 return dex::kDexNoIndex;
44 } else if (IsNterpMethodHeader()) {
45 return NterpGetDexPC(frame);
46 } else {
47 DCHECK(IsOptimized());
48 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
49 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset);
50 if (stack_map.IsValid()) {
51 return stack_map.GetDexPc();
52 }
53 }
54 if (abort_on_failure) {
55 LOG(FATAL) << "Failed to find Dex offset for PC offset "
56 << reinterpret_cast<void*>(sought_offset)
57 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
58 << " current entry_point=" << method->GetEntryPointFromQuickCompiledCode()
59 << ") in " << method->PrettyMethod();
60 }
61 return dex::kDexNoIndex;
62 }
63
ToNativeQuickPc(ArtMethod * method,const uint32_t dex_pc,bool abort_on_failure) const64 uintptr_t OatQuickMethodHeader::ToNativeQuickPc(ArtMethod* method,
65 const uint32_t dex_pc,
66 bool abort_on_failure) const {
67 const void* entry_point = GetEntryPoint();
68 DCHECK(!method->IsNative());
69 // For catch handlers use the ArrayRef<const uint32_t> version of ToNativeQuickPc.
70 DCHECK(!IsNterpMethodHeader());
71 DCHECK(IsOptimized());
72 // Search for the dex-to-pc mapping in stack maps.
73 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
74
75 StackMap stack_map = code_info.GetStackMapForDexPc(dex_pc);
76 if (stack_map.IsValid()) {
77 return reinterpret_cast<uintptr_t>(entry_point) + stack_map.GetNativePcOffset(kRuntimeISA);
78 }
79 if (abort_on_failure) {
80 ScopedObjectAccess soa(Thread::Current());
81 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc << " in "
82 << method->PrettyMethod();
83 }
84 return UINTPTR_MAX;
85 }
86
ToNativeQuickPcForCatchHandlers(ArtMethod * method,ArrayRef<const uint32_t> dex_pc_list,uint32_t * stack_map_row,bool abort_on_failure) const87 uintptr_t OatQuickMethodHeader::ToNativeQuickPcForCatchHandlers(
88 ArtMethod* method,
89 ArrayRef<const uint32_t> dex_pc_list,
90 /* out */ uint32_t* stack_map_row,
91 bool abort_on_failure) const {
92 const void* entry_point = GetEntryPoint();
93 DCHECK(!method->IsNative());
94 if (IsNterpMethodHeader()) {
95 return NterpGetCatchHandler();
96 }
97 DCHECK(IsOptimized());
98 // Search for the dex-to-pc mapping in stack maps.
99 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
100
101 StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc_list);
102 *stack_map_row = stack_map.Row();
103 if (stack_map.IsValid()) {
104 return reinterpret_cast<uintptr_t>(entry_point) +
105 stack_map.GetNativePcOffset(kRuntimeISA);
106 }
107 if (abort_on_failure) {
108 std::stringstream ss;
109 bool first = true;
110 ss << "Failed to find native offset for dex pcs (from outermost to innermost) " << std::hex;
111 for (auto dex_pc : dex_pc_list) {
112 if (!first) {
113 ss << ", ";
114 }
115 first = false;
116 ss << "0x" << dex_pc;
117 }
118 ScopedObjectAccess soa(Thread::Current());
119 ss << " in " << method->PrettyMethod();
120 LOG(FATAL) << ss.str();
121 }
122 return UINTPTR_MAX;
123 }
124
GetNterpMethodHeader()125 static inline OatQuickMethodHeader* GetNterpMethodHeader() {
126 if (!interpreter::IsNterpSupported()) {
127 return nullptr;
128 }
129 const void* nterp_entrypoint = interpreter::GetNterpEntryPoint();
130 uintptr_t nterp_code_pointer =
131 reinterpret_cast<uintptr_t>(EntryPointToCodePointer(nterp_entrypoint));
132 return reinterpret_cast<OatQuickMethodHeader*>(nterp_code_pointer - sizeof(OatQuickMethodHeader));
133 }
134
135 OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader = GetNterpMethodHeader();
136
137 ArrayRef<const uint8_t> OatQuickMethodHeader::NterpWithClinitImpl =
138 interpreter::NterpWithClinitImpl();
139
140 ArrayRef<const uint8_t> OatQuickMethodHeader::NterpImpl = interpreter::NterpImpl();
141
IsNterpMethodHeader() const142 bool OatQuickMethodHeader::IsNterpMethodHeader() const {
143 return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
144 }
145
146 // Find memory range where all libart code is located in memory.
FindLibartCode()147 static ArrayRef<const uint8_t> FindLibartCode() {
148 ArrayRef<const uint8_t> result;
149 #ifndef __APPLE__
150 auto callback = [](dl_phdr_info* info, size_t, void* ctx) {
151 auto res = reinterpret_cast<decltype(result)*>(ctx);
152 for (size_t i = 0; i < info->dlpi_phnum; i++) {
153 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
154 uintptr_t self = reinterpret_cast<uintptr_t>(Runtime::Current);
155 uintptr_t code = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr;
156 uintptr_t size = info->dlpi_phdr[i].p_memsz;
157 if (code <= self && self - code < size) {
158 *res = ArrayRef<const uint8_t>(reinterpret_cast<const uint8_t*>(code), size);
159 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
160 }
161 }
162 }
163 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
164 };
165 bool ok = dl_iterate_phdr(callback, &result) != 0;
166 CHECK(ok) << "Can not find libart code in memory";
167 #endif
168 return result;
169 }
170
171 // Check if the current method header is in libart.
IsStub(const uint8_t * pc)172 std::optional<bool> OatQuickMethodHeader::IsStub(const uint8_t* pc) {
173 #ifndef __APPLE__
174 static ArrayRef<const uint8_t> libart_code = FindLibartCode();
175 return libart_code.begin() <= pc && pc < libart_code.end();
176 #else
177 return std::nullopt;
178 #endif
179 }
180
181 } // namespace art
182