Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 17 | #include "art_method-inl.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 18 | #include "base/callee_save_type.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 19 | #include "base/enums.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 20 | #include "callee_save_frame.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 21 | #include "common_throws.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 22 | #include "class_root-inl.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 23 | #include "debug_print.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file-inl.h" |
| 26 | #include "dex/dex_file_types.h" |
| 27 | #include "dex/dex_instruction-inl.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 28 | #include "dex/method_reference.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 29 | #include "entrypoints/entrypoint_utils-inl.h" |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 30 | #include "entrypoints/quick/callee_save_frame.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 31 | #include "entrypoints/runtime_asm_entrypoints.h" |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 32 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 33 | #include "imt_conflict_table.h" |
| 34 | #include "imtable-inl.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 35 | #include "instrumentation.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "interpreter/interpreter.h" |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 37 | #include "interpreter/interpreter_common.h" |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 38 | #include "interpreter/shadow_frame-inl.h" |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 39 | #include "jit/jit.h" |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 40 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 41 | #include "linear_alloc.h" |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 42 | #include "method_handles.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 43 | #include "mirror/class-inl.h" |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 44 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 45 | #include "mirror/method.h" |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 46 | #include "mirror/method_handle_impl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 47 | #include "mirror/object-inl.h" |
| 48 | #include "mirror/object_array-inl.h" |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 49 | #include "mirror/var_handle.h" |
Vladimir Marko | d3d00c0 | 2019-11-07 15:09:07 +0000 | [diff] [blame] | 50 | #include "oat.h" |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 51 | #include "oat_file.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 52 | #include "oat_quick_method_header.h" |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 53 | #include "quick_exception_handler.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 54 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 56 | #include "stack.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 57 | #include "thread-inl.h" |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 58 | #include "var_handles.h" |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 59 | #include "well_known_classes.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 63 | extern "C" NO_RETURN void artDeoptimizeFromCompiledCode(DeoptimizationKind kind, Thread* self); |
| 64 | extern "C" NO_RETURN void artDeoptimize(Thread* self); |
| 65 | |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 66 | // Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 67 | class QuickArgumentVisitor { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 68 | // Number of bytes for each out register in the caller method's frame. |
| 69 | static constexpr size_t kBytesStackArgLocation = 4; |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 70 | // Frame size in bytes of a callee-save frame for RefsAndArgs. |
| 71 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 72 | RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs); |
| 73 | // Offset of first GPR arg. |
| 74 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = |
| 75 | RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); |
| 76 | // Offset of first FPR arg. |
| 77 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = |
| 78 | RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); |
| 79 | // Offset of return address. |
| 80 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset = |
| 81 | RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 82 | #if defined(__arm__) |
| 83 | // The callee save frame is pointed to by SP. |
| 84 | // | argN | | |
| 85 | // | ... | | |
| 86 | // | arg4 | | |
| 87 | // | arg3 spill | | Caller's frame |
| 88 | // | arg2 spill | | |
| 89 | // | arg1 spill | | |
| 90 | // | Method* | --- |
| 91 | // | LR | |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 92 | // | ... | 4x6 bytes callee saves |
| 93 | // | R3 | |
| 94 | // | R2 | |
| 95 | // | R1 | |
| 96 | // | S15 | |
| 97 | // | : | |
| 98 | // | S0 | |
| 99 | // | | 4x2 bytes padding |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 100 | // | Method* | <- sp |
Andreas Gampe | 217d6d3 | 2017-09-18 12:48:20 -0700 | [diff] [blame] | 101 | static constexpr bool kSplitPairAcrossRegisterAndStack = false; |
| 102 | static constexpr bool kAlignPairRegister = true; |
| 103 | static constexpr bool kQuickSoftFloatAbi = false; |
| 104 | static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true; |
Goran Jakovljevic | ff73498 | 2015-08-24 12:58:55 +0000 | [diff] [blame] | 105 | static constexpr bool kQuickSkipOddFpRegisters = false; |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 106 | static constexpr size_t kNumQuickGprArgs = 3; |
Andreas Gampe | 217d6d3 | 2017-09-18 12:48:20 -0700 | [diff] [blame] | 107 | static constexpr size_t kNumQuickFprArgs = 16; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 108 | static constexpr bool kGprFprLockstep = false; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 109 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 110 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 111 | } |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 112 | #elif defined(__aarch64__) |
| 113 | // The callee save frame is pointed to by SP. |
| 114 | // | argN | | |
| 115 | // | ... | | |
| 116 | // | arg4 | | |
| 117 | // | arg3 spill | | Caller's frame |
| 118 | // | arg2 spill | | |
| 119 | // | arg1 spill | | |
| 120 | // | Method* | --- |
| 121 | // | LR | |
Zheng Xu | b551fdc | 2014-07-25 11:49:42 +0800 | [diff] [blame] | 122 | // | X29 | |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 123 | // | : | |
Serban Constantinescu | 9bd88b0 | 2015-04-22 16:24:46 +0100 | [diff] [blame] | 124 | // | X20 | |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 125 | // | X7 | |
| 126 | // | : | |
| 127 | // | X1 | |
Zheng Xu | b551fdc | 2014-07-25 11:49:42 +0800 | [diff] [blame] | 128 | // | D7 | |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 129 | // | : | |
| 130 | // | D0 | |
| 131 | // | | padding |
| 132 | // | Method* | <- sp |
Mark Mendell | 3e6a3bf | 2015-01-19 14:09:22 -0500 | [diff] [blame] | 133 | static constexpr bool kSplitPairAcrossRegisterAndStack = false; |
Nicolas Geoffray | 69c15d3 | 2015-01-13 11:42:13 +0000 | [diff] [blame] | 134 | static constexpr bool kAlignPairRegister = false; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 135 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 136 | static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; |
Goran Jakovljevic | ff73498 | 2015-08-24 12:58:55 +0000 | [diff] [blame] | 137 | static constexpr bool kQuickSkipOddFpRegisters = false; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 138 | static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs. |
| 139 | static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 140 | static constexpr bool kGprFprLockstep = false; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 141 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 142 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 143 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 144 | #elif defined(__i386__) |
| 145 | // The callee save frame is pointed to by SP. |
| 146 | // | argN | | |
| 147 | // | ... | | |
| 148 | // | arg4 | | |
| 149 | // | arg3 spill | | Caller's frame |
| 150 | // | arg2 spill | | |
| 151 | // | arg1 spill | | |
| 152 | // | Method* | --- |
| 153 | // | Return | |
| 154 | // | EBP,ESI,EDI | callee saves |
| 155 | // | EBX | arg3 |
| 156 | // | EDX | arg2 |
| 157 | // | ECX | arg1 |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 158 | // | XMM3 | float arg 4 |
| 159 | // | XMM2 | float arg 3 |
| 160 | // | XMM1 | float arg 2 |
| 161 | // | XMM0 | float arg 1 |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 162 | // | EAX/Method* | <- sp |
Mark Mendell | 3e6a3bf | 2015-01-19 14:09:22 -0500 | [diff] [blame] | 163 | static constexpr bool kSplitPairAcrossRegisterAndStack = false; |
Nicolas Geoffray | 69c15d3 | 2015-01-13 11:42:13 +0000 | [diff] [blame] | 164 | static constexpr bool kAlignPairRegister = false; |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 165 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 166 | static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; |
Goran Jakovljevic | ff73498 | 2015-08-24 12:58:55 +0000 | [diff] [blame] | 167 | static constexpr bool kQuickSkipOddFpRegisters = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 168 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
Mark P Mendell | 966c3ae | 2015-01-27 15:45:27 +0000 | [diff] [blame] | 169 | static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs. |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 170 | static constexpr bool kGprFprLockstep = false; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 171 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 172 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 173 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 174 | #elif defined(__x86_64__) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 175 | // The callee save frame is pointed to by SP. |
| 176 | // | argN | | |
| 177 | // | ... | | |
| 178 | // | reg. arg spills | | Caller's frame |
| 179 | // | Method* | --- |
| 180 | // | Return | |
| 181 | // | R15 | callee save |
| 182 | // | R14 | callee save |
| 183 | // | R13 | callee save |
| 184 | // | R12 | callee save |
| 185 | // | R9 | arg5 |
| 186 | // | R8 | arg4 |
| 187 | // | RSI/R6 | arg1 |
| 188 | // | RBP/R5 | callee save |
| 189 | // | RBX/R3 | callee save |
| 190 | // | RDX/R2 | arg2 |
| 191 | // | RCX/R1 | arg3 |
| 192 | // | XMM7 | float arg 8 |
| 193 | // | XMM6 | float arg 7 |
| 194 | // | XMM5 | float arg 6 |
| 195 | // | XMM4 | float arg 5 |
| 196 | // | XMM3 | float arg 4 |
| 197 | // | XMM2 | float arg 3 |
| 198 | // | XMM1 | float arg 2 |
| 199 | // | XMM0 | float arg 1 |
| 200 | // | Padding | |
| 201 | // | RDI/Method* | <- sp |
Mark Mendell | 3e6a3bf | 2015-01-19 14:09:22 -0500 | [diff] [blame] | 202 | static constexpr bool kSplitPairAcrossRegisterAndStack = false; |
Nicolas Geoffray | 69c15d3 | 2015-01-13 11:42:13 +0000 | [diff] [blame] | 203 | static constexpr bool kAlignPairRegister = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 204 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 205 | static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false; |
Goran Jakovljevic | ff73498 | 2015-08-24 12:58:55 +0000 | [diff] [blame] | 206 | static constexpr bool kQuickSkipOddFpRegisters = false; |
Dmitry Petrochenko | 58994cd | 2014-05-17 01:02:18 +0700 | [diff] [blame] | 207 | static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs. |
Dmitry Petrochenko | 58994cd | 2014-05-17 01:02:18 +0700 | [diff] [blame] | 208 | static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 209 | static constexpr bool kGprFprLockstep = false; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 210 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 211 | switch (gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 212 | case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 213 | case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 214 | case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 215 | case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 216 | case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 217 | default: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 218 | LOG(FATAL) << "Unexpected GPR index: " << gpr_index; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 219 | UNREACHABLE(); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 222 | #else |
| 223 | #error "Unsupported architecture" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 224 | #endif |
| 225 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 226 | public: |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 227 | // Special handling for proxy methods. Proxy methods are instance methods so the |
| 228 | // 'this' object is the 1st argument. They also have the same frame layout as the |
| 229 | // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the |
| 230 | // 1st GPR. |
Roland Levillain | fa854e4 | 2018-02-07 13:09:55 +0000 | [diff] [blame] | 231 | static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 232 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 233 | CHECK((*sp)->IsProxyMethod()); |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 234 | CHECK_GT(kNumQuickGprArgs, 0u); |
| 235 | constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR. |
| 236 | size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset + |
| 237 | GprIndexToGprOffset(kThisGprIndex); |
| 238 | uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset; |
Roland Levillain | fa854e4 | 2018-02-07 13:09:55 +0000 | [diff] [blame] | 239 | return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address); |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 242 | static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 243 | DCHECK((*sp)->IsCalleeSaveMethod()); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 244 | return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs); |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 247 | static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 248 | DCHECK((*sp)->IsCalleeSaveMethod()); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 249 | uint8_t* previous_sp = |
| 250 | reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 251 | return *reinterpret_cast<ArtMethod**>(previous_sp); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 254 | static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 255 | DCHECK((*sp)->IsCalleeSaveMethod()); |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 256 | constexpr size_t callee_frame_size = |
| 257 | RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 258 | ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>( |
| 259 | reinterpret_cast<uintptr_t>(sp) + callee_frame_size); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 260 | uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 261 | const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc); |
| 262 | uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 263 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 264 | if (current_code->IsOptimized()) { |
David Srbecky | 0d4567f | 2019-05-30 22:45:40 +0100 | [diff] [blame] | 265 | CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 266 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 267 | DCHECK(stack_map.IsValid()); |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 268 | BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map); |
| 269 | if (!inline_infos.empty()) { |
| 270 | return inline_infos.back().GetDexPc(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 271 | } else { |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 272 | return stack_map.GetDexPc(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 273 | } |
| 274 | } else { |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 275 | return current_code->ToDexPc(caller_sp, outer_pc); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 276 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 279 | static uint8_t* GetCallingPcAddr(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 280 | DCHECK((*sp)->IsCalleeSaveMethod()); |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 281 | uint8_t* return_adress_spill = |
| 282 | reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset; |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 283 | return return_adress_spill; |
| 284 | } |
| 285 | |
| 286 | // For the given quick ref and args quick frame, return the caller's PC. |
| 287 | static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 288 | return *reinterpret_cast<uintptr_t*>(GetCallingPcAddr(sp)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 291 | QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 292 | uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 293 | is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 294 | gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), |
| 295 | fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), |
| 296 | stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 297 | + sizeof(ArtMethod*)), // Skip ArtMethod*. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 298 | gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0), |
| 299 | cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) { |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 300 | static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0), |
| 301 | "Number of Quick FPR arguments unexpected"); |
| 302 | static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled), |
| 303 | "Double alignment unexpected"); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 304 | // For register alignment, we want to assume that counters(fpr_double_index_) are even if the |
| 305 | // next register is even. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 306 | static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0, |
| 307 | "Number of Quick FPR arguments not even"); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 308 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 309 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 310 | |
| 311 | virtual ~QuickArgumentVisitor() {} |
| 312 | |
| 313 | virtual void Visit() = 0; |
| 314 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 315 | Primitive::Type GetParamPrimitiveType() const { |
| 316 | return cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 319 | uint8_t* GetParamAddress() const { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 320 | if (!kQuickSoftFloatAbi) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 321 | Primitive::Type type = GetParamPrimitiveType(); |
| 322 | if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 323 | if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) { |
| 324 | if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) { |
| 325 | return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); |
| 326 | } |
| 327 | } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 328 | return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 329 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 330 | return stack_args_ + (stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 333 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 334 | return gpr_args_ + GprIndexToGprOffset(gpr_index_); |
| 335 | } |
| 336 | return stack_args_ + (stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | bool IsSplitLongOrDouble() const { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 340 | if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || |
| 341 | (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 342 | return is_split_long_or_double_; |
| 343 | } else { |
| 344 | return false; // An optimization for when GPR and FPRs are 64bit. |
| 345 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 348 | bool IsParamAReference() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 349 | return GetParamPrimitiveType() == Primitive::kPrimNot; |
| 350 | } |
| 351 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 352 | bool IsParamALongOrDouble() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 353 | Primitive::Type type = GetParamPrimitiveType(); |
| 354 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 355 | } |
| 356 | |
| 357 | uint64_t ReadSplitLongParam() const { |
Nicolas Geoffray | 425f239 | 2015-01-08 14:52:29 +0000 | [diff] [blame] | 358 | // The splitted long is always available through the stack. |
| 359 | return *reinterpret_cast<uint64_t*>(stack_args_ |
| 360 | + stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 363 | void IncGprIndex() { |
| 364 | gpr_index_++; |
| 365 | if (kGprFprLockstep) { |
| 366 | fpr_index_++; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void IncFprIndex() { |
| 371 | fpr_index_++; |
| 372 | if (kGprFprLockstep) { |
| 373 | gpr_index_++; |
| 374 | } |
| 375 | } |
| 376 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 377 | void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 378 | // (a) 'stack_args_' should point to the first method's argument |
| 379 | // (b) whatever the argument type it is, the 'stack_index_' should |
| 380 | // be moved forward along with every visiting. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 381 | gpr_index_ = 0; |
| 382 | fpr_index_ = 0; |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 383 | if (kQuickDoubleRegAlignedFloatBackFilled) { |
| 384 | fpr_double_index_ = 0; |
| 385 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 386 | stack_index_ = 0; |
| 387 | if (!is_static_) { // Handle this. |
| 388 | cur_type_ = Primitive::kPrimNot; |
| 389 | is_split_long_or_double_ = false; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 390 | Visit(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 391 | stack_index_++; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 392 | if (kNumQuickGprArgs > 0) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 393 | IncGprIndex(); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 394 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 395 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 396 | for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { |
| 397 | cur_type_ = Primitive::GetType(shorty_[shorty_index]); |
| 398 | switch (cur_type_) { |
| 399 | case Primitive::kPrimNot: |
| 400 | case Primitive::kPrimBoolean: |
| 401 | case Primitive::kPrimByte: |
| 402 | case Primitive::kPrimChar: |
| 403 | case Primitive::kPrimShort: |
| 404 | case Primitive::kPrimInt: |
| 405 | is_split_long_or_double_ = false; |
| 406 | Visit(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 407 | stack_index_++; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 408 | if (gpr_index_ < kNumQuickGprArgs) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 409 | IncGprIndex(); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 410 | } |
| 411 | break; |
| 412 | case Primitive::kPrimFloat: |
| 413 | is_split_long_or_double_ = false; |
| 414 | Visit(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 415 | stack_index_++; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 416 | if (kQuickSoftFloatAbi) { |
| 417 | if (gpr_index_ < kNumQuickGprArgs) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 418 | IncGprIndex(); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 419 | } |
| 420 | } else { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 421 | if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 422 | IncFprIndex(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 423 | if (kQuickDoubleRegAlignedFloatBackFilled) { |
| 424 | // Double should not overlap with float. |
| 425 | // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4. |
| 426 | fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2)); |
| 427 | // Float should not overlap with double. |
| 428 | if (fpr_index_ % 2 == 0) { |
| 429 | fpr_index_ = std::max(fpr_double_index_, fpr_index_); |
| 430 | } |
Goran Jakovljevic | ff73498 | 2015-08-24 12:58:55 +0000 | [diff] [blame] | 431 | } else if (kQuickSkipOddFpRegisters) { |
| 432 | IncFprIndex(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 433 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | break; |
| 437 | case Primitive::kPrimDouble: |
| 438 | case Primitive::kPrimLong: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 439 | if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { |
Alexey Frunze | 1b8464d | 2016-11-12 17:22:05 -0800 | [diff] [blame] | 440 | if (cur_type_ == Primitive::kPrimLong && |
Alexey Frunze | 1b8464d | 2016-11-12 17:22:05 -0800 | [diff] [blame] | 441 | gpr_index_ == 0 && |
Alexey Frunze | 1b8464d | 2016-11-12 17:22:05 -0800 | [diff] [blame] | 442 | kAlignPairRegister) { |
Vladimir Marko | 41b605c | 2020-02-12 10:52:22 +0000 | [diff] [blame] | 443 | // Currently, this is only for ARM, where we align long parameters with |
| 444 | // even-numbered registers by skipping R1 and using R2 instead. |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 445 | IncGprIndex(); |
Nicolas Geoffray | 69c15d3 | 2015-01-13 11:42:13 +0000 | [diff] [blame] | 446 | } |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 447 | is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) && |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 448 | ((gpr_index_ + 1) == kNumQuickGprArgs); |
Mark Mendell | 3e6a3bf | 2015-01-19 14:09:22 -0500 | [diff] [blame] | 449 | if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) { |
| 450 | // We don't want to split this. Pass over this register. |
| 451 | gpr_index_++; |
| 452 | is_split_long_or_double_ = false; |
| 453 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 454 | Visit(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 455 | if (kBytesStackArgLocation == 4) { |
| 456 | stack_index_+= 2; |
| 457 | } else { |
| 458 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 459 | stack_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 460 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 461 | if (gpr_index_ < kNumQuickGprArgs) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 462 | IncGprIndex(); |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 463 | if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 464 | if (gpr_index_ < kNumQuickGprArgs) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 465 | IncGprIndex(); |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 469 | } else { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 470 | is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) && |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 471 | ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 472 | Visit(); |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 473 | if (kBytesStackArgLocation == 4) { |
| 474 | stack_index_+= 2; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 475 | } else { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 476 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 477 | stack_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 478 | } |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 479 | if (kQuickDoubleRegAlignedFloatBackFilled) { |
| 480 | if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) { |
| 481 | fpr_double_index_ += 2; |
| 482 | // Float should not overlap with double. |
| 483 | if (fpr_index_ % 2 == 0) { |
| 484 | fpr_index_ = std::max(fpr_double_index_, fpr_index_); |
| 485 | } |
| 486 | } |
| 487 | } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 488 | IncFprIndex(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 489 | if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) { |
| 490 | if (fpr_index_ + 1 < kNumQuickFprArgs + 1) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 491 | IncFprIndex(); |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 495 | } |
| 496 | break; |
| 497 | default: |
| 498 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; |
| 499 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 503 | protected: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 504 | const bool is_static_; |
| 505 | const char* const shorty_; |
| 506 | const uint32_t shorty_len_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 507 | |
| 508 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 509 | uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame. |
| 510 | uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame. |
| 511 | uint8_t* const stack_args_; // Address of stack arguments in caller's frame. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 512 | uint32_t gpr_index_; // Index into spilled GPRs. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 513 | // Index into spilled FPRs. |
| 514 | // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_ |
| 515 | // holds a higher register number. |
| 516 | uint32_t fpr_index_; |
| 517 | // Index into spilled FPRs for aligned double. |
| 518 | // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in |
| 519 | // terms of singles, may be behind fpr_index. |
| 520 | uint32_t fpr_double_index_; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 521 | uint32_t stack_index_; // Index into arguments on the stack. |
| 522 | // The current type of argument during VisitArguments. |
| 523 | Primitive::Type cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 524 | // Does a 64bit parameter straddle the register and stack arguments? |
| 525 | bool is_split_long_or_double_; |
| 526 | }; |
| 527 | |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 528 | // Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It |
| 529 | // allows to use the QuickArgumentVisitor constants without moving all the code in its own module. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 530 | extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 531 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Roland Levillain | fa854e4 | 2018-02-07 13:09:55 +0000 | [diff] [blame] | 532 | return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr(); |
| 533 | } |
Sebastien Hertz | a836bc9 | 2014-11-25 16:30:53 +0100 | [diff] [blame] | 534 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 535 | // Visits arguments on the stack placing them into the shadow frame. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 536 | class BuildQuickShadowFrameVisitor final : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 537 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 538 | BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty, |
| 539 | uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 540 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 541 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 542 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 543 | |
| 544 | private: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 545 | ShadowFrame* const sf_; |
| 546 | uint32_t cur_reg_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 547 | |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 548 | DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 549 | }; |
| 550 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 551 | void BuildQuickShadowFrameVisitor::Visit() { |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 552 | Primitive::Type type = GetParamPrimitiveType(); |
| 553 | switch (type) { |
| 554 | case Primitive::kPrimLong: // Fall-through. |
| 555 | case Primitive::kPrimDouble: |
| 556 | if (IsSplitLongOrDouble()) { |
| 557 | sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); |
| 558 | } else { |
| 559 | sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); |
| 560 | } |
| 561 | ++cur_reg_; |
| 562 | break; |
| 563 | case Primitive::kPrimNot: { |
| 564 | StackReference<mirror::Object>* stack_ref = |
| 565 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 566 | sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); |
| 567 | } |
| 568 | break; |
| 569 | case Primitive::kPrimBoolean: // Fall-through. |
| 570 | case Primitive::kPrimByte: // Fall-through. |
| 571 | case Primitive::kPrimChar: // Fall-through. |
| 572 | case Primitive::kPrimShort: // Fall-through. |
| 573 | case Primitive::kPrimInt: // Fall-through. |
| 574 | case Primitive::kPrimFloat: |
| 575 | sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); |
| 576 | break; |
| 577 | case Primitive::kPrimVoid: |
| 578 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 579 | UNREACHABLE(); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 580 | } |
| 581 | ++cur_reg_; |
| 582 | } |
| 583 | |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 584 | // Don't inline. See b/65159206. |
| 585 | NO_INLINE |
| 586 | static void HandleDeoptimization(JValue* result, |
| 587 | ArtMethod* method, |
| 588 | ShadowFrame* deopt_frame, |
| 589 | ManagedStack* fragment) |
| 590 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 591 | // Coming from partial-fragment deopt. |
| 592 | Thread* self = Thread::Current(); |
| 593 | if (kIsDebugBuild) { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 594 | // Consistency-check: are the methods as expected? We check that the last shadow frame |
| 595 | // (the bottom of the call-stack) corresponds to the called method. |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 596 | ShadowFrame* linked = deopt_frame; |
| 597 | while (linked->GetLink() != nullptr) { |
| 598 | linked = linked->GetLink(); |
| 599 | } |
| 600 | CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " " |
| 601 | << ArtMethod::PrettyMethod(linked->GetMethod()); |
| 602 | } |
| 603 | |
| 604 | if (VLOG_IS_ON(deopt)) { |
| 605 | // Print out the stack to verify that it was a partial-fragment deopt. |
| 606 | LOG(INFO) << "Continue-ing from deopt. Stack is:"; |
| 607 | QuickExceptionHandler::DumpFramesWithType(self, true); |
| 608 | } |
| 609 | |
| 610 | ObjPtr<mirror::Throwable> pending_exception; |
| 611 | bool from_code = false; |
| 612 | DeoptimizationMethodType method_type; |
| 613 | self->PopDeoptimizationContext(/* out */ result, |
| 614 | /* out */ &pending_exception, |
| 615 | /* out */ &from_code, |
| 616 | /* out */ &method_type); |
| 617 | |
| 618 | // Push a transition back into managed code onto the linked list in thread. |
| 619 | self->PushManagedStackFragment(fragment); |
| 620 | |
| 621 | // Ensure that the stack is still in order. |
| 622 | if (kIsDebugBuild) { |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 623 | class EntireStackVisitor : public StackVisitor { |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 624 | public: |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 625 | explicit EntireStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_) |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 626 | : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} |
| 627 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 628 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 629 | // Nothing to do here. In a debug build, ValidateFrame will do the work in the walking |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 630 | // logic. Just always say we want to continue. |
| 631 | return true; |
| 632 | } |
| 633 | }; |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 634 | EntireStackVisitor esv(self); |
| 635 | esv.WalkStack(); |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // Restore the exception that was pending before deoptimization then interpret the |
| 639 | // deoptimized frames. |
| 640 | if (pending_exception != nullptr) { |
| 641 | self->SetException(pending_exception); |
| 642 | } |
| 643 | interpreter::EnterInterpreterFromDeoptimize(self, |
| 644 | deopt_frame, |
| 645 | result, |
| 646 | from_code, |
Nicolas Geoffray | 5f77889 | 2021-11-26 14:51:50 +0000 | [diff] [blame] | 647 | method_type); |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 650 | extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 651 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 652 | // Ensure we don't get thread suspension until the object arguments are safely in the shadow |
| 653 | // frame. |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 654 | ScopedQuickEntrypointChecks sqec(self); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 655 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 656 | if (UNLIKELY(!method->IsInvokable())) { |
| 657 | method->ThrowInvocationTimeError(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 658 | return 0; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | JValue tmp_value; |
| 662 | ShadowFrame* deopt_frame = self->PopStackedShadowFrame( |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 663 | StackedShadowFrameType::kDeoptimizationShadowFrame, false); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 664 | ManagedStack fragment; |
| 665 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 666 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 667 | uint32_t shorty_len = 0; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 668 | ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 669 | DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod(); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 670 | CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData()); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 671 | const char* shorty = non_proxy_method->GetShorty(&shorty_len); |
| 672 | |
| 673 | JValue result; |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 674 | bool force_frame_pop = false; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 675 | |
Mingyao Yang | 417528d | 2017-09-13 12:10:40 -0700 | [diff] [blame] | 676 | if (UNLIKELY(deopt_frame != nullptr)) { |
| 677 | HandleDeoptimization(&result, method, deopt_frame, &fragment); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 678 | } else { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 679 | const char* old_cause = self->StartAssertNoThreadSuspension( |
| 680 | "Building interpreter shadow frame"); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 681 | uint16_t num_regs = accessor.RegistersSize(); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 682 | // No last shadow coming from quick. |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 683 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 684 | CREATE_SHADOW_FRAME(num_regs, /* link= */ nullptr, method, /* dex_pc= */ 0); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 685 | ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get(); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 686 | size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 687 | BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len, |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 688 | shadow_frame, first_arg_reg); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 689 | shadow_frame_builder.VisitArguments(); |
| 690 | // Push a transition back into managed code onto the linked list in thread. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 691 | self->PushManagedStackFragment(&fragment); |
| 692 | self->PushShadowFrame(shadow_frame); |
| 693 | self->EndAssertNoThreadSuspension(old_cause); |
| 694 | |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 695 | if (NeedsClinitCheckBeforeCall(method)) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 696 | ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass(); |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 697 | if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 698 | // Ensure static method's class is initialized. |
| 699 | StackHandleScope<1> hs(self); |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 700 | Handle<mirror::Class> h_class(hs.NewHandle(declaring_class)); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 701 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 702 | DCHECK(Thread::Current()->IsExceptionPending()) << method->PrettyMethod(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 703 | self->PopManagedStackFragment(fragment); |
| 704 | return 0; |
| 705 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 708 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 709 | result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame); |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 710 | force_frame_pop = shadow_frame->GetForcePopFrame(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 711 | } |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 712 | |
| 713 | // Pop transition. |
| 714 | self->PopManagedStackFragment(fragment); |
| 715 | |
| 716 | // Request a stack deoptimization if needed |
| 717 | ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 718 | uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp); |
Mingyao Yang | a3549d2 | 2016-06-02 17:01:02 -0700 | [diff] [blame] | 719 | // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 720 | // should be done and it knows the real return pc. NB If the upcall is null we don't need to do |
| 721 | // anything. This can happen during shutdown or early startup. |
| 722 | if (UNLIKELY( |
| 723 | caller != nullptr && |
| 724 | caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) && |
| 725 | (self->IsForceInterpreter() || Dbg::IsForcedInterpreterNeededForUpcall(self, caller)))) { |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 726 | if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) { |
| 727 | LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method " |
| 728 | << caller->PrettyMethod(); |
| 729 | } else { |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 730 | VLOG(deopt) << "Forcing deoptimization on return from method " << method->PrettyMethod() |
| 731 | << " to " << caller->PrettyMethod() |
| 732 | << (force_frame_pop ? " for frame-pop" : ""); |
| 733 | DCHECK(!force_frame_pop || result.GetJ() == 0) << "Force frame pop should have no result."; |
| 734 | if (force_frame_pop && self->GetException() != nullptr) { |
| 735 | LOG(WARNING) << "Suppressing exception for instruction-retry: " |
| 736 | << self->GetException()->Dump(); |
| 737 | } |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 738 | // Push the context of the deoptimization stack so we can restore the return value and the |
| 739 | // exception before executing the deoptimized frames. |
| 740 | self->PushDeoptimizationContext( |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 741 | result, |
| 742 | shorty[0] == 'L' || shorty[0] == '[', /* class or array */ |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 743 | force_frame_pop ? nullptr : self->GetException(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 744 | /* from_code= */ false, |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 745 | DeoptimizationMethodType::kDefault); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 746 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 747 | // Set special exception to cause deoptimization. |
| 748 | self->SetException(Thread::GetDeoptimizationException()); |
| 749 | } |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // No need to restore the args since the method has already been run by the interpreter. |
| 753 | return result.GetJ(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Visits arguments on the stack placing them into the args vector, Object* arguments are converted |
| 757 | // to jobjects. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 758 | class BuildQuickArgumentVisitor final : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 759 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 760 | BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 761 | ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 762 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 763 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 764 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 765 | |
| 766 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 767 | ScopedObjectAccessUnchecked* const soa_; |
| 768 | std::vector<jvalue>* const args_; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 769 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 770 | DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); |
| 771 | }; |
| 772 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 773 | void BuildQuickArgumentVisitor::Visit() { |
| 774 | jvalue val; |
| 775 | Primitive::Type type = GetParamPrimitiveType(); |
| 776 | switch (type) { |
| 777 | case Primitive::kPrimNot: { |
| 778 | StackReference<mirror::Object>* stack_ref = |
| 779 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 780 | val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | case Primitive::kPrimLong: // Fall-through. |
| 784 | case Primitive::kPrimDouble: |
| 785 | if (IsSplitLongOrDouble()) { |
| 786 | val.j = ReadSplitLongParam(); |
| 787 | } else { |
| 788 | val.j = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 789 | } |
| 790 | break; |
| 791 | case Primitive::kPrimBoolean: // Fall-through. |
| 792 | case Primitive::kPrimByte: // Fall-through. |
| 793 | case Primitive::kPrimChar: // Fall-through. |
| 794 | case Primitive::kPrimShort: // Fall-through. |
| 795 | case Primitive::kPrimInt: // Fall-through. |
| 796 | case Primitive::kPrimFloat: |
| 797 | val.i = *reinterpret_cast<jint*>(GetParamAddress()); |
| 798 | break; |
| 799 | case Primitive::kPrimVoid: |
| 800 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 801 | UNREACHABLE(); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 802 | } |
| 803 | args_->push_back(val); |
| 804 | } |
| 805 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 806 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 807 | // which is responsible for recording callee save registers. We explicitly place into jobjects the |
| 808 | // incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a |
| 809 | // field within the proxy object, which will box the primitive arguments and deal with error cases. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 810 | extern "C" uint64_t artQuickProxyInvokeHandler( |
| 811 | ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 812 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 813 | DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod(); |
| 814 | DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 815 | // Ensure we don't get thread suspension until the object arguments are safely in jobjects. |
| 816 | const char* old_cause = |
| 817 | self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); |
| 818 | // Register the top of the managed stack, making stack crawlable. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 819 | DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 820 | self->VerifyStack(); |
| 821 | // Start new JNI local reference state. |
| 822 | JNIEnvExt* env = self->GetJniEnv(); |
| 823 | ScopedObjectAccessUnchecked soa(env); |
| 824 | ScopedJniEnvLocalRefState env_state(env); |
| 825 | // Create local ref. copies of proxy method and the receiver. |
| 826 | jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); |
| 827 | |
| 828 | // Placing arguments into args vector and remove the receiver. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 829 | ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 830 | CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " " |
| 831 | << non_proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 832 | std::vector<jvalue> args; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 833 | uint32_t shorty_len = 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 834 | const char* shorty = non_proxy_method->GetShorty(&shorty_len); |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 835 | BuildQuickArgumentVisitor local_ref_visitor( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 836 | sp, /* is_static= */ false, shorty, shorty_len, &soa, &args); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 837 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 838 | local_ref_visitor.VisitArguments(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 839 | DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 840 | args.erase(args.begin()); |
| 841 | |
| 842 | // Convert proxy method into expected interface method. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 843 | ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 844 | DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod(); |
| 845 | DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 846 | self->EndAssertNoThreadSuspension(old_cause); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 847 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 848 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Andreas Gampe | ee29a07 | 2017-11-02 15:28:09 -0700 | [diff] [blame] | 849 | ObjPtr<mirror::Method> interface_reflect_method = |
Vladimir Marko | b6f4c79 | 2020-05-04 15:37:29 +0100 | [diff] [blame] | 850 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), interface_method); |
Andreas Gampe | ee29a07 | 2017-11-02 15:28:09 -0700 | [diff] [blame] | 851 | if (interface_reflect_method == nullptr) { |
| 852 | soa.Self()->AssertPendingOOMException(); |
| 853 | return 0; |
| 854 | } |
| 855 | jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 856 | |
| 857 | // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 858 | // that performs allocations or instrumentation events. |
| 859 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
| 860 | if (instr->HasMethodEntryListeners()) { |
Mythri Alle | 9cc65df | 2021-09-21 15:09:58 +0000 | [diff] [blame] | 861 | instr->MethodEnterEvent(soa.Self(), proxy_method); |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 862 | if (soa.Self()->IsExceptionPending()) { |
| 863 | instr->MethodUnwindEvent(self, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 864 | soa.Decode<mirror::Object>(rcvr_jobj), |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 865 | proxy_method, |
| 866 | 0); |
| 867 | return 0; |
| 868 | } |
| 869 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 870 | JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args); |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 871 | if (soa.Self()->IsExceptionPending()) { |
| 872 | if (instr->HasMethodUnwindListeners()) { |
| 873 | instr->MethodUnwindEvent(self, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 874 | soa.Decode<mirror::Object>(rcvr_jobj), |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 875 | proxy_method, |
| 876 | 0); |
| 877 | } |
| 878 | } else if (instr->HasMethodExitListeners()) { |
| 879 | instr->MethodExitEvent(self, |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 880 | proxy_method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 881 | {}, |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 882 | result); |
| 883 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 884 | return result.GetJ(); |
| 885 | } |
| 886 | |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 887 | // Visitor returning a reference argument at a given position in a Quick stack frame. |
| 888 | // NOTE: Only used for testing purposes. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 889 | class GetQuickReferenceArgumentAtVisitor final : public QuickArgumentVisitor { |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 890 | public: |
| 891 | GetQuickReferenceArgumentAtVisitor(ArtMethod** sp, |
| 892 | const char* shorty, |
| 893 | uint32_t shorty_len, |
| 894 | size_t arg_pos) |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 895 | : QuickArgumentVisitor(sp, /* is_static= */ false, shorty, shorty_len), |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 896 | cur_pos_(0u), |
| 897 | arg_pos_(arg_pos), |
| 898 | ref_arg_(nullptr) { |
| 899 | CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments"; |
| 900 | } |
| 901 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 902 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override { |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 903 | if (cur_pos_ == arg_pos_) { |
| 904 | Primitive::Type type = GetParamPrimitiveType(); |
| 905 | CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference"; |
| 906 | ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 907 | } |
| 908 | ++cur_pos_; |
| 909 | } |
| 910 | |
| 911 | StackReference<mirror::Object>* GetReferenceArgument() { |
| 912 | return ref_arg_; |
| 913 | } |
| 914 | |
| 915 | private: |
| 916 | // The position of the currently visited argument. |
| 917 | size_t cur_pos_; |
| 918 | // The position of the searched argument. |
| 919 | const size_t arg_pos_; |
| 920 | // The reference argument, if found. |
| 921 | StackReference<mirror::Object>* ref_arg_; |
| 922 | |
| 923 | DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor); |
| 924 | }; |
| 925 | |
| 926 | // Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`. |
| 927 | // NOTE: Only used for testing purposes. |
| 928 | extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos, |
| 929 | ArtMethod** sp) |
| 930 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 931 | ArtMethod* proxy_method = *sp; |
| 932 | ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 933 | CHECK(!non_proxy_method->IsStatic()) |
| 934 | << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod(); |
| 935 | uint32_t shorty_len = 0; |
| 936 | const char* shorty = non_proxy_method->GetShorty(&shorty_len); |
| 937 | GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos); |
| 938 | ref_arg_visitor.VisitArguments(); |
| 939 | StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument(); |
| 940 | return ref_arg; |
| 941 | } |
| 942 | |
| 943 | // Visitor returning all the reference arguments in a Quick stack frame. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 944 | class GetQuickReferenceArgumentsVisitor final : public QuickArgumentVisitor { |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 945 | public: |
| 946 | GetQuickReferenceArgumentsVisitor(ArtMethod** sp, |
| 947 | bool is_static, |
| 948 | const char* shorty, |
| 949 | uint32_t shorty_len) |
| 950 | : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {} |
| 951 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 952 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override { |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 953 | Primitive::Type type = GetParamPrimitiveType(); |
| 954 | if (type == Primitive::kPrimNot) { |
| 955 | StackReference<mirror::Object>* ref_arg = |
| 956 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 957 | ref_args_.push_back(ref_arg); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | std::vector<StackReference<mirror::Object>*> GetReferenceArguments() { |
| 962 | return ref_args_; |
| 963 | } |
| 964 | |
| 965 | private: |
| 966 | // The reference arguments. |
| 967 | std::vector<StackReference<mirror::Object>*> ref_args_; |
| 968 | |
| 969 | DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor); |
| 970 | }; |
| 971 | |
| 972 | // Returning all reference arguments in Quick stack frame at address `sp`. |
| 973 | std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp) |
| 974 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 975 | ArtMethod* proxy_method = *sp; |
| 976 | ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 977 | CHECK(!non_proxy_method->IsStatic()) |
| 978 | << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod(); |
| 979 | uint32_t shorty_len = 0; |
| 980 | const char* shorty = non_proxy_method->GetShorty(&shorty_len); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 981 | GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /*is_static=*/ false, shorty, shorty_len); |
Roland Levillain | ad0777d | 2018-02-12 20:00:18 +0000 | [diff] [blame] | 982 | ref_args_visitor.VisitArguments(); |
| 983 | std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments(); |
| 984 | return ref_args; |
| 985 | } |
| 986 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 987 | // Read object references held in arguments from quick frames and place in a JNI local references, |
| 988 | // so they don't get garbage collected. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 989 | class RememberForGcArgumentVisitor final : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 990 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 991 | RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, |
| 992 | uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 993 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 994 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 995 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override; |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 996 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 997 | void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 998 | |
| 999 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1000 | ScopedObjectAccessUnchecked* const soa_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 1001 | // References which we must update when exiting in case the GC moved the objects. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1002 | std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; |
| 1003 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1004 | DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1005 | }; |
| 1006 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1007 | void RememberForGcArgumentVisitor::Visit() { |
| 1008 | if (IsParamAReference()) { |
| 1009 | StackReference<mirror::Object>* stack_ref = |
| 1010 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 1011 | jobject reference = |
| 1012 | soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 1013 | references_.push_back(std::make_pair(reference, stack_ref)); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | void RememberForGcArgumentVisitor::FixupReferences() { |
| 1018 | // Fixup any references which may have changed. |
| 1019 | for (const auto& pair : references_) { |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 1020 | pair.second->Assign(soa_->Decode<mirror::Object>(pair.first)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1021 | soa_->Env()->DeleteLocalRef(pair.first); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1025 | extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method, |
| 1026 | mirror::Object* this_object, |
| 1027 | Thread* self, |
| 1028 | ArtMethod** sp) |
| 1029 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1030 | const void* result; |
| 1031 | // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip |
| 1032 | // that part. |
| 1033 | ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false); |
| 1034 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Alex Light | 6cae5ea | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 1035 | DCHECK(!method->IsProxyMethod()) |
| 1036 | << "Proxy method " << method->PrettyMethod() |
| 1037 | << " (declaring class: " << method->GetDeclaringClass()->PrettyClass() << ")" |
| 1038 | << " should not hit instrumentation entrypoint."; |
Nicolas Geoffray | c25a9f9 | 2021-12-13 17:22:43 +0000 | [diff] [blame^] | 1039 | DCHECK(!instrumentation->IsDeoptimized(method)); |
| 1040 | // This will get the entry point either from the oat file, the JIT or the appropriate bridge |
| 1041 | // method if none of those can be found. |
| 1042 | result = instrumentation->GetCodeForInvoke(method); |
| 1043 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 1044 | DCHECK_NE(result, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod(); |
| 1045 | DCHECK(jit == nullptr || |
| 1046 | // Native methods come through here in Interpreter entrypoints. We might not have |
| 1047 | // disabled jit-gc but that is fine since we won't return jit-code for native methods. |
| 1048 | method->IsNative() || |
| 1049 | !jit->GetCodeCache()->GetGarbageCollectCode()); |
| 1050 | DCHECK(!method->IsNative() || |
| 1051 | jit == nullptr || |
| 1052 | !jit->GetCodeCache()->ContainsPc(result)) |
| 1053 | << method->PrettyMethod() << " code will jump to possibly cleaned up jit code!"; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1054 | |
| 1055 | bool interpreter_entry = (result == GetQuickToInterpreterBridge()); |
| 1056 | bool is_static = method->IsStatic(); |
| 1057 | uint32_t shorty_len; |
| 1058 | const char* shorty = |
| 1059 | method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len); |
| 1060 | |
| 1061 | ScopedObjectAccessUnchecked soa(self); |
| 1062 | RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa); |
| 1063 | visitor.VisitArguments(); |
| 1064 | |
Nicolas Geoffray | c71fe16 | 2021-12-09 14:02:48 +0000 | [diff] [blame] | 1065 | StackHandleScope<2> hs(self); |
| 1066 | Handle<mirror::Object> h_object(hs.NewHandle(is_static ? nullptr : this_object)); |
| 1067 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1068 | |
| 1069 | // Ensure that the called method's class is initialized. |
| 1070 | if (NeedsClinitCheckBeforeCall(method) && !h_class->IsVisiblyInitialized()) { |
| 1071 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
| 1072 | visitor.FixupReferences(); |
| 1073 | DCHECK(self->IsExceptionPending()); |
| 1074 | return nullptr; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1078 | instrumentation->PushInstrumentationStackFrame(self, |
Nicolas Geoffray | c71fe16 | 2021-12-09 14:02:48 +0000 | [diff] [blame] | 1079 | is_static ? nullptr : h_object.Get(), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1080 | method, |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 1081 | reinterpret_cast<uintptr_t>( |
| 1082 | QuickArgumentVisitor::GetCallingPcAddr(sp)), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1083 | QuickArgumentVisitor::GetCallingPc(sp), |
| 1084 | interpreter_entry); |
| 1085 | |
| 1086 | visitor.FixupReferences(); |
| 1087 | if (UNLIKELY(self->IsExceptionPending())) { |
| 1088 | return nullptr; |
| 1089 | } |
| 1090 | CHECK(result != nullptr) << method->PrettyMethod(); |
| 1091 | return result; |
| 1092 | } |
| 1093 | |
| 1094 | extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self, |
| 1095 | ArtMethod** sp, |
| 1096 | uint64_t* gpr_result, |
| 1097 | uint64_t* fpr_result) |
| 1098 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1099 | DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current())); |
| 1100 | CHECK(gpr_result != nullptr); |
| 1101 | CHECK(fpr_result != nullptr); |
| 1102 | // Instrumentation exit stub must not be entered with a pending exception. |
| 1103 | CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception " |
| 1104 | << self->GetException()->Dump(); |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1105 | // Compute address of return PC and check that it currently holds 0. |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 1106 | constexpr size_t return_pc_offset = |
| 1107 | RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything); |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 1108 | uintptr_t* return_pc_addr = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + |
| 1109 | return_pc_offset); |
| 1110 | CHECK_EQ(*return_pc_addr, 0U); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1111 | |
| 1112 | // Pop the frame filling in the return pc. The low half of the return value is 0 when |
| 1113 | // deoptimization shouldn't be performed with the high-half having the return address. When |
| 1114 | // deoptimization should be performed the low half is zero and the high-half the address of the |
| 1115 | // deoptimization entry point. |
| 1116 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 1117 | TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame( |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 1118 | self, return_pc_addr, gpr_result, fpr_result); |
Vladimir Marko | fac2178 | 2018-03-13 17:01:09 +0000 | [diff] [blame] | 1119 | if (self->IsExceptionPending() || self->ObserveAsyncException()) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1120 | return GetTwoWordFailureValue(); |
| 1121 | } |
| 1122 | return return_or_deoptimize_pc; |
| 1123 | } |
| 1124 | |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1125 | static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc) |
| 1126 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1127 | if (dex_pc == static_cast<uint32_t>(-1)) { |
| 1128 | CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt)); |
| 1129 | return "<native>"; |
| 1130 | } else { |
| 1131 | CodeItemInstructionAccessor accessor = method->DexInstructions(); |
| 1132 | CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits()); |
| 1133 | return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile()); |
| 1134 | } |
| 1135 | } |
| 1136 | |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 1137 | static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass) |
| 1138 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1139 | std::string storage; |
| 1140 | const char* descriptor = klass->GetDescriptor(&storage); |
| 1141 | LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor); |
| 1142 | const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile(); |
| 1143 | if (oat_dex_file != nullptr) { |
| 1144 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 1145 | const char* dex2oat_cmdline = |
| 1146 | oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey); |
| 1147 | LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation() |
| 1148 | << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>"); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1152 | static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1153 | // Mimick the search for the caller and dump some data while doing so. |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 1154 | LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240."; |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1155 | |
| 1156 | constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs; |
| 1157 | CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type)); |
| 1158 | |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 1159 | constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1160 | auto** caller_sp = reinterpret_cast<ArtMethod**>( |
| 1161 | reinterpret_cast<uintptr_t>(sp) + callee_frame_size); |
Vladimir Marko | d3083dd | 2018-05-17 08:43:47 +0100 | [diff] [blame] | 1162 | constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1163 | uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>( |
| 1164 | (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset)); |
| 1165 | ArtMethod* outer_method = *caller_sp; |
| 1166 | |
| 1167 | if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) { |
| 1168 | LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod() |
| 1169 | << " native pc: " << caller_pc << " Instrumented!"; |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc); |
| 1174 | CHECK(current_code != nullptr); |
| 1175 | CHECK(current_code->IsOptimized()); |
| 1176 | uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1177 | CodeInfo code_info(current_code); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1178 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1179 | CHECK(stack_map.IsValid()); |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 1180 | uint32_t dex_pc = stack_map.GetDexPc(); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Log the outer method and its associated dex file and class table pointer which can be used |
| 1183 | // to find out if the inlined methods were defined by other dex file(s) or class loader(s). |
| 1184 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1185 | LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod() |
| 1186 | << " native pc: " << caller_pc |
| 1187 | << " dex pc: " << dex_pc |
| 1188 | << " dex file: " << outer_method->GetDexFile()->GetLocation() |
| 1189 | << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader()); |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 1190 | DumpB74410240ClassData(outer_method->GetDeclaringClass()); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1191 | LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc); |
| 1192 | |
| 1193 | ArtMethod* caller = outer_method; |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 1194 | BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map); |
| 1195 | for (InlineInfo inline_info : inline_infos) { |
| 1196 | const char* tag = ""; |
| 1197 | dex_pc = inline_info.GetDexPc(); |
| 1198 | if (inline_info.EncodesArtMethod()) { |
| 1199 | tag = "encoded "; |
| 1200 | caller = inline_info.GetArtMethod(); |
| 1201 | } else { |
David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 1202 | uint32_t method_index = code_info.GetMethodIndexOf(inline_info); |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 1203 | if (dex_pc == static_cast<uint32_t>(-1)) { |
| 1204 | tag = "special "; |
| 1205 | CHECK(inline_info.Equals(inline_infos.back())); |
| 1206 | caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt); |
| 1207 | CHECK_EQ(caller->GetDexMethodIndex(), method_index); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1208 | } else { |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 1209 | ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache(); |
| 1210 | ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader(); |
| 1211 | caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader); |
| 1212 | CHECK(caller != nullptr); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1213 | } |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1214 | } |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 1215 | LOG(FATAL_WITHOUT_ABORT) << "InlineInfo #" << inline_info.Row() |
| 1216 | << ": " << tag << caller->PrettyMethod() |
| 1217 | << " dex pc: " << dex_pc |
| 1218 | << " dex file: " << caller->GetDexFile()->GetLocation() |
| 1219 | << " class table: " |
| 1220 | << class_linker->ClassTableForClassLoader(caller->GetClassLoader()); |
| 1221 | DumpB74410240ClassData(caller->GetDeclaringClass()); |
| 1222 | LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc); |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1226 | // Lazily resolve a method for quick. Called by stub code. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1227 | extern "C" const void* artQuickResolutionTrampoline( |
| 1228 | ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1229 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 1230 | // The resolution trampoline stashes the resolved method into the callee-save frame to transport |
| 1231 | // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely |
| 1232 | // does not have the same stack layout as the callee-save method). |
| 1233 | ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1234 | // Start new JNI local reference state |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1235 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1236 | ScopedObjectAccessUnchecked soa(env); |
| 1237 | ScopedJniEnvLocalRefState env_state(env); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1238 | const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1239 | |
| 1240 | // Compute details about the called method (avoid GCs) |
| 1241 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1242 | InvokeType invoke_type; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1243 | MethodReference called_method(nullptr, 0); |
| 1244 | const bool called_method_known_on_entry = !called->IsRuntimeMethod(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1245 | ArtMethod* caller = nullptr; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1246 | if (!called_method_known_on_entry) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 1247 | caller = QuickArgumentVisitor::GetCallingMethod(sp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1248 | called_method.dex_file = caller->GetDexFile(); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1249 | |
David Srbecky | 2c76257 | 2018-06-27 10:09:11 +0100 | [diff] [blame] | 1250 | { |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1251 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1252 | CodeItemInstructionAccessor accessor(caller->DexInstructions()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1253 | CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits()); |
| 1254 | const Instruction& instr = accessor.InstructionAt(dex_pc); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 1255 | Instruction::Code instr_code = instr.Opcode(); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1256 | bool is_range; |
| 1257 | switch (instr_code) { |
| 1258 | case Instruction::INVOKE_DIRECT: |
| 1259 | invoke_type = kDirect; |
| 1260 | is_range = false; |
| 1261 | break; |
| 1262 | case Instruction::INVOKE_DIRECT_RANGE: |
| 1263 | invoke_type = kDirect; |
| 1264 | is_range = true; |
| 1265 | break; |
| 1266 | case Instruction::INVOKE_STATIC: |
| 1267 | invoke_type = kStatic; |
| 1268 | is_range = false; |
| 1269 | break; |
| 1270 | case Instruction::INVOKE_STATIC_RANGE: |
| 1271 | invoke_type = kStatic; |
| 1272 | is_range = true; |
| 1273 | break; |
| 1274 | case Instruction::INVOKE_SUPER: |
| 1275 | invoke_type = kSuper; |
| 1276 | is_range = false; |
| 1277 | break; |
| 1278 | case Instruction::INVOKE_SUPER_RANGE: |
| 1279 | invoke_type = kSuper; |
| 1280 | is_range = true; |
| 1281 | break; |
| 1282 | case Instruction::INVOKE_VIRTUAL: |
| 1283 | invoke_type = kVirtual; |
| 1284 | is_range = false; |
| 1285 | break; |
| 1286 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 1287 | invoke_type = kVirtual; |
| 1288 | is_range = true; |
| 1289 | break; |
| 1290 | case Instruction::INVOKE_INTERFACE: |
| 1291 | invoke_type = kInterface; |
| 1292 | is_range = false; |
| 1293 | break; |
| 1294 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 1295 | invoke_type = kInterface; |
| 1296 | is_range = true; |
| 1297 | break; |
| 1298 | default: |
Vladimir Marko | 5b4b9a0 | 2018-03-16 09:42:09 +0000 | [diff] [blame] | 1299 | DumpB74410240DebugData(sp); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 1300 | LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1301 | UNREACHABLE(); |
| 1302 | } |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 1303 | called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c(); |
David Srbecky | 2c76257 | 2018-06-27 10:09:11 +0100 | [diff] [blame] | 1304 | VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " " |
| 1305 | << called_method.index; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1306 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1307 | } else { |
| 1308 | invoke_type = kStatic; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1309 | called_method.dex_file = called->GetDexFile(); |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1310 | called_method.index = called->GetDexMethodIndex(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1311 | } |
| 1312 | uint32_t shorty_len; |
| 1313 | const char* shorty = |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1314 | called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1315 | RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1316 | visitor.VisitArguments(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1317 | self->EndAssertNoThreadSuspension(old_cause); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1318 | const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1319 | // Resolve method filling in dex cache. |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1320 | if (!called_method_known_on_entry) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1321 | StackHandleScope<1> hs(self); |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 1322 | mirror::Object* fake_receiver = nullptr; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1323 | HandleWrapper<mirror::Object> h_receiver( |
Orion Hodson | 6aaa49d | 2020-07-28 15:53:04 +0100 | [diff] [blame] | 1324 | hs.NewHandleWrapper(virtual_or_interface ? &receiver : &fake_receiver)); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1325 | DCHECK_EQ(caller->GetDexFile(), called_method.dex_file); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1326 | called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1327 | self, called_method.index, caller, invoke_type); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1328 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1329 | const void* code = nullptr; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1330 | if (LIKELY(!self->IsExceptionPending())) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1331 | // Incompatible class change should have been handled in resolve method. |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1332 | CHECK(!called->CheckIncompatibleClassChange(invoke_type)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1333 | << called->PrettyMethod() << " " << invoke_type; |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1334 | if (virtual_or_interface || invoke_type == kSuper) { |
| 1335 | // Refine called method based on receiver for kVirtual/kInterface, and |
| 1336 | // caller for kSuper. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1337 | ArtMethod* orig_called = called; |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 1338 | if (invoke_type == kVirtual) { |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1339 | CHECK(receiver != nullptr) << invoke_type; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1340 | called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1341 | } else if (invoke_type == kInterface) { |
| 1342 | CHECK(receiver != nullptr) << invoke_type; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1343 | called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1344 | } else { |
| 1345 | DCHECK_EQ(invoke_type, kSuper); |
| 1346 | CHECK(caller != nullptr) << invoke_type; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 1347 | ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1348 | caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1349 | if (ref_class->IsInterface()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1350 | called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1351 | } else { |
| 1352 | called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1353 | called->GetMethodIndex(), kRuntimePointerSize); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1354 | } |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 1355 | } |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 1356 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1357 | CHECK(called != nullptr) << orig_called->PrettyMethod() << " " |
| 1358 | << mirror::Object::PrettyTypeOf(receiver) << " " |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 1359 | << invoke_type << " " << orig_called->GetVtableIndex(); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 1360 | } |
Nicolas Geoffray | 1de6f01 | 2020-09-23 13:28:00 +0100 | [diff] [blame] | 1361 | // Now that we know the actual target, update .bss entry in oat file, if |
| 1362 | // any. |
| 1363 | if (!called_method_known_on_entry) { |
| 1364 | // We only put non copied methods in the BSS. Putting a copy can lead to an |
| 1365 | // odd situation where the ArtMethod being executed is unrelated to the |
| 1366 | // receiver of the method. |
| 1367 | called = called->GetCanonicalMethod(); |
Nicolas Geoffray | ec06809 | 2021-05-10 17:28:32 +0000 | [diff] [blame] | 1368 | if (invoke_type == kSuper || invoke_type == kInterface || invoke_type == kVirtual) { |
Nicolas Geoffray | 1de6f01 | 2020-09-23 13:28:00 +0100 | [diff] [blame] | 1369 | if (called->GetDexFile() == called_method.dex_file) { |
| 1370 | called_method.index = called->GetDexMethodIndex(); |
| 1371 | } else { |
| 1372 | called_method.index = called->FindDexMethodIndexInOtherDexFile( |
| 1373 | *called_method.dex_file, called_method.index); |
| 1374 | DCHECK_NE(called_method.index, dex::kDexNoIndex); |
| 1375 | } |
| 1376 | } |
| 1377 | MaybeUpdateBssMethodEntry(called, called_method); |
| 1378 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1379 | |
Vladimir Marko | 6217fc2 | 2020-05-21 11:59:11 +0100 | [diff] [blame] | 1380 | // Static invokes need class initialization check but instance invokes can proceed even if |
| 1381 | // the class is erroneous, i.e. in the edge case of escaping instances of erroneous classes. |
| 1382 | bool success = true; |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 1383 | ObjPtr<mirror::Class> called_class = called->GetDeclaringClass(); |
| 1384 | if (NeedsClinitCheckBeforeCall(called) && !called_class->IsVisiblyInitialized()) { |
| 1385 | // Ensure that the called method's class is initialized. |
| 1386 | StackHandleScope<1> hs(soa.Self()); |
| 1387 | HandleWrapperObjPtr<mirror::Class> h_called_class(hs.NewHandleWrapper(&called_class)); |
Vladimir Marko | 6217fc2 | 2020-05-21 11:59:11 +0100 | [diff] [blame] | 1388 | success = linker->EnsureInitialized(soa.Self(), h_called_class, true, true); |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 1389 | } |
Vladimir Marko | 6217fc2 | 2020-05-21 11:59:11 +0100 | [diff] [blame] | 1390 | if (success) { |
Nicolas Geoffray | c25a9f9 | 2021-12-13 17:22:43 +0000 | [diff] [blame^] | 1391 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 1392 | code = instrumentation->GetCodeForInvoke(called); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1393 | } else { |
| 1394 | DCHECK(called_class->IsErroneous()); |
Vladimir Marko | 6217fc2 | 2020-05-21 11:59:11 +0100 | [diff] [blame] | 1395 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1398 | CHECK_EQ(code == nullptr, self->IsExceptionPending()); |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 1399 | // Fixup any locally saved objects may have moved during a GC. |
| 1400 | visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1401 | // Place called method in callee-save frame to be placed as first argument to quick method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1402 | *sp = called; |
| 1403 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1404 | return code; |
| 1405 | } |
| 1406 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1407 | /* |
| 1408 | * This class uses a couple of observations to unite the different calling conventions through |
| 1409 | * a few constants. |
| 1410 | * |
| 1411 | * 1) Number of registers used for passing is normally even, so counting down has no penalty for |
| 1412 | * possible alignment. |
| 1413 | * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point |
| 1414 | * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote |
| 1415 | * when we have to split things |
| 1416 | * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats |
| 1417 | * and we can use Int handling directly. |
| 1418 | * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code |
| 1419 | * necessary when widening. Also, widening of Ints will take place implicitly, and the |
| 1420 | * extension should be compatible with Aarch64, which mandates copying the available bits |
| 1421 | * into LSB and leaving the rest unspecified. |
| 1422 | * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on |
| 1423 | * the stack. |
| 1424 | * 6) There is only little endian. |
| 1425 | * |
| 1426 | * |
| 1427 | * Actual work is supposed to be done in a delegate of the template type. The interface is as |
| 1428 | * follows: |
| 1429 | * |
| 1430 | * void PushGpr(uintptr_t): Add a value for the next GPR |
| 1431 | * |
| 1432 | * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need |
| 1433 | * padding, that is, think the architecture is 32b and aligns 64b. |
| 1434 | * |
| 1435 | * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to |
| 1436 | * split this if necessary. The current state will have aligned, if |
| 1437 | * necessary. |
| 1438 | * |
| 1439 | * void PushStack(uintptr_t): Push a value to the stack. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1440 | */ |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1441 | template<class T> class BuildNativeCallFrameStateMachine { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1442 | public: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1443 | #if defined(__arm__) |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1444 | static constexpr bool kNativeSoftFloatAbi = true; |
| 1445 | static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1446 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
| 1447 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1448 | static constexpr size_t kRegistersNeededForLong = 2; |
| 1449 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1450 | static constexpr bool kMultiRegistersAligned = true; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1451 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1452 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1453 | static constexpr bool kAlignLongOnStack = true; |
| 1454 | static constexpr bool kAlignDoubleOnStack = true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1455 | #elif defined(__aarch64__) |
| 1456 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1457 | static constexpr size_t kNumNativeGprArgs = 8; // 8 arguments passed in GPRs. |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1458 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 1459 | |
| 1460 | static constexpr size_t kRegistersNeededForLong = 1; |
| 1461 | static constexpr size_t kRegistersNeededForDouble = 1; |
| 1462 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1463 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1464 | static constexpr bool kMultiGPRegistersWidened = false; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1465 | static constexpr bool kAlignLongOnStack = false; |
| 1466 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1467 | #elif defined(__i386__) |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1468 | static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1469 | static constexpr size_t kNumNativeGprArgs = 0; // 0 arguments passed in GPRs. |
| 1470 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1471 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1472 | static constexpr size_t kRegistersNeededForLong = 2; |
| 1473 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1474 | static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1475 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1476 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1477 | static constexpr bool kAlignLongOnStack = false; |
| 1478 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1479 | #elif defined(__x86_64__) |
| 1480 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 1481 | static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. |
| 1482 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 1483 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1484 | static constexpr size_t kRegistersNeededForLong = 1; |
| 1485 | static constexpr size_t kRegistersNeededForDouble = 1; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1486 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1487 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1488 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1489 | static constexpr bool kAlignLongOnStack = false; |
| 1490 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1491 | #else |
| 1492 | #error "Unsupported architecture" |
| 1493 | #endif |
| 1494 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1495 | public: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1496 | explicit BuildNativeCallFrameStateMachine(T* delegate) |
| 1497 | : gpr_index_(kNumNativeGprArgs), |
| 1498 | fpr_index_(kNumNativeFprArgs), |
| 1499 | stack_entries_(0), |
| 1500 | delegate_(delegate) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1501 | // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff |
| 1502 | // the next register is even; counting down is just to make the compiler happy... |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 1503 | static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even"); |
| 1504 | static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even"); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1505 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1506 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1507 | virtual ~BuildNativeCallFrameStateMachine() {} |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1508 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1509 | bool HavePointerGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1510 | return gpr_index_ > 0; |
| 1511 | } |
| 1512 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1513 | void AdvancePointer(const void* val) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1514 | if (HavePointerGpr()) { |
| 1515 | gpr_index_--; |
| 1516 | PushGpr(reinterpret_cast<uintptr_t>(val)); |
| 1517 | } else { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1518 | stack_entries_++; // TODO: have a field for pointer length as multiple of 32b |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1519 | PushStack(reinterpret_cast<uintptr_t>(val)); |
| 1520 | gpr_index_ = 0; |
| 1521 | } |
| 1522 | } |
| 1523 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1524 | bool HaveIntGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1525 | return gpr_index_ > 0; |
| 1526 | } |
| 1527 | |
| 1528 | void AdvanceInt(uint32_t val) { |
| 1529 | if (HaveIntGpr()) { |
| 1530 | gpr_index_--; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1531 | if (kMultiGPRegistersWidened) { |
| 1532 | DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1533 | PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1534 | } else { |
| 1535 | PushGpr(val); |
| 1536 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1537 | } else { |
| 1538 | stack_entries_++; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1539 | if (kMultiGPRegistersWidened) { |
| 1540 | DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1541 | PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1542 | } else { |
| 1543 | PushStack(val); |
| 1544 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1545 | gpr_index_ = 0; |
| 1546 | } |
| 1547 | } |
| 1548 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1549 | bool HaveLongGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1550 | return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); |
| 1551 | } |
| 1552 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1553 | bool LongGprNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1554 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1555 | kAlignLongOnStack && // and when it needs alignment |
| 1556 | (gpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1557 | } |
| 1558 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1559 | bool LongStackNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1560 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1561 | kAlignLongOnStack && // and when it needs 8B alignment |
| 1562 | (stack_entries_ & 1) == 1; // counter is odd |
| 1563 | } |
| 1564 | |
| 1565 | void AdvanceLong(uint64_t val) { |
| 1566 | if (HaveLongGpr()) { |
| 1567 | if (LongGprNeedsPadding()) { |
| 1568 | PushGpr(0); |
| 1569 | gpr_index_--; |
| 1570 | } |
| 1571 | if (kRegistersNeededForLong == 1) { |
| 1572 | PushGpr(static_cast<uintptr_t>(val)); |
| 1573 | } else { |
| 1574 | PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1575 | PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1576 | } |
| 1577 | gpr_index_ -= kRegistersNeededForLong; |
| 1578 | } else { |
| 1579 | if (LongStackNeedsPadding()) { |
| 1580 | PushStack(0); |
| 1581 | stack_entries_++; |
| 1582 | } |
| 1583 | if (kRegistersNeededForLong == 1) { |
| 1584 | PushStack(static_cast<uintptr_t>(val)); |
| 1585 | stack_entries_++; |
| 1586 | } else { |
| 1587 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1588 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1589 | stack_entries_ += 2; |
| 1590 | } |
| 1591 | gpr_index_ = 0; |
| 1592 | } |
| 1593 | } |
| 1594 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1595 | bool HaveFloatFpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1596 | return fpr_index_ > 0; |
| 1597 | } |
| 1598 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1599 | void AdvanceFloat(float val) { |
| 1600 | if (kNativeSoftFloatAbi) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1601 | AdvanceInt(bit_cast<uint32_t, float>(val)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1602 | } else { |
| 1603 | if (HaveFloatFpr()) { |
| 1604 | fpr_index_--; |
| 1605 | if (kRegistersNeededForDouble == 1) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1606 | if (kMultiFPRegistersWidened) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1607 | PushFpr8(bit_cast<uint64_t, double>(val)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1608 | } else { |
| 1609 | // No widening, just use the bits. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1610 | PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1611 | } |
| 1612 | } else { |
| 1613 | PushFpr4(val); |
| 1614 | } |
| 1615 | } else { |
| 1616 | stack_entries_++; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1617 | if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1618 | // Need to widen before storing: Note the "double" in the template instantiation. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1619 | // Note: We need to jump through those hoops to make the compiler happy. |
| 1620 | DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1621 | PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1622 | } else { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1623 | PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1624 | } |
| 1625 | fpr_index_ = 0; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1630 | bool HaveDoubleFpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1631 | return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); |
| 1632 | } |
| 1633 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1634 | bool DoubleFprNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1635 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1636 | kAlignDoubleOnStack && // and when it needs alignment |
| 1637 | (fpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1638 | } |
| 1639 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1640 | bool DoubleStackNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1641 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1642 | kAlignDoubleOnStack && // and when it needs 8B alignment |
| 1643 | (stack_entries_ & 1) == 1; // counter is odd |
| 1644 | } |
| 1645 | |
| 1646 | void AdvanceDouble(uint64_t val) { |
| 1647 | if (kNativeSoftFloatAbi) { |
| 1648 | AdvanceLong(val); |
| 1649 | } else { |
| 1650 | if (HaveDoubleFpr()) { |
| 1651 | if (DoubleFprNeedsPadding()) { |
| 1652 | PushFpr4(0); |
| 1653 | fpr_index_--; |
| 1654 | } |
| 1655 | PushFpr8(val); |
| 1656 | fpr_index_ -= kRegistersNeededForDouble; |
| 1657 | } else { |
| 1658 | if (DoubleStackNeedsPadding()) { |
| 1659 | PushStack(0); |
| 1660 | stack_entries_++; |
| 1661 | } |
| 1662 | if (kRegistersNeededForDouble == 1) { |
| 1663 | PushStack(static_cast<uintptr_t>(val)); |
| 1664 | stack_entries_++; |
| 1665 | } else { |
| 1666 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1667 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1668 | stack_entries_ += 2; |
| 1669 | } |
| 1670 | fpr_index_ = 0; |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1675 | uint32_t GetStackEntries() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1676 | return stack_entries_; |
| 1677 | } |
| 1678 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1679 | uint32_t GetNumberOfUsedGprs() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1680 | return kNumNativeGprArgs - gpr_index_; |
| 1681 | } |
| 1682 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1683 | uint32_t GetNumberOfUsedFprs() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1684 | return kNumNativeFprArgs - fpr_index_; |
| 1685 | } |
| 1686 | |
| 1687 | private: |
| 1688 | void PushGpr(uintptr_t val) { |
| 1689 | delegate_->PushGpr(val); |
| 1690 | } |
| 1691 | void PushFpr4(float val) { |
| 1692 | delegate_->PushFpr4(val); |
| 1693 | } |
| 1694 | void PushFpr8(uint64_t val) { |
| 1695 | delegate_->PushFpr8(val); |
| 1696 | } |
| 1697 | void PushStack(uintptr_t val) { |
| 1698 | delegate_->PushStack(val); |
| 1699 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1700 | |
| 1701 | uint32_t gpr_index_; // Number of free GPRs |
| 1702 | uint32_t fpr_index_; // Number of free FPRs |
| 1703 | uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not |
| 1704 | // extended |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1705 | T* const delegate_; // What Push implementation gets called |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1706 | }; |
| 1707 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1708 | // Computes the sizes of register stacks and call stack area. Handling of references can be extended |
| 1709 | // in subclasses. |
| 1710 | // |
| 1711 | // To handle native pointers, use "L" in the shorty for an object reference, which simulates |
| 1712 | // them with handles. |
| 1713 | class ComputeNativeCallFrameSize { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1714 | public: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1715 | ComputeNativeCallFrameSize() : num_stack_entries_(0) {} |
| 1716 | |
| 1717 | virtual ~ComputeNativeCallFrameSize() {} |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1718 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1719 | uint32_t GetStackSize() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1720 | return num_stack_entries_ * sizeof(uintptr_t); |
| 1721 | } |
| 1722 | |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1723 | uint8_t* LayoutStackArgs(uint8_t* sp8) const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1724 | sp8 -= GetStackSize(); |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1725 | // Align by kStackAlignment; it is at least as strict as native stack alignment. |
Andreas Gampe | 779f8c9 | 2014-06-09 18:29:38 -0700 | [diff] [blame] | 1726 | sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment)); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1727 | return sp8; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1728 | } |
| 1729 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1730 | virtual void WalkHeader( |
| 1731 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1732 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1733 | } |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1734 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1735 | void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1736 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this); |
| 1737 | |
| 1738 | WalkHeader(&sm); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1739 | |
| 1740 | for (uint32_t i = 1; i < shorty_len; ++i) { |
| 1741 | Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); |
| 1742 | switch (cur_type_) { |
| 1743 | case Primitive::kPrimNot: |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1744 | sm.AdvancePointer(nullptr); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1745 | break; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1746 | case Primitive::kPrimBoolean: |
| 1747 | case Primitive::kPrimByte: |
| 1748 | case Primitive::kPrimChar: |
| 1749 | case Primitive::kPrimShort: |
| 1750 | case Primitive::kPrimInt: |
| 1751 | sm.AdvanceInt(0); |
| 1752 | break; |
| 1753 | case Primitive::kPrimFloat: |
| 1754 | sm.AdvanceFloat(0); |
| 1755 | break; |
| 1756 | case Primitive::kPrimDouble: |
| 1757 | sm.AdvanceDouble(0); |
| 1758 | break; |
| 1759 | case Primitive::kPrimLong: |
| 1760 | sm.AdvanceLong(0); |
| 1761 | break; |
| 1762 | default: |
| 1763 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1764 | UNREACHABLE(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1768 | num_stack_entries_ = sm.GetStackEntries(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | void PushGpr(uintptr_t /* val */) { |
| 1772 | // not optimizing registers, yet |
| 1773 | } |
| 1774 | |
| 1775 | void PushFpr4(float /* val */) { |
| 1776 | // not optimizing registers, yet |
| 1777 | } |
| 1778 | |
| 1779 | void PushFpr8(uint64_t /* val */) { |
| 1780 | // not optimizing registers, yet |
| 1781 | } |
| 1782 | |
| 1783 | void PushStack(uintptr_t /* val */) { |
| 1784 | // counting is already done in the superclass |
| 1785 | } |
| 1786 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1787 | protected: |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1788 | uint32_t num_stack_entries_; |
| 1789 | }; |
| 1790 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1791 | class ComputeGenericJniFrameSize final : public ComputeNativeCallFrameSize { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1792 | public: |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1793 | explicit ComputeGenericJniFrameSize(bool critical_native) |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1794 | : critical_native_(critical_native) {} |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1795 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1796 | uintptr_t* ComputeLayout(ArtMethod** managed_sp, const char* shorty, uint32_t shorty_len) |
| 1797 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1798 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1799 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1800 | Walk(shorty, shorty_len); |
| 1801 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1802 | // Add space for cookie. |
| 1803 | DCHECK_ALIGNED(managed_sp, sizeof(uintptr_t)); |
| 1804 | static_assert(sizeof(uintptr_t) >= sizeof(IRTSegmentState)); |
| 1805 | uint8_t* sp8 = reinterpret_cast<uint8_t*>(managed_sp) - sizeof(uintptr_t); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1806 | |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1807 | // Layout stack arguments. |
| 1808 | sp8 = LayoutStackArgs(sp8); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1809 | |
| 1810 | // Return the new bottom. |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1811 | DCHECK_ALIGNED(sp8, sizeof(uintptr_t)); |
| 1812 | return reinterpret_cast<uintptr_t*>(sp8); |
| 1813 | } |
| 1814 | |
| 1815 | static uintptr_t* GetStartGprRegs(uintptr_t* reserved_area) { |
| 1816 | return reserved_area; |
| 1817 | } |
| 1818 | |
| 1819 | static uint32_t* GetStartFprRegs(uintptr_t* reserved_area) { |
| 1820 | constexpr size_t num_gprs = |
| 1821 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs; |
| 1822 | return reinterpret_cast<uint32_t*>(GetStartGprRegs(reserved_area) + num_gprs); |
| 1823 | } |
| 1824 | |
| 1825 | static uintptr_t* GetHiddenArgSlot(uintptr_t* reserved_area) { |
| 1826 | // Note: `num_fprs` is 0 on architectures where sizeof(uintptr_t) does not match the |
| 1827 | // FP register size (it is actually 0 on all supported 32-bit architectures). |
| 1828 | constexpr size_t num_fprs = |
| 1829 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs; |
| 1830 | return reinterpret_cast<uintptr_t*>(GetStartFprRegs(reserved_area)) + num_fprs; |
| 1831 | } |
| 1832 | |
| 1833 | static uintptr_t* GetOutArgsSpSlot(uintptr_t* reserved_area) { |
| 1834 | return GetHiddenArgSlot(reserved_area) + 1; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1837 | // Add JNIEnv* and jobj/jclass before the shorty-derived elements. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1838 | void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) override |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1839 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1840 | |
| 1841 | private: |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1842 | const bool critical_native_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1843 | }; |
| 1844 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1845 | void ComputeGenericJniFrameSize::WalkHeader( |
| 1846 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) { |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1847 | // First 2 parameters are always excluded for @CriticalNative. |
| 1848 | if (UNLIKELY(critical_native_)) { |
| 1849 | return; |
| 1850 | } |
| 1851 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1852 | // JNIEnv |
| 1853 | sm->AdvancePointer(nullptr); |
| 1854 | |
| 1855 | // Class object or this as first argument |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1856 | sm->AdvancePointer(nullptr); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | // Class to push values to three separate regions. Used to fill the native call part. Adheres to |
| 1860 | // the template requirements of BuildGenericJniFrameStateMachine. |
| 1861 | class FillNativeCall { |
| 1862 | public: |
| 1863 | FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) : |
| 1864 | cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {} |
| 1865 | |
| 1866 | virtual ~FillNativeCall() {} |
| 1867 | |
| 1868 | void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) { |
| 1869 | cur_gpr_reg_ = gpr_regs; |
| 1870 | cur_fpr_reg_ = fpr_regs; |
| 1871 | cur_stack_arg_ = stack_args; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | void PushGpr(uintptr_t val) { |
| 1875 | *cur_gpr_reg_ = val; |
| 1876 | cur_gpr_reg_++; |
| 1877 | } |
| 1878 | |
| 1879 | void PushFpr4(float val) { |
| 1880 | *cur_fpr_reg_ = val; |
| 1881 | cur_fpr_reg_++; |
| 1882 | } |
| 1883 | |
| 1884 | void PushFpr8(uint64_t val) { |
| 1885 | uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); |
| 1886 | *tmp = val; |
| 1887 | cur_fpr_reg_ += 2; |
| 1888 | } |
| 1889 | |
| 1890 | void PushStack(uintptr_t val) { |
| 1891 | *cur_stack_arg_ = val; |
| 1892 | cur_stack_arg_++; |
| 1893 | } |
| 1894 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1895 | private: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1896 | uintptr_t* cur_gpr_reg_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1897 | uint32_t* cur_fpr_reg_; |
| 1898 | uintptr_t* cur_stack_arg_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1899 | }; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1900 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1901 | // Visits arguments on the stack placing them into a region lower down the stack for the benefit |
| 1902 | // of transitioning into native code. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1903 | class BuildGenericJniFrameVisitor final : public QuickArgumentVisitor { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1904 | public: |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1905 | BuildGenericJniFrameVisitor(Thread* self, |
| 1906 | bool is_static, |
| 1907 | bool critical_native, |
| 1908 | const char* shorty, |
| 1909 | uint32_t shorty_len, |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1910 | ArtMethod** managed_sp, |
| 1911 | uintptr_t* reserved_area) |
| 1912 | : QuickArgumentVisitor(managed_sp, is_static, shorty, shorty_len), |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1913 | jni_call_(nullptr, nullptr, nullptr, critical_native), |
| 1914 | sm_(&jni_call_), |
| 1915 | current_vreg_(nullptr) { |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1916 | DCHECK_ALIGNED(managed_sp, kStackAlignment); |
| 1917 | DCHECK_ALIGNED(reserved_area, sizeof(uintptr_t)); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1918 | |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1919 | ComputeGenericJniFrameSize fsc(critical_native); |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1920 | uintptr_t* out_args_sp = fsc.ComputeLayout(managed_sp, shorty, shorty_len); |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1921 | |
| 1922 | // Store hidden argument for @CriticalNative. |
| 1923 | uintptr_t* hidden_arg_slot = fsc.GetHiddenArgSlot(reserved_area); |
| 1924 | constexpr uintptr_t kGenericJniTag = 1u; |
| 1925 | ArtMethod* method = *managed_sp; |
| 1926 | *hidden_arg_slot = critical_native ? (reinterpret_cast<uintptr_t>(method) | kGenericJniTag) |
| 1927 | : 0xebad6a89u; // Bad value. |
| 1928 | |
| 1929 | // Set out args SP. |
| 1930 | uintptr_t* out_args_sp_slot = fsc.GetOutArgsSpSlot(reserved_area); |
| 1931 | *out_args_sp_slot = reinterpret_cast<uintptr_t>(out_args_sp); |
| 1932 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1933 | // Prepare vreg pointer for spilling references. |
| 1934 | static constexpr size_t frame_size = |
| 1935 | RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs); |
| 1936 | current_vreg_ = reinterpret_cast<uint32_t*>( |
| 1937 | reinterpret_cast<uint8_t*>(managed_sp) + frame_size + sizeof(ArtMethod*)); |
| 1938 | |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 1939 | jni_call_.Reset(fsc.GetStartGprRegs(reserved_area), |
| 1940 | fsc.GetStartFprRegs(reserved_area), |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1941 | out_args_sp); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1942 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1943 | // First 2 parameters are always excluded for CriticalNative methods. |
| 1944 | if (LIKELY(!critical_native)) { |
| 1945 | // jni environment is always first argument |
| 1946 | sm_.AdvancePointer(self->GetJniEnv()); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1947 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1948 | if (is_static) { |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1949 | // The `jclass` is a pointer to the method's declaring class. |
| 1950 | // The declaring class must be marked. |
Vladimir Marko | 0eebdcc | 2021-02-26 09:34:13 +0000 | [diff] [blame] | 1951 | auto* declaring_class = reinterpret_cast<mirror::CompressedReference<mirror::Class>*>( |
| 1952 | method->GetDeclaringClassAddressWithoutBarrier()); |
| 1953 | if (kUseReadBarrier) { |
Vladimir Marko | 8439e45 | 2021-11-25 15:36:29 +0000 | [diff] [blame] | 1954 | artJniReadBarrier(method); |
Vladimir Marko | 0eebdcc | 2021-02-26 09:34:13 +0000 | [diff] [blame] | 1955 | } |
| 1956 | sm_.AdvancePointer(declaring_class); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1957 | } // else "this" reference is already handled by QuickArgumentVisitor. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1961 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1962 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1963 | private: |
| 1964 | // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1965 | class FillJniCall final : public FillNativeCall { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1966 | public: |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1967 | FillJniCall(uintptr_t* gpr_regs, |
| 1968 | uint32_t* fpr_regs, |
| 1969 | uintptr_t* stack_args, |
| 1970 | bool critical_native) |
| 1971 | : FillNativeCall(gpr_regs, fpr_regs, stack_args), |
| 1972 | cur_entry_(0), |
| 1973 | critical_native_(critical_native) {} |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1974 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1975 | void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1976 | FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1977 | cur_entry_ = 0U; |
| 1978 | } |
| 1979 | |
Mathieu Chartier | 1432a5b | 2016-10-04 15:41:42 -0700 | [diff] [blame] | 1980 | bool CriticalNative() const { |
| 1981 | return critical_native_; |
| 1982 | } |
| 1983 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1984 | private: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1985 | size_t cur_entry_; |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1986 | const bool critical_native_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1987 | }; |
| 1988 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1989 | FillJniCall jni_call_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1990 | BuildNativeCallFrameStateMachine<FillJniCall> sm_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1991 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 1992 | // Pointer to the current vreg in caller's reserved out vreg area. |
| 1993 | // Used for spilling reference arguments. |
| 1994 | uint32_t* current_vreg_; |
| 1995 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1996 | DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); |
| 1997 | }; |
| 1998 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1999 | void BuildGenericJniFrameVisitor::Visit() { |
| 2000 | Primitive::Type type = GetParamPrimitiveType(); |
| 2001 | switch (type) { |
| 2002 | case Primitive::kPrimLong: { |
| 2003 | jlong long_arg; |
| 2004 | if (IsSplitLongOrDouble()) { |
| 2005 | long_arg = ReadSplitLongParam(); |
| 2006 | } else { |
| 2007 | long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 2008 | } |
| 2009 | sm_.AdvanceLong(long_arg); |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 2010 | current_vreg_ += 2u; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2011 | break; |
| 2012 | } |
| 2013 | case Primitive::kPrimDouble: { |
| 2014 | uint64_t double_arg; |
| 2015 | if (IsSplitLongOrDouble()) { |
| 2016 | // Read into union so that we don't case to a double. |
| 2017 | double_arg = ReadSplitLongParam(); |
| 2018 | } else { |
| 2019 | double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); |
| 2020 | } |
| 2021 | sm_.AdvanceDouble(double_arg); |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 2022 | current_vreg_ += 2u; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2023 | break; |
| 2024 | } |
| 2025 | case Primitive::kPrimNot: { |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 2026 | mirror::Object* obj = |
| 2027 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress())->AsMirrorPtr(); |
| 2028 | StackReference<mirror::Object>* spill_ref = |
| 2029 | reinterpret_cast<StackReference<mirror::Object>*>(current_vreg_); |
| 2030 | spill_ref->Assign(obj); |
| 2031 | sm_.AdvancePointer(obj != nullptr ? spill_ref : nullptr); |
| 2032 | current_vreg_ += 1u; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2033 | break; |
| 2034 | } |
| 2035 | case Primitive::kPrimFloat: |
| 2036 | sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 2037 | current_vreg_ += 1u; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | case Primitive::kPrimBoolean: // Fall-through. |
| 2040 | case Primitive::kPrimByte: // Fall-through. |
| 2041 | case Primitive::kPrimChar: // Fall-through. |
| 2042 | case Primitive::kPrimShort: // Fall-through. |
| 2043 | case Primitive::kPrimInt: // Fall-through. |
| 2044 | sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 2045 | current_vreg_ += 1u; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2046 | break; |
| 2047 | case Primitive::kPrimVoid: |
| 2048 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 2049 | UNREACHABLE(); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 2053 | /* |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2054 | * Initializes the reserved area assumed to be directly below `managed_sp` for a native call: |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 2055 | * |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2056 | * On entry, the stack has a standard callee-save frame above `managed_sp`, |
| 2057 | * and the reserved area below it. Starting below `managed_sp`, we reserve space |
| 2058 | * for local reference cookie (not present for @CriticalNative), HandleScope |
| 2059 | * (not present for @CriticalNative) and stack args (if args do not fit into |
| 2060 | * registers). At the bottom of the reserved area, there is space for register |
| 2061 | * arguments, hidden arg (for @CriticalNative) and the SP for the native call |
| 2062 | * (i.e. pointer to the stack args area), which the calling stub shall load |
| 2063 | * to perform the native call. We fill all these fields, perform class init |
| 2064 | * check (for static methods) and/or locking (for synchronized methods) if |
| 2065 | * needed and return to the stub. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 2066 | * |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2067 | * The return value is the pointer to the native code, null on failure. |
Vladimir Marko | 4d52715 | 2021-11-23 12:07:04 +0000 | [diff] [blame] | 2068 | * |
| 2069 | * NO_THREAD_SAFETY_ANALYSIS: Depending on the use case, the trampoline may |
| 2070 | * or may not lock a synchronization object and transition out of Runnable. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 2071 | */ |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2072 | extern "C" const void* artQuickGenericJniTrampoline(Thread* self, |
| 2073 | ArtMethod** managed_sp, |
| 2074 | uintptr_t* reserved_area) |
Vladimir Marko | 4d52715 | 2021-11-23 12:07:04 +0000 | [diff] [blame] | 2075 | REQUIRES_SHARED(Locks::mutator_lock_) NO_THREAD_SAFETY_ANALYSIS { |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 2076 | // Note: We cannot walk the stack properly until fixed up below. |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2077 | ArtMethod* called = *managed_sp; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2078 | DCHECK(called->IsNative()) << called->PrettyMethod(true); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 2079 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2080 | uint32_t shorty_len = 0; |
| 2081 | const char* shorty = called->GetShorty(&shorty_len); |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 2082 | bool critical_native = called->IsCriticalNative(); |
| 2083 | bool fast_native = called->IsFastNative(); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2084 | bool normal_native = !critical_native && !fast_native; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2085 | |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2086 | // Run the visitor and update sp. |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2087 | BuildGenericJniFrameVisitor visitor(self, |
| 2088 | called->IsStatic(), |
| 2089 | critical_native, |
| 2090 | shorty, |
| 2091 | shorty_len, |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2092 | managed_sp, |
| 2093 | reserved_area); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 2094 | { |
| 2095 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| 2096 | visitor.VisitArguments(); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 2097 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2098 | |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 2099 | // Fix up managed-stack things in Thread. After this we can walk the stack. |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2100 | self->SetTopOfStackTagged(managed_sp); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2101 | |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 2102 | self->VerifyStack(); |
| 2103 | |
Vladimir Marko | f8655b3 | 2018-03-21 17:53:56 +0000 | [diff] [blame] | 2104 | // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use. |
| 2105 | jit::Jit* jit = runtime->GetJit(); |
| 2106 | if (jit != nullptr) { |
| 2107 | jit->MethodEntered(self, called); |
| 2108 | } |
| 2109 | |
Nicolas Geoffray | 5a0b672 | 2019-09-24 15:09:40 +0100 | [diff] [blame] | 2110 | // We can set the entrypoint of a native method to generic JNI even when the |
| 2111 | // class hasn't been initialized, so we need to do the initialization check |
| 2112 | // before invoking the native code. |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 2113 | if (NeedsClinitCheckBeforeCall(called)) { |
| 2114 | ObjPtr<mirror::Class> declaring_class = called->GetDeclaringClass(); |
| 2115 | if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) { |
| 2116 | // Ensure static method's class is initialized. |
| 2117 | StackHandleScope<1> hs(self); |
| 2118 | Handle<mirror::Class> h_class(hs.NewHandle(declaring_class)); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 2119 | if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 2120 | DCHECK(Thread::Current()->IsExceptionPending()) << called->PrettyMethod(); |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2121 | return nullptr; // Report error. |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 2122 | } |
Nicolas Geoffray | 5a0b672 | 2019-09-24 15:09:40 +0100 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 2126 | // Skip calling `artJniMethodStart()` for @CriticalNative and @FastNative. |
Vladimir Marko | 46a8910 | 2021-10-21 13:05:46 +0000 | [diff] [blame] | 2127 | if (LIKELY(normal_native)) { |
Vladimir Marko | d95a1f2 | 2021-03-23 16:32:52 +0000 | [diff] [blame] | 2128 | // Start JNI. |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2129 | if (called->IsSynchronized()) { |
Vladimir Marko | 4d52715 | 2021-11-23 12:07:04 +0000 | [diff] [blame] | 2130 | ObjPtr<mirror::Object> lock = GetGenericJniSynchronizationObject(self, called); |
| 2131 | DCHECK(lock != nullptr); |
| 2132 | lock->MonitorEnter(self); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2133 | if (self->IsExceptionPending()) { |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2134 | return nullptr; // Report error. |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2135 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2136 | } |
Vladimir Marko | ce2a344 | 2021-11-24 15:10:26 +0000 | [diff] [blame] | 2137 | artJniMethodStart(self); |
Vladimir Marko | 46a8910 | 2021-10-21 13:05:46 +0000 | [diff] [blame] | 2138 | } else { |
| 2139 | DCHECK(!called->IsSynchronized()) |
| 2140 | << "@FastNative/@CriticalNative and synchronize is not supported"; |
| 2141 | } |
Vladimir Marko | d95a1f2 | 2021-03-23 16:32:52 +0000 | [diff] [blame] | 2142 | |
Vladimir Marko | 46a8910 | 2021-10-21 13:05:46 +0000 | [diff] [blame] | 2143 | // Skip pushing IRT frame for @CriticalNative. |
| 2144 | if (LIKELY(!critical_native)) { |
Vladimir Marko | d95a1f2 | 2021-03-23 16:32:52 +0000 | [diff] [blame] | 2145 | // Push local reference frame. |
| 2146 | JNIEnvExt* env = self->GetJniEnv(); |
| 2147 | DCHECK(env != nullptr); |
| 2148 | uint32_t cookie = bit_cast<uint32_t>(env->GetLocalRefCookie()); |
| 2149 | env->SetLocalRefCookie(env->GetLocalsSegmentState()); |
| 2150 | |
| 2151 | // Save the cookie on the stack. |
| 2152 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(managed_sp); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2153 | *(sp32 - 1) = cookie; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2154 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2155 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2156 | // Retrieve the stored native code. |
Vladimir Marko | 08d0984 | 2019-12-02 12:38:49 +0000 | [diff] [blame] | 2157 | // Note that it may point to the lookup stub or trampoline. |
| 2158 | // FIXME: This is broken for @CriticalNative as the art_jni_dlsym_lookup_stub |
| 2159 | // does not handle that case. Calls from compiled stubs are also broken. |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 2160 | void const* nativeCode = called->GetEntryPointFromJni(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2161 | |
Andreas Gampe | 21a6ec5 | 2019-03-28 10:55:50 -0700 | [diff] [blame] | 2162 | VLOG(third_party_jni) << "GenericJNI: " |
| 2163 | << called->PrettyMethod() |
| 2164 | << " -> " |
| 2165 | << std::hex << reinterpret_cast<uintptr_t>(nativeCode); |
| 2166 | |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2167 | // Return native code. |
| 2168 | return nativeCode; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2169 | } |
| 2170 | |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 2171 | // Defined in quick_jni_entrypoints.cc. |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2172 | extern uint64_t GenericJniMethodEnd(Thread* self, |
| 2173 | uint32_t saved_local_ref_cookie, |
| 2174 | jvalue result, |
| 2175 | uint64_t result_f, |
| 2176 | ArtMethod* called); |
| 2177 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2178 | /* |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2179 | * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2180 | * unlocking. |
| 2181 | */ |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 2182 | extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, |
| 2183 | jvalue result, |
| 2184 | uint64_t result_f) { |
| 2185 | // We're here just back from a native call. We don't have the shared mutator lock at this point |
| 2186 | // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing |
| 2187 | // anything that requires a mutator lock before that would cause problems as GC may have the |
| 2188 | // exclusive mutator lock and may be moving objects, etc. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2189 | ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame(); |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 2190 | DCHECK(self->GetManagedStack()->GetTopQuickFrameTag()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2191 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2192 | ArtMethod* called = *sp; |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 2193 | uint32_t cookie = *(sp32 - 1); |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 2194 | return GenericJniMethodEnd(self, cookie, result, result_f, called); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2197 | // We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value |
| 2198 | // for the method pointer. |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2199 | // |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2200 | // It is valid to use this, as at the usage points here (returns from C functions) we are assuming |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2201 | // to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations). |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2202 | |
Vladimir Marko | f79aa7f | 2017-07-04 16:58:55 +0100 | [diff] [blame] | 2203 | template <InvokeType type, bool access_check> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2204 | static TwoWordReturn artInvokeCommon(uint32_t method_idx, |
| 2205 | ObjPtr<mirror::Object> this_object, |
| 2206 | Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2207 | ArtMethod** sp) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2208 | ScopedQuickEntrypointChecks sqec(self); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 2209 | DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2210 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
Vladimir Marko | f79aa7f | 2017-07-04 16:58:55 +0100 | [diff] [blame] | 2211 | ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2212 | if (UNLIKELY(method == nullptr)) { |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 2213 | const DexFile* dex_file = caller_method->GetDexFile(); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2214 | uint32_t shorty_len; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2215 | const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2216 | { |
| 2217 | // Remember the args in case a GC happens in FindMethodFromCode. |
| 2218 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 2219 | RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); |
| 2220 | visitor.VisitArguments(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2221 | method = FindMethodFromCode<type, access_check>(method_idx, |
| 2222 | &this_object, |
| 2223 | caller_method, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2224 | self); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2225 | visitor.FixupReferences(); |
| 2226 | } |
| 2227 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2228 | if (UNLIKELY(method == nullptr)) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2229 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2230 | return GetTwoWordFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | DCHECK(!self->IsExceptionPending()); |
| 2234 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 2235 | |
| 2236 | // When we return, the caller will branch to this address, so it had better not be 0! |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2237 | DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2238 | << " location: " |
| 2239 | << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2240 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2241 | return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), |
| 2242 | reinterpret_cast<uintptr_t>(method)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 2245 | // Explicit artInvokeCommon template function declarations to please analysis tool. |
| 2246 | #define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2247 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2248 | TwoWordReturn artInvokeCommon<type, access_check>( \ |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2249 | uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp) |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 2250 | |
| 2251 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); |
| 2252 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); |
| 2253 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); |
| 2254 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); |
| 2255 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); |
| 2256 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); |
| 2257 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); |
| 2258 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); |
| 2259 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); |
| 2260 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); |
| 2261 | #undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL |
| 2262 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2263 | // See comments in runtime_support_asm.S |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2264 | extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2265 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2266 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2267 | return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2270 | extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2271 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2272 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2273 | return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2276 | extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2277 | uint32_t method_idx, |
| 2278 | mirror::Object* this_object ATTRIBUTE_UNUSED, |
| 2279 | Thread* self, |
| 2280 | ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2281 | // For static, this_object is not required and may be random garbage. Don't pass it down so that |
| 2282 | // it doesn't cause ObjPtr alignment failure check. |
| 2283 | return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2286 | extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2287 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2288 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2289 | return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2292 | extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2293 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2294 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2295 | return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2298 | // Determine target of interface dispatch. The interface method and this object are known non-null. |
| 2299 | // The interface method is the method returned by the dex cache in the conflict trampoline. |
| 2300 | extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2301 | mirror::Object* raw_this_object, |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2302 | Thread* self, |
| 2303 | ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2304 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2305 | ScopedQuickEntrypointChecks sqec(self); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2306 | |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2307 | Runtime* runtime = Runtime::Current(); |
| 2308 | bool resolve_method = ((interface_method == nullptr) || interface_method->IsRuntimeMethod()); |
| 2309 | if (UNLIKELY(resolve_method)) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2310 | // The interface method is unresolved, so resolve it in the dex file of the caller. |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2311 | // Fetch the dex_method_idx of the target interface method from the caller. |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2312 | StackHandleScope<1> hs(self); |
| 2313 | Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object); |
| 2314 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2315 | uint32_t dex_method_idx; |
| 2316 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 2317 | const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2318 | Instruction::Code instr_code = instr.Opcode(); |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2319 | DCHECK(instr_code == Instruction::INVOKE_INTERFACE || |
| 2320 | instr_code == Instruction::INVOKE_INTERFACE_RANGE) |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2321 | << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr); |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2322 | if (instr_code == Instruction::INVOKE_INTERFACE) { |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2323 | dex_method_idx = instr.VRegB_35c(); |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2324 | } else { |
| 2325 | DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2326 | dex_method_idx = instr.VRegB_3rc(); |
Jeff Hao | 5667f56 | 2017-02-27 19:32:01 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Vladimir Marko | 7e09737 | 2018-11-28 16:40:59 +0000 | [diff] [blame] | 2329 | const DexFile& dex_file = *caller_method->GetDexFile(); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2330 | uint32_t shorty_len; |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2331 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx), |
| 2332 | &shorty_len); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2333 | { |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2334 | // Remember the args in case a GC happens in ClassLinker::ResolveMethod(). |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2335 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 2336 | RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); |
| 2337 | visitor.VisitArguments(); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2338 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2339 | interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
| 2340 | self, dex_method_idx, caller_method, kInterface); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2341 | visitor.FixupReferences(); |
| 2342 | } |
| 2343 | |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2344 | if (UNLIKELY(interface_method == nullptr)) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2345 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2346 | return GetTwoWordFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2347 | } |
Nicolas Geoffray | 8d34a18 | 2020-09-16 09:46:58 +0100 | [diff] [blame] | 2348 | MaybeUpdateBssMethodEntry(interface_method, MethodReference(&dex_file, dex_method_idx)); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2349 | |
| 2350 | // Refresh `raw_this_object` which may have changed after resolution. |
| 2351 | raw_this_object = this_object.Get(); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
Nicolas Geoffray | 2925311 | 2020-02-17 09:40:14 +0000 | [diff] [blame] | 2354 | // The compiler and interpreter make sure the conflict trampoline is never |
| 2355 | // called on a method that resolves to j.l.Object. |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2356 | DCHECK(!interface_method->GetDeclaringClass()->IsObjectClass()); |
| 2357 | DCHECK(interface_method->GetDeclaringClass()->IsInterface()); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2358 | DCHECK(!interface_method->IsRuntimeMethod()); |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 2359 | DCHECK(!interface_method->IsCopied()); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2360 | |
| 2361 | ObjPtr<mirror::Object> obj_this = raw_this_object; |
| 2362 | ObjPtr<mirror::Class> cls = obj_this->GetClass(); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 2363 | uint32_t imt_index = interface_method->GetImtIndex(); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2364 | ImTable* imt = cls->GetImt(kRuntimePointerSize); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2365 | ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2366 | DCHECK(conflict_method->IsRuntimeMethod()); |
| 2367 | |
| 2368 | if (UNLIKELY(resolve_method)) { |
| 2369 | // Now that we know the interface method, look it up in the conflict table. |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2370 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
| 2371 | DCHECK(current_table != nullptr); |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2372 | ArtMethod* method = current_table->Lookup(interface_method, kRuntimePointerSize); |
| 2373 | if (method != nullptr) { |
| 2374 | return GetTwoWordSuccessValue( |
| 2375 | reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()), |
| 2376 | reinterpret_cast<uintptr_t>(method)); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2377 | } |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2378 | // Interface method is not in the conflict table. Continue looking up in the |
| 2379 | // iftable. |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2380 | } |
| 2381 | |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2382 | ArtMethod* method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2383 | if (UNLIKELY(method == nullptr)) { |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2384 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2385 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch( |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2386 | interface_method, obj_this.Ptr(), caller_method); |
| 2387 | return GetTwoWordFailureValue(); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2388 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2389 | |
| 2390 | // We arrive here if we have found an implementation, and it is not in the ImtConflictTable. |
| 2391 | // We create a new table with the new pair { interface_method, method }. |
Nicolas Geoffray | d8fd599 | 2020-02-19 22:25:19 +0000 | [diff] [blame] | 2392 | |
| 2393 | // Classes in the boot image should never need to update conflict methods in |
| 2394 | // their IMT. |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2395 | CHECK(!runtime->GetHeap()->ObjectIsInBootImageSpace(cls.Ptr())) << cls->PrettyClass(); |
| 2396 | ArtMethod* new_conflict_method = runtime->GetClassLinker()->AddMethodToConflictTable( |
| 2397 | cls.Ptr(), |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2398 | conflict_method, |
| 2399 | interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 2400 | method); |
Vladimir Marko | 302f69c | 2017-07-25 15:27:15 +0100 | [diff] [blame] | 2401 | if (new_conflict_method != conflict_method) { |
| 2402 | // Update the IMT if we create a new conflict method. No fence needed here, as the |
| 2403 | // data is consistent. |
| 2404 | imt->Set(imt_index, |
| 2405 | new_conflict_method, |
| 2406 | kRuntimePointerSize); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2409 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 2410 | |
| 2411 | // When we return, the caller will branch to this address, so it had better not be 0! |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2412 | DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2413 | << " location: " << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2414 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2415 | return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), |
| 2416 | reinterpret_cast<uintptr_t>(method)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2419 | // Returns uint64_t representing raw bits from JValue. |
| 2420 | extern "C" uint64_t artInvokePolymorphic(mirror::Object* raw_receiver, Thread* self, ArtMethod** sp) |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2421 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2422 | ScopedQuickEntrypointChecks sqec(self); |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2423 | DCHECK(raw_receiver != nullptr); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 2424 | DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Start new JNI local reference state |
| 2427 | JNIEnvExt* env = self->GetJniEnv(); |
| 2428 | ScopedObjectAccessUnchecked soa(env); |
| 2429 | ScopedJniEnvLocalRefState env_state(env); |
| 2430 | const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe."); |
| 2431 | |
| 2432 | // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC. |
| 2433 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
| 2434 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 2435 | const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2436 | DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC || |
| 2437 | inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 2438 | const dex::ProtoIndex proto_idx(inst.VRegH()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2439 | const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2440 | const size_t shorty_length = strlen(shorty); |
| 2441 | static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static. |
| 2442 | RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa); |
Orion Hodson | fea84dd | 2017-01-16 13:52:20 +0000 | [diff] [blame] | 2443 | gc_visitor.VisitArguments(); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2444 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 2445 | // Wrap raw_receiver in a Handle for safety. |
| 2446 | StackHandleScope<3> hs(self); |
| 2447 | Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver)); |
| 2448 | raw_receiver = nullptr; |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2449 | self->EndAssertNoThreadSuspension(old_cause); |
| 2450 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 2451 | // Resolve method. |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2452 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 2453 | ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2454 | self, inst.VRegB(), caller_method, kVirtual); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 2455 | |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 2456 | Handle<mirror::MethodType> method_type( |
| 2457 | hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method))); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2458 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2459 | // This implies we couldn't resolve one or more types in this method handle. |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2460 | CHECK(self->IsExceptionPending()); |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2461 | return 0UL; |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2464 | DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA()); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2465 | DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic); |
| 2466 | |
| 2467 | // Fix references before constructing the shadow frame. |
| 2468 | gc_visitor.FixupReferences(); |
| 2469 | |
| 2470 | // Construct shadow frame placing arguments consecutively from |first_arg|. |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2471 | const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 2472 | const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc(); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2473 | const size_t first_arg = 0; |
| 2474 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2475 | CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, resolved_method, dex_pc); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2476 | ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get(); |
| 2477 | ScopedStackedShadowFramePusher |
| 2478 | frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
| 2479 | BuildQuickShadowFrameVisitor shadow_frame_builder(sp, |
| 2480 | kMethodIsStatic, |
| 2481 | shorty, |
| 2482 | strlen(shorty), |
| 2483 | shadow_frame, |
| 2484 | first_arg); |
| 2485 | shadow_frame_builder.VisitArguments(); |
| 2486 | |
| 2487 | // Push a transition back into managed code onto the linked list in thread. |
| 2488 | ManagedStack fragment; |
| 2489 | self->PushManagedStackFragment(&fragment); |
| 2490 | |
| 2491 | // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in |
| 2492 | // consecutive order. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 2493 | RangeInstructionOperands operands(first_arg + 1, num_vregs - 1); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2494 | Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic()); |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2495 | JValue result; |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 2496 | bool success = false; |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 2497 | if (resolved_method->GetDeclaringClass() == GetClassRoot<mirror::MethodHandle>(linker)) { |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2498 | Handle<mirror::MethodHandle> method_handle(hs.NewHandle( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 2499 | ObjPtr<mirror::MethodHandle>::DownCast(receiver_handle.Get()))); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2500 | if (intrinsic == Intrinsics::kMethodHandleInvokeExact) { |
| 2501 | success = MethodHandleInvokeExact(self, |
| 2502 | *shadow_frame, |
| 2503 | method_handle, |
| 2504 | method_type, |
| 2505 | &operands, |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2506 | &result); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2507 | } else { |
| 2508 | DCHECK_EQ(static_cast<uint32_t>(intrinsic), |
| 2509 | static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke)); |
| 2510 | success = MethodHandleInvoke(self, |
| 2511 | *shadow_frame, |
| 2512 | method_handle, |
| 2513 | method_type, |
| 2514 | &operands, |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2515 | &result); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2516 | } |
| 2517 | } else { |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 2518 | DCHECK_EQ(GetClassRoot<mirror::VarHandle>(linker), resolved_method->GetDeclaringClass()); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2519 | Handle<mirror::VarHandle> var_handle(hs.NewHandle( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 2520 | ObjPtr<mirror::VarHandle>::DownCast(receiver_handle.Get()))); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2521 | mirror::VarHandle::AccessMode access_mode = |
| 2522 | mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic); |
| 2523 | success = VarHandleInvokeAccessor(self, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 2524 | *shadow_frame, |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2525 | var_handle, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 2526 | method_type, |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2527 | access_mode, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 2528 | &operands, |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2529 | &result); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2530 | } |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 2531 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 2532 | DCHECK(success || self->IsExceptionPending()); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Pop transition record. |
| 2535 | self->PopManagedStackFragment(fragment); |
| 2536 | |
Orion Hodson | cd260eb | 2018-06-06 09:04:17 +0100 | [diff] [blame] | 2537 | return result.GetJ(); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 2540 | // Returns uint64_t representing raw bits from JValue. |
| 2541 | extern "C" uint64_t artInvokeCustom(uint32_t call_site_idx, Thread* self, ArtMethod** sp) |
| 2542 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2543 | ScopedQuickEntrypointChecks sqec(self); |
| 2544 | DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
| 2545 | |
| 2546 | // invoke-custom is effectively a static call (no receiver). |
| 2547 | static constexpr bool kMethodIsStatic = true; |
| 2548 | |
| 2549 | // Start new JNI local reference state |
| 2550 | JNIEnvExt* env = self->GetJniEnv(); |
| 2551 | ScopedObjectAccessUnchecked soa(env); |
| 2552 | ScopedJniEnvLocalRefState env_state(env); |
| 2553 | |
| 2554 | const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe."); |
| 2555 | |
| 2556 | // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC. |
| 2557 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
| 2558 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
| 2559 | const DexFile* dex_file = caller_method->GetDexFile(); |
| 2560 | const dex::ProtoIndex proto_idx(dex_file->GetProtoIndexForCallSite(call_site_idx)); |
| 2561 | const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx); |
| 2562 | const uint32_t shorty_len = strlen(shorty); |
| 2563 | |
| 2564 | // Construct the shadow frame placing arguments consecutively from |first_arg|. |
| 2565 | const size_t first_arg = 0; |
| 2566 | const size_t num_vregs = ArtMethod::NumArgRegisters(shorty); |
| 2567 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2568 | CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, caller_method, dex_pc); |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 2569 | ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get(); |
| 2570 | ScopedStackedShadowFramePusher |
| 2571 | frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
| 2572 | BuildQuickShadowFrameVisitor shadow_frame_builder(sp, |
| 2573 | kMethodIsStatic, |
| 2574 | shorty, |
| 2575 | shorty_len, |
| 2576 | shadow_frame, |
| 2577 | first_arg); |
| 2578 | shadow_frame_builder.VisitArguments(); |
| 2579 | |
| 2580 | // Push a transition back into managed code onto the linked list in thread. |
| 2581 | ManagedStack fragment; |
| 2582 | self->PushManagedStackFragment(&fragment); |
| 2583 | self->EndAssertNoThreadSuspension(old_cause); |
| 2584 | |
| 2585 | // Perform the invoke-custom operation. |
| 2586 | RangeInstructionOperands operands(first_arg, num_vregs); |
| 2587 | JValue result; |
| 2588 | bool success = |
| 2589 | interpreter::DoInvokeCustom(self, *shadow_frame, call_site_idx, &operands, &result); |
| 2590 | DCHECK(success || self->IsExceptionPending()); |
| 2591 | |
| 2592 | // Pop transition record. |
| 2593 | self->PopManagedStackFragment(fragment); |
| 2594 | |
| 2595 | return result.GetJ(); |
| 2596 | } |
| 2597 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 2598 | extern "C" void artMethodEntryHook(ArtMethod* method, Thread* self, ArtMethod** sp ATTRIBUTE_UNUSED) |
| 2599 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2600 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
| 2601 | instr->MethodEnterEvent(self, method); |
| 2602 | if (instr->IsDeoptimized(method)) { |
| 2603 | // Instrumentation can request deoptimizing only a particular method (for |
| 2604 | // ex: when there are break points on the method). In such cases deoptimize |
| 2605 | // only this method. FullFrame deoptimizations are handled on method exits. |
| 2606 | artDeoptimizeFromCompiledCode(DeoptimizationKind::kDebugging, self); |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | extern "C" int artMethodExitHook(Thread* self, |
| 2611 | ArtMethod* method, |
| 2612 | uint64_t* gpr_result, |
| 2613 | uint64_t* fpr_result) |
| 2614 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2615 | DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current())); |
| 2616 | CHECK(gpr_result != nullptr); |
| 2617 | CHECK(fpr_result != nullptr); |
| 2618 | // Instrumentation exit stub must not be entered with a pending exception. |
| 2619 | CHECK(!self->IsExceptionPending()) |
| 2620 | << "Enter instrumentation exit stub with pending exception " << self->GetException()->Dump(); |
| 2621 | |
| 2622 | instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation(); |
Mythri Alle | 9575c12 | 2021-11-12 12:04:41 +0000 | [diff] [blame] | 2623 | DCHECK(instr->AreExitStubsInstalled()); |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 2624 | bool is_ref; |
| 2625 | JValue return_value = instr->GetReturnValue(self, method, &is_ref, gpr_result, fpr_result); |
| 2626 | bool deoptimize = false; |
| 2627 | { |
| 2628 | StackHandleScope<1> hs(self); |
| 2629 | MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr)); |
| 2630 | if (is_ref) { |
| 2631 | // Take a handle to the return value so we won't lose it if we suspend. |
| 2632 | res.Assign(return_value.GetL()); |
| 2633 | } |
| 2634 | DCHECK(!method->IsRuntimeMethod()); |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 2635 | |
| 2636 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 2637 | // back to an upcall. |
| 2638 | NthCallerVisitor visitor(self, 1, true); |
| 2639 | visitor.WalkStack(true); |
| 2640 | deoptimize = instr->ShouldDeoptimizeMethod(self, visitor); |
| 2641 | |
Mythri Alle | 2a0ad75 | 2021-12-02 17:45:50 +0000 | [diff] [blame] | 2642 | // If we need a deoptimization MethodExitEvent will be called by the interpreter when it |
| 2643 | // re-executes the return instruction. |
| 2644 | if (!deoptimize) { |
| 2645 | instr->MethodExitEvent(self, |
| 2646 | method, |
| 2647 | /* frame= */ {}, |
| 2648 | return_value); |
| 2649 | } |
| 2650 | |
Mythri Alle | 5097f83 | 2021-11-02 14:52:30 +0000 | [diff] [blame] | 2651 | if (is_ref) { |
| 2652 | // Restore the return value if it's a reference since it might have moved. |
| 2653 | *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get(); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | if (self->IsExceptionPending() || self->ObserveAsyncException()) { |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | if (deoptimize) { |
| 2662 | DeoptimizationMethodType deopt_method_type = instr->GetDeoptimizationMethodType(method); |
| 2663 | self->PushDeoptimizationContext(return_value, is_ref, nullptr, false, deopt_method_type); |
| 2664 | artDeoptimize(self); |
| 2665 | UNREACHABLE(); |
| 2666 | } |
| 2667 | |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2671 | } // namespace art |