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 "callee_save_frame.h"
20#include "common_runtime_test.h"
21#include "mirror/art_method-inl.h"
22#include "quick/quick_method_frame_info.h"
23
24namespace art {
25
26class QuickTrampolineEntrypointsTest : public CommonRuntimeTest {
27 protected:
28  static mirror::ArtMethod* CreateCalleeSaveMethod(InstructionSet isa,
29                                                   Runtime::CalleeSaveType type)
30      NO_THREAD_SAFETY_ANALYSIS {
31    Runtime* r = Runtime::Current();
32
33    Thread* t = Thread::Current();
34    t->TransitionFromSuspendedToRunnable();  // So we can create callee-save methods.
35
36    r->SetInstructionSet(isa);
37    mirror::ArtMethod* save_method = r->CreateCalleeSaveMethod(type);
38    r->SetCalleeSaveMethod(save_method, type);
39
40    t->TransitionFromRunnableToSuspended(ThreadState::kNative);  // So we can shut down.
41
42    return save_method;
43  }
44
45  static void CheckFrameSize(InstructionSet isa, Runtime::CalleeSaveType type, uint32_t save_size)
46      NO_THREAD_SAFETY_ANALYSIS {
47    mirror::ArtMethod* save_method = CreateCalleeSaveMethod(isa, type);
48    QuickMethodFrameInfo frame_info = save_method->GetQuickFrameInfo();
49    EXPECT_EQ(frame_info.FrameSizeInBytes(), save_size) << "Expected and real size differs for "
50        << type << " core spills=" << std::hex << frame_info.CoreSpillMask() << " fp spills="
51        << frame_info.FpSpillMask() << std::dec << " ISA " << isa;
52  }
53
54  static void CheckPCOffset(InstructionSet isa, Runtime::CalleeSaveType type, size_t pc_offset)
55      NO_THREAD_SAFETY_ANALYSIS {
56    mirror::ArtMethod* save_method = CreateCalleeSaveMethod(isa, type);
57    QuickMethodFrameInfo frame_info = save_method->GetQuickFrameInfo();
58    EXPECT_EQ(save_method->GetReturnPcOffsetInBytes(), pc_offset) << "Expected and real pc offset"
59        " differs for " << type << " core spills=" << std::hex << frame_info.CoreSpillMask() <<
60        " fp spills=" << frame_info.FpSpillMask() << std::dec << " ISA " << isa;
61  }
62};
63
64// Note: these tests are all runtime tests. They let the Runtime create the corresponding ArtMethod
65// and check against it. Technically we know and expect certain values, but the Runtime code is
66// not constexpr, so we cannot make this compile-time checks (and I want the Runtime code tested).
67
68// This test ensures that kQuickCalleeSaveFrame_RefAndArgs_FrameSize is correct.
69TEST_F(QuickTrampolineEntrypointsTest, FrameSize) {
70  // We have to use a define here as the callee_save_frame.h functions are constexpr.
71#define CHECK_FRAME_SIZE(isa)                                                                     \
72  CheckFrameSize(isa, Runtime::kRefsAndArgs, GetCalleeSaveFrameSize(isa, Runtime::kRefsAndArgs)); \
73  CheckFrameSize(isa, Runtime::kRefsOnly, GetCalleeSaveFrameSize(isa, Runtime::kRefsOnly));       \
74  CheckFrameSize(isa, Runtime::kSaveAll, GetCalleeSaveFrameSize(isa, Runtime::kSaveAll))
75
76  CHECK_FRAME_SIZE(kArm);
77  CHECK_FRAME_SIZE(kArm64);
78  CHECK_FRAME_SIZE(kMips);
79  CHECK_FRAME_SIZE(kX86);
80  CHECK_FRAME_SIZE(kX86_64);
81}
82
83// This test ensures that GetConstExprPointerSize is correct with respect to
84// GetInstructionSetPointerSize.
85TEST_F(QuickTrampolineEntrypointsTest, PointerSize) {
86  EXPECT_EQ(GetInstructionSetPointerSize(kArm), GetConstExprPointerSize(kArm));
87  EXPECT_EQ(GetInstructionSetPointerSize(kArm64), GetConstExprPointerSize(kArm64));
88  EXPECT_EQ(GetInstructionSetPointerSize(kMips), GetConstExprPointerSize(kMips));
89  EXPECT_EQ(GetInstructionSetPointerSize(kX86), GetConstExprPointerSize(kX86));
90  EXPECT_EQ(GetInstructionSetPointerSize(kX86_64), GetConstExprPointerSize(kX86_64));
91}
92
93// This test ensures that the constexpr specialization of the return PC offset computation in
94// GetCalleeSavePCOffset is correct.
95TEST_F(QuickTrampolineEntrypointsTest, ReturnPC) {
96  // Ensure that the computation in callee_save_frame.h correct.
97  // Note: we can only check against the kRuntimeISA, because the ArtMethod computation uses
98  // kPointerSize, which is wrong when the target bitwidth is not the same as the host's.
99  CheckPCOffset(kRuntimeISA, Runtime::kRefsAndArgs,
100                GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsAndArgs));
101  CheckPCOffset(kRuntimeISA, Runtime::kRefsOnly,
102                GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsOnly));
103  CheckPCOffset(kRuntimeISA, Runtime::kSaveAll,
104                GetCalleeSavePCOffset(kRuntimeISA, Runtime::kSaveAll));
105}
106
107}  // namespace art
108