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" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 23 | #include "base/malloc_arena_pool.h" |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 24 | #include "cfi_test.h" |
| 25 | #include "gtest/gtest.h" |
| 26 | #include "jni/quick/calling_convention.h" |
Andreas Gampe | 217488a | 2017-09-18 08:34:42 -0700 | [diff] [blame] | 27 | #include "read_barrier_config.h" |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 28 | #include "utils/assembler.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 29 | #include "utils/jni_macro_assembler.h" |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 30 | |
| 31 | #include "jni/jni_cfi_test_expected.inc" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | // Run the tests only on host. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 36 | #ifndef ART_TARGET_ANDROID |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 37 | |
| 38 | class JNICFITest : public CFITest { |
| 39 | public: |
| 40 | // Enable this flag to generate the expected outputs. |
| 41 | static constexpr bool kGenerateExpected = false; |
| 42 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 43 | void TestImpl(InstructionSet isa, |
| 44 | const char* isa_str, |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 45 | const std::vector<uint8_t>& expected_asm, |
| 46 | const std::vector<uint8_t>& expected_cfi) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 47 | if (Is64BitInstructionSet(isa)) { |
| 48 | TestImplSized<PointerSize::k64>(isa, isa_str, expected_asm, expected_cfi); |
| 49 | } else { |
| 50 | TestImplSized<PointerSize::k32>(isa, isa_str, expected_asm, expected_cfi); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | template <PointerSize kPointerSize> |
| 56 | void TestImplSized(InstructionSet isa, |
| 57 | const char* isa_str, |
| 58 | const std::vector<uint8_t>& expected_asm, |
| 59 | const std::vector<uint8_t>& expected_cfi) { |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 60 | // Description of simple method. |
| 61 | const bool is_static = true; |
| 62 | const bool is_synchronized = false; |
| 63 | const char* shorty = "IIFII"; |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 64 | |
David Sehr | 3215fff | 2018-04-03 17:10:12 -0700 | [diff] [blame] | 65 | MallocArenaPool pool; |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 66 | ArenaAllocator allocator(&pool); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 67 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 68 | std::unique_ptr<JniCallingConvention> jni_conv( |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 69 | JniCallingConvention::Create(&allocator, |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 70 | is_static, |
| 71 | is_synchronized, |
| 72 | /*is_critical_native*/false, |
| 73 | shorty, |
| 74 | isa)); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 75 | std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv( |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 76 | ManagedRuntimeCallingConvention::Create( |
| 77 | &allocator, is_static, is_synchronized, shorty, isa)); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 78 | const int frame_size(jni_conv->FrameSize()); |
Vladimir Marko | 3224838 | 2016-05-19 10:37:24 +0100 | [diff] [blame] | 79 | ArrayRef<const ManagedRegister> callee_save_regs = jni_conv->CalleeSaveRegisters(); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 80 | |
| 81 | // Assemble the method. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 82 | std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm( |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 83 | JNIMacroAssembler<kPointerSize>::Create(&allocator, isa)); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 84 | jni_asm->cfi().SetEnabled(true); |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 85 | jni_asm->BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs); |
| 86 | // Spill arguments. |
| 87 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 88 | for (; mr_conv->HasNext(); mr_conv->Next()) { |
| 89 | if (mr_conv->IsCurrentParamInRegister()) { |
| 90 | size_t size = mr_conv->IsCurrentParamALongOrDouble() ? 8u : 4u; |
| 91 | jni_asm->Store(mr_conv->CurrentParamStackOffset(), mr_conv->CurrentParamRegister(), size); |
| 92 | } |
| 93 | } |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 94 | jni_asm->IncreaseFrameSize(32); |
| 95 | jni_asm->DecreaseFrameSize(32); |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 96 | jni_asm->RemoveFrame(frame_size, callee_save_regs, /* may_suspend= */ true); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 97 | jni_asm->FinalizeCode(); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 98 | std::vector<uint8_t> actual_asm(jni_asm->CodeSize()); |
| 99 | MemoryRegion code(&actual_asm[0], actual_asm.size()); |
| 100 | jni_asm->FinalizeInstructions(code); |
| 101 | ASSERT_EQ(jni_asm->cfi().GetCurrentCFAOffset(), frame_size); |
| 102 | const std::vector<uint8_t>& actual_cfi = *(jni_asm->cfi().data()); |
| 103 | |
| 104 | if (kGenerateExpected) { |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 105 | GenerateExpected(stdout, |
| 106 | isa, |
| 107 | isa_str, |
| 108 | ArrayRef<const uint8_t>(actual_asm), |
| 109 | ArrayRef<const uint8_t>(actual_cfi)); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 110 | } else { |
| 111 | EXPECT_EQ(expected_asm, actual_asm); |
| 112 | EXPECT_EQ(expected_cfi, actual_cfi); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 117 | #define TEST_ISA(isa) \ |
| 118 | TEST_F(JNICFITest, isa) { \ |
| 119 | std::vector<uint8_t> expected_asm(expected_asm_##isa, \ |
| 120 | expected_asm_##isa + arraysize(expected_asm_##isa)); \ |
| 121 | std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \ |
| 122 | expected_cfi_##isa + arraysize(expected_cfi_##isa)); \ |
| 123 | TestImpl(InstructionSet::isa, #isa, expected_asm, expected_cfi); \ |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 126 | #ifdef ART_ENABLE_CODEGEN_arm |
Roland Levillain | 6d729a7 | 2017-06-30 18:34:01 +0100 | [diff] [blame] | 127 | // Run the tests for ARM only with Baker read barriers, as the |
| 128 | // expected generated code contains a Marking Register refresh |
| 129 | // instruction. |
| 130 | #if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER) |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 131 | TEST_ISA(kThumb2) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 132 | #endif |
Roland Levillain | 6d729a7 | 2017-06-30 18:34:01 +0100 | [diff] [blame] | 133 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 134 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 135 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 136 | // Run the tests for ARM64 only with Baker read barriers, as the |
| 137 | // expected generated code contains a Marking Register refresh |
| 138 | // instruction. |
| 139 | #if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER) |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 140 | TEST_ISA(kArm64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 141 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 142 | #endif |
| 143 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 144 | #ifdef ART_ENABLE_CODEGEN_x86 |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 145 | TEST_ISA(kX86) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 146 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 147 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 148 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 149 | TEST_ISA(kX86_64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 150 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 151 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 152 | #endif // ART_TARGET_ANDROID |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 153 | |
| 154 | } // namespace art |