| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 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 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 20 | #include "callee_save_frame.h" |
| 21 | #include "common_runtime_test.h" |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 22 | #include "quick/quick_method_frame_info.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class QuickTrampolineEntrypointsTest : public CommonRuntimeTest { |
| 27 | protected: |
| Jeff Hao | 6d25419 | 2015-02-17 18:01:00 -0800 | [diff] [blame] | 28 | void SetUpRuntimeOptions(RuntimeOptions *options) OVERRIDE { |
| 29 | // Use 64-bit ISA for runtime setup to make method size potentially larger |
| 30 | // than necessary (rather than smaller) during CreateCalleeSaveMethod |
| 31 | options->push_back(std::make_pair("imageinstructionset", "x86_64")); |
| 32 | } |
| 33 | |
| Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 34 | // Do not do any of the finalization. We don't want to run any code, we don't need the heap |
| 35 | // prepared, it actually will be a problem with setting the instruction set to x86_64 in |
| 36 | // SetUpRuntimeOptions. |
| 37 | void FinalizeSetup() OVERRIDE { |
| 38 | ASSERT_EQ(InstructionSet::kX86_64, Runtime::Current()->GetInstructionSet()); |
| 39 | } |
| 40 | |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 41 | static ArtMethod* CreateCalleeSaveMethod(InstructionSet isa, Runtime::CalleeSaveType type) |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 42 | NO_THREAD_SAFETY_ANALYSIS { |
| 43 | Runtime* r = Runtime::Current(); |
| 44 | |
| 45 | Thread* t = Thread::Current(); |
| Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 46 | |
| 47 | ScopedObjectAccess soa(t); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 48 | |
| 49 | r->SetInstructionSet(isa); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 50 | ArtMethod* save_method = r->CreateCalleeSaveMethod(); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 51 | r->SetCalleeSaveMethod(save_method, type); |
| 52 | |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 53 | return save_method; |
| 54 | } |
| 55 | |
| 56 | static void CheckFrameSize(InstructionSet isa, Runtime::CalleeSaveType type, uint32_t save_size) |
| 57 | NO_THREAD_SAFETY_ANALYSIS { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 58 | ArtMethod* save_method = CreateCalleeSaveMethod(isa, type); |
| Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 59 | QuickMethodFrameInfo frame_info = Runtime::Current()->GetRuntimeMethodFrameInfo(save_method); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 60 | EXPECT_EQ(frame_info.FrameSizeInBytes(), save_size) << "Expected and real size differs for " |
| 61 | << type << " core spills=" << std::hex << frame_info.CoreSpillMask() << " fp spills=" |
| 62 | << frame_info.FpSpillMask() << std::dec << " ISA " << isa; |
| 63 | } |
| 64 | |
| 65 | static void CheckPCOffset(InstructionSet isa, Runtime::CalleeSaveType type, size_t pc_offset) |
| 66 | NO_THREAD_SAFETY_ANALYSIS { |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 67 | ArtMethod* save_method = CreateCalleeSaveMethod(isa, type); |
| Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 68 | QuickMethodFrameInfo frame_info = Runtime::Current()->GetRuntimeMethodFrameInfo(save_method); |
| 69 | EXPECT_EQ(frame_info.GetReturnPcOffset(), pc_offset) |
| Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 70 | << "Expected and real pc offset differs for " << type |
| 71 | << " core spills=" << std::hex << frame_info.CoreSpillMask() |
| 72 | << " fp spills=" << frame_info.FpSpillMask() << std::dec << " ISA " << isa; |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | // Note: these tests are all runtime tests. They let the Runtime create the corresponding ArtMethod |
| 77 | // and check against it. Technically we know and expect certain values, but the Runtime code is |
| 78 | // not constexpr, so we cannot make this compile-time checks (and I want the Runtime code tested). |
| 79 | |
| 80 | // This test ensures that kQuickCalleeSaveFrame_RefAndArgs_FrameSize is correct. |
| 81 | TEST_F(QuickTrampolineEntrypointsTest, FrameSize) { |
| 82 | // We have to use a define here as the callee_save_frame.h functions are constexpr. |
| Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 83 | #define CHECK_FRAME_SIZE(isa) \ |
| 84 | CheckFrameSize(isa, \ |
| 85 | Runtime::kSaveRefsAndArgs, \ |
| 86 | GetCalleeSaveFrameSize(isa, Runtime::kSaveRefsAndArgs)); \ |
| 87 | CheckFrameSize(isa, \ |
| 88 | Runtime::kSaveRefsOnly, \ |
| 89 | GetCalleeSaveFrameSize(isa, Runtime::kSaveRefsOnly)); \ |
| 90 | CheckFrameSize(isa, \ |
| 91 | Runtime::kSaveAllCalleeSaves, \ |
| 92 | GetCalleeSaveFrameSize(isa, Runtime::kSaveAllCalleeSaves)) |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 93 | |
| 94 | CHECK_FRAME_SIZE(kArm); |
| 95 | CHECK_FRAME_SIZE(kArm64); |
| 96 | CHECK_FRAME_SIZE(kMips); |
| Goran Jakovljevic | 1506bf3 | 2016-10-10 19:12:09 +0200 | [diff] [blame] | 97 | CHECK_FRAME_SIZE(kMips64); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 98 | CHECK_FRAME_SIZE(kX86); |
| 99 | CHECK_FRAME_SIZE(kX86_64); |
| 100 | } |
| 101 | |
| 102 | // This test ensures that GetConstExprPointerSize is correct with respect to |
| 103 | // GetInstructionSetPointerSize. |
| 104 | TEST_F(QuickTrampolineEntrypointsTest, PointerSize) { |
| 105 | EXPECT_EQ(GetInstructionSetPointerSize(kArm), GetConstExprPointerSize(kArm)); |
| 106 | EXPECT_EQ(GetInstructionSetPointerSize(kArm64), GetConstExprPointerSize(kArm64)); |
| 107 | EXPECT_EQ(GetInstructionSetPointerSize(kMips), GetConstExprPointerSize(kMips)); |
| Goran Jakovljevic | 1506bf3 | 2016-10-10 19:12:09 +0200 | [diff] [blame] | 108 | EXPECT_EQ(GetInstructionSetPointerSize(kMips64), GetConstExprPointerSize(kMips64)); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 109 | EXPECT_EQ(GetInstructionSetPointerSize(kX86), GetConstExprPointerSize(kX86)); |
| 110 | EXPECT_EQ(GetInstructionSetPointerSize(kX86_64), GetConstExprPointerSize(kX86_64)); |
| 111 | } |
| 112 | |
| 113 | // This test ensures that the constexpr specialization of the return PC offset computation in |
| 114 | // GetCalleeSavePCOffset is correct. |
| 115 | TEST_F(QuickTrampolineEntrypointsTest, ReturnPC) { |
| 116 | // Ensure that the computation in callee_save_frame.h correct. |
| 117 | // Note: we can only check against the kRuntimeISA, because the ArtMethod computation uses |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 118 | // sizeof(void*), which is wrong when the target bitwidth is not the same as the host's. |
| Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 119 | CheckPCOffset(kRuntimeISA, Runtime::kSaveRefsAndArgs, |
| 120 | GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kSaveRefsAndArgs)); |
| 121 | CheckPCOffset(kRuntimeISA, Runtime::kSaveRefsOnly, |
| 122 | GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kSaveRefsOnly)); |
| 123 | CheckPCOffset(kRuntimeISA, Runtime::kSaveAllCalleeSaves, |
| 124 | GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kSaveAllCalleeSaves)); |
| Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace art |