xref: /aosp_15_r20/art/runtime/oat/oat_quick_method_header.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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) +
78         stack_map.GetNativePcOffset(kRuntimeQuickCodeISA);
79   }
80   if (abort_on_failure) {
81     ScopedObjectAccess soa(Thread::Current());
82     LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc << " in "
83                << method->PrettyMethod();
84   }
85   return UINTPTR_MAX;
86 }
87 
ToNativeQuickPcForCatchHandlers(ArtMethod * method,ArrayRef<const uint32_t> dex_pc_list,uint32_t * stack_map_row,bool abort_on_failure) const88 uintptr_t OatQuickMethodHeader::ToNativeQuickPcForCatchHandlers(
89     ArtMethod* method,
90     ArrayRef<const uint32_t> dex_pc_list,
91     /* out */ uint32_t* stack_map_row,
92     bool abort_on_failure) const {
93   const void* entry_point = GetEntryPoint();
94   DCHECK(!method->IsNative());
95   if (IsNterpMethodHeader()) {
96     return NterpGetCatchHandler();
97   }
98   DCHECK(IsOptimized());
99   // Search for the dex-to-pc mapping in stack maps.
100   CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
101 
102   StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc_list);
103   *stack_map_row = stack_map.Row();
104   if (stack_map.IsValid()) {
105     return reinterpret_cast<uintptr_t>(entry_point) +
106            stack_map.GetNativePcOffset(kRuntimeQuickCodeISA);
107   }
108   if (abort_on_failure) {
109     std::stringstream ss;
110     bool first = true;
111     ss << "Failed to find native offset for dex pcs (from outermost to innermost) " << std::hex;
112     for (auto dex_pc : dex_pc_list) {
113       if (!first) {
114         ss << ", ";
115       }
116       first = false;
117       ss << "0x" << dex_pc;
118     }
119     ScopedObjectAccess soa(Thread::Current());
120     ss << " in " << method->PrettyMethod();
121     LOG(FATAL) << ss.str();
122   }
123   return UINTPTR_MAX;
124 }
125 
GetNterpMethodHeader()126 static inline OatQuickMethodHeader* GetNterpMethodHeader() {
127   if (!interpreter::IsNterpSupported()) {
128     return nullptr;
129   }
130   const void* nterp_entrypoint = interpreter::GetNterpEntryPoint();
131   uintptr_t nterp_code_pointer =
132       reinterpret_cast<uintptr_t>(EntryPointToCodePointer(nterp_entrypoint));
133   return reinterpret_cast<OatQuickMethodHeader*>(nterp_code_pointer - sizeof(OatQuickMethodHeader));
134 }
135 
136 OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader = GetNterpMethodHeader();
137 
138 ArrayRef<const uint8_t> OatQuickMethodHeader::NterpWithClinitImpl =
139     interpreter::NterpWithClinitImpl();
140 
141 ArrayRef<const uint8_t> OatQuickMethodHeader::NterpImpl = interpreter::NterpImpl();
142 
IsNterpMethodHeader() const143 bool OatQuickMethodHeader::IsNterpMethodHeader() const {
144   return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
145 }
146 
147 // Find memory range where all libart code is located in memory.
FindLibartCode()148 static ArrayRef<const uint8_t> FindLibartCode() {
149   ArrayRef<const uint8_t> result;
150 #ifndef __APPLE__
151   auto callback = [](dl_phdr_info* info, size_t, void* ctx) {
152     auto res = reinterpret_cast<decltype(result)*>(ctx);
153     for (size_t i = 0; i < info->dlpi_phnum; i++) {
154       if (info->dlpi_phdr[i].p_type == PT_LOAD) {
155         uintptr_t self = reinterpret_cast<uintptr_t>(Runtime::Current);
156         uintptr_t code = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr;
157         uintptr_t size = info->dlpi_phdr[i].p_memsz;
158         if (code <= self && self - code < size) {
159           *res = ArrayRef<const uint8_t>(reinterpret_cast<const uint8_t*>(code), size);
160           return 1;  // Stop iteration and return 1 from dl_iterate_phdr.
161         }
162       }
163     }
164     return 0;  // Continue iteration and return 0 from dl_iterate_phdr when finished.
165   };
166   bool ok = dl_iterate_phdr(callback, &result) != 0;
167   CHECK(ok) << "Can not find libart code in memory";
168 #endif
169   return result;
170 }
171 
172 // Check if the current method header is in libart.
IsStub(const uint8_t * pc)173 std::optional<bool> OatQuickMethodHeader::IsStub(const uint8_t* pc) {
174 #ifndef __APPLE__
175   static ArrayRef<const uint8_t> libart_code = FindLibartCode();
176   return libart_code.begin() <= pc && pc < libart_code.end();
177 #else
178   return std::nullopt;
179 #endif
180 }
181 
182 }  // namespace art
183