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