1 /*
2  * Copyright (C) 2014 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 <stdint.h>
18 
19 #include "art_method-inl.h"
20 #include "base/callee_save_type.h"
21 #include "entrypoints/quick/callee_save_frame.h"
22 #include "common_runtime_test.h"
23 #include "quick/quick_method_frame_info.h"
24 
25 namespace art {
26 
27 class ArchTest : public CommonRuntimeTest {
28  protected:
SetUpRuntimeOptions(RuntimeOptions * options)29   void SetUpRuntimeOptions(RuntimeOptions *options) override {
30     // Use 64-bit ISA for runtime setup to make method size potentially larger
31     // than necessary (rather than smaller) during CreateCalleeSaveMethod
32     options->push_back(std::make_pair("imageinstructionset", "x86_64"));
33   }
34 
35   // Do not do any of the finalization. We don't want to run any code, we don't need the heap
36   // prepared, it actually will be a problem with setting the instruction set to x86_64 in
37   // SetUpRuntimeOptions.
FinalizeSetup()38   void FinalizeSetup() override {
39     ASSERT_EQ(InstructionSet::kX86_64, Runtime::Current()->GetInstructionSet());
40   }
41 };
42 
43 // Grab architecture specific constants.
44 namespace arm {
45 #include "arch/arm/asm_support_arm.h"
46 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
47 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
48 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
49 #undef FRAME_SIZE_SAVE_REFS_ONLY
50 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
51 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
52 static constexpr size_t kFrameSizeSaveEverythingForClinit = FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT;
53 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT
54 static constexpr size_t kFrameSizeSaveEverythingForSuspendCheck =
55     FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK;
56 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK
57 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
58 #undef FRAME_SIZE_SAVE_EVERYTHING
59 #undef BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET
60 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
61 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
62 #undef BAKER_MARK_INTROSPECTION_ARRAY_SWITCH_OFFSET
63 #undef BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
64 #undef BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
65 #undef BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET
66 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET
67 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
68 }  // namespace arm
69 
70 namespace arm64 {
71 #include "arch/arm64/asm_support_arm64.h"
72 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
73 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
74 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
75 #undef FRAME_SIZE_SAVE_REFS_ONLY
76 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
77 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
78 static constexpr size_t kFrameSizeSaveEverythingForClinit = FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT;
79 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT
80 static constexpr size_t kFrameSizeSaveEverythingForSuspendCheck =
81     FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK;
82 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK
83 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
84 #undef FRAME_SIZE_SAVE_EVERYTHING
85 #undef BAKER_MARK_INTROSPECTION_ARRAY_SWITCH_OFFSET
86 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET
87 #undef BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET
88 #undef BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET
89 #undef BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET
90 }  // namespace arm64
91 
92 namespace x86 {
93 #include "arch/x86/asm_support_x86.h"
94 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
95 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
96 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
97 #undef FRAME_SIZE_SAVE_REFS_ONLY
98 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
99 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
100 static constexpr size_t kFrameSizeSaveEverythingForClinit = FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT;
101 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT
102 static constexpr size_t kFrameSizeSaveEverythingForSuspendCheck =
103     FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK;
104 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK
105 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
106 #undef FRAME_SIZE_SAVE_EVERYTHING
107 }  // namespace x86
108 
109 namespace x86_64 {
110 #include "arch/x86_64/asm_support_x86_64.h"
111 static constexpr size_t kFrameSizeSaveAllCalleeSaves = FRAME_SIZE_SAVE_ALL_CALLEE_SAVES;
112 #undef FRAME_SIZE_SAVE_ALL_CALLEE_SAVES
113 static constexpr size_t kFrameSizeSaveRefsOnly = FRAME_SIZE_SAVE_REFS_ONLY;
114 #undef FRAME_SIZE_SAVE_REFS_ONLY
115 static constexpr size_t kFrameSizeSaveRefsAndArgs = FRAME_SIZE_SAVE_REFS_AND_ARGS;
116 #undef FRAME_SIZE_SAVE_REFS_AND_ARGS
117 static constexpr size_t kFrameSizeSaveEverythingForClinit = FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT;
118 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_CLINIT
119 static constexpr size_t kFrameSizeSaveEverythingForSuspendCheck =
120     FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK;
121 #undef FRAME_SIZE_SAVE_EVERYTHING_FOR_SUSPEND_CHECK
122 static constexpr size_t kFrameSizeSaveEverything = FRAME_SIZE_SAVE_EVERYTHING;
123 #undef FRAME_SIZE_SAVE_EVERYTHING
124 }  // namespace x86_64
125 
126 // Check architecture specific constants are sound.
127 // We expect the return PC to be stored at the highest address slot in the frame.
128 #define TEST_ARCH_TYPE(Arch, arch, type)                                              \
129   EXPECT_EQ(arch::Arch##CalleeSaveFrame::GetFrameSize(CalleeSaveType::k##type),       \
130             arch::kFrameSize##type);                                                  \
131   EXPECT_EQ(arch::Arch##CalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::k##type),  \
132             arch::kFrameSize##type - static_cast<size_t>(k##Arch##PointerSize))
133 #define TEST_ARCH(Arch, arch)                                   \
134   TEST_F(ArchTest, Arch) {                                      \
135     TEST_ARCH_TYPE(Arch, arch, SaveAllCalleeSaves);             \
136     TEST_ARCH_TYPE(Arch, arch, SaveRefsOnly);                   \
137     TEST_ARCH_TYPE(Arch, arch, SaveRefsAndArgs);                \
138     TEST_ARCH_TYPE(Arch, arch, SaveEverything);                 \
139     TEST_ARCH_TYPE(Arch, arch, SaveEverythingForClinit);        \
140     TEST_ARCH_TYPE(Arch, arch, SaveEverythingForSuspendCheck);  \
141   }
142 TEST_ARCH(Arm, arm)
143 TEST_ARCH(Arm64, arm64)
144 TEST_ARCH(X86, x86)
145 TEST_ARCH(X86_64, x86_64)
146 
147 }  // namespace art
148