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