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 #ifndef ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
18 #define ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
19 
20 #include "arch/instruction_set.h"
21 #include "base/locks.h"
22 #include "base/macros.h"
23 #include "base/utils.h"
24 #include "quick/quick_method_frame_info.h"
25 #include "stack_map.h"
26 
27 namespace art {
28 
29 class ArtMethod;
30 
31 // OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
32 class PACKED(4) OatQuickMethodHeader {
33  public:
34   OatQuickMethodHeader(uint32_t code_info_offset = 0) {
35     SetCodeInfoOffset(code_info_offset);
36   }
37 
38   static OatQuickMethodHeader* NterpMethodHeader;
39 
40   bool IsNterpMethodHeader() const;
41 
FromCodePointer(const void * code_ptr)42   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_);
45     DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
46            IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
47         << std::hex << code << " " << std::hex << header;
48     return reinterpret_cast<OatQuickMethodHeader*>(header);
49   }
50 
FromEntryPoint(const void * entry_point)51   static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
52     return FromCodePointer(EntryPointToCodePointer(entry_point));
53   }
54 
InstructionAlignedSize()55   static size_t InstructionAlignedSize() {
56     return RoundUp(sizeof(OatQuickMethodHeader), GetInstructionSetAlignment(kRuntimeISA));
57   }
58 
59   OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
60   OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
61 
NativeQuickPcOffset(const uintptr_t pc)62   uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
63     return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
64   }
65 
IsOptimized()66   ALWAYS_INLINE bool IsOptimized() const {
67     uintptr_t code = reinterpret_cast<uintptr_t>(code_);
68     DCHECK_NE(data_, 0u) << std::hex << code;          // Probably a padding of native code.
69     DCHECK_NE(data_, 0xFFFFFFFF) << std::hex << code;  // Probably a stub or trampoline.
70     return (data_ & kIsCodeInfoMask) != 0;
71   }
72 
GetOptimizedCodeInfoPtr()73   ALWAYS_INLINE const uint8_t* GetOptimizedCodeInfoPtr() const {
74     uint32_t offset = GetCodeInfoOffset();
75     DCHECK_NE(offset, 0u);
76     return code_ - offset;
77   }
78 
GetOptimizedCodeInfoPtr()79   ALWAYS_INLINE uint8_t* GetOptimizedCodeInfoPtr() {
80     uint32_t offset = GetCodeInfoOffset();
81     DCHECK_NE(offset, 0u);
82     return code_ - offset;
83   }
84 
GetCode()85   ALWAYS_INLINE const uint8_t* GetCode() const {
86     return code_;
87   }
88 
GetCodeSize()89   ALWAYS_INLINE uint32_t GetCodeSize() const {
90     return LIKELY(IsOptimized())
91         ? CodeInfo::DecodeCodeSize(GetOptimizedCodeInfoPtr())
92         : (data_ & kCodeSizeMask);
93   }
94 
GetCodeInfoOffset()95   ALWAYS_INLINE uint32_t GetCodeInfoOffset() const {
96     DCHECK(IsOptimized());
97     return data_ & kCodeInfoMask;
98   }
99 
SetCodeInfoOffset(uint32_t offset)100   void SetCodeInfoOffset(uint32_t offset) {
101     data_ = kIsCodeInfoMask | offset;
102     DCHECK_EQ(GetCodeInfoOffset(), offset);
103   }
104 
Contains(uintptr_t pc)105   bool Contains(uintptr_t pc) const {
106     // Remove hwasan tag to make comparison below valid. The PC from the stack does not have it.
107     uintptr_t code_start = reinterpret_cast<uintptr_t>(HWASanUntag(code_));
108     static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
109     if (kRuntimeISA == InstructionSet::kArm) {
110       // On Thumb-2, the pc is offset by one.
111       code_start++;
112     }
113     return code_start <= pc && pc <= (code_start + GetCodeSize());
114   }
115 
GetEntryPoint()116   const uint8_t* GetEntryPoint() const {
117     // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
118     // (not `kThumb2`), *but* we always generate code for the Thumb-2
119     // instruction set anyway. Thumb-2 requires the entrypoint to be of
120     // offset 1.
121     static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
122     return (kRuntimeISA == InstructionSet::kArm)
123         ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
124         : code_;
125   }
126 
127   template <bool kCheckFrameSize = true>
GetFrameSizeInBytes()128   uint32_t GetFrameSizeInBytes() const {
129     uint32_t result = GetFrameInfo().FrameSizeInBytes();
130     if (kCheckFrameSize) {
131       DCHECK_ALIGNED(result, kStackAlignment);
132     }
133     return result;
134   }
135 
GetFrameInfo()136   QuickMethodFrameInfo GetFrameInfo() const {
137     DCHECK(IsOptimized());
138     return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
139   }
140 
141   uintptr_t ToNativeQuickPc(ArtMethod* method,
142                             const uint32_t dex_pc,
143                             bool is_for_catch_handler,
144                             bool abort_on_failure = true) const;
145 
146   uint32_t ToDexPc(ArtMethod** frame,
147                    const uintptr_t pc,
148                    bool abort_on_failure = true) const
149       REQUIRES_SHARED(Locks::mutator_lock_);
150 
SetHasShouldDeoptimizeFlag()151   void SetHasShouldDeoptimizeFlag() {
152     DCHECK(!HasShouldDeoptimizeFlag());
153     data_ |= kShouldDeoptimizeMask;
154   }
155 
HasShouldDeoptimizeFlag()156   bool HasShouldDeoptimizeFlag() const {
157     return (data_ & kShouldDeoptimizeMask) != 0;
158   }
159 
160  private:
161   static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
162   static constexpr uint32_t kIsCodeInfoMask       = 0x40000000;
163   static constexpr uint32_t kCodeInfoMask         = 0x3FFFFFFF;  // If kIsCodeInfoMask is set.
164   static constexpr uint32_t kCodeSizeMask         = 0x3FFFFFFF;  // If kIsCodeInfoMask is clear.
165 
166   uint32_t data_ = 0u;  // Combination of fields using the above masks.
167   uint8_t code_[0];     // The actual method code.
168 };
169 
170 }  // namespace art
171 
172 #endif  // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
173