blob: a725724afbf601561d01fbb8e8d7188fc6711d69 [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#ifndef ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
18#define ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
19
20#include "arch/instruction_set.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000021#include "base/locks.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010022#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "quick/quick_method_frame_info.h"
David Srbecky79aa6242018-07-12 13:28:42 +000025#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026
27namespace art {
28
29class ArtMethod;
30
31// OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
32class PACKED(4) OatQuickMethodHeader {
33 public:
David Srbecky113d6ea2021-03-02 22:49:46 +000034 OatQuickMethodHeader(uint32_t code_info_offset = 0) {
35 SetCodeInfoOffset(code_info_offset);
David Srbecky88087562018-06-23 22:05:56 +010036 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010037
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000038 static OatQuickMethodHeader* NterpMethodHeader;
39
40 bool IsNterpMethodHeader() const;
41
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010042 static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
43 uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
44 uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
Vladimir Marko562ff442015-10-27 18:51:20 +000045 DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
Jeff Haodcdc85b2015-12-04 14:06:18 -080046 IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
47 << std::hex << code << " " << std::hex << header;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010048 return reinterpret_cast<OatQuickMethodHeader*>(header);
49 }
50
51 static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
52 return FromCodePointer(EntryPointToCodePointer(entry_point));
53 }
54
David Srbeckyadb66f92019-10-10 12:59:43 +000055 static size_t InstructionAlignedSize() {
56 return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA));
57 }
58
Yi Kong2665bc82017-05-09 15:55:57 -070059 OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010060 OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
61
62 uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
63 return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
64 }
65
David Srbecky0983f592021-04-08 16:36:19 +010066 ALWAYS_INLINE bool IsOptimized() const {
David Srbecky113d6ea2021-03-02 22:49:46 +000067 uintptr_t code = reinterpret_cast<uintptr_t>(code_);
68 DCHECK_NE(data_, 0u) << std::hex << code; // Probably a padding of native code.
Nicolas Geoffray248d5c42021-09-13 09:53:10 +010069 DCHECK_NE(data_, kInvalidData) << std::hex << code; // Probably a stub or trampoline.
David Srbecky113d6ea2021-03-02 22:49:46 +000070 return (data_ & kIsCodeInfoMask) != 0;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 }
72
David Srbecky0983f592021-04-08 16:36:19 +010073 ALWAYS_INLINE const uint8_t* GetOptimizedCodeInfoPtr() const {
David Srbecky113d6ea2021-03-02 22:49:46 +000074 uint32_t offset = GetCodeInfoOffset();
75 DCHECK_NE(offset, 0u);
76 return code_ - offset;
David Srbecky5d950762016-03-07 20:47:29 +000077 }
78
David Srbecky0983f592021-04-08 16:36:19 +010079 ALWAYS_INLINE uint8_t* GetOptimizedCodeInfoPtr() {
David Srbecky113d6ea2021-03-02 22:49:46 +000080 uint32_t offset = GetCodeInfoOffset();
81 DCHECK_NE(offset, 0u);
82 return code_ - offset;
Nicolas Geoffray132d8362016-11-16 09:19:42 +000083 }
84
David Srbecky0983f592021-04-08 16:36:19 +010085 ALWAYS_INLINE const uint8_t* GetCode() const {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010086 return code_;
87 }
88
David Srbecky0983f592021-04-08 16:36:19 +010089 ALWAYS_INLINE uint32_t GetCodeSize() const {
90 return LIKELY(IsOptimized())
91 ? CodeInfo::DecodeCodeSize(GetOptimizedCodeInfoPtr())
92 : (data_ & kCodeSizeMask);
Nicolas Geoffray57083762019-03-05 09:24:45 +000093 }
94
David Srbecky0983f592021-04-08 16:36:19 +010095 ALWAYS_INLINE uint32_t GetCodeInfoOffset() const {
David Srbecky113d6ea2021-03-02 22:49:46 +000096 DCHECK(IsOptimized());
97 return data_ & kCodeInfoMask;
Mingyao Yang063fc772016-08-02 11:02:54 -070098 }
99
David Srbecky113d6ea2021-03-02 22:49:46 +0000100 void SetCodeInfoOffset(uint32_t offset) {
101 data_ = kIsCodeInfoMask | offset;
102 DCHECK_EQ(GetCodeInfoOffset(), offset);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100103 }
104
105 bool Contains(uintptr_t pc) const {
Nicolas Geoffray248d5c42021-09-13 09:53:10 +0100106 // We should not call `Contains` on a stub or trampoline.
107 DCHECK_NE(data_, kInvalidData) << std::hex << reinterpret_cast<uintptr_t>(code_);
David Srbecky2acd1ec2020-05-16 01:38:49 +0100108 // Remove hwasan tag to make comparison below valid. The PC from the stack does not have it.
109 uintptr_t code_start = reinterpret_cast<uintptr_t>(HWASanUntag(code_));
Vladimir Marko33bff252017-11-01 14:35:42 +0000110 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
111 if (kRuntimeISA == InstructionSet::kArm) {
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100112 // On Thumb-2, the pc is offset by one.
113 code_start++;
114 }
Mingyao Yang063fc772016-08-02 11:02:54 -0700115 return code_start <= pc && pc <= (code_start + GetCodeSize());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100116 }
117
118 const uint8_t* GetEntryPoint() const {
119 // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
120 // (not `kThumb2`), *but* we always generate code for the Thumb-2
121 // instruction set anyway. Thumb-2 requires the entrypoint to be of
122 // offset 1.
Vladimir Marko33bff252017-11-01 14:35:42 +0000123 static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
124 return (kRuntimeISA == InstructionSet::kArm)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100125 ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
126 : code_;
127 }
128
129 template <bool kCheckFrameSize = true>
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000130 uint32_t GetFrameSizeInBytes() const {
David Srbecky88087562018-06-23 22:05:56 +0100131 uint32_t result = GetFrameInfo().FrameSizeInBytes();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100132 if (kCheckFrameSize) {
David Srbecky6832fbe2016-03-11 18:48:55 +0000133 DCHECK_ALIGNED(result, kStackAlignment);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100134 }
135 return result;
136 }
137
138 QuickMethodFrameInfo GetFrameInfo() const {
David Srbecky79aa6242018-07-12 13:28:42 +0000139 DCHECK(IsOptimized());
David Srbecky88087562018-06-23 22:05:56 +0100140 return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100141 }
142
143 uintptr_t ToNativeQuickPc(ArtMethod* method,
144 const uint32_t dex_pc,
145 bool is_for_catch_handler,
146 bool abort_on_failure = true) const;
147
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +0000148 uint32_t ToDexPc(ArtMethod** frame,
149 const uintptr_t pc,
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000150 bool abort_on_failure = true) const
151 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100152
Mingyao Yang063fc772016-08-02 11:02:54 -0700153 void SetHasShouldDeoptimizeFlag() {
David Srbecky113d6ea2021-03-02 22:49:46 +0000154 DCHECK(!HasShouldDeoptimizeFlag());
155 data_ |= kShouldDeoptimizeMask;
Mingyao Yang063fc772016-08-02 11:02:54 -0700156 }
157
158 bool HasShouldDeoptimizeFlag() const {
David Srbecky113d6ea2021-03-02 22:49:46 +0000159 return (data_ & kShouldDeoptimizeMask) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700160 }
161
162 private:
163 static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
David Srbecky113d6ea2021-03-02 22:49:46 +0000164 static constexpr uint32_t kIsCodeInfoMask = 0x40000000;
165 static constexpr uint32_t kCodeInfoMask = 0x3FFFFFFF; // If kIsCodeInfoMask is set.
166 static constexpr uint32_t kCodeSizeMask = 0x3FFFFFFF; // If kIsCodeInfoMask is clear.
Mingyao Yang063fc772016-08-02 11:02:54 -0700167
Nicolas Geoffray248d5c42021-09-13 09:53:10 +0100168 // In order to not confuse a stub with Java-generated code, we prefix each
169 // stub with a 0xFFFFFFFF marker.
170 static constexpr uint32_t kInvalidData = 0xFFFFFFFF;
171
David Srbecky113d6ea2021-03-02 22:49:46 +0000172 uint32_t data_ = 0u; // Combination of fields using the above masks.
173 uint8_t code_[0]; // The actual method code.
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100174};
175
176} // namespace art
177
178#endif // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_