1 /*
2  * Copyright (C) 2015 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 <memory>
18 #include <vector>
19 
20 #include "arch/instruction_set.h"
21 #include "cfi_test.h"
22 #include "gtest/gtest.h"
23 #include "jni/quick/calling_convention.h"
24 #include "utils/assembler.h"
25 
26 #include "jni/jni_cfi_test_expected.inc"
27 
28 namespace art {
29 
30 // Run the tests only on host.
31 #ifndef HAVE_ANDROID_OS
32 
33 class JNICFITest : public CFITest {
34  public:
35   // Enable this flag to generate the expected outputs.
36   static constexpr bool kGenerateExpected = false;
37 
TestImpl(InstructionSet isa,const char * isa_str,const std::vector<uint8_t> & expected_asm,const std::vector<uint8_t> & expected_cfi)38   void TestImpl(InstructionSet isa, const char* isa_str,
39                 const std::vector<uint8_t>& expected_asm,
40                 const std::vector<uint8_t>& expected_cfi) {
41     // Description of simple method.
42     const bool is_static = true;
43     const bool is_synchronized = false;
44     const char* shorty = "IIFII";
45     std::unique_ptr<JniCallingConvention> jni_conv(
46         JniCallingConvention::Create(is_static, is_synchronized, shorty, isa));
47     std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
48         ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, isa));
49     const int frame_size(jni_conv->FrameSize());
50     const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters();
51 
52     // Assemble the method.
53     std::unique_ptr<Assembler> jni_asm(Assembler::Create(isa));
54     jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(),
55                         callee_save_regs, mr_conv->EntrySpills());
56     jni_asm->IncreaseFrameSize(32);
57     jni_asm->DecreaseFrameSize(32);
58     jni_asm->RemoveFrame(frame_size, callee_save_regs);
59     jni_asm->EmitSlowPaths();
60     std::vector<uint8_t> actual_asm(jni_asm->CodeSize());
61     MemoryRegion code(&actual_asm[0], actual_asm.size());
62     jni_asm->FinalizeInstructions(code);
63     ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size);
64     const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data());
65 
66     if (kGenerateExpected) {
67       GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi);
68     } else {
69       EXPECT_EQ(expected_asm, actual_asm);
70       EXPECT_EQ(expected_cfi, actual_cfi);
71     }
72   }
73 };
74 
75 #define TEST_ISA(isa) \
76   TEST_F(JNICFITest, isa) { \
77     std::vector<uint8_t> expected_asm(expected_asm_##isa, \
78         expected_asm_##isa + arraysize(expected_asm_##isa)); \
79     std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \
80         expected_cfi_##isa + arraysize(expected_cfi_##isa)); \
81     TestImpl(isa, #isa, expected_asm, expected_cfi); \
82   }
83 
84 TEST_ISA(kThumb2)
85 TEST_ISA(kArm64)
86 TEST_ISA(kX86)
87 TEST_ISA(kX86_64)
88 TEST_ISA(kMips)
89 TEST_ISA(kMips64)
90 
91 #endif  // HAVE_ANDROID_OS
92 
93 }  // namespace art
94