blob: 0a186f4dc543934a590a246315922f334a3178a9 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
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 Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "common_throws.h"
Vladimir Marko606adb32018-04-05 14:49:24 +010022#include "debug_print.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070023#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file-inl.h"
25#include "dex/dex_file_types.h"
26#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070030#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imt_conflict_table.h"
32#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000033#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070034#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "interpreter/interpreter.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000036#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000037#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000038#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070039#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070040#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070041#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000042#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010045#include "mirror/var_handle.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010046#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010047#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070048#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070049#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070050#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070051#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070052#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010053#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000054#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070055
56namespace art {
57
Andreas Gampe8228cdf2017-05-30 15:03:54 -070058// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070059class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080060 // Number of bytes for each out register in the caller method's frame.
61 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070062 // Frame size in bytes of a callee-save frame for RefsAndArgs.
63 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070064 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070065#if defined(__arm__)
66 // The callee save frame is pointed to by SP.
67 // | argN | |
68 // | ... | |
69 // | arg4 | |
70 // | arg3 spill | | Caller's frame
71 // | arg2 spill | |
72 // | arg1 spill | |
73 // | Method* | ---
74 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080075 // | ... | 4x6 bytes callee saves
76 // | R3 |
77 // | R2 |
78 // | R1 |
79 // | S15 |
80 // | : |
81 // | S0 |
82 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070083 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070084 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
85 static constexpr bool kAlignPairRegister = true;
86 static constexpr bool kQuickSoftFloatAbi = false;
87 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000088 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080089 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -070090 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080091 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070093 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080094 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070095 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080096 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070097 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080098 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000099 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800100 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101#elif defined(__aarch64__)
102 // The callee save frame is pointed to by SP.
103 // | argN | |
104 // | ... | |
105 // | arg4 | |
106 // | arg3 spill | | Caller's frame
107 // | arg2 spill | |
108 // | arg1 spill | |
109 // | Method* | ---
110 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800111 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100113 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 // | X7 |
115 // | : |
116 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800117 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 // | : |
119 // | D0 |
120 // | | padding
121 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500122 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000123 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000124 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800125 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000126 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000127 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
128 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800129 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700130 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800131 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700132 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
133 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800134 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700135 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
136 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800137 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700138 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000139 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000140 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000141 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800142#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700143 // The callee save frame is pointed to by SP.
144 // | argN | |
145 // | ... | |
146 // | arg4 | |
147 // | arg3 spill | | Caller's frame
148 // | arg2 spill | |
149 // | arg1 spill | |
150 // | Method* | ---
151 // | RA |
152 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | T1 | arg5
154 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700155 // | A3 | arg3
156 // | A2 | arg2
157 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800158 // | F19 |
159 // | F18 | f_arg5
160 // | F17 |
161 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000162 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800163 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000164 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800165 // | F12 | f_arg2
166 // | F11 |
167 // | F10 | f_arg1
168 // | F9 |
169 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000170 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700171 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000172 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
173 static constexpr bool kAlignPairRegister = true;
174 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800175 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000176 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800177 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
178 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
179 // passed only in even numbered registers and each
180 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800181 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800182 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800185 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000186 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800187 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800188#elif defined(__mips__) && defined(__LP64__)
189 // The callee save frame is pointed to by SP.
190 // | argN | |
191 // | ... | |
192 // | arg4 | |
193 // | arg3 spill | | Caller's frame
194 // | arg2 spill | |
195 // | arg1 spill | |
196 // | Method* | ---
197 // | RA |
198 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800199 // | A7 | arg7
200 // | A6 | arg6
201 // | A5 | arg5
202 // | A4 | arg4
203 // | A3 | arg3
204 // | A2 | arg2
205 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000206 // | F19 | f_arg7
207 // | F18 | f_arg6
208 // | F17 | f_arg5
209 // | F16 | f_arg4
210 // | F15 | f_arg3
211 // | F14 | f_arg2
212 // | F13 | f_arg1
213 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800214 // | | padding
215 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800216 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800217 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800218 static constexpr bool kAlignPairRegister = false;
219 static constexpr bool kQuickSoftFloatAbi = false;
220 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000221 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800222 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
223 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
224 static constexpr bool kGprFprLockstep = true;
225
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800226 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800227 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
228 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
229 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
230 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
231 }
Ian Rogers848871b2013-08-05 10:56:33 -0700232#elif defined(__i386__)
233 // The callee save frame is pointed to by SP.
234 // | argN | |
235 // | ... | |
236 // | arg4 | |
237 // | arg3 spill | | Caller's frame
238 // | arg2 spill | |
239 // | arg1 spill | |
240 // | Method* | ---
241 // | Return |
242 // | EBP,ESI,EDI | callee saves
243 // | EBX | arg3
244 // | EDX | arg2
245 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000246 // | XMM3 | float arg 4
247 // | XMM2 | float arg 3
248 // | XMM1 | float arg 2
249 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700250 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500251 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000252 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000253 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800254 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000255 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800256 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000257 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800258 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000259 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
260 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
261 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000263 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800264 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800266 // The callee save frame is pointed to by SP.
267 // | argN | |
268 // | ... | |
269 // | reg. arg spills | | Caller's frame
270 // | Method* | ---
271 // | Return |
272 // | R15 | callee save
273 // | R14 | callee save
274 // | R13 | callee save
275 // | R12 | callee save
276 // | R9 | arg5
277 // | R8 | arg4
278 // | RSI/R6 | arg1
279 // | RBP/R5 | callee save
280 // | RBX/R3 | callee save
281 // | RDX/R2 | arg2
282 // | RCX/R1 | arg3
283 // | XMM7 | float arg 8
284 // | XMM6 | float arg 7
285 // | XMM5 | float arg 6
286 // | XMM4 | float arg 5
287 // | XMM3 | float arg 4
288 // | XMM2 | float arg 3
289 // | XMM1 | float arg 2
290 // | XMM0 | float arg 1
291 // | Padding |
292 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500293 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000294 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800295 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800296 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000297 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700298 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700299 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800300 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700302 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
303 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800304 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
305 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000306 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
307 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
308 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
309 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
310 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800311 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700312 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
313 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800314 }
315 }
Ian Rogers848871b2013-08-05 10:56:33 -0700316#else
317#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700318#endif
319
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100321 // Special handling for proxy methods. Proxy methods are instance methods so the
322 // 'this' object is the 1st argument. They also have the same frame layout as the
323 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
324 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000325 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000327 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100328 CHECK_GT(kNumQuickGprArgs, 0u);
329 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
330 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
331 GprIndexToGprOffset(kThisGprIndex);
332 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000333 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700338 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100339 }
340
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700341 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 uint8_t* previous_sp =
344 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100346 }
347
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700348 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700350 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
351 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
353 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100354 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100355 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
356 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100357
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100358 if (current_code->IsOptimized()) {
359 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000360 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100361 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100362 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100364 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800365 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
366 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100367 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800368 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100369 }
370 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100371 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100372 }
Ian Rogers848871b2013-08-05 10:56:33 -0700373 }
374
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800375 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
376 REQUIRES_SHARED(Locks::mutator_lock_) {
377 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700378 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
379 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800380 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
381 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
382 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
383 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
384 if (!current_code->IsOptimized()) {
385 return false;
386 }
387 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
388 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
389 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700390 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800391 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
392 if (invoke.IsValid()) {
393 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700394 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800395 return true;
396 }
397 return false;
398 }
399
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700403 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700404 return *reinterpret_cast<uintptr_t*>(lr);
405 }
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700408 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700409 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700410 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
411 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
412 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700413 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800414 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
415 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800416 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
417 "Number of Quick FPR arguments unexpected");
418 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
419 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800420 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
421 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800422 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
423 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700424 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800425 }
Ian Rogers848871b2013-08-05 10:56:33 -0700426
427 virtual ~QuickArgumentVisitor() {}
428
429 virtual void Visit() = 0;
430
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 Primitive::Type GetParamPrimitiveType() const {
432 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700433 }
434
Ian Rogers13735952014-10-08 12:43:28 -0700435 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800436 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800437 Primitive::Type type = GetParamPrimitiveType();
438 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800439 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
440 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
441 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
442 }
443 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000444 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700446 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800447 }
448 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800449 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800450 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
451 }
452 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700453 }
454
455 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
457 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 return is_split_long_or_double_;
459 } else {
460 return false; // An optimization for when GPR and FPRs are 64bit.
461 }
Ian Rogers848871b2013-08-05 10:56:33 -0700462 }
463
Ian Rogers936b37f2014-02-14 00:52:24 -0800464 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700465 return GetParamPrimitiveType() == Primitive::kPrimNot;
466 }
467
Ian Rogers936b37f2014-02-14 00:52:24 -0800468 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700469 Primitive::Type type = GetParamPrimitiveType();
470 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
471 }
472
473 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000474 // The splitted long is always available through the stack.
475 return *reinterpret_cast<uint64_t*>(stack_args_
476 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700477 }
478
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800479 void IncGprIndex() {
480 gpr_index_++;
481 if (kGprFprLockstep) {
482 fpr_index_++;
483 }
484 }
485
486 void IncFprIndex() {
487 fpr_index_++;
488 if (kGprFprLockstep) {
489 gpr_index_++;
490 }
491 }
492
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700493 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800494 // (a) 'stack_args_' should point to the first method's argument
495 // (b) whatever the argument type it is, the 'stack_index_' should
496 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 gpr_index_ = 0;
498 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800499 if (kQuickDoubleRegAlignedFloatBackFilled) {
500 fpr_double_index_ = 0;
501 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800502 stack_index_ = 0;
503 if (!is_static_) { // Handle this.
504 cur_type_ = Primitive::kPrimNot;
505 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700506 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800507 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800508 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800509 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800510 }
Ian Rogers848871b2013-08-05 10:56:33 -0700511 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
513 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
514 switch (cur_type_) {
515 case Primitive::kPrimNot:
516 case Primitive::kPrimBoolean:
517 case Primitive::kPrimByte:
518 case Primitive::kPrimChar:
519 case Primitive::kPrimShort:
520 case Primitive::kPrimInt:
521 is_split_long_or_double_ = false;
522 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800523 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800524 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800525 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800526 }
527 break;
528 case Primitive::kPrimFloat:
529 is_split_long_or_double_ = false;
530 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800531 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800532 if (kQuickSoftFloatAbi) {
533 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800534 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800535 }
536 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800537 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800538 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800539 if (kQuickDoubleRegAlignedFloatBackFilled) {
540 // Double should not overlap with float.
541 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
542 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
543 // Float should not overlap with double.
544 if (fpr_index_ % 2 == 0) {
545 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
546 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000547 } else if (kQuickSkipOddFpRegisters) {
548 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800549 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800550 }
551 }
552 break;
553 case Primitive::kPrimDouble:
554 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800555 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800556 if (cur_type_ == Primitive::kPrimLong &&
557#if defined(__mips__) && !defined(__LP64__)
558 (gpr_index_ == 0 || gpr_index_ == 2) &&
559#else
560 gpr_index_ == 0 &&
561#endif
562 kAlignPairRegister) {
563 // Currently, this is only for ARM and MIPS, where we align long parameters with
564 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
565 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800566 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000567 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000568 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800569 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500570 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
571 // We don't want to split this. Pass over this register.
572 gpr_index_++;
573 is_split_long_or_double_ = false;
574 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800576 if (kBytesStackArgLocation == 4) {
577 stack_index_+= 2;
578 } else {
579 CHECK_EQ(kBytesStackArgLocation, 8U);
580 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700582 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800583 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000584 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700585 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800586 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700587 }
588 }
589 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800590 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000591 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800592 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800593 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700594 if (kBytesStackArgLocation == 4) {
595 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800596 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700597 CHECK_EQ(kBytesStackArgLocation, 8U);
598 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800599 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800600 if (kQuickDoubleRegAlignedFloatBackFilled) {
601 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
602 fpr_double_index_ += 2;
603 // Float should not overlap with double.
604 if (fpr_index_ % 2 == 0) {
605 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
606 }
607 }
608 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800609 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800610 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
611 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800612 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800613 }
614 }
615 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800616 }
617 break;
618 default:
619 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
620 }
Ian Rogers848871b2013-08-05 10:56:33 -0700621 }
622 }
623
Andreas Gampec200a4a2014-06-16 18:39:09 -0700624 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700625 const bool is_static_;
626 const char* const shorty_;
627 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700628
629 private:
Ian Rogers13735952014-10-08 12:43:28 -0700630 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
631 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
632 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800633 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800634 // Index into spilled FPRs.
635 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
636 // holds a higher register number.
637 uint32_t fpr_index_;
638 // Index into spilled FPRs for aligned double.
639 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
640 // terms of singles, may be behind fpr_index.
641 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800642 uint32_t stack_index_; // Index into arguments on the stack.
643 // The current type of argument during VisitArguments.
644 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700645 // Does a 64bit parameter straddle the register and stack arguments?
646 bool is_split_long_or_double_;
647};
648
Sebastien Hertza836bc92014-11-25 16:30:53 +0100649// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
650// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700652 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000653 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
654}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100655
Ian Rogers848871b2013-08-05 10:56:33 -0700656// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800657class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700658 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
660 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700661 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700662
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700663 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700664
665 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800666 ShadowFrame* const sf_;
667 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700668
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700669 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700670};
671
Andreas Gampec200a4a2014-06-16 18:39:09 -0700672void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700673 Primitive::Type type = GetParamPrimitiveType();
674 switch (type) {
675 case Primitive::kPrimLong: // Fall-through.
676 case Primitive::kPrimDouble:
677 if (IsSplitLongOrDouble()) {
678 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
679 } else {
680 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
681 }
682 ++cur_reg_;
683 break;
684 case Primitive::kPrimNot: {
685 StackReference<mirror::Object>* stack_ref =
686 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
687 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
688 }
689 break;
690 case Primitive::kPrimBoolean: // Fall-through.
691 case Primitive::kPrimByte: // Fall-through.
692 case Primitive::kPrimChar: // Fall-through.
693 case Primitive::kPrimShort: // Fall-through.
694 case Primitive::kPrimInt: // Fall-through.
695 case Primitive::kPrimFloat:
696 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
697 break;
698 case Primitive::kPrimVoid:
699 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700700 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700701 }
702 ++cur_reg_;
703}
704
Mingyao Yang417528d2017-09-13 12:10:40 -0700705// Don't inline. See b/65159206.
706NO_INLINE
707static void HandleDeoptimization(JValue* result,
708 ArtMethod* method,
709 ShadowFrame* deopt_frame,
710 ManagedStack* fragment)
711 REQUIRES_SHARED(Locks::mutator_lock_) {
712 // Coming from partial-fragment deopt.
713 Thread* self = Thread::Current();
714 if (kIsDebugBuild) {
715 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
716 // of the call-stack) corresponds to the called method.
717 ShadowFrame* linked = deopt_frame;
718 while (linked->GetLink() != nullptr) {
719 linked = linked->GetLink();
720 }
721 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
722 << ArtMethod::PrettyMethod(linked->GetMethod());
723 }
724
725 if (VLOG_IS_ON(deopt)) {
726 // Print out the stack to verify that it was a partial-fragment deopt.
727 LOG(INFO) << "Continue-ing from deopt. Stack is:";
728 QuickExceptionHandler::DumpFramesWithType(self, true);
729 }
730
731 ObjPtr<mirror::Throwable> pending_exception;
732 bool from_code = false;
733 DeoptimizationMethodType method_type;
734 self->PopDeoptimizationContext(/* out */ result,
735 /* out */ &pending_exception,
736 /* out */ &from_code,
737 /* out */ &method_type);
738
739 // Push a transition back into managed code onto the linked list in thread.
740 self->PushManagedStackFragment(fragment);
741
742 // Ensure that the stack is still in order.
743 if (kIsDebugBuild) {
744 class DummyStackVisitor : public StackVisitor {
745 public:
746 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
747 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
748
749 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
750 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
751 // logic. Just always say we want to continue.
752 return true;
753 }
754 };
755 DummyStackVisitor dsv(self);
756 dsv.WalkStack();
757 }
758
759 // Restore the exception that was pending before deoptimization then interpret the
760 // deoptimized frames.
761 if (pending_exception != nullptr) {
762 self->SetException(pending_exception);
763 }
764 interpreter::EnterInterpreterFromDeoptimize(self,
765 deopt_frame,
766 result,
767 from_code,
768 DeoptimizationMethodType::kDefault);
769}
770
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700772 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700773 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
774 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700775 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700776
Alex Light9139e002015-10-09 15:59:48 -0700777 if (UNLIKELY(!method->IsInvokable())) {
778 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700779 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700780 }
781
782 JValue tmp_value;
783 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700784 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700785 ManagedStack fragment;
786
David Sehr709b0702016-10-13 09:12:37 -0700787 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700788 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700789 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800790 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000791 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700792 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
793
794 JValue result;
795
Mingyao Yang417528d2017-09-13 12:10:40 -0700796 if (UNLIKELY(deopt_frame != nullptr)) {
797 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700798 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700799 const char* old_cause = self->StartAssertNoThreadSuspension(
800 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800801 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700802 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700803 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700804 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700805 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800806 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700807 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800808 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700809 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800810 const bool needs_initialization =
811 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700812 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700813 self->PushManagedStackFragment(&fragment);
814 self->PushShadowFrame(shadow_frame);
815 self->EndAssertNoThreadSuspension(old_cause);
816
Ian Rogerse94652f2014-12-02 11:13:19 -0800817 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700818 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800819 StackHandleScope<1> hs(self);
820 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700821 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700822 DCHECK(Thread::Current()->IsExceptionPending())
823 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700824 self->PopManagedStackFragment(fragment);
825 return 0;
826 }
827 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200828
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800829 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700830 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700831
832 // Pop transition.
833 self->PopManagedStackFragment(fragment);
834
835 // Request a stack deoptimization if needed
836 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700837 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700838 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
839 // should be done and it knows the real return pc.
840 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000841 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
842 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
843 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
844 << caller->PrettyMethod();
845 } else {
846 // Push the context of the deoptimization stack so we can restore the return value and the
847 // exception before executing the deoptimized frames.
848 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700849 result,
850 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
851 self->GetException(),
852 false /* from_code */,
853 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700854
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000855 // Set special exception to cause deoptimization.
856 self->SetException(Thread::GetDeoptimizationException());
857 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700858 }
859
860 // No need to restore the args since the method has already been run by the interpreter.
861 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700862}
863
864// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
865// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800866class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700867 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700869 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700870 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700871
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700872 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700873
874 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700875 ScopedObjectAccessUnchecked* const soa_;
876 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700877
Ian Rogers848871b2013-08-05 10:56:33 -0700878 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
879};
880
Ian Rogers9758f792014-03-13 09:02:55 -0700881void BuildQuickArgumentVisitor::Visit() {
882 jvalue val;
883 Primitive::Type type = GetParamPrimitiveType();
884 switch (type) {
885 case Primitive::kPrimNot: {
886 StackReference<mirror::Object>* stack_ref =
887 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
888 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700889 break;
890 }
891 case Primitive::kPrimLong: // Fall-through.
892 case Primitive::kPrimDouble:
893 if (IsSplitLongOrDouble()) {
894 val.j = ReadSplitLongParam();
895 } else {
896 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
897 }
898 break;
899 case Primitive::kPrimBoolean: // Fall-through.
900 case Primitive::kPrimByte: // Fall-through.
901 case Primitive::kPrimChar: // Fall-through.
902 case Primitive::kPrimShort: // Fall-through.
903 case Primitive::kPrimInt: // Fall-through.
904 case Primitive::kPrimFloat:
905 val.i = *reinterpret_cast<jint*>(GetParamAddress());
906 break;
907 case Primitive::kPrimVoid:
908 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700909 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700910 }
911 args_->push_back(val);
912}
913
Ian Rogers848871b2013-08-05 10:56:33 -0700914// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
915// which is responsible for recording callee save registers. We explicitly place into jobjects the
916// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
917// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918extern "C" uint64_t artQuickProxyInvokeHandler(
919 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700920 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700921 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
922 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700923 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
924 const char* old_cause =
925 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
926 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700927 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700928 self->VerifyStack();
929 // Start new JNI local reference state.
930 JNIEnvExt* env = self->GetJniEnv();
931 ScopedObjectAccessUnchecked soa(env);
932 ScopedJniEnvLocalRefState env_state(env);
933 // Create local ref. copies of proxy method and the receiver.
934 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
935
936 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700937 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700938 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
939 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700940 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700941 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700942 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000943 BuildQuickArgumentVisitor local_ref_visitor(
944 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700945
Ian Rogers848871b2013-08-05 10:56:33 -0700946 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700947 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700948 args.erase(args.begin());
949
950 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700951 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700952 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
953 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700954 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700955 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700956 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700957 ObjPtr<mirror::Method> interface_reflect_method =
958 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
959 if (interface_reflect_method == nullptr) {
960 soa.Self()->AssertPendingOOMException();
961 return 0;
962 }
963 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700964
965 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
966 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700967 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700968 return result.GetJ();
969}
970
Roland Levillainad0777d2018-02-12 20:00:18 +0000971// Visitor returning a reference argument at a given position in a Quick stack frame.
972// NOTE: Only used for testing purposes.
973class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
974 public:
975 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
976 const char* shorty,
977 uint32_t shorty_len,
978 size_t arg_pos)
979 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
980 cur_pos_(0u),
981 arg_pos_(arg_pos),
982 ref_arg_(nullptr) {
983 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
984 }
985
986 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
987 if (cur_pos_ == arg_pos_) {
988 Primitive::Type type = GetParamPrimitiveType();
989 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
990 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
991 }
992 ++cur_pos_;
993 }
994
995 StackReference<mirror::Object>* GetReferenceArgument() {
996 return ref_arg_;
997 }
998
999 private:
1000 // The position of the currently visited argument.
1001 size_t cur_pos_;
1002 // The position of the searched argument.
1003 const size_t arg_pos_;
1004 // The reference argument, if found.
1005 StackReference<mirror::Object>* ref_arg_;
1006
1007 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
1008};
1009
1010// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
1011// NOTE: Only used for testing purposes.
1012extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
1013 ArtMethod** sp)
1014 REQUIRES_SHARED(Locks::mutator_lock_) {
1015 ArtMethod* proxy_method = *sp;
1016 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1017 CHECK(!non_proxy_method->IsStatic())
1018 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1019 uint32_t shorty_len = 0;
1020 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1021 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1022 ref_arg_visitor.VisitArguments();
1023 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1024 return ref_arg;
1025}
1026
1027// Visitor returning all the reference arguments in a Quick stack frame.
1028class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1029 public:
1030 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1031 bool is_static,
1032 const char* shorty,
1033 uint32_t shorty_len)
1034 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1035
1036 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1037 Primitive::Type type = GetParamPrimitiveType();
1038 if (type == Primitive::kPrimNot) {
1039 StackReference<mirror::Object>* ref_arg =
1040 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1041 ref_args_.push_back(ref_arg);
1042 }
1043 }
1044
1045 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1046 return ref_args_;
1047 }
1048
1049 private:
1050 // The reference arguments.
1051 std::vector<StackReference<mirror::Object>*> ref_args_;
1052
1053 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1054};
1055
1056// Returning all reference arguments in Quick stack frame at address `sp`.
1057std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1058 REQUIRES_SHARED(Locks::mutator_lock_) {
1059 ArtMethod* proxy_method = *sp;
1060 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1061 CHECK(!non_proxy_method->IsStatic())
1062 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1063 uint32_t shorty_len = 0;
1064 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1065 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1066 ref_args_visitor.VisitArguments();
1067 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1068 return ref_args;
1069}
1070
Ian Rogers848871b2013-08-05 10:56:33 -07001071// Read object references held in arguments from quick frames and place in a JNI local references,
1072// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001073class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001074 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1076 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001077 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001078
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001079 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001080
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001081 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001082
1083 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001084 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001085 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001086 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1087
Mathieu Chartier590fee92013-09-13 13:46:47 -07001088 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001089};
1090
Ian Rogers9758f792014-03-13 09:02:55 -07001091void RememberForGcArgumentVisitor::Visit() {
1092 if (IsParamAReference()) {
1093 StackReference<mirror::Object>* stack_ref =
1094 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1095 jobject reference =
1096 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1097 references_.push_back(std::make_pair(reference, stack_ref));
1098 }
1099}
1100
1101void RememberForGcArgumentVisitor::FixupReferences() {
1102 // Fixup any references which may have changed.
1103 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001104 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001105 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001106 }
1107}
1108
Alex Lightb7edcda2017-04-27 13:20:31 -07001109extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1110 mirror::Object* this_object,
1111 Thread* self,
1112 ArtMethod** sp)
1113 REQUIRES_SHARED(Locks::mutator_lock_) {
1114 const void* result;
1115 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1116 // that part.
1117 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1118 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1119 if (instrumentation->IsDeoptimized(method)) {
1120 result = GetQuickToInterpreterBridge();
1121 } else {
1122 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1123 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1124 }
1125
1126 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1127 bool is_static = method->IsStatic();
1128 uint32_t shorty_len;
1129 const char* shorty =
1130 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1131
1132 ScopedObjectAccessUnchecked soa(self);
1133 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1134 visitor.VisitArguments();
1135
1136 instrumentation->PushInstrumentationStackFrame(self,
1137 is_static ? nullptr : this_object,
1138 method,
1139 QuickArgumentVisitor::GetCallingPc(sp),
1140 interpreter_entry);
1141
1142 visitor.FixupReferences();
1143 if (UNLIKELY(self->IsExceptionPending())) {
1144 return nullptr;
1145 }
1146 CHECK(result != nullptr) << method->PrettyMethod();
1147 return result;
1148}
1149
1150extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1151 ArtMethod** sp,
1152 uint64_t* gpr_result,
1153 uint64_t* fpr_result)
1154 REQUIRES_SHARED(Locks::mutator_lock_) {
1155 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1156 CHECK(gpr_result != nullptr);
1157 CHECK(fpr_result != nullptr);
1158 // Instrumentation exit stub must not be entered with a pending exception.
1159 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1160 << self->GetException()->Dump();
1161 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001162 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1163 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001164 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1165 return_pc_offset);
1166 CHECK_EQ(*return_pc, 0U);
1167
1168 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1169 // deoptimization shouldn't be performed with the high-half having the return address. When
1170 // deoptimization should be performed the low half is zero and the high-half the address of the
1171 // deoptimization entry point.
1172 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1173 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1174 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001175 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001176 return GetTwoWordFailureValue();
1177 }
1178 return return_or_deoptimize_pc;
1179}
1180
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001181static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1182 REQUIRES_SHARED(Locks::mutator_lock_) {
1183 if (dex_pc == static_cast<uint32_t>(-1)) {
1184 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1185 return "<native>";
1186 } else {
1187 CodeItemInstructionAccessor accessor = method->DexInstructions();
1188 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1189 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1190 }
1191}
1192
Vladimir Marko606adb32018-04-05 14:49:24 +01001193static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1194 REQUIRES_SHARED(Locks::mutator_lock_) {
1195 std::string storage;
1196 const char* descriptor = klass->GetDescriptor(&storage);
1197 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1198 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1199 if (oat_dex_file != nullptr) {
1200 const OatFile* oat_file = oat_dex_file->GetOatFile();
1201 const char* dex2oat_cmdline =
1202 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1203 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1204 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1205 }
1206}
1207
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001208static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1209 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001210 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001211
1212 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1213 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1214
1215 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
1216 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1217 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
1218 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
1219 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1220 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1221 ArtMethod* outer_method = *caller_sp;
1222
1223 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1224 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1225 << " native pc: " << caller_pc << " Instrumented!";
1226 return;
1227 }
1228
1229 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1230 CHECK(current_code != nullptr);
1231 CHECK(current_code->IsOptimized());
1232 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
1233 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
1234 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
1235 CodeInfoEncoding encoding = code_info.ExtractEncoding();
1236 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
1237 CHECK(stack_map.IsValid());
1238 uint32_t dex_pc = stack_map.GetDexPc(encoding.stack_map.encoding);
1239
1240 // Log the outer method and its associated dex file and class table pointer which can be used
1241 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1242 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1243 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1244 << " native pc: " << caller_pc
1245 << " dex pc: " << dex_pc
1246 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1247 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001248 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001249 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1250
1251 ArtMethod* caller = outer_method;
1252 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
1253 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
1254 const InlineInfoEncoding& inline_info_encoding = encoding.inline_info.encoding;
1255 size_t depth = inline_info.GetDepth(inline_info_encoding);
1256 for (size_t d = 0; d < depth; ++d) {
1257 const char* tag = "";
1258 dex_pc = inline_info.GetDexPcAtDepth(inline_info_encoding, d);
1259 if (inline_info.EncodesArtMethodAtDepth(inline_info_encoding, d)) {
1260 tag = "encoded ";
1261 caller = inline_info.GetArtMethodAtDepth(inline_info_encoding, d);
1262 } else {
1263 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info_encoding,
1264 method_info,
1265 d);
1266 if (dex_pc == static_cast<uint32_t>(-1)) {
1267 tag = "special ";
1268 CHECK_EQ(d + 1u, depth);
1269 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1270 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1271 } else {
1272 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1273 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1274 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1275 CHECK(caller != nullptr);
1276 }
1277 }
1278 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1279 << " dex pc: " << dex_pc
1280 << " dex file: " << caller->GetDexFile()->GetLocation()
1281 << " class table: "
Vladimir Marko606adb32018-04-05 14:49:24 +01001282 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1283 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001284 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1285 }
1286 }
1287}
1288
Ian Rogers848871b2013-08-05 10:56:33 -07001289// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001290extern "C" const void* artQuickResolutionTrampoline(
1291 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001292 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001293 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1294 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1295 // does not have the same stack layout as the callee-save method).
1296 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001297 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001298 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001299 ScopedObjectAccessUnchecked soa(env);
1300 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001301 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001302
1303 // Compute details about the called method (avoid GCs)
1304 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001305 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001306 MethodReference called_method(nullptr, 0);
1307 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001309 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001310 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001311 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001312
1313 InvokeType stack_map_invoke_type;
1314 uint32_t stack_map_dex_method_idx;
1315 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1316 &stack_map_invoke_type,
1317 &stack_map_dex_method_idx);
1318 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1319 // code.
1320 if (!found_stack_map || kIsDebugBuild) {
1321 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001322 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001323 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1324 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001325 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001326 bool is_range;
1327 switch (instr_code) {
1328 case Instruction::INVOKE_DIRECT:
1329 invoke_type = kDirect;
1330 is_range = false;
1331 break;
1332 case Instruction::INVOKE_DIRECT_RANGE:
1333 invoke_type = kDirect;
1334 is_range = true;
1335 break;
1336 case Instruction::INVOKE_STATIC:
1337 invoke_type = kStatic;
1338 is_range = false;
1339 break;
1340 case Instruction::INVOKE_STATIC_RANGE:
1341 invoke_type = kStatic;
1342 is_range = true;
1343 break;
1344 case Instruction::INVOKE_SUPER:
1345 invoke_type = kSuper;
1346 is_range = false;
1347 break;
1348 case Instruction::INVOKE_SUPER_RANGE:
1349 invoke_type = kSuper;
1350 is_range = true;
1351 break;
1352 case Instruction::INVOKE_VIRTUAL:
1353 invoke_type = kVirtual;
1354 is_range = false;
1355 break;
1356 case Instruction::INVOKE_VIRTUAL_RANGE:
1357 invoke_type = kVirtual;
1358 is_range = true;
1359 break;
1360 case Instruction::INVOKE_INTERFACE:
1361 invoke_type = kInterface;
1362 is_range = false;
1363 break;
1364 case Instruction::INVOKE_INTERFACE_RANGE:
1365 invoke_type = kInterface;
1366 is_range = true;
1367 break;
1368 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001369 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001370 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001371 UNREACHABLE();
1372 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001373 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001374 // Check that the invoke matches what we expected, note that this path only happens for debug
1375 // builds.
1376 if (found_stack_map) {
1377 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1378 if (invoke_type != kSuper) {
1379 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001380 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001381 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001382 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001383 }
1384 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001385 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001386 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001387 }
1388 } else {
1389 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001390 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001391 }
Ian Rogers848871b2013-08-05 10:56:33 -07001392 } else {
1393 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001394 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001395 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001396 }
1397 uint32_t shorty_len;
1398 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001399 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001400 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001401 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001402 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001403 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001404 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001405 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001406 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001407 mirror::Object* dummy = nullptr;
1408 HandleWrapper<mirror::Object> h_receiver(
1409 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001410 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001411 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001412 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001413
1414 // Update .bss entry in oat file if any.
1415 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001416 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1417 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1418 called_method.index,
1419 called_method.dex_file->NumMethodIds(),
1420 static_cast<size_t>(kRuntimePointerSize));
1421 if (bss_offset != IndexBssMappingLookup::npos) {
1422 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1423 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1424 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1425 oat_file->BssBegin() + bss_offset));
1426 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1427 DCHECK_LT(method_entry,
1428 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1429 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001430 }
1431 }
Ian Rogers848871b2013-08-05 10:56:33 -07001432 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001433 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001434 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001435 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001436 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001437 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001438 if (virtual_or_interface || invoke_type == kSuper) {
1439 // Refine called method based on receiver for kVirtual/kInterface, and
1440 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001441 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001442 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001443 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001444 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001445 } else if (invoke_type == kInterface) {
1446 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001447 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001448 } else {
1449 DCHECK_EQ(invoke_type, kSuper);
1450 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001451 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001452 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001453 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001454 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001455 } else {
1456 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001457 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001458 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001459 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001460
David Sehr709b0702016-10-13 09:12:37 -07001461 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1462 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001463 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001464 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001465
Ian Rogers848871b2013-08-05 10:56:33 -07001466 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001467 StackHandleScope<1> hs(soa.Self());
1468 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001469 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001470 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001471 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1472 // If we are single-stepping or the called method is deoptimized (by a
1473 // breakpoint, for example), then we have to execute the called method
1474 // with the interpreter.
1475 code = GetQuickToInterpreterBridge();
1476 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1477 // If the caller is deoptimized (by a breakpoint, for example), we have to
1478 // continue its execution with interpreter when returning from the called
1479 // method. Because we do not want to execute the called method with the
1480 // interpreter, we wrap its execution into the instrumentation stubs.
1481 // When the called method returns, it will execute the instrumentation
1482 // exit hook that will determine the need of the interpreter with a call
1483 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1484 // it is needed.
1485 code = GetQuickInstrumentationEntryPoint();
1486 } else {
1487 code = called->GetEntryPointFromQuickCompiledCode();
1488 }
Ian Rogers848871b2013-08-05 10:56:33 -07001489 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001490 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1491 // If we are single-stepping or the called method is deoptimized (by a
1492 // breakpoint, for example), then we have to execute the called method
1493 // with the interpreter.
1494 code = GetQuickToInterpreterBridge();
1495 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001496 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1497 // until class is initialized to stop races between threads).
1498 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001499 } else {
1500 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001501 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001502 }
1503 } else {
1504 DCHECK(called_class->IsErroneous());
1505 }
1506 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001507 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001508 // Fixup any locally saved objects may have moved during a GC.
1509 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001510 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001511 *sp = called;
1512
Ian Rogers848871b2013-08-05 10:56:33 -07001513 return code;
1514}
1515
Andreas Gampec147b002014-03-06 18:11:06 -08001516/*
1517 * This class uses a couple of observations to unite the different calling conventions through
1518 * a few constants.
1519 *
1520 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1521 * possible alignment.
1522 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1523 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1524 * when we have to split things
1525 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1526 * and we can use Int handling directly.
1527 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1528 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1529 * extension should be compatible with Aarch64, which mandates copying the available bits
1530 * into LSB and leaving the rest unspecified.
1531 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1532 * the stack.
1533 * 6) There is only little endian.
1534 *
1535 *
1536 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1537 * follows:
1538 *
1539 * void PushGpr(uintptr_t): Add a value for the next GPR
1540 *
1541 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1542 * padding, that is, think the architecture is 32b and aligns 64b.
1543 *
1544 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1545 * split this if necessary. The current state will have aligned, if
1546 * necessary.
1547 *
1548 * void PushStack(uintptr_t): Push a value to the stack.
1549 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001550 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001551 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001552 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001553 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001554 *
1555 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001556template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001557 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001558#if defined(__arm__)
1559 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001560 static constexpr bool kNativeSoftFloatAbi = true;
1561 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001562 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1563
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001564 static constexpr size_t kRegistersNeededForLong = 2;
1565 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001566 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001567 static constexpr bool kMultiFPRegistersWidened = false;
1568 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001569 static constexpr bool kAlignLongOnStack = true;
1570 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001571#elif defined(__aarch64__)
1572 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1573 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1574 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1575
1576 static constexpr size_t kRegistersNeededForLong = 1;
1577 static constexpr size_t kRegistersNeededForDouble = 1;
1578 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001579 static constexpr bool kMultiFPRegistersWidened = false;
1580 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001581 static constexpr bool kAlignLongOnStack = false;
1582 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001583#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001584 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001585 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1586 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001587
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001588 static constexpr size_t kRegistersNeededForLong = 2;
1589 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001590 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001591 static constexpr bool kMultiFPRegistersWidened = true;
1592 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001593 static constexpr bool kAlignLongOnStack = true;
1594 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001595#elif defined(__mips__) && defined(__LP64__)
1596 // Let the code prepare GPRs only and we will load the FPRs with same data.
1597 static constexpr bool kNativeSoftFloatAbi = true;
1598 static constexpr size_t kNumNativeGprArgs = 8;
1599 static constexpr size_t kNumNativeFprArgs = 0;
1600
1601 static constexpr size_t kRegistersNeededForLong = 1;
1602 static constexpr size_t kRegistersNeededForDouble = 1;
1603 static constexpr bool kMultiRegistersAligned = false;
1604 static constexpr bool kMultiFPRegistersWidened = false;
1605 static constexpr bool kMultiGPRegistersWidened = true;
1606 static constexpr bool kAlignLongOnStack = false;
1607 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001608#elif defined(__i386__)
1609 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001610 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001611 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1612 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1613
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001614 static constexpr size_t kRegistersNeededForLong = 2;
1615 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001617 static constexpr bool kMultiFPRegistersWidened = false;
1618 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001619 static constexpr bool kAlignLongOnStack = false;
1620 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001621#elif defined(__x86_64__)
1622 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1623 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1624 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1625
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001626 static constexpr size_t kRegistersNeededForLong = 1;
1627 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001628 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001629 static constexpr bool kMultiFPRegistersWidened = false;
1630 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001631 static constexpr bool kAlignLongOnStack = false;
1632 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001633#else
1634#error "Unsupported architecture"
1635#endif
1636
Andreas Gampec147b002014-03-06 18:11:06 -08001637 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 explicit BuildNativeCallFrameStateMachine(T* delegate)
1639 : gpr_index_(kNumNativeGprArgs),
1640 fpr_index_(kNumNativeFprArgs),
1641 stack_entries_(0),
1642 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001643 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1644 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001645 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1646 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001647 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001648
Andreas Gampec200a4a2014-06-16 18:39:09 -07001649 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001650
Ian Rogers1428dce2014-10-21 15:02:15 -07001651 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001652 return gpr_index_ > 0;
1653 }
1654
Andreas Gampec200a4a2014-06-16 18:39:09 -07001655 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001656 if (HavePointerGpr()) {
1657 gpr_index_--;
1658 PushGpr(reinterpret_cast<uintptr_t>(val));
1659 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001661 PushStack(reinterpret_cast<uintptr_t>(val));
1662 gpr_index_ = 0;
1663 }
1664 }
1665
Ian Rogers1428dce2014-10-21 15:02:15 -07001666 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001667 return gpr_index_ > 0;
1668 }
1669
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001670 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001671 uintptr_t handle = PushHandle(ptr);
1672 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001673 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001674 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001675 } else {
1676 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001677 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001678 gpr_index_ = 0;
1679 }
1680 }
1681
Ian Rogers1428dce2014-10-21 15:02:15 -07001682 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001683 return gpr_index_ > 0;
1684 }
1685
1686 void AdvanceInt(uint32_t val) {
1687 if (HaveIntGpr()) {
1688 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001689 if (kMultiGPRegistersWidened) {
1690 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001691 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001692 } else {
1693 PushGpr(val);
1694 }
Andreas Gampec147b002014-03-06 18:11:06 -08001695 } else {
1696 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001697 if (kMultiGPRegistersWidened) {
1698 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001699 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001700 } else {
1701 PushStack(val);
1702 }
Andreas Gampec147b002014-03-06 18:11:06 -08001703 gpr_index_ = 0;
1704 }
1705 }
1706
Ian Rogers1428dce2014-10-21 15:02:15 -07001707 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001708 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1709 }
1710
Ian Rogers1428dce2014-10-21 15:02:15 -07001711 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001712 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1713 kAlignLongOnStack && // and when it needs alignment
1714 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1715 }
1716
Ian Rogers1428dce2014-10-21 15:02:15 -07001717 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001718 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1719 kAlignLongOnStack && // and when it needs 8B alignment
1720 (stack_entries_ & 1) == 1; // counter is odd
1721 }
1722
1723 void AdvanceLong(uint64_t val) {
1724 if (HaveLongGpr()) {
1725 if (LongGprNeedsPadding()) {
1726 PushGpr(0);
1727 gpr_index_--;
1728 }
1729 if (kRegistersNeededForLong == 1) {
1730 PushGpr(static_cast<uintptr_t>(val));
1731 } else {
1732 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1733 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1734 }
1735 gpr_index_ -= kRegistersNeededForLong;
1736 } else {
1737 if (LongStackNeedsPadding()) {
1738 PushStack(0);
1739 stack_entries_++;
1740 }
1741 if (kRegistersNeededForLong == 1) {
1742 PushStack(static_cast<uintptr_t>(val));
1743 stack_entries_++;
1744 } else {
1745 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1746 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1747 stack_entries_ += 2;
1748 }
1749 gpr_index_ = 0;
1750 }
1751 }
1752
Ian Rogers1428dce2014-10-21 15:02:15 -07001753 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001754 return fpr_index_ > 0;
1755 }
1756
Andreas Gampec147b002014-03-06 18:11:06 -08001757 void AdvanceFloat(float val) {
1758 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001759 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001760 } else {
1761 if (HaveFloatFpr()) {
1762 fpr_index_--;
1763 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001764 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001765 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001766 } else {
1767 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001768 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001769 }
1770 } else {
1771 PushFpr4(val);
1772 }
1773 } else {
1774 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001775 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001776 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777 // Note: We need to jump through those hoops to make the compiler happy.
1778 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001779 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001780 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001781 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001782 }
1783 fpr_index_ = 0;
1784 }
1785 }
1786 }
1787
Ian Rogers1428dce2014-10-21 15:02:15 -07001788 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001789 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1790 }
1791
Ian Rogers1428dce2014-10-21 15:02:15 -07001792 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001793 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1794 kAlignDoubleOnStack && // and when it needs alignment
1795 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1796 }
1797
Ian Rogers1428dce2014-10-21 15:02:15 -07001798 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001799 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1800 kAlignDoubleOnStack && // and when it needs 8B alignment
1801 (stack_entries_ & 1) == 1; // counter is odd
1802 }
1803
1804 void AdvanceDouble(uint64_t val) {
1805 if (kNativeSoftFloatAbi) {
1806 AdvanceLong(val);
1807 } else {
1808 if (HaveDoubleFpr()) {
1809 if (DoubleFprNeedsPadding()) {
1810 PushFpr4(0);
1811 fpr_index_--;
1812 }
1813 PushFpr8(val);
1814 fpr_index_ -= kRegistersNeededForDouble;
1815 } else {
1816 if (DoubleStackNeedsPadding()) {
1817 PushStack(0);
1818 stack_entries_++;
1819 }
1820 if (kRegistersNeededForDouble == 1) {
1821 PushStack(static_cast<uintptr_t>(val));
1822 stack_entries_++;
1823 } else {
1824 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1825 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1826 stack_entries_ += 2;
1827 }
1828 fpr_index_ = 0;
1829 }
1830 }
1831 }
1832
Ian Rogers1428dce2014-10-21 15:02:15 -07001833 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001834 return stack_entries_;
1835 }
1836
Ian Rogers1428dce2014-10-21 15:02:15 -07001837 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001838 return kNumNativeGprArgs - gpr_index_;
1839 }
1840
Ian Rogers1428dce2014-10-21 15:02:15 -07001841 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001842 return kNumNativeFprArgs - fpr_index_;
1843 }
1844
1845 private:
1846 void PushGpr(uintptr_t val) {
1847 delegate_->PushGpr(val);
1848 }
1849 void PushFpr4(float val) {
1850 delegate_->PushFpr4(val);
1851 }
1852 void PushFpr8(uint64_t val) {
1853 delegate_->PushFpr8(val);
1854 }
1855 void PushStack(uintptr_t val) {
1856 delegate_->PushStack(val);
1857 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001858 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001859 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001860 }
1861
1862 uint32_t gpr_index_; // Number of free GPRs
1863 uint32_t fpr_index_; // Number of free FPRs
1864 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1865 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001866 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001867};
1868
Andreas Gampec200a4a2014-06-16 18:39:09 -07001869// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1870// in subclasses.
1871//
1872// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1873// them with handles.
1874class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001875 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001876 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1877
1878 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001879
Ian Rogers1428dce2014-10-21 15:02:15 -07001880 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001881 return num_stack_entries_ * sizeof(uintptr_t);
1882 }
1883
Ian Rogers1428dce2014-10-21 15:02:15 -07001884 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001885 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001886 // Align by kStackAlignment.
1887 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001889 }
1890
Ian Rogers1428dce2014-10-21 15:02:15 -07001891 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1892 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001893 // Assumption is OK right now, as we have soft-float arm
1894 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1895 sp8 -= fregs * sizeof(uintptr_t);
1896 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1897 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1898 sp8 -= iregs * sizeof(uintptr_t);
1899 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1900 return sp8;
1901 }
Andreas Gampec147b002014-03-06 18:11:06 -08001902
Andreas Gampec200a4a2014-06-16 18:39:09 -07001903 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001904 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001905 // Native call stack.
1906 sp8 = LayoutCallStack(sp8);
1907 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001908
Andreas Gampec200a4a2014-06-16 18:39:09 -07001909 // Put fprs and gprs below.
1910 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001911
Andreas Gampec200a4a2014-06-16 18:39:09 -07001912 // Return the new bottom.
1913 return sp8;
1914 }
1915
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001916 virtual void WalkHeader(
1917 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001918 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001919 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001920
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001921 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001922 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1923
1924 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001925
1926 for (uint32_t i = 1; i < shorty_len; ++i) {
1927 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1928 switch (cur_type_) {
1929 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001930 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001931 sm.AdvanceHandleScope(
1932 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001933 break;
1934
1935 case Primitive::kPrimBoolean:
1936 case Primitive::kPrimByte:
1937 case Primitive::kPrimChar:
1938 case Primitive::kPrimShort:
1939 case Primitive::kPrimInt:
1940 sm.AdvanceInt(0);
1941 break;
1942 case Primitive::kPrimFloat:
1943 sm.AdvanceFloat(0);
1944 break;
1945 case Primitive::kPrimDouble:
1946 sm.AdvanceDouble(0);
1947 break;
1948 case Primitive::kPrimLong:
1949 sm.AdvanceLong(0);
1950 break;
1951 default:
1952 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001953 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001954 }
1955 }
1956
Ian Rogers1428dce2014-10-21 15:02:15 -07001957 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001958 }
1959
1960 void PushGpr(uintptr_t /* val */) {
1961 // not optimizing registers, yet
1962 }
1963
1964 void PushFpr4(float /* val */) {
1965 // not optimizing registers, yet
1966 }
1967
1968 void PushFpr8(uint64_t /* val */) {
1969 // not optimizing registers, yet
1970 }
1971
1972 void PushStack(uintptr_t /* val */) {
1973 // counting is already done in the superclass
1974 }
1975
Andreas Gampec200a4a2014-06-16 18:39:09 -07001976 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001977 return reinterpret_cast<uintptr_t>(nullptr);
1978 }
1979
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001981 uint32_t num_stack_entries_;
1982};
1983
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001985 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001986 explicit ComputeGenericJniFrameSize(bool critical_native)
1987 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001988
Andreas Gampec200a4a2014-06-16 18:39:09 -07001989 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1990 // is at *m = sp. Will update to point to the bottom of the save frame.
1991 //
1992 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001994 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 ArtMethod* method = **m;
1996
Andreas Gampe542451c2016-07-26 09:02:02 -07001997 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001998
Andreas Gampec200a4a2014-06-16 18:39:09 -07001999 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
2000
2001 // First, fix up the layout of the callee-save frame.
2002 // We have to squeeze in the HandleScope, and relocate the method pointer.
2003
2004 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07002005 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002006
2007 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002009 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002010
2011 sp8 -= scope_and_method;
2012 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002013 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07002014
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07002016 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
2017 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002018
2019 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2020 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2022 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002024 }
2025
Andreas Gampec200a4a2014-06-16 18:39:09 -07002026 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002027 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002028 // Reference cookie and padding
2029 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002030 }
2031
Andreas Gampec200a4a2014-06-16 18:39:09 -07002032 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2033 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002035 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002036 // First, fix up the layout of the callee-save frame.
2037 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002038 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002039
2040 // The bottom of the callee-save frame is now where the method is, *m.
2041 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2042
2043 // Add space for cookie.
2044 LayoutCookie(&sp8);
2045
2046 return sp8;
2047 }
2048
2049 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002050 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2051 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2052 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002053 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002054 Walk(shorty, shorty_len);
2055
2056 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002057 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002058
2059 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2060
2061 // Return the new bottom.
2062 return sp8;
2063 }
2064
2065 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2066
2067 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2068 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002069 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002070
2071 private:
2072 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002073 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002074};
2075
2076uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2077 num_handle_scope_references_++;
2078 return reinterpret_cast<uintptr_t>(nullptr);
2079}
2080
2081void ComputeGenericJniFrameSize::WalkHeader(
2082 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002083 // First 2 parameters are always excluded for @CriticalNative.
2084 if (UNLIKELY(critical_native_)) {
2085 return;
2086 }
2087
Andreas Gampec200a4a2014-06-16 18:39:09 -07002088 // JNIEnv
2089 sm->AdvancePointer(nullptr);
2090
2091 // Class object or this as first argument
2092 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2093}
2094
2095// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2096// the template requirements of BuildGenericJniFrameStateMachine.
2097class FillNativeCall {
2098 public:
2099 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2100 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2101
2102 virtual ~FillNativeCall() {}
2103
2104 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2105 cur_gpr_reg_ = gpr_regs;
2106 cur_fpr_reg_ = fpr_regs;
2107 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002108 }
2109
2110 void PushGpr(uintptr_t val) {
2111 *cur_gpr_reg_ = val;
2112 cur_gpr_reg_++;
2113 }
2114
2115 void PushFpr4(float val) {
2116 *cur_fpr_reg_ = val;
2117 cur_fpr_reg_++;
2118 }
2119
2120 void PushFpr8(uint64_t val) {
2121 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2122 *tmp = val;
2123 cur_fpr_reg_ += 2;
2124 }
2125
2126 void PushStack(uintptr_t val) {
2127 *cur_stack_arg_ = val;
2128 cur_stack_arg_++;
2129 }
2130
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002131 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002132 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002133 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002134 }
2135
2136 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002137 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002138 uint32_t* cur_fpr_reg_;
2139 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002140};
Andreas Gampec147b002014-03-06 18:11:06 -08002141
Andreas Gampec200a4a2014-06-16 18:39:09 -07002142// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2143// of transitioning into native code.
2144class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2145 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002146 BuildGenericJniFrameVisitor(Thread* self,
2147 bool is_static,
2148 bool critical_native,
2149 const char* shorty,
2150 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002151 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002152 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002153 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2154 sm_(&jni_call_) {
2155 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002156 uintptr_t* start_gpr_reg;
2157 uint32_t* start_fpr_reg;
2158 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002159 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002160 &handle_scope_,
2161 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002162 &start_gpr_reg, &start_fpr_reg);
2163
Andreas Gampec200a4a2014-06-16 18:39:09 -07002164 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2165
Igor Murashkin06a04e02016-09-13 15:57:37 -07002166 // First 2 parameters are always excluded for CriticalNative methods.
2167 if (LIKELY(!critical_native)) {
2168 // jni environment is always first argument
2169 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002170
Igor Murashkin06a04e02016-09-13 15:57:37 -07002171 if (is_static) {
2172 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2173 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002174 }
2175 }
2176
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002177 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002178
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002179 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002180
Vladimir Markof39745e2016-01-26 12:16:55 +00002181 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002182 return handle_scope_->GetHandle(0).GetReference();
2183 }
2184
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002185 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002186 return handle_scope_->GetHandle(0).ToJObject();
2187 }
2188
Ian Rogers1428dce2014-10-21 15:02:15 -07002189 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002190 return bottom_of_used_area_;
2191 }
2192
2193 private:
2194 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2195 class FillJniCall FINAL : public FillNativeCall {
2196 public:
2197 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002198 HandleScope* handle_scope, bool critical_native)
2199 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2200 handle_scope_(handle_scope),
2201 cur_entry_(0),
2202 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002204 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002205
2206 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2207 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2208 handle_scope_ = scope;
2209 cur_entry_ = 0U;
2210 }
2211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002212 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002213 // Initialize padding entries.
2214 size_t expected_slots = handle_scope_->NumberOfReferences();
2215 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002216 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002217 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002218
2219 if (!critical_native_) {
2220 // Non-critical natives have at least the self class (jclass) or this (jobject).
2221 DCHECK_NE(cur_entry_, 0U);
2222 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002223 }
2224
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002225 bool CriticalNative() const {
2226 return critical_native_;
2227 }
2228
Andreas Gampec200a4a2014-06-16 18:39:09 -07002229 private:
2230 HandleScope* handle_scope_;
2231 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002232 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002233 };
2234
2235 HandleScope* handle_scope_;
2236 FillJniCall jni_call_;
2237 void* bottom_of_used_area_;
2238
2239 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002240
2241 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2242};
2243
Andreas Gampec200a4a2014-06-16 18:39:09 -07002244uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2245 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002246 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002247 h.Assign(ref);
2248 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2249 cur_entry_++;
2250 return tmp;
2251}
2252
Ian Rogers9758f792014-03-13 09:02:55 -07002253void BuildGenericJniFrameVisitor::Visit() {
2254 Primitive::Type type = GetParamPrimitiveType();
2255 switch (type) {
2256 case Primitive::kPrimLong: {
2257 jlong long_arg;
2258 if (IsSplitLongOrDouble()) {
2259 long_arg = ReadSplitLongParam();
2260 } else {
2261 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2262 }
2263 sm_.AdvanceLong(long_arg);
2264 break;
2265 }
2266 case Primitive::kPrimDouble: {
2267 uint64_t double_arg;
2268 if (IsSplitLongOrDouble()) {
2269 // Read into union so that we don't case to a double.
2270 double_arg = ReadSplitLongParam();
2271 } else {
2272 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2273 }
2274 sm_.AdvanceDouble(double_arg);
2275 break;
2276 }
2277 case Primitive::kPrimNot: {
2278 StackReference<mirror::Object>* stack_ref =
2279 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002280 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002281 break;
2282 }
2283 case Primitive::kPrimFloat:
2284 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2285 break;
2286 case Primitive::kPrimBoolean: // Fall-through.
2287 case Primitive::kPrimByte: // Fall-through.
2288 case Primitive::kPrimChar: // Fall-through.
2289 case Primitive::kPrimShort: // Fall-through.
2290 case Primitive::kPrimInt: // Fall-through.
2291 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2292 break;
2293 case Primitive::kPrimVoid:
2294 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002295 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002296 }
2297}
2298
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002299void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002300 // Clear out rest of the scope.
2301 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002302 if (!jni_call_.CriticalNative()) {
2303 // Install HandleScope.
2304 self->PushHandleScope(handle_scope_);
2305 }
Ian Rogers9758f792014-03-13 09:02:55 -07002306}
2307
Ian Rogers04c31d22014-07-07 21:44:06 -07002308#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002309extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002310#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002311extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002312#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002313
Igor Murashkin06a04e02016-09-13 15:57:37 -07002314static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2315 uint32_t cookie,
2316 bool fast_native ATTRIBUTE_UNUSED,
2317 jobject l,
2318 jobject lock) {
2319 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002320 if (lock != nullptr) {
2321 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2322 } else {
2323 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2324 }
2325}
2326
Igor Murashkin06a04e02016-09-13 15:57:37 -07002327static void artQuickGenericJniEndJNINonRef(Thread* self,
2328 uint32_t cookie,
2329 bool fast_native,
2330 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002331 if (lock != nullptr) {
2332 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002333 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002334 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002335 if (UNLIKELY(fast_native)) {
2336 JniMethodFastEnd(cookie, self);
2337 } else {
2338 JniMethodEnd(cookie, self);
2339 }
Andreas Gampead615172014-04-04 16:20:13 -07002340 }
2341}
2342
Andreas Gampec147b002014-03-06 18:11:06 -08002343/*
2344 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002345 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers.
Andreas Gampec147b002014-03-06 18:11:06 -08002346 * The final element on the stack is a pointer to the native code.
2347 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002348 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002349 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002350 *
Andreas Gampec147b002014-03-06 18:11:06 -08002351 * The return of this function denotes:
2352 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2353 * 2) An error, if the value is negative.
2354 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002355extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002356 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002357 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002358 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002359 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002360 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002361 uint32_t shorty_len = 0;
2362 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002363 bool critical_native = called->IsCriticalNative();
2364 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002365 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002366
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002367 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002368 BuildGenericJniFrameVisitor visitor(self,
2369 called->IsStatic(),
2370 critical_native,
2371 shorty,
2372 shorty_len,
2373 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002374 {
2375 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2376 visitor.VisitArguments();
2377 // FinalizeHandleScope pushes the handle scope on the thread.
2378 visitor.FinalizeHandleScope(self);
2379 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002380
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002381 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002382 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002383
Ian Rogerse0dcd462014-03-08 15:21:04 -08002384 self->VerifyStack();
2385
Vladimir Markof8655b32018-03-21 17:53:56 +00002386 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2387 jit::Jit* jit = runtime->GetJit();
2388 if (jit != nullptr) {
2389 jit->MethodEntered(self, called);
2390 }
2391
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002392 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002393 uint32_t* sp32;
2394 // Skip calling JniMethodStart for @CriticalNative.
2395 if (LIKELY(!critical_native)) {
2396 // Start JNI, save the cookie.
2397 if (called->IsSynchronized()) {
2398 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2399 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2400 if (self->IsExceptionPending()) {
2401 self->PopHandleScope();
2402 // A negative value denotes an error.
2403 return GetTwoWordFailureValue();
2404 }
2405 } else {
2406 if (fast_native) {
2407 cookie = JniMethodFastStart(self);
2408 } else {
2409 DCHECK(normal_native);
2410 cookie = JniMethodStart(self);
2411 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002412 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002413 sp32 = reinterpret_cast<uint32_t*>(sp);
2414 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002415 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002416
Andreas Gampe90546832014-03-12 18:07:19 -07002417 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002418 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002419
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002420 // There are two cases for the content of nativeCode:
2421 // 1) Pointer to the native function.
2422 // 2) Pointer to the trampoline for native code binding.
2423 // In the second case, we need to execute the binding and continue with the actual native function
2424 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002425 DCHECK(nativeCode != nullptr);
2426 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002427#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002428 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002429#else
2430 nativeCode = artFindNativeMethod(self);
2431#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002432
2433 if (nativeCode == nullptr) {
2434 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002435
Igor Murashkin06a04e02016-09-13 15:57:37 -07002436 // @CriticalNative calls do not need to call back into JniMethodEnd.
2437 if (LIKELY(!critical_native)) {
2438 // End JNI, as the assembly will move to deliver the exception.
2439 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2440 if (shorty[0] == 'L') {
2441 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2442 } else {
2443 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2444 }
Andreas Gampead615172014-04-04 16:20:13 -07002445 }
2446
Andreas Gampec200a4a2014-06-16 18:39:09 -07002447 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002448 }
2449 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002450 }
2451
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002452#if defined(__mips__) && !defined(__LP64__)
2453 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2454 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2455 // and load into floating-point registers.
2456 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2457 // view):
2458 // (1)
2459 // | DOUBLE | DOUBLE | other args, if any
2460 // | F12 | F13 | F14 | F15 |
2461 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2462 // (2)
2463 // | DOUBLE | FLOAT | (PAD) | other args, if any
2464 // | F12 | F13 | F14 | |
2465 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2466 // (3)
2467 // | FLOAT | (PAD) | DOUBLE | other args, if any
2468 // | F12 | | F14 | F15 |
2469 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2470 // (4)
2471 // | FLOAT | FLOAT | other args, if any
2472 // | F12 | F14 |
2473 // | SP+0 | SP+4 | SP+8
2474 // As you can see, only the last case (4) is special. In all others we can just
2475 // load F12/F13 and F14/F15 in the same manner.
2476 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2477 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2478 if (nativeCode != nullptr &&
2479 shorty != nullptr &&
2480 shorty_len >= 3 &&
2481 shorty[1] == 'F' &&
2482 shorty[2] == 'F') {
2483 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2484 }
2485#endif
2486
Andreas Gampec200a4a2014-06-16 18:39:09 -07002487 // Return native code addr(lo) and bottom of alloca address(hi).
2488 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2489 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002490}
2491
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002492// Defined in quick_jni_entrypoints.cc.
2493extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2494 jvalue result, uint64_t result_f, ArtMethod* called,
2495 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002496/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002497 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002498 * unlocking.
2499 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002500extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2501 jvalue result,
2502 uint64_t result_f) {
2503 // We're here just back from a native call. We don't have the shared mutator lock at this point
2504 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2505 // anything that requires a mutator lock before that would cause problems as GC may have the
2506 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002507 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002508 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002509 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002510 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002511 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002512 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2513 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002514}
2515
Andreas Gamped58342c2014-06-05 14:18:08 -07002516// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2517// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002518//
Andreas Gamped58342c2014-06-05 14:18:08 -07002519// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002520// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002521
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002522template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002523static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2524 ObjPtr<mirror::Object> this_object,
2525 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002526 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002527 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002528 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002529 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002530 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002531 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002532 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2533 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002534 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002535 {
2536 // Remember the args in case a GC happens in FindMethodFromCode.
2537 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2538 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2539 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002540 method = FindMethodFromCode<type, access_check>(method_idx,
2541 &this_object,
2542 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002543 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002544 visitor.FixupReferences();
2545 }
2546
Ian Rogerse0a02da2014-12-02 14:10:53 -08002547 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002548 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002549 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002550 }
2551 }
2552 DCHECK(!self->IsExceptionPending());
2553 const void* code = method->GetEntryPointFromQuickCompiledCode();
2554
2555 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002556 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002557 << " location: "
2558 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002559
Andreas Gamped58342c2014-06-05 14:18:08 -07002560 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2561 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002562}
2563
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002564// Explicit artInvokeCommon template function declarations to please analysis tool.
2565#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002566 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002567 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002568 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002569
2570EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2571EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2572EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2573EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2574EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2575EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2576EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2577EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2578EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2579EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2580#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2581
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002582// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002583extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002584 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002585 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002586 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002587}
2588
Andreas Gampec200a4a2014-06-16 18:39:09 -07002589extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002590 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002591 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002592 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002593}
2594
Andreas Gampec200a4a2014-06-16 18:39:09 -07002595extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002596 uint32_t method_idx,
2597 mirror::Object* this_object ATTRIBUTE_UNUSED,
2598 Thread* self,
2599 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2600 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2601 // it doesn't cause ObjPtr alignment failure check.
2602 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002603}
2604
Andreas Gampec200a4a2014-06-16 18:39:09 -07002605extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002606 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002607 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002608 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002609}
2610
Andreas Gampec200a4a2014-06-16 18:39:09 -07002611extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002612 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002613 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002614 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002615}
2616
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002617// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2618extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2619 REQUIRES_SHARED(Locks::mutator_lock_) {
2620 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2621 DCHECK(!referrer->IsProxyMethod());
2622 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2623 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2624 DCHECK(result == nullptr ||
2625 result->GetDeclaringClass()->IsInterface() ||
2626 result->GetDeclaringClass() ==
2627 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2628 << result->PrettyMethod();
2629 return result;
2630}
2631
Jeff Hao5667f562017-02-27 19:32:01 -08002632// Determine target of interface dispatch. The interface method and this object are known non-null.
2633// The interface method is the method returned by the dex cache in the conflict trampoline.
2634extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002635 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002636 Thread* self,
2637 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002638 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002639 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002640 StackHandleScope<2> hs(self);
2641 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2642 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002643
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002644 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002645 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002646 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002647
Vladimir Marko302f69c2017-07-25 15:27:15 +01002648 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002649 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002650 // Fetch the dex_method_idx of the target interface method from the caller.
2651 uint32_t dex_method_idx;
2652 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002653 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002654 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002655 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2656 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002657 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002658 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002659 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002660 } else {
2661 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002662 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002663 }
2664
Vladimir Marko302f69c2017-07-25 15:27:15 +01002665 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002666 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002667 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2668 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002669 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002670 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002671 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2672 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2673 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002674 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2675 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2676 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002677 visitor.FixupReferences();
2678 }
2679
Vladimir Marko302f69c2017-07-25 15:27:15 +01002680 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002681 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002682 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002683 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002684 }
2685
2686 DCHECK(!interface_method->IsRuntimeMethod());
2687 // Look whether we have a match in the ImtConflictTable.
2688 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2689 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2690 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2691 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2692 DCHECK(current_table != nullptr);
2693 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2694 } else {
2695 // It seems we aren't really a conflict method!
2696 if (kIsDebugBuild) {
2697 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2698 CHECK_EQ(conflict_method, m)
2699 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2700 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2701 }
2702 method = conflict_method;
2703 }
2704 if (method != nullptr) {
2705 return GetTwoWordSuccessValue(
2706 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2707 reinterpret_cast<uintptr_t>(method));
2708 }
2709
2710 // No match, use the IfTable.
2711 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2712 if (UNLIKELY(method == nullptr)) {
2713 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2714 interface_method, this_object.Get(), caller_method);
2715 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002716 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002717
2718 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2719 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002720 DCHECK(conflict_method->IsRuntimeMethod());
2721 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2722 cls.Get(),
2723 conflict_method,
2724 interface_method,
2725 method,
2726 /*force_new_conflict_method*/false);
2727 if (new_conflict_method != conflict_method) {
2728 // Update the IMT if we create a new conflict method. No fence needed here, as the
2729 // data is consistent.
2730 imt->Set(imt_index,
2731 new_conflict_method,
2732 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002733 }
2734
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002735 const void* code = method->GetEntryPointFromQuickCompiledCode();
2736
2737 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002738 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002739 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002740
Andreas Gamped58342c2014-06-05 14:18:08 -07002741 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2742 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002743}
2744
Orion Hodsonac141392017-01-13 11:53:47 +00002745// Returns shorty type so the caller can determine how to put |result|
2746// into expected registers. The shorty type is static so the compiler
2747// could call different flavors of this code path depending on the
2748// shorty type though this would require different entry points for
2749// each type.
2750extern "C" uintptr_t artInvokePolymorphic(
2751 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002752 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002753 Thread* self,
2754 ArtMethod** sp)
2755 REQUIRES_SHARED(Locks::mutator_lock_) {
2756 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002757 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002758
2759 // Start new JNI local reference state
2760 JNIEnvExt* env = self->GetJniEnv();
2761 ScopedObjectAccessUnchecked soa(env);
2762 ScopedJniEnvLocalRefState env_state(env);
2763 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2764
2765 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2766 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2767 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002768 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2770 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002771 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002772 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002773 const size_t shorty_length = strlen(shorty);
2774 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2775 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002776 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002777
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002778 // Wrap raw_receiver in a Handle for safety.
2779 StackHandleScope<3> hs(self);
2780 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2781 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002782 self->EndAssertNoThreadSuspension(old_cause);
2783
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002784 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002785 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002786 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002787 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002788
2789 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002790 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2791 return static_cast<uintptr_t>('V');
2792 }
2793
Orion Hodsone7732be2017-10-11 14:35:20 +01002794 Handle<mirror::MethodType> method_type(
2795 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2796
Orion Hodsonac141392017-01-13 11:53:47 +00002797 // This implies we couldn't resolve one or more types in this method handle.
2798 if (UNLIKELY(method_type.IsNull())) {
2799 CHECK(self->IsExceptionPending());
2800 return static_cast<uintptr_t>('V');
2801 }
2802
Vladimir Markod7559b72017-09-28 13:50:37 +01002803 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002804 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2805
2806 // Fix references before constructing the shadow frame.
2807 gc_visitor.FixupReferences();
2808
2809 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002810 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2811 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002812 const size_t first_arg = 0;
2813 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2814 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2815 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2816 ScopedStackedShadowFramePusher
2817 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2818 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2819 kMethodIsStatic,
2820 shorty,
2821 strlen(shorty),
2822 shadow_frame,
2823 first_arg);
2824 shadow_frame_builder.VisitArguments();
2825
2826 // Push a transition back into managed code onto the linked list in thread.
2827 ManagedStack fragment;
2828 self->PushManagedStackFragment(&fragment);
2829
2830 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2831 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002832 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002833 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002834 bool success = false;
Orion Hodson537a4fe2018-05-15 13:57:58 +01002835 if (resolved_method->GetDeclaringClass() == mirror::MethodHandle::StaticClass()) {
2836 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2837 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2838 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2839 success = MethodHandleInvokeExact(self,
2840 *shadow_frame,
2841 method_handle,
2842 method_type,
2843 &operands,
2844 result);
2845 } else {
2846 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2847 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2848 success = MethodHandleInvoke(self,
2849 *shadow_frame,
2850 method_handle,
2851 method_type,
2852 &operands,
2853 result);
2854 }
2855 } else {
2856 DCHECK_EQ(mirror::VarHandle::StaticClass(), resolved_method->GetDeclaringClass());
2857 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
2858 ObjPtr<mirror::VarHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2859 mirror::VarHandle::AccessMode access_mode =
2860 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2861 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002862 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002863 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002864 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002865 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002866 &operands,
2867 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002868 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002869
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002870 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002871
2872 // Pop transition record.
2873 self->PopManagedStackFragment(fragment);
2874
2875 return static_cast<uintptr_t>(shorty[0]);
2876}
2877
Ian Rogers848871b2013-08-05 10:56:33 -07002878} // namespace art