• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mterp/nterp.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 abort_on_failure) const58 uintptr_t OatQuickMethodHeader::ToNativeQuickPc(ArtMethod* method,
59                                                 const uint32_t dex_pc,
60                                                 bool abort_on_failure) const {
61   const void* entry_point = GetEntryPoint();
62   DCHECK(!method->IsNative());
63   // For catch handlers use the ArrayRef<const uint32_t> version of ToNativeQuickPc.
64   DCHECK(!IsNterpMethodHeader());
65   DCHECK(IsOptimized());
66   // Search for the dex-to-pc mapping in stack maps.
67   CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
68 
69   StackMap stack_map = code_info.GetStackMapForDexPc(dex_pc);
70   if (stack_map.IsValid()) {
71     return reinterpret_cast<uintptr_t>(entry_point) + stack_map.GetNativePcOffset(kRuntimeISA);
72   }
73   if (abort_on_failure) {
74     ScopedObjectAccess soa(Thread::Current());
75     LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc << " in "
76                << method->PrettyMethod();
77   }
78   return UINTPTR_MAX;
79 }
80 
ToNativeQuickPcForCatchHandlers(ArtMethod * method,ArrayRef<const uint32_t> dex_pc_list,uint32_t * stack_map_row,bool abort_on_failure) const81 uintptr_t OatQuickMethodHeader::ToNativeQuickPcForCatchHandlers(
82     ArtMethod* method,
83     ArrayRef<const uint32_t> dex_pc_list,
84     /* out */ uint32_t* stack_map_row,
85     bool abort_on_failure) const {
86   const void* entry_point = GetEntryPoint();
87   DCHECK(!method->IsNative());
88   if (IsNterpMethodHeader()) {
89     return NterpGetCatchHandler();
90   }
91   DCHECK(IsOptimized());
92   // Search for the dex-to-pc mapping in stack maps.
93   CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
94 
95   StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc_list);
96   *stack_map_row = stack_map.Row();
97   if (stack_map.IsValid()) {
98     return reinterpret_cast<uintptr_t>(entry_point) +
99            stack_map.GetNativePcOffset(kRuntimeISA);
100   }
101   if (abort_on_failure) {
102     std::stringstream ss;
103     bool first = true;
104     ss << "Failed to find native offset for dex pcs (from outermost to innermost) " << std::hex;
105     for (auto dex_pc : dex_pc_list) {
106       if (!first) {
107         ss << ", ";
108       }
109       first = false;
110       ss << "0x" << dex_pc;
111     }
112     ScopedObjectAccess soa(Thread::Current());
113     ss << " in " << method->PrettyMethod();
114     LOG(FATAL) << ss.str();
115   }
116   return UINTPTR_MAX;
117 }
118 
GetNterpMethodHeader()119 static inline OatQuickMethodHeader* GetNterpMethodHeader() {
120   if (!interpreter::IsNterpSupported()) {
121     return nullptr;
122   }
123   const void* nterp_entrypoint = interpreter::GetNterpEntryPoint();
124   uintptr_t nterp_code_pointer =
125       reinterpret_cast<uintptr_t>(EntryPointToCodePointer(nterp_entrypoint));
126   return reinterpret_cast<OatQuickMethodHeader*>(nterp_code_pointer - sizeof(OatQuickMethodHeader));
127 }
128 
129 OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader = GetNterpMethodHeader();
130 
IsNterpMethodHeader() const131 bool OatQuickMethodHeader::IsNterpMethodHeader() const {
132   return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
133 }
134 
135 }  // namespace art
136