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 "art_method.h"
20 #include "dex/dex_file_types.h"
21 #include "interpreter/interpreter_mterp_impl.h"
22 #include "interpreter/mterp/mterp.h"
23 #include "nterp_helpers.h"
24 #include "scoped_thread_state_change-inl.h"
25 #include "stack_map.h"
26 #include "thread.h"
27
28 namespace art {
29
ToDexPc(ArtMethod ** frame,const uintptr_t pc,bool abort_on_failure) const30 uint32_t OatQuickMethodHeader::ToDexPc(ArtMethod** frame,
31 const uintptr_t pc,
32 bool abort_on_failure) const {
33 ArtMethod* method = *frame;
34 const void* entry_point = GetEntryPoint();
35 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
36 if (method->IsNative()) {
37 return dex::kDexNoIndex;
38 } else if (IsNterpMethodHeader()) {
39 return NterpGetDexPC(frame);
40 } else {
41 DCHECK(IsOptimized());
42 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
43 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset);
44 if (stack_map.IsValid()) {
45 return stack_map.GetDexPc();
46 }
47 }
48 if (abort_on_failure) {
49 LOG(FATAL) << "Failed to find Dex offset for PC offset "
50 << reinterpret_cast<void*>(sought_offset)
51 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
52 << " current entry_point=" << method->GetEntryPointFromQuickCompiledCode()
53 << ") in " << method->PrettyMethod();
54 }
55 return dex::kDexNoIndex;
56 }
57
ToNativeQuickPc(ArtMethod * method,const uint32_t dex_pc,bool is_for_catch_handler,bool abort_on_failure) const58 uintptr_t OatQuickMethodHeader::ToNativeQuickPc(ArtMethod* method,
59 const uint32_t dex_pc,
60 bool is_for_catch_handler,
61 bool abort_on_failure) const {
62 const void* entry_point = GetEntryPoint();
63 DCHECK(!method->IsNative());
64 if (IsNterpMethodHeader()) {
65 // This should only be called on an nterp frame for getting a catch handler.
66 CHECK(is_for_catch_handler);
67 return NterpGetCatchHandler();
68 }
69 DCHECK(IsOptimized());
70 // Search for the dex-to-pc mapping in stack maps.
71 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
72
73 // All stack maps are stored in the same CodeItem section, safepoint stack
74 // maps first, then catch stack maps. We use `is_for_catch_handler` to select
75 // the order of iteration.
76 StackMap stack_map =
77 LIKELY(is_for_catch_handler) ? code_info.GetCatchStackMapForDexPc(dex_pc)
78 : code_info.GetStackMapForDexPc(dex_pc);
79 if (stack_map.IsValid()) {
80 return reinterpret_cast<uintptr_t>(entry_point) +
81 stack_map.GetNativePcOffset(kRuntimeISA);
82 }
83 if (abort_on_failure) {
84 ScopedObjectAccess soa(Thread::Current());
85 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
86 << " in " << method->PrettyMethod();
87 }
88 return UINTPTR_MAX;
89 }
90
91 OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader =
92 (interpreter::IsNterpSupported()
93 ? reinterpret_cast<OatQuickMethodHeader*>(
94 reinterpret_cast<uintptr_t>(interpreter::GetNterpEntryPoint()) -
95 sizeof(OatQuickMethodHeader))
96 : nullptr);
97
IsNterpMethodHeader() const98 bool OatQuickMethodHeader::IsNterpMethodHeader() const {
99 return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
100 }
101
102 } // namespace art
103