David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 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" |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame^] | 21 | #include "base/arena_allocator.h" |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 22 | #include "cfi_test.h" |
| 23 | #include "gtest/gtest.h" |
| 24 | #include "jni/quick/calling_convention.h" |
| 25 | #include "utils/assembler.h" |
| 26 | |
| 27 | #include "jni/jni_cfi_test_expected.inc" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | // Run the tests only on host. |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 32 | #ifndef __ANDROID__ |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 33 | |
| 34 | class JNICFITest : public CFITest { |
| 35 | public: |
| 36 | // Enable this flag to generate the expected outputs. |
| 37 | static constexpr bool kGenerateExpected = false; |
| 38 | |
| 39 | void TestImpl(InstructionSet isa, const char* isa_str, |
| 40 | const std::vector<uint8_t>& expected_asm, |
| 41 | const std::vector<uint8_t>& expected_cfi) { |
| 42 | // Description of simple method. |
| 43 | const bool is_static = true; |
| 44 | const bool is_synchronized = false; |
| 45 | const char* shorty = "IIFII"; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame^] | 46 | |
| 47 | ArenaPool pool; |
| 48 | ArenaAllocator arena(&pool); |
| 49 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 50 | std::unique_ptr<JniCallingConvention> jni_conv( |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame^] | 51 | JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa)); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 52 | std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv( |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame^] | 53 | ManagedRuntimeCallingConvention::Create(&arena, is_static, is_synchronized, shorty, isa)); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 54 | const int frame_size(jni_conv->FrameSize()); |
| 55 | const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters(); |
| 56 | |
| 57 | // Assemble the method. |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame^] | 58 | std::unique_ptr<Assembler> jni_asm(Assembler::Create(&arena, isa)); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 59 | jni_asm->cfi().SetEnabled(true); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 60 | jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(), |
| 61 | callee_save_regs, mr_conv->EntrySpills()); |
| 62 | jni_asm->IncreaseFrameSize(32); |
| 63 | jni_asm->DecreaseFrameSize(32); |
| 64 | jni_asm->RemoveFrame(frame_size, callee_save_regs); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 65 | jni_asm->FinalizeCode(); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 66 | std::vector<uint8_t> actual_asm(jni_asm->CodeSize()); |
| 67 | MemoryRegion code(&actual_asm[0], actual_asm.size()); |
| 68 | jni_asm->FinalizeInstructions(code); |
| 69 | ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size); |
| 70 | const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data()); |
| 71 | |
| 72 | if (kGenerateExpected) { |
| 73 | GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi); |
| 74 | } else { |
| 75 | EXPECT_EQ(expected_asm, actual_asm); |
| 76 | EXPECT_EQ(expected_cfi, actual_cfi); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | #define TEST_ISA(isa) \ |
| 82 | TEST_F(JNICFITest, isa) { \ |
| 83 | std::vector<uint8_t> expected_asm(expected_asm_##isa, \ |
| 84 | expected_asm_##isa + arraysize(expected_asm_##isa)); \ |
| 85 | std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \ |
| 86 | expected_cfi_##isa + arraysize(expected_cfi_##isa)); \ |
| 87 | TestImpl(isa, #isa, expected_asm, expected_cfi); \ |
| 88 | } |
| 89 | |
| 90 | TEST_ISA(kThumb2) |
| 91 | TEST_ISA(kArm64) |
| 92 | TEST_ISA(kX86) |
| 93 | TEST_ISA(kX86_64) |
| 94 | TEST_ISA(kMips) |
| 95 | TEST_ISA(kMips64) |
| 96 | |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 97 | #endif // __ANDROID__ |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 98 | |
| 99 | } // namespace art |