Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 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 | |
Vladimir Marko | b69312d | 2020-04-22 14:30:33 +0100 | [diff] [blame] | 19 | #include "arch/instruction_set.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 20 | #include "art_method.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 21 | #include "dex/dex_file_types.h" |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 22 | #include "interpreter/interpreter_mterp_impl.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 23 | #include "interpreter/mterp/nterp.h" |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 24 | #include "nterp_helpers.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 26 | #include "stack_map.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 27 | #include "thread.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 31 | uint32_t OatQuickMethodHeader::ToDexPc(ArtMethod** frame, |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 32 | const uintptr_t pc, |
| 33 | bool abort_on_failure) const { |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 34 | ArtMethod* method = *frame; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 35 | const void* entry_point = GetEntryPoint(); |
| 36 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point); |
David Srbecky | afc97bc | 2018-07-05 08:14:35 +0000 | [diff] [blame] | 37 | if (method->IsNative()) { |
| 38 | return dex::kDexNoIndex; |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 39 | } else if (IsNterpMethodHeader()) { |
| 40 | return NterpGetDexPC(frame); |
David Srbecky | afc97bc | 2018-07-05 08:14:35 +0000 | [diff] [blame] | 41 | } else { |
| 42 | DCHECK(IsOptimized()); |
David Srbecky | 0d4567f | 2019-05-30 22:45:40 +0100 | [diff] [blame] | 43 | CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 44 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 45 | if (stack_map.IsValid()) { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 46 | return stack_map.GetDexPc(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 47 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 48 | } |
| 49 | if (abort_on_failure) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 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() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 54 | << ") in " << method->PrettyMethod(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 55 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 56 | return dex::kDexNoIndex; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | 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(); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 64 | DCHECK(!method->IsNative()); |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 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 | } |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 70 | DCHECK(IsOptimized()); |
| 71 | // Search for the dex-to-pc mapping in stack maps. |
David Srbecky | 0d4567f | 2019-05-30 22:45:40 +0100 | [diff] [blame] | 72 | CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 73 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 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 = |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 78 | LIKELY(is_for_catch_handler) ? code_info.GetCatchStackMapForDexPc(dex_pc) |
| 79 | : code_info.GetStackMapForDexPc(dex_pc); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 80 | if (stack_map.IsValid()) { |
| 81 | return reinterpret_cast<uintptr_t>(entry_point) + |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 82 | stack_map.GetNativePcOffset(kRuntimeISA); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 83 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 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 |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 87 | << " in " << method->PrettyMethod(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 88 | } |
| 89 | return UINTPTR_MAX; |
| 90 | } |
| 91 | |
Vladimir Marko | b69312d | 2020-04-22 14:30:33 +0100 | [diff] [blame] | 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(); |
Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 105 | |
| 106 | bool OatQuickMethodHeader::IsNterpMethodHeader() const { |
| 107 | return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false; |
| 108 | } |
| 109 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 110 | } // namespace art |