blob: 543ff720efd3bf255a1302fd22b57877b7a4400f [file] [log] [blame]
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001/*
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
Vladimir Markob69312d2020-04-22 14:30:33 +010019#include "arch/instruction_set.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010020#include "art_method.h"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/dex_file_types.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000022#include "interpreter/interpreter_mterp_impl.h"
Nicolas Geoffray0315efa2020-06-26 11:42:39 +010023#include "interpreter/mterp/nterp.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000024#include "nterp_helpers.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010026#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010027#include "thread.h"
28
29namespace art {
30
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000031uint32_t OatQuickMethodHeader::ToDexPc(ArtMethod** frame,
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010032 const uintptr_t pc,
33 bool abort_on_failure) const {
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000034 ArtMethod* method = *frame;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035 const void* entry_point = GetEntryPoint();
36 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
David Srbeckyafc97bc2018-07-05 08:14:35 +000037 if (method->IsNative()) {
38 return dex::kDexNoIndex;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000039 } else if (IsNterpMethodHeader()) {
40 return NterpGetDexPC(frame);
David Srbeckyafc97bc2018-07-05 08:14:35 +000041 } else {
42 DCHECK(IsOptimized());
David Srbecky0d4567f2019-05-30 22:45:40 +010043 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
David Srbecky052f8ca2018-04-26 15:42:54 +010044 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010045 if (stack_map.IsValid()) {
David Srbecky052f8ca2018-04-26 15:42:54 +010046 return stack_map.GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010047 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010048 }
49 if (abort_on_failure) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010050 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()
David Sehr709b0702016-10-13 09:12:37 -070054 << ") in " << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010055 }
Andreas Gampee2abbc62017-09-15 11:59:26 -070056 return dex::kDexNoIndex;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010057}
58
59uintptr_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();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010064 DCHECK(!method->IsNative());
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000065 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 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010070 DCHECK(IsOptimized());
71 // Search for the dex-to-pc mapping in stack maps.
David Srbecky0d4567f2019-05-30 22:45:40 +010072 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010073
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010074 // 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 =
David Srbecky052f8ca2018-04-26 15:42:54 +010078 LIKELY(is_for_catch_handler) ? code_info.GetCatchStackMapForDexPc(dex_pc)
79 : code_info.GetStackMapForDexPc(dex_pc);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010080 if (stack_map.IsValid()) {
81 return reinterpret_cast<uintptr_t>(entry_point) +
David Srbecky052f8ca2018-04-26 15:42:54 +010082 stack_map.GetNativePcOffset(kRuntimeISA);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010083 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010084 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
David Sehr709b0702016-10-13 09:12:37 -070087 << " in " << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010088 }
89 return UINTPTR_MAX;
90}
91
Vladimir Markob69312d2020-04-22 14:30:33 +010092static 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
104OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader = GetNterpMethodHeader();
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000105
106bool OatQuickMethodHeader::IsNterpMethodHeader() const {
107 return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
108}
109
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100110} // namespace art