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