blob: 4ca12fe472cfe3bf2637799b7f8738250c87eb77 [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 Marko6ec2a1b2018-05-22 15:33:48 +010036#include "interpreter/shadow_frame-inl.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000037#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000038#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000039#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070041#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070042#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000043#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010046#include "mirror/var_handle.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010047#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010048#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070049#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070050#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070051#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070052#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070053#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010054#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000055#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070056
57namespace art {
58
Andreas Gampe8228cdf2017-05-30 15:03:54 -070059// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070060class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080061 // Number of bytes for each out register in the caller method's frame.
62 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070063 // Frame size in bytes of a callee-save frame for RefsAndArgs.
64 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070065 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070066#if defined(__arm__)
67 // The callee save frame is pointed to by SP.
68 // | argN | |
69 // | ... | |
70 // | arg4 | |
71 // | arg3 spill | | Caller's frame
72 // | arg2 spill | |
73 // | arg1 spill | |
74 // | Method* | ---
75 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080076 // | ... | 4x6 bytes callee saves
77 // | R3 |
78 // | R2 |
79 // | R1 |
80 // | S15 |
81 // | : |
82 // | S0 |
83 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070084 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070085 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
86 static constexpr bool kAlignPairRegister = true;
87 static constexpr bool kQuickSoftFloatAbi = false;
88 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000089 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080090 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -070091 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080092 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080093 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070094 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080095 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070096 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080097 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070098 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080099 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000100 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800101 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000102#elif defined(__aarch64__)
103 // The callee save frame is pointed to by SP.
104 // | argN | |
105 // | ... | |
106 // | arg4 | |
107 // | arg3 spill | | Caller's frame
108 // | arg2 spill | |
109 // | arg1 spill | |
110 // | Method* | ---
111 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800112 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000113 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100114 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000115 // | X7 |
116 // | : |
117 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800118 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 // | : |
120 // | D0 |
121 // | | padding
122 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500123 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000124 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000125 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800126 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000127 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000128 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
129 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800130 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700131 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800132 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700133 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
134 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800135 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700136 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
137 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800138 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700139 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000140 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000141 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000142 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800143#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700144 // The callee save frame is pointed to by SP.
145 // | argN | |
146 // | ... | |
147 // | arg4 | |
148 // | arg3 spill | | Caller's frame
149 // | arg2 spill | |
150 // | arg1 spill | |
151 // | Method* | ---
152 // | RA |
153 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800154 // | T1 | arg5
155 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700156 // | A3 | arg3
157 // | A2 | arg2
158 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800159 // | F19 |
160 // | F18 | f_arg5
161 // | F17 |
162 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000163 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800164 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000165 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800166 // | F12 | f_arg2
167 // | F11 |
168 // | F10 | f_arg1
169 // | F9 |
170 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000171 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700172 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000173 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
174 static constexpr bool kAlignPairRegister = true;
175 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800176 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000177 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800178 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
179 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
180 // passed only in even numbered registers and each
181 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800182 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800186 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000187 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800188 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800189#elif defined(__mips__) && defined(__LP64__)
190 // The callee save frame is pointed to by SP.
191 // | argN | |
192 // | ... | |
193 // | arg4 | |
194 // | arg3 spill | | Caller's frame
195 // | arg2 spill | |
196 // | arg1 spill | |
197 // | Method* | ---
198 // | RA |
199 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800200 // | A7 | arg7
201 // | A6 | arg6
202 // | A5 | arg5
203 // | A4 | arg4
204 // | A3 | arg3
205 // | A2 | arg2
206 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000207 // | F19 | f_arg7
208 // | F18 | f_arg6
209 // | F17 | f_arg5
210 // | F16 | f_arg4
211 // | F15 | f_arg3
212 // | F14 | f_arg2
213 // | F13 | f_arg1
214 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800215 // | | padding
216 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800217 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800218 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800219 static constexpr bool kAlignPairRegister = false;
220 static constexpr bool kQuickSoftFloatAbi = false;
221 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000222 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800223 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
224 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
225 static constexpr bool kGprFprLockstep = true;
226
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800227 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800228 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
229 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
230 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
231 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
232 }
Ian Rogers848871b2013-08-05 10:56:33 -0700233#elif defined(__i386__)
234 // The callee save frame is pointed to by SP.
235 // | argN | |
236 // | ... | |
237 // | arg4 | |
238 // | arg3 spill | | Caller's frame
239 // | arg2 spill | |
240 // | arg1 spill | |
241 // | Method* | ---
242 // | Return |
243 // | EBP,ESI,EDI | callee saves
244 // | EBX | arg3
245 // | EDX | arg2
246 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 // | XMM3 | float arg 4
248 // | XMM2 | float arg 3
249 // | XMM1 | float arg 2
250 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700251 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500252 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000253 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000254 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800255 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000256 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800257 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000258 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800259 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000260 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
261 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
262 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800263 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000264 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800265 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800266#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800267 // The callee save frame is pointed to by SP.
268 // | argN | |
269 // | ... | |
270 // | reg. arg spills | | Caller's frame
271 // | Method* | ---
272 // | Return |
273 // | R15 | callee save
274 // | R14 | callee save
275 // | R13 | callee save
276 // | R12 | callee save
277 // | R9 | arg5
278 // | R8 | arg4
279 // | RSI/R6 | arg1
280 // | RBP/R5 | callee save
281 // | RBX/R3 | callee save
282 // | RDX/R2 | arg2
283 // | RCX/R1 | arg3
284 // | XMM7 | float arg 8
285 // | XMM6 | float arg 7
286 // | XMM5 | float arg 6
287 // | XMM4 | float arg 5
288 // | XMM3 | float arg 4
289 // | XMM2 | float arg 3
290 // | XMM1 | float arg 2
291 // | XMM0 | float arg 1
292 // | Padding |
293 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500294 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000295 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800296 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800297 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000298 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700299 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700300 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800301 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700303 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
304 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800305 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
306 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000307 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
308 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
309 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
310 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
311 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700313 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
314 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800315 }
316 }
Ian Rogers848871b2013-08-05 10:56:33 -0700317#else
318#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700319#endif
320
Ian Rogers936b37f2014-02-14 00:52:24 -0800321 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100322 // Special handling for proxy methods. Proxy methods are instance methods so the
323 // 'this' object is the 1st argument. They also have the same frame layout as the
324 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
325 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000326 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700327 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000328 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100329 CHECK_GT(kNumQuickGprArgs, 0u);
330 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
331 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
332 GprIndexToGprOffset(kThisGprIndex);
333 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000334 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100335 }
336
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700339 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100340 }
341
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700342 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344 uint8_t* previous_sp =
345 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100347 }
348
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700351 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
352 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
354 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100355 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100356 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
357 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100358
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100359 if (current_code->IsOptimized()) {
360 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000361 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100362 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100363 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800364 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100365 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800366 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
367 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100368 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800369 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100370 }
371 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100372 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100373 }
Ian Rogers848871b2013-08-05 10:56:33 -0700374 }
375
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800376 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
377 REQUIRES_SHARED(Locks::mutator_lock_) {
378 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700379 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
380 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800381 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
382 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
383 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
384 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
385 if (!current_code->IsOptimized()) {
386 return false;
387 }
388 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
389 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
390 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700391 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800392 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
393 if (invoke.IsValid()) {
394 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700395 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800396 return true;
397 }
398 return false;
399 }
400
Ian Rogers936b37f2014-02-14 00:52:24 -0800401 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700402 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700404 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700405 return *reinterpret_cast<uintptr_t*>(lr);
406 }
407
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700409 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700410 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700411 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
412 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
413 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800415 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
416 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800417 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
418 "Number of Quick FPR arguments unexpected");
419 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
420 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
422 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800423 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
424 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700425 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800426 }
Ian Rogers848871b2013-08-05 10:56:33 -0700427
428 virtual ~QuickArgumentVisitor() {}
429
430 virtual void Visit() = 0;
431
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 Primitive::Type GetParamPrimitiveType() const {
433 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700434 }
435
Ian Rogers13735952014-10-08 12:43:28 -0700436 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800437 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800438 Primitive::Type type = GetParamPrimitiveType();
439 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800440 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
441 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
442 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
443 }
444 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000445 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700447 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800448 }
449 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800450 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800451 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
452 }
453 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700454 }
455
456 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
458 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800459 return is_split_long_or_double_;
460 } else {
461 return false; // An optimization for when GPR and FPRs are 64bit.
462 }
Ian Rogers848871b2013-08-05 10:56:33 -0700463 }
464
Ian Rogers936b37f2014-02-14 00:52:24 -0800465 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700466 return GetParamPrimitiveType() == Primitive::kPrimNot;
467 }
468
Ian Rogers936b37f2014-02-14 00:52:24 -0800469 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700470 Primitive::Type type = GetParamPrimitiveType();
471 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
472 }
473
474 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000475 // The splitted long is always available through the stack.
476 return *reinterpret_cast<uint64_t*>(stack_args_
477 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700478 }
479
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800480 void IncGprIndex() {
481 gpr_index_++;
482 if (kGprFprLockstep) {
483 fpr_index_++;
484 }
485 }
486
487 void IncFprIndex() {
488 fpr_index_++;
489 if (kGprFprLockstep) {
490 gpr_index_++;
491 }
492 }
493
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700494 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800495 // (a) 'stack_args_' should point to the first method's argument
496 // (b) whatever the argument type it is, the 'stack_index_' should
497 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 gpr_index_ = 0;
499 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800500 if (kQuickDoubleRegAlignedFloatBackFilled) {
501 fpr_double_index_ = 0;
502 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800503 stack_index_ = 0;
504 if (!is_static_) { // Handle this.
505 cur_type_ = Primitive::kPrimNot;
506 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700507 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800508 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800509 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800510 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800511 }
Ian Rogers848871b2013-08-05 10:56:33 -0700512 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800513 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
514 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
515 switch (cur_type_) {
516 case Primitive::kPrimNot:
517 case Primitive::kPrimBoolean:
518 case Primitive::kPrimByte:
519 case Primitive::kPrimChar:
520 case Primitive::kPrimShort:
521 case Primitive::kPrimInt:
522 is_split_long_or_double_ = false;
523 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800524 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800525 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800526 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800527 }
528 break;
529 case Primitive::kPrimFloat:
530 is_split_long_or_double_ = false;
531 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800532 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800533 if (kQuickSoftFloatAbi) {
534 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800535 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800536 }
537 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800538 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800539 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800540 if (kQuickDoubleRegAlignedFloatBackFilled) {
541 // Double should not overlap with float.
542 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
543 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
544 // Float should not overlap with double.
545 if (fpr_index_ % 2 == 0) {
546 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
547 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000548 } else if (kQuickSkipOddFpRegisters) {
549 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800550 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800551 }
552 }
553 break;
554 case Primitive::kPrimDouble:
555 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800556 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800557 if (cur_type_ == Primitive::kPrimLong &&
558#if defined(__mips__) && !defined(__LP64__)
559 (gpr_index_ == 0 || gpr_index_ == 2) &&
560#else
561 gpr_index_ == 0 &&
562#endif
563 kAlignPairRegister) {
564 // Currently, this is only for ARM and MIPS, where we align long parameters with
565 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
566 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800567 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000568 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000569 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800570 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500571 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
572 // We don't want to split this. Pass over this register.
573 gpr_index_++;
574 is_split_long_or_double_ = false;
575 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800576 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800577 if (kBytesStackArgLocation == 4) {
578 stack_index_+= 2;
579 } else {
580 CHECK_EQ(kBytesStackArgLocation, 8U);
581 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800582 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700583 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800584 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000585 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700586 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800587 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700588 }
589 }
590 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800591 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000592 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800593 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800594 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700595 if (kBytesStackArgLocation == 4) {
596 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800597 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700598 CHECK_EQ(kBytesStackArgLocation, 8U);
599 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800600 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800601 if (kQuickDoubleRegAlignedFloatBackFilled) {
602 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
603 fpr_double_index_ += 2;
604 // Float should not overlap with double.
605 if (fpr_index_ % 2 == 0) {
606 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
607 }
608 }
609 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800610 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800611 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
612 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800613 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800614 }
615 }
616 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800617 }
618 break;
619 default:
620 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
621 }
Ian Rogers848871b2013-08-05 10:56:33 -0700622 }
623 }
624
Andreas Gampec200a4a2014-06-16 18:39:09 -0700625 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700626 const bool is_static_;
627 const char* const shorty_;
628 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700629
630 private:
Ian Rogers13735952014-10-08 12:43:28 -0700631 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
632 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
633 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800634 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800635 // Index into spilled FPRs.
636 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
637 // holds a higher register number.
638 uint32_t fpr_index_;
639 // Index into spilled FPRs for aligned double.
640 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
641 // terms of singles, may be behind fpr_index.
642 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800643 uint32_t stack_index_; // Index into arguments on the stack.
644 // The current type of argument during VisitArguments.
645 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700646 // Does a 64bit parameter straddle the register and stack arguments?
647 bool is_split_long_or_double_;
648};
649
Sebastien Hertza836bc92014-11-25 16:30:53 +0100650// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
651// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700652extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700653 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000654 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
655}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100656
Ian Rogers848871b2013-08-05 10:56:33 -0700657// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800658class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700659 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700660 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
661 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700662 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700663
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700664 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700665
666 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800667 ShadowFrame* const sf_;
668 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700669
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700670 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700671};
672
Andreas Gampec200a4a2014-06-16 18:39:09 -0700673void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700674 Primitive::Type type = GetParamPrimitiveType();
675 switch (type) {
676 case Primitive::kPrimLong: // Fall-through.
677 case Primitive::kPrimDouble:
678 if (IsSplitLongOrDouble()) {
679 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
680 } else {
681 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
682 }
683 ++cur_reg_;
684 break;
685 case Primitive::kPrimNot: {
686 StackReference<mirror::Object>* stack_ref =
687 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
688 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
689 }
690 break;
691 case Primitive::kPrimBoolean: // Fall-through.
692 case Primitive::kPrimByte: // Fall-through.
693 case Primitive::kPrimChar: // Fall-through.
694 case Primitive::kPrimShort: // Fall-through.
695 case Primitive::kPrimInt: // Fall-through.
696 case Primitive::kPrimFloat:
697 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
698 break;
699 case Primitive::kPrimVoid:
700 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700701 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700702 }
703 ++cur_reg_;
704}
705
Mingyao Yang417528d2017-09-13 12:10:40 -0700706// Don't inline. See b/65159206.
707NO_INLINE
708static void HandleDeoptimization(JValue* result,
709 ArtMethod* method,
710 ShadowFrame* deopt_frame,
711 ManagedStack* fragment)
712 REQUIRES_SHARED(Locks::mutator_lock_) {
713 // Coming from partial-fragment deopt.
714 Thread* self = Thread::Current();
715 if (kIsDebugBuild) {
716 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
717 // of the call-stack) corresponds to the called method.
718 ShadowFrame* linked = deopt_frame;
719 while (linked->GetLink() != nullptr) {
720 linked = linked->GetLink();
721 }
722 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
723 << ArtMethod::PrettyMethod(linked->GetMethod());
724 }
725
726 if (VLOG_IS_ON(deopt)) {
727 // Print out the stack to verify that it was a partial-fragment deopt.
728 LOG(INFO) << "Continue-ing from deopt. Stack is:";
729 QuickExceptionHandler::DumpFramesWithType(self, true);
730 }
731
732 ObjPtr<mirror::Throwable> pending_exception;
733 bool from_code = false;
734 DeoptimizationMethodType method_type;
735 self->PopDeoptimizationContext(/* out */ result,
736 /* out */ &pending_exception,
737 /* out */ &from_code,
738 /* out */ &method_type);
739
740 // Push a transition back into managed code onto the linked list in thread.
741 self->PushManagedStackFragment(fragment);
742
743 // Ensure that the stack is still in order.
744 if (kIsDebugBuild) {
745 class DummyStackVisitor : public StackVisitor {
746 public:
747 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
748 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
749
750 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
751 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
752 // logic. Just always say we want to continue.
753 return true;
754 }
755 };
756 DummyStackVisitor dsv(self);
757 dsv.WalkStack();
758 }
759
760 // Restore the exception that was pending before deoptimization then interpret the
761 // deoptimized frames.
762 if (pending_exception != nullptr) {
763 self->SetException(pending_exception);
764 }
765 interpreter::EnterInterpreterFromDeoptimize(self,
766 deopt_frame,
767 result,
768 from_code,
769 DeoptimizationMethodType::kDefault);
770}
771
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700773 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700774 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
775 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700776 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700777
Alex Light9139e002015-10-09 15:59:48 -0700778 if (UNLIKELY(!method->IsInvokable())) {
779 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700780 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700781 }
782
783 JValue tmp_value;
784 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700785 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700786 ManagedStack fragment;
787
David Sehr709b0702016-10-13 09:12:37 -0700788 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700789 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700790 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800791 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000792 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700793 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
794
795 JValue result;
796
Mingyao Yang417528d2017-09-13 12:10:40 -0700797 if (UNLIKELY(deopt_frame != nullptr)) {
798 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700799 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700800 const char* old_cause = self->StartAssertNoThreadSuspension(
801 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800802 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700803 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700804 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700805 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700806 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800807 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700808 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800809 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700810 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800811 const bool needs_initialization =
812 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700813 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700814 self->PushManagedStackFragment(&fragment);
815 self->PushShadowFrame(shadow_frame);
816 self->EndAssertNoThreadSuspension(old_cause);
817
Ian Rogerse94652f2014-12-02 11:13:19 -0800818 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700819 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800820 StackHandleScope<1> hs(self);
821 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700822 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700823 DCHECK(Thread::Current()->IsExceptionPending())
824 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700825 self->PopManagedStackFragment(fragment);
826 return 0;
827 }
828 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200829
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800830 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700831 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700832
833 // Pop transition.
834 self->PopManagedStackFragment(fragment);
835
836 // Request a stack deoptimization if needed
837 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700838 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700839 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
840 // should be done and it knows the real return pc.
841 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000842 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
843 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
844 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
845 << caller->PrettyMethod();
846 } else {
847 // Push the context of the deoptimization stack so we can restore the return value and the
848 // exception before executing the deoptimized frames.
849 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700850 result,
851 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
852 self->GetException(),
853 false /* from_code */,
854 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700855
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000856 // Set special exception to cause deoptimization.
857 self->SetException(Thread::GetDeoptimizationException());
858 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700859 }
860
861 // No need to restore the args since the method has already been run by the interpreter.
862 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700863}
864
865// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
866// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800867class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700868 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700870 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700871 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700872
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700873 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700874
875 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700876 ScopedObjectAccessUnchecked* const soa_;
877 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700878
Ian Rogers848871b2013-08-05 10:56:33 -0700879 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
880};
881
Ian Rogers9758f792014-03-13 09:02:55 -0700882void BuildQuickArgumentVisitor::Visit() {
883 jvalue val;
884 Primitive::Type type = GetParamPrimitiveType();
885 switch (type) {
886 case Primitive::kPrimNot: {
887 StackReference<mirror::Object>* stack_ref =
888 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
889 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700890 break;
891 }
892 case Primitive::kPrimLong: // Fall-through.
893 case Primitive::kPrimDouble:
894 if (IsSplitLongOrDouble()) {
895 val.j = ReadSplitLongParam();
896 } else {
897 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
898 }
899 break;
900 case Primitive::kPrimBoolean: // Fall-through.
901 case Primitive::kPrimByte: // Fall-through.
902 case Primitive::kPrimChar: // Fall-through.
903 case Primitive::kPrimShort: // Fall-through.
904 case Primitive::kPrimInt: // Fall-through.
905 case Primitive::kPrimFloat:
906 val.i = *reinterpret_cast<jint*>(GetParamAddress());
907 break;
908 case Primitive::kPrimVoid:
909 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700910 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700911 }
912 args_->push_back(val);
913}
914
Ian Rogers848871b2013-08-05 10:56:33 -0700915// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
916// which is responsible for recording callee save registers. We explicitly place into jobjects the
917// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
918// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919extern "C" uint64_t artQuickProxyInvokeHandler(
920 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700921 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700922 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
923 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700924 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
925 const char* old_cause =
926 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
927 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700928 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700929 self->VerifyStack();
930 // Start new JNI local reference state.
931 JNIEnvExt* env = self->GetJniEnv();
932 ScopedObjectAccessUnchecked soa(env);
933 ScopedJniEnvLocalRefState env_state(env);
934 // Create local ref. copies of proxy method and the receiver.
935 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
936
937 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700938 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700939 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
940 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700941 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700942 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700943 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000944 BuildQuickArgumentVisitor local_ref_visitor(
945 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700946
Ian Rogers848871b2013-08-05 10:56:33 -0700947 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700948 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700949 args.erase(args.begin());
950
951 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700952 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700953 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
954 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700955 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700956 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700957 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700958 ObjPtr<mirror::Method> interface_reflect_method =
959 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
960 if (interface_reflect_method == nullptr) {
961 soa.Self()->AssertPendingOOMException();
962 return 0;
963 }
964 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700965
966 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
967 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700968 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700969 return result.GetJ();
970}
971
Roland Levillainad0777d2018-02-12 20:00:18 +0000972// Visitor returning a reference argument at a given position in a Quick stack frame.
973// NOTE: Only used for testing purposes.
974class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
975 public:
976 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
977 const char* shorty,
978 uint32_t shorty_len,
979 size_t arg_pos)
980 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
981 cur_pos_(0u),
982 arg_pos_(arg_pos),
983 ref_arg_(nullptr) {
984 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
985 }
986
987 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
988 if (cur_pos_ == arg_pos_) {
989 Primitive::Type type = GetParamPrimitiveType();
990 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
991 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
992 }
993 ++cur_pos_;
994 }
995
996 StackReference<mirror::Object>* GetReferenceArgument() {
997 return ref_arg_;
998 }
999
1000 private:
1001 // The position of the currently visited argument.
1002 size_t cur_pos_;
1003 // The position of the searched argument.
1004 const size_t arg_pos_;
1005 // The reference argument, if found.
1006 StackReference<mirror::Object>* ref_arg_;
1007
1008 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
1009};
1010
1011// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
1012// NOTE: Only used for testing purposes.
1013extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
1014 ArtMethod** sp)
1015 REQUIRES_SHARED(Locks::mutator_lock_) {
1016 ArtMethod* proxy_method = *sp;
1017 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1018 CHECK(!non_proxy_method->IsStatic())
1019 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1020 uint32_t shorty_len = 0;
1021 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1022 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1023 ref_arg_visitor.VisitArguments();
1024 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1025 return ref_arg;
1026}
1027
1028// Visitor returning all the reference arguments in a Quick stack frame.
1029class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1030 public:
1031 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1032 bool is_static,
1033 const char* shorty,
1034 uint32_t shorty_len)
1035 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1036
1037 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1038 Primitive::Type type = GetParamPrimitiveType();
1039 if (type == Primitive::kPrimNot) {
1040 StackReference<mirror::Object>* ref_arg =
1041 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1042 ref_args_.push_back(ref_arg);
1043 }
1044 }
1045
1046 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1047 return ref_args_;
1048 }
1049
1050 private:
1051 // The reference arguments.
1052 std::vector<StackReference<mirror::Object>*> ref_args_;
1053
1054 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1055};
1056
1057// Returning all reference arguments in Quick stack frame at address `sp`.
1058std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1059 REQUIRES_SHARED(Locks::mutator_lock_) {
1060 ArtMethod* proxy_method = *sp;
1061 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1062 CHECK(!non_proxy_method->IsStatic())
1063 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1064 uint32_t shorty_len = 0;
1065 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1066 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1067 ref_args_visitor.VisitArguments();
1068 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1069 return ref_args;
1070}
1071
Ian Rogers848871b2013-08-05 10:56:33 -07001072// Read object references held in arguments from quick frames and place in a JNI local references,
1073// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001074class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001075 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001076 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1077 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001078 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001079
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001080 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001081
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001082 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001083
1084 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001085 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001086 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001087 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1088
Mathieu Chartier590fee92013-09-13 13:46:47 -07001089 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001090};
1091
Ian Rogers9758f792014-03-13 09:02:55 -07001092void RememberForGcArgumentVisitor::Visit() {
1093 if (IsParamAReference()) {
1094 StackReference<mirror::Object>* stack_ref =
1095 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1096 jobject reference =
1097 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1098 references_.push_back(std::make_pair(reference, stack_ref));
1099 }
1100}
1101
1102void RememberForGcArgumentVisitor::FixupReferences() {
1103 // Fixup any references which may have changed.
1104 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001105 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001106 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001107 }
1108}
1109
Alex Lightb7edcda2017-04-27 13:20:31 -07001110extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1111 mirror::Object* this_object,
1112 Thread* self,
1113 ArtMethod** sp)
1114 REQUIRES_SHARED(Locks::mutator_lock_) {
1115 const void* result;
1116 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1117 // that part.
1118 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1119 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1120 if (instrumentation->IsDeoptimized(method)) {
1121 result = GetQuickToInterpreterBridge();
1122 } else {
1123 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1124 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1125 }
1126
1127 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1128 bool is_static = method->IsStatic();
1129 uint32_t shorty_len;
1130 const char* shorty =
1131 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1132
1133 ScopedObjectAccessUnchecked soa(self);
1134 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1135 visitor.VisitArguments();
1136
1137 instrumentation->PushInstrumentationStackFrame(self,
1138 is_static ? nullptr : this_object,
1139 method,
1140 QuickArgumentVisitor::GetCallingPc(sp),
1141 interpreter_entry);
1142
1143 visitor.FixupReferences();
1144 if (UNLIKELY(self->IsExceptionPending())) {
1145 return nullptr;
1146 }
1147 CHECK(result != nullptr) << method->PrettyMethod();
1148 return result;
1149}
1150
1151extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1152 ArtMethod** sp,
1153 uint64_t* gpr_result,
1154 uint64_t* fpr_result)
1155 REQUIRES_SHARED(Locks::mutator_lock_) {
1156 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1157 CHECK(gpr_result != nullptr);
1158 CHECK(fpr_result != nullptr);
1159 // Instrumentation exit stub must not be entered with a pending exception.
1160 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1161 << self->GetException()->Dump();
1162 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001163 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1164 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001165 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1166 return_pc_offset);
1167 CHECK_EQ(*return_pc, 0U);
1168
1169 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1170 // deoptimization shouldn't be performed with the high-half having the return address. When
1171 // deoptimization should be performed the low half is zero and the high-half the address of the
1172 // deoptimization entry point.
1173 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1174 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1175 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001176 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001177 return GetTwoWordFailureValue();
1178 }
1179 return return_or_deoptimize_pc;
1180}
1181
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001182static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1183 REQUIRES_SHARED(Locks::mutator_lock_) {
1184 if (dex_pc == static_cast<uint32_t>(-1)) {
1185 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1186 return "<native>";
1187 } else {
1188 CodeItemInstructionAccessor accessor = method->DexInstructions();
1189 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1190 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1191 }
1192}
1193
Vladimir Marko606adb32018-04-05 14:49:24 +01001194static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1195 REQUIRES_SHARED(Locks::mutator_lock_) {
1196 std::string storage;
1197 const char* descriptor = klass->GetDescriptor(&storage);
1198 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1199 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1200 if (oat_dex_file != nullptr) {
1201 const OatFile* oat_file = oat_dex_file->GetOatFile();
1202 const char* dex2oat_cmdline =
1203 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1204 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1205 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1206 }
1207}
1208
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001209static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1210 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001211 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001212
1213 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1214 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1215
1216 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
1217 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1218 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
1219 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
1220 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1221 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1222 ArtMethod* outer_method = *caller_sp;
1223
1224 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1225 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1226 << " native pc: " << caller_pc << " Instrumented!";
1227 return;
1228 }
1229
1230 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1231 CHECK(current_code != nullptr);
1232 CHECK(current_code->IsOptimized());
1233 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
1234 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
1235 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
1236 CodeInfoEncoding encoding = code_info.ExtractEncoding();
1237 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
1238 CHECK(stack_map.IsValid());
1239 uint32_t dex_pc = stack_map.GetDexPc(encoding.stack_map.encoding);
1240
1241 // Log the outer method and its associated dex file and class table pointer which can be used
1242 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1244 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1245 << " native pc: " << caller_pc
1246 << " dex pc: " << dex_pc
1247 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1248 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001249 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001250 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1251
1252 ArtMethod* caller = outer_method;
1253 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
1254 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
1255 const InlineInfoEncoding& inline_info_encoding = encoding.inline_info.encoding;
1256 size_t depth = inline_info.GetDepth(inline_info_encoding);
1257 for (size_t d = 0; d < depth; ++d) {
1258 const char* tag = "";
1259 dex_pc = inline_info.GetDexPcAtDepth(inline_info_encoding, d);
1260 if (inline_info.EncodesArtMethodAtDepth(inline_info_encoding, d)) {
1261 tag = "encoded ";
1262 caller = inline_info.GetArtMethodAtDepth(inline_info_encoding, d);
1263 } else {
1264 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info_encoding,
1265 method_info,
1266 d);
1267 if (dex_pc == static_cast<uint32_t>(-1)) {
1268 tag = "special ";
1269 CHECK_EQ(d + 1u, depth);
1270 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1271 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1272 } else {
1273 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1274 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1275 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1276 CHECK(caller != nullptr);
1277 }
1278 }
1279 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1280 << " dex pc: " << dex_pc
1281 << " dex file: " << caller->GetDexFile()->GetLocation()
1282 << " class table: "
Vladimir Marko606adb32018-04-05 14:49:24 +01001283 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1284 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001285 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1286 }
1287 }
1288}
1289
Ian Rogers848871b2013-08-05 10:56:33 -07001290// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001291extern "C" const void* artQuickResolutionTrampoline(
1292 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001293 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001294 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1295 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1296 // does not have the same stack layout as the callee-save method).
1297 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001298 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001299 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001300 ScopedObjectAccessUnchecked soa(env);
1301 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001302 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001303
1304 // Compute details about the called method (avoid GCs)
1305 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001306 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001307 MethodReference called_method(nullptr, 0);
1308 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001309 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001310 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001311 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001312 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001313
1314 InvokeType stack_map_invoke_type;
1315 uint32_t stack_map_dex_method_idx;
1316 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1317 &stack_map_invoke_type,
1318 &stack_map_dex_method_idx);
1319 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1320 // code.
1321 if (!found_stack_map || kIsDebugBuild) {
1322 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001323 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001324 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1325 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001326 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001327 bool is_range;
1328 switch (instr_code) {
1329 case Instruction::INVOKE_DIRECT:
1330 invoke_type = kDirect;
1331 is_range = false;
1332 break;
1333 case Instruction::INVOKE_DIRECT_RANGE:
1334 invoke_type = kDirect;
1335 is_range = true;
1336 break;
1337 case Instruction::INVOKE_STATIC:
1338 invoke_type = kStatic;
1339 is_range = false;
1340 break;
1341 case Instruction::INVOKE_STATIC_RANGE:
1342 invoke_type = kStatic;
1343 is_range = true;
1344 break;
1345 case Instruction::INVOKE_SUPER:
1346 invoke_type = kSuper;
1347 is_range = false;
1348 break;
1349 case Instruction::INVOKE_SUPER_RANGE:
1350 invoke_type = kSuper;
1351 is_range = true;
1352 break;
1353 case Instruction::INVOKE_VIRTUAL:
1354 invoke_type = kVirtual;
1355 is_range = false;
1356 break;
1357 case Instruction::INVOKE_VIRTUAL_RANGE:
1358 invoke_type = kVirtual;
1359 is_range = true;
1360 break;
1361 case Instruction::INVOKE_INTERFACE:
1362 invoke_type = kInterface;
1363 is_range = false;
1364 break;
1365 case Instruction::INVOKE_INTERFACE_RANGE:
1366 invoke_type = kInterface;
1367 is_range = true;
1368 break;
1369 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001370 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001371 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001372 UNREACHABLE();
1373 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001374 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001375 // Check that the invoke matches what we expected, note that this path only happens for debug
1376 // builds.
1377 if (found_stack_map) {
1378 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1379 if (invoke_type != kSuper) {
1380 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001381 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001382 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001383 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001384 }
1385 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001386 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001387 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001388 }
1389 } else {
1390 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001391 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001392 }
Ian Rogers848871b2013-08-05 10:56:33 -07001393 } else {
1394 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001395 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001396 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001397 }
1398 uint32_t shorty_len;
1399 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001400 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001401 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001402 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001403 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001404 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001405 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001406 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001407 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001408 mirror::Object* dummy = nullptr;
1409 HandleWrapper<mirror::Object> h_receiver(
1410 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001411 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001412 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001413 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001414
1415 // Update .bss entry in oat file if any.
1416 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001417 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1418 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1419 called_method.index,
1420 called_method.dex_file->NumMethodIds(),
1421 static_cast<size_t>(kRuntimePointerSize));
1422 if (bss_offset != IndexBssMappingLookup::npos) {
1423 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1424 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1425 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1426 oat_file->BssBegin() + bss_offset));
1427 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1428 DCHECK_LT(method_entry,
1429 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1430 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001431 }
1432 }
Ian Rogers848871b2013-08-05 10:56:33 -07001433 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001434 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001435 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001436 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001437 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001438 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001439 if (virtual_or_interface || invoke_type == kSuper) {
1440 // Refine called method based on receiver for kVirtual/kInterface, and
1441 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001442 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001443 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001444 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001445 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001446 } else if (invoke_type == kInterface) {
1447 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001448 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001449 } else {
1450 DCHECK_EQ(invoke_type, kSuper);
1451 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001452 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001453 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001454 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001455 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001456 } else {
1457 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001458 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001459 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001460 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001461
David Sehr709b0702016-10-13 09:12:37 -07001462 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1463 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001464 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001465 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001466
Ian Rogers848871b2013-08-05 10:56:33 -07001467 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001468 StackHandleScope<1> hs(soa.Self());
1469 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001470 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001471 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001472 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1473 // If we are single-stepping or the called method is deoptimized (by a
1474 // breakpoint, for example), then we have to execute the called method
1475 // with the interpreter.
1476 code = GetQuickToInterpreterBridge();
1477 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1478 // If the caller is deoptimized (by a breakpoint, for example), we have to
1479 // continue its execution with interpreter when returning from the called
1480 // method. Because we do not want to execute the called method with the
1481 // interpreter, we wrap its execution into the instrumentation stubs.
1482 // When the called method returns, it will execute the instrumentation
1483 // exit hook that will determine the need of the interpreter with a call
1484 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1485 // it is needed.
1486 code = GetQuickInstrumentationEntryPoint();
1487 } else {
1488 code = called->GetEntryPointFromQuickCompiledCode();
1489 }
Ian Rogers848871b2013-08-05 10:56:33 -07001490 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001491 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1492 // If we are single-stepping or the called method is deoptimized (by a
1493 // breakpoint, for example), then we have to execute the called method
1494 // with the interpreter.
1495 code = GetQuickToInterpreterBridge();
1496 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001497 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1498 // until class is initialized to stop races between threads).
1499 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001500 } else {
1501 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001502 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001503 }
1504 } else {
1505 DCHECK(called_class->IsErroneous());
1506 }
1507 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001508 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001509 // Fixup any locally saved objects may have moved during a GC.
1510 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001511 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001512 *sp = called;
1513
Ian Rogers848871b2013-08-05 10:56:33 -07001514 return code;
1515}
1516
Andreas Gampec147b002014-03-06 18:11:06 -08001517/*
1518 * This class uses a couple of observations to unite the different calling conventions through
1519 * a few constants.
1520 *
1521 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1522 * possible alignment.
1523 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1524 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1525 * when we have to split things
1526 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1527 * and we can use Int handling directly.
1528 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1529 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1530 * extension should be compatible with Aarch64, which mandates copying the available bits
1531 * into LSB and leaving the rest unspecified.
1532 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1533 * the stack.
1534 * 6) There is only little endian.
1535 *
1536 *
1537 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1538 * follows:
1539 *
1540 * void PushGpr(uintptr_t): Add a value for the next GPR
1541 *
1542 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1543 * padding, that is, think the architecture is 32b and aligns 64b.
1544 *
1545 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1546 * split this if necessary. The current state will have aligned, if
1547 * necessary.
1548 *
1549 * void PushStack(uintptr_t): Push a value to the stack.
1550 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001551 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001552 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001553 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001554 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001555 *
1556 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001557template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001558 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001559#if defined(__arm__)
1560 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001561 static constexpr bool kNativeSoftFloatAbi = true;
1562 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1564
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001565 static constexpr size_t kRegistersNeededForLong = 2;
1566 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001567 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001568 static constexpr bool kMultiFPRegistersWidened = false;
1569 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001570 static constexpr bool kAlignLongOnStack = true;
1571 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001572#elif defined(__aarch64__)
1573 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1574 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1575 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1576
1577 static constexpr size_t kRegistersNeededForLong = 1;
1578 static constexpr size_t kRegistersNeededForDouble = 1;
1579 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001580 static constexpr bool kMultiFPRegistersWidened = false;
1581 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001582 static constexpr bool kAlignLongOnStack = false;
1583 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001584#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001585 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001586 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1587 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001588
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001589 static constexpr size_t kRegistersNeededForLong = 2;
1590 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001591 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001592 static constexpr bool kMultiFPRegistersWidened = true;
1593 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001594 static constexpr bool kAlignLongOnStack = true;
1595 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001596#elif defined(__mips__) && defined(__LP64__)
1597 // Let the code prepare GPRs only and we will load the FPRs with same data.
1598 static constexpr bool kNativeSoftFloatAbi = true;
1599 static constexpr size_t kNumNativeGprArgs = 8;
1600 static constexpr size_t kNumNativeFprArgs = 0;
1601
1602 static constexpr size_t kRegistersNeededForLong = 1;
1603 static constexpr size_t kRegistersNeededForDouble = 1;
1604 static constexpr bool kMultiRegistersAligned = false;
1605 static constexpr bool kMultiFPRegistersWidened = false;
1606 static constexpr bool kMultiGPRegistersWidened = true;
1607 static constexpr bool kAlignLongOnStack = false;
1608 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001609#elif defined(__i386__)
1610 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001611 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001612 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1613 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1614
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001615 static constexpr size_t kRegistersNeededForLong = 2;
1616 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001617 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001618 static constexpr bool kMultiFPRegistersWidened = false;
1619 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001620 static constexpr bool kAlignLongOnStack = false;
1621 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001622#elif defined(__x86_64__)
1623 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1624 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1625 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1626
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001627 static constexpr size_t kRegistersNeededForLong = 1;
1628 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001629 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001630 static constexpr bool kMultiFPRegistersWidened = false;
1631 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001632 static constexpr bool kAlignLongOnStack = false;
1633 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001634#else
1635#error "Unsupported architecture"
1636#endif
1637
Andreas Gampec147b002014-03-06 18:11:06 -08001638 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001639 explicit BuildNativeCallFrameStateMachine(T* delegate)
1640 : gpr_index_(kNumNativeGprArgs),
1641 fpr_index_(kNumNativeFprArgs),
1642 stack_entries_(0),
1643 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001644 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1645 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001646 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1647 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001648 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001649
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001651
Ian Rogers1428dce2014-10-21 15:02:15 -07001652 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001653 return gpr_index_ > 0;
1654 }
1655
Andreas Gampec200a4a2014-06-16 18:39:09 -07001656 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001657 if (HavePointerGpr()) {
1658 gpr_index_--;
1659 PushGpr(reinterpret_cast<uintptr_t>(val));
1660 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001661 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001662 PushStack(reinterpret_cast<uintptr_t>(val));
1663 gpr_index_ = 0;
1664 }
1665 }
1666
Ian Rogers1428dce2014-10-21 15:02:15 -07001667 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001668 return gpr_index_ > 0;
1669 }
1670
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001671 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001672 uintptr_t handle = PushHandle(ptr);
1673 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001674 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001675 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001676 } else {
1677 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001678 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001679 gpr_index_ = 0;
1680 }
1681 }
1682
Ian Rogers1428dce2014-10-21 15:02:15 -07001683 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001684 return gpr_index_ > 0;
1685 }
1686
1687 void AdvanceInt(uint32_t val) {
1688 if (HaveIntGpr()) {
1689 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001690 if (kMultiGPRegistersWidened) {
1691 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001692 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001693 } else {
1694 PushGpr(val);
1695 }
Andreas Gampec147b002014-03-06 18:11:06 -08001696 } else {
1697 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001698 if (kMultiGPRegistersWidened) {
1699 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001700 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001701 } else {
1702 PushStack(val);
1703 }
Andreas Gampec147b002014-03-06 18:11:06 -08001704 gpr_index_ = 0;
1705 }
1706 }
1707
Ian Rogers1428dce2014-10-21 15:02:15 -07001708 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001709 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1710 }
1711
Ian Rogers1428dce2014-10-21 15:02:15 -07001712 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001713 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1714 kAlignLongOnStack && // and when it needs alignment
1715 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1716 }
1717
Ian Rogers1428dce2014-10-21 15:02:15 -07001718 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001719 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1720 kAlignLongOnStack && // and when it needs 8B alignment
1721 (stack_entries_ & 1) == 1; // counter is odd
1722 }
1723
1724 void AdvanceLong(uint64_t val) {
1725 if (HaveLongGpr()) {
1726 if (LongGprNeedsPadding()) {
1727 PushGpr(0);
1728 gpr_index_--;
1729 }
1730 if (kRegistersNeededForLong == 1) {
1731 PushGpr(static_cast<uintptr_t>(val));
1732 } else {
1733 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1734 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1735 }
1736 gpr_index_ -= kRegistersNeededForLong;
1737 } else {
1738 if (LongStackNeedsPadding()) {
1739 PushStack(0);
1740 stack_entries_++;
1741 }
1742 if (kRegistersNeededForLong == 1) {
1743 PushStack(static_cast<uintptr_t>(val));
1744 stack_entries_++;
1745 } else {
1746 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1747 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1748 stack_entries_ += 2;
1749 }
1750 gpr_index_ = 0;
1751 }
1752 }
1753
Ian Rogers1428dce2014-10-21 15:02:15 -07001754 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001755 return fpr_index_ > 0;
1756 }
1757
Andreas Gampec147b002014-03-06 18:11:06 -08001758 void AdvanceFloat(float val) {
1759 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001760 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001761 } else {
1762 if (HaveFloatFpr()) {
1763 fpr_index_--;
1764 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001765 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001766 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001767 } else {
1768 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001769 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001770 }
1771 } else {
1772 PushFpr4(val);
1773 }
1774 } else {
1775 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001776 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001777 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001778 // Note: We need to jump through those hoops to make the compiler happy.
1779 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001780 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001781 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001782 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001783 }
1784 fpr_index_ = 0;
1785 }
1786 }
1787 }
1788
Ian Rogers1428dce2014-10-21 15:02:15 -07001789 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001790 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1791 }
1792
Ian Rogers1428dce2014-10-21 15:02:15 -07001793 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001794 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1795 kAlignDoubleOnStack && // and when it needs alignment
1796 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1797 }
1798
Ian Rogers1428dce2014-10-21 15:02:15 -07001799 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001800 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1801 kAlignDoubleOnStack && // and when it needs 8B alignment
1802 (stack_entries_ & 1) == 1; // counter is odd
1803 }
1804
1805 void AdvanceDouble(uint64_t val) {
1806 if (kNativeSoftFloatAbi) {
1807 AdvanceLong(val);
1808 } else {
1809 if (HaveDoubleFpr()) {
1810 if (DoubleFprNeedsPadding()) {
1811 PushFpr4(0);
1812 fpr_index_--;
1813 }
1814 PushFpr8(val);
1815 fpr_index_ -= kRegistersNeededForDouble;
1816 } else {
1817 if (DoubleStackNeedsPadding()) {
1818 PushStack(0);
1819 stack_entries_++;
1820 }
1821 if (kRegistersNeededForDouble == 1) {
1822 PushStack(static_cast<uintptr_t>(val));
1823 stack_entries_++;
1824 } else {
1825 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1826 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1827 stack_entries_ += 2;
1828 }
1829 fpr_index_ = 0;
1830 }
1831 }
1832 }
1833
Ian Rogers1428dce2014-10-21 15:02:15 -07001834 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001835 return stack_entries_;
1836 }
1837
Ian Rogers1428dce2014-10-21 15:02:15 -07001838 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001839 return kNumNativeGprArgs - gpr_index_;
1840 }
1841
Ian Rogers1428dce2014-10-21 15:02:15 -07001842 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001843 return kNumNativeFprArgs - fpr_index_;
1844 }
1845
1846 private:
1847 void PushGpr(uintptr_t val) {
1848 delegate_->PushGpr(val);
1849 }
1850 void PushFpr4(float val) {
1851 delegate_->PushFpr4(val);
1852 }
1853 void PushFpr8(uint64_t val) {
1854 delegate_->PushFpr8(val);
1855 }
1856 void PushStack(uintptr_t val) {
1857 delegate_->PushStack(val);
1858 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001859 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001860 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001861 }
1862
1863 uint32_t gpr_index_; // Number of free GPRs
1864 uint32_t fpr_index_; // Number of free FPRs
1865 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1866 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001867 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001868};
1869
Andreas Gampec200a4a2014-06-16 18:39:09 -07001870// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1871// in subclasses.
1872//
1873// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1874// them with handles.
1875class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001876 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001877 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1878
1879 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001880
Ian Rogers1428dce2014-10-21 15:02:15 -07001881 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001882 return num_stack_entries_ * sizeof(uintptr_t);
1883 }
1884
Ian Rogers1428dce2014-10-21 15:02:15 -07001885 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001886 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001887 // Align by kStackAlignment.
1888 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001889 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001890 }
1891
Ian Rogers1428dce2014-10-21 15:02:15 -07001892 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1893 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894 // Assumption is OK right now, as we have soft-float arm
1895 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1896 sp8 -= fregs * sizeof(uintptr_t);
1897 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1898 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1899 sp8 -= iregs * sizeof(uintptr_t);
1900 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1901 return sp8;
1902 }
Andreas Gampec147b002014-03-06 18:11:06 -08001903
Andreas Gampec200a4a2014-06-16 18:39:09 -07001904 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001905 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001906 // Native call stack.
1907 sp8 = LayoutCallStack(sp8);
1908 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001909
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 // Put fprs and gprs below.
1911 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001912
Andreas Gampec200a4a2014-06-16 18:39:09 -07001913 // Return the new bottom.
1914 return sp8;
1915 }
1916
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001917 virtual void WalkHeader(
1918 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001919 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001920 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001921
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001922 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001923 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1924
1925 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001926
1927 for (uint32_t i = 1; i < shorty_len; ++i) {
1928 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1929 switch (cur_type_) {
1930 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001931 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001932 sm.AdvanceHandleScope(
1933 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001934 break;
1935
1936 case Primitive::kPrimBoolean:
1937 case Primitive::kPrimByte:
1938 case Primitive::kPrimChar:
1939 case Primitive::kPrimShort:
1940 case Primitive::kPrimInt:
1941 sm.AdvanceInt(0);
1942 break;
1943 case Primitive::kPrimFloat:
1944 sm.AdvanceFloat(0);
1945 break;
1946 case Primitive::kPrimDouble:
1947 sm.AdvanceDouble(0);
1948 break;
1949 case Primitive::kPrimLong:
1950 sm.AdvanceLong(0);
1951 break;
1952 default:
1953 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001954 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001955 }
1956 }
1957
Ian Rogers1428dce2014-10-21 15:02:15 -07001958 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001959 }
1960
1961 void PushGpr(uintptr_t /* val */) {
1962 // not optimizing registers, yet
1963 }
1964
1965 void PushFpr4(float /* val */) {
1966 // not optimizing registers, yet
1967 }
1968
1969 void PushFpr8(uint64_t /* val */) {
1970 // not optimizing registers, yet
1971 }
1972
1973 void PushStack(uintptr_t /* val */) {
1974 // counting is already done in the superclass
1975 }
1976
Andreas Gampec200a4a2014-06-16 18:39:09 -07001977 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001978 return reinterpret_cast<uintptr_t>(nullptr);
1979 }
1980
Andreas Gampec200a4a2014-06-16 18:39:09 -07001981 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001982 uint32_t num_stack_entries_;
1983};
1984
Andreas Gampec200a4a2014-06-16 18:39:09 -07001985class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001986 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001987 explicit ComputeGenericJniFrameSize(bool critical_native)
1988 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001989
Andreas Gampec200a4a2014-06-16 18:39:09 -07001990 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1991 // is at *m = sp. Will update to point to the bottom of the save frame.
1992 //
1993 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001994 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001995 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 ArtMethod* method = **m;
1997
Andreas Gampe542451c2016-07-26 09:02:02 -07001998 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001999
Andreas Gampec200a4a2014-06-16 18:39:09 -07002000 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
2001
2002 // First, fix up the layout of the callee-save frame.
2003 // We have to squeeze in the HandleScope, and relocate the method pointer.
2004
2005 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07002006 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007
2008 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002011
2012 sp8 -= scope_and_method;
2013 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07002015
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07002017 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
2018 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019
2020 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2021 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2023 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002024 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002025 }
2026
Andreas Gampec200a4a2014-06-16 18:39:09 -07002027 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002028 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002029 // Reference cookie and padding
2030 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002031 }
2032
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2034 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002036 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037 // First, fix up the layout of the callee-save frame.
2038 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002039 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002040
2041 // The bottom of the callee-save frame is now where the method is, *m.
2042 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2043
2044 // Add space for cookie.
2045 LayoutCookie(&sp8);
2046
2047 return sp8;
2048 }
2049
2050 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002051 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2052 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2053 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002054 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002055 Walk(shorty, shorty_len);
2056
2057 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002058 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002059
2060 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2061
2062 // Return the new bottom.
2063 return sp8;
2064 }
2065
2066 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2067
2068 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2069 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002070 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002071
2072 private:
2073 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002074 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002075};
2076
2077uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2078 num_handle_scope_references_++;
2079 return reinterpret_cast<uintptr_t>(nullptr);
2080}
2081
2082void ComputeGenericJniFrameSize::WalkHeader(
2083 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002084 // First 2 parameters are always excluded for @CriticalNative.
2085 if (UNLIKELY(critical_native_)) {
2086 return;
2087 }
2088
Andreas Gampec200a4a2014-06-16 18:39:09 -07002089 // JNIEnv
2090 sm->AdvancePointer(nullptr);
2091
2092 // Class object or this as first argument
2093 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2094}
2095
2096// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2097// the template requirements of BuildGenericJniFrameStateMachine.
2098class FillNativeCall {
2099 public:
2100 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2101 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2102
2103 virtual ~FillNativeCall() {}
2104
2105 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2106 cur_gpr_reg_ = gpr_regs;
2107 cur_fpr_reg_ = fpr_regs;
2108 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002109 }
2110
2111 void PushGpr(uintptr_t val) {
2112 *cur_gpr_reg_ = val;
2113 cur_gpr_reg_++;
2114 }
2115
2116 void PushFpr4(float val) {
2117 *cur_fpr_reg_ = val;
2118 cur_fpr_reg_++;
2119 }
2120
2121 void PushFpr8(uint64_t val) {
2122 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2123 *tmp = val;
2124 cur_fpr_reg_ += 2;
2125 }
2126
2127 void PushStack(uintptr_t val) {
2128 *cur_stack_arg_ = val;
2129 cur_stack_arg_++;
2130 }
2131
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002132 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002133 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002134 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002135 }
2136
2137 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002138 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002139 uint32_t* cur_fpr_reg_;
2140 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002141};
Andreas Gampec147b002014-03-06 18:11:06 -08002142
Andreas Gampec200a4a2014-06-16 18:39:09 -07002143// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2144// of transitioning into native code.
2145class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2146 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002147 BuildGenericJniFrameVisitor(Thread* self,
2148 bool is_static,
2149 bool critical_native,
2150 const char* shorty,
2151 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002152 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002153 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002154 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2155 sm_(&jni_call_) {
2156 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002157 uintptr_t* start_gpr_reg;
2158 uint32_t* start_fpr_reg;
2159 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002160 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002161 &handle_scope_,
2162 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002163 &start_gpr_reg, &start_fpr_reg);
2164
Andreas Gampec200a4a2014-06-16 18:39:09 -07002165 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2166
Igor Murashkin06a04e02016-09-13 15:57:37 -07002167 // First 2 parameters are always excluded for CriticalNative methods.
2168 if (LIKELY(!critical_native)) {
2169 // jni environment is always first argument
2170 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002171
Igor Murashkin06a04e02016-09-13 15:57:37 -07002172 if (is_static) {
2173 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2174 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002175 }
2176 }
2177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002178 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002179
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002180 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002181
Vladimir Markof39745e2016-01-26 12:16:55 +00002182 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002183 return handle_scope_->GetHandle(0).GetReference();
2184 }
2185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002186 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002187 return handle_scope_->GetHandle(0).ToJObject();
2188 }
2189
Ian Rogers1428dce2014-10-21 15:02:15 -07002190 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002191 return bottom_of_used_area_;
2192 }
2193
2194 private:
2195 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2196 class FillJniCall FINAL : public FillNativeCall {
2197 public:
2198 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002199 HandleScope* handle_scope, bool critical_native)
2200 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2201 handle_scope_(handle_scope),
2202 cur_entry_(0),
2203 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002204
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002205 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002206
2207 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2208 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2209 handle_scope_ = scope;
2210 cur_entry_ = 0U;
2211 }
2212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002213 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002214 // Initialize padding entries.
2215 size_t expected_slots = handle_scope_->NumberOfReferences();
2216 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002217 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002218 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002219
2220 if (!critical_native_) {
2221 // Non-critical natives have at least the self class (jclass) or this (jobject).
2222 DCHECK_NE(cur_entry_, 0U);
2223 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002224 }
2225
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002226 bool CriticalNative() const {
2227 return critical_native_;
2228 }
2229
Andreas Gampec200a4a2014-06-16 18:39:09 -07002230 private:
2231 HandleScope* handle_scope_;
2232 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002233 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002234 };
2235
2236 HandleScope* handle_scope_;
2237 FillJniCall jni_call_;
2238 void* bottom_of_used_area_;
2239
2240 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002241
2242 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2243};
2244
Andreas Gampec200a4a2014-06-16 18:39:09 -07002245uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2246 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002247 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002248 h.Assign(ref);
2249 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2250 cur_entry_++;
2251 return tmp;
2252}
2253
Ian Rogers9758f792014-03-13 09:02:55 -07002254void BuildGenericJniFrameVisitor::Visit() {
2255 Primitive::Type type = GetParamPrimitiveType();
2256 switch (type) {
2257 case Primitive::kPrimLong: {
2258 jlong long_arg;
2259 if (IsSplitLongOrDouble()) {
2260 long_arg = ReadSplitLongParam();
2261 } else {
2262 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2263 }
2264 sm_.AdvanceLong(long_arg);
2265 break;
2266 }
2267 case Primitive::kPrimDouble: {
2268 uint64_t double_arg;
2269 if (IsSplitLongOrDouble()) {
2270 // Read into union so that we don't case to a double.
2271 double_arg = ReadSplitLongParam();
2272 } else {
2273 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2274 }
2275 sm_.AdvanceDouble(double_arg);
2276 break;
2277 }
2278 case Primitive::kPrimNot: {
2279 StackReference<mirror::Object>* stack_ref =
2280 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002281 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002282 break;
2283 }
2284 case Primitive::kPrimFloat:
2285 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2286 break;
2287 case Primitive::kPrimBoolean: // Fall-through.
2288 case Primitive::kPrimByte: // Fall-through.
2289 case Primitive::kPrimChar: // Fall-through.
2290 case Primitive::kPrimShort: // Fall-through.
2291 case Primitive::kPrimInt: // Fall-through.
2292 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2293 break;
2294 case Primitive::kPrimVoid:
2295 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002296 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002297 }
2298}
2299
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002300void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002301 // Clear out rest of the scope.
2302 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002303 if (!jni_call_.CriticalNative()) {
2304 // Install HandleScope.
2305 self->PushHandleScope(handle_scope_);
2306 }
Ian Rogers9758f792014-03-13 09:02:55 -07002307}
2308
Ian Rogers04c31d22014-07-07 21:44:06 -07002309#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002310extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002311#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002312extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002313#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002314
Igor Murashkin06a04e02016-09-13 15:57:37 -07002315static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2316 uint32_t cookie,
2317 bool fast_native ATTRIBUTE_UNUSED,
2318 jobject l,
2319 jobject lock) {
2320 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002321 if (lock != nullptr) {
2322 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2323 } else {
2324 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2325 }
2326}
2327
Igor Murashkin06a04e02016-09-13 15:57:37 -07002328static void artQuickGenericJniEndJNINonRef(Thread* self,
2329 uint32_t cookie,
2330 bool fast_native,
2331 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002332 if (lock != nullptr) {
2333 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002334 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002335 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002336 if (UNLIKELY(fast_native)) {
2337 JniMethodFastEnd(cookie, self);
2338 } else {
2339 JniMethodEnd(cookie, self);
2340 }
Andreas Gampead615172014-04-04 16:20:13 -07002341 }
2342}
2343
Andreas Gampec147b002014-03-06 18:11:06 -08002344/*
2345 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002346 * 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 -08002347 * The final element on the stack is a pointer to the native code.
2348 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002349 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002350 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002351 *
Andreas Gampec147b002014-03-06 18:11:06 -08002352 * The return of this function denotes:
2353 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2354 * 2) An error, if the value is negative.
2355 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002356extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002357 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002358 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002360 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002361 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002362 uint32_t shorty_len = 0;
2363 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002364 bool critical_native = called->IsCriticalNative();
2365 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002366 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002367
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002368 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002369 BuildGenericJniFrameVisitor visitor(self,
2370 called->IsStatic(),
2371 critical_native,
2372 shorty,
2373 shorty_len,
2374 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002375 {
2376 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2377 visitor.VisitArguments();
2378 // FinalizeHandleScope pushes the handle scope on the thread.
2379 visitor.FinalizeHandleScope(self);
2380 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002381
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002382 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002383 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002384
Ian Rogerse0dcd462014-03-08 15:21:04 -08002385 self->VerifyStack();
2386
Vladimir Markof8655b32018-03-21 17:53:56 +00002387 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2388 jit::Jit* jit = runtime->GetJit();
2389 if (jit != nullptr) {
2390 jit->MethodEntered(self, called);
2391 }
2392
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002393 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002394 uint32_t* sp32;
2395 // Skip calling JniMethodStart for @CriticalNative.
2396 if (LIKELY(!critical_native)) {
2397 // Start JNI, save the cookie.
2398 if (called->IsSynchronized()) {
2399 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2400 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2401 if (self->IsExceptionPending()) {
2402 self->PopHandleScope();
2403 // A negative value denotes an error.
2404 return GetTwoWordFailureValue();
2405 }
2406 } else {
2407 if (fast_native) {
2408 cookie = JniMethodFastStart(self);
2409 } else {
2410 DCHECK(normal_native);
2411 cookie = JniMethodStart(self);
2412 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002413 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002414 sp32 = reinterpret_cast<uint32_t*>(sp);
2415 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002416 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002417
Andreas Gampe90546832014-03-12 18:07:19 -07002418 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002419 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002420
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002421 // There are two cases for the content of nativeCode:
2422 // 1) Pointer to the native function.
2423 // 2) Pointer to the trampoline for native code binding.
2424 // In the second case, we need to execute the binding and continue with the actual native function
2425 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002426 DCHECK(nativeCode != nullptr);
2427 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002428#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002429 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002430#else
2431 nativeCode = artFindNativeMethod(self);
2432#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002433
2434 if (nativeCode == nullptr) {
2435 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002436
Igor Murashkin06a04e02016-09-13 15:57:37 -07002437 // @CriticalNative calls do not need to call back into JniMethodEnd.
2438 if (LIKELY(!critical_native)) {
2439 // End JNI, as the assembly will move to deliver the exception.
2440 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2441 if (shorty[0] == 'L') {
2442 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2443 } else {
2444 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2445 }
Andreas Gampead615172014-04-04 16:20:13 -07002446 }
2447
Andreas Gampec200a4a2014-06-16 18:39:09 -07002448 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002449 }
2450 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002451 }
2452
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002453#if defined(__mips__) && !defined(__LP64__)
2454 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2455 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2456 // and load into floating-point registers.
2457 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2458 // view):
2459 // (1)
2460 // | DOUBLE | DOUBLE | other args, if any
2461 // | F12 | F13 | F14 | F15 |
2462 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2463 // (2)
2464 // | DOUBLE | FLOAT | (PAD) | other args, if any
2465 // | F12 | F13 | F14 | |
2466 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2467 // (3)
2468 // | FLOAT | (PAD) | DOUBLE | other args, if any
2469 // | F12 | | F14 | F15 |
2470 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2471 // (4)
2472 // | FLOAT | FLOAT | other args, if any
2473 // | F12 | F14 |
2474 // | SP+0 | SP+4 | SP+8
2475 // As you can see, only the last case (4) is special. In all others we can just
2476 // load F12/F13 and F14/F15 in the same manner.
2477 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2478 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2479 if (nativeCode != nullptr &&
2480 shorty != nullptr &&
2481 shorty_len >= 3 &&
2482 shorty[1] == 'F' &&
2483 shorty[2] == 'F') {
2484 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2485 }
2486#endif
2487
Andreas Gampec200a4a2014-06-16 18:39:09 -07002488 // Return native code addr(lo) and bottom of alloca address(hi).
2489 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2490 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002491}
2492
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002493// Defined in quick_jni_entrypoints.cc.
2494extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2495 jvalue result, uint64_t result_f, ArtMethod* called,
2496 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002497/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002498 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002499 * unlocking.
2500 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002501extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2502 jvalue result,
2503 uint64_t result_f) {
2504 // We're here just back from a native call. We don't have the shared mutator lock at this point
2505 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2506 // anything that requires a mutator lock before that would cause problems as GC may have the
2507 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002508 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002509 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002510 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002511 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002512 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002513 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2514 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002515}
2516
Andreas Gamped58342c2014-06-05 14:18:08 -07002517// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2518// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002519//
Andreas Gamped58342c2014-06-05 14:18:08 -07002520// 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 -07002521// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002522
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002523template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002524static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2525 ObjPtr<mirror::Object> this_object,
2526 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002527 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002528 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002529 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002530 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002531 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002532 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002533 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2534 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002535 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002536 {
2537 // Remember the args in case a GC happens in FindMethodFromCode.
2538 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2539 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2540 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002541 method = FindMethodFromCode<type, access_check>(method_idx,
2542 &this_object,
2543 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002544 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002545 visitor.FixupReferences();
2546 }
2547
Ian Rogerse0a02da2014-12-02 14:10:53 -08002548 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002549 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002550 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002551 }
2552 }
2553 DCHECK(!self->IsExceptionPending());
2554 const void* code = method->GetEntryPointFromQuickCompiledCode();
2555
2556 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002557 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002558 << " location: "
2559 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002560
Andreas Gamped58342c2014-06-05 14:18:08 -07002561 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2562 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002563}
2564
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002565// Explicit artInvokeCommon template function declarations to please analysis tool.
2566#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002567 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002568 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002569 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002570
2571EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2572EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2573EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2574EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2575EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2576EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2577EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2578EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2579EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2580EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2581#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2582
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002583// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002584extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002585 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002586 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002587 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002588}
2589
Andreas Gampec200a4a2014-06-16 18:39:09 -07002590extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002591 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002592 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002593 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002594}
2595
Andreas Gampec200a4a2014-06-16 18:39:09 -07002596extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002597 uint32_t method_idx,
2598 mirror::Object* this_object ATTRIBUTE_UNUSED,
2599 Thread* self,
2600 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2601 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2602 // it doesn't cause ObjPtr alignment failure check.
2603 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002604}
2605
Andreas Gampec200a4a2014-06-16 18:39:09 -07002606extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002607 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002608 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002609 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002610}
2611
Andreas Gampec200a4a2014-06-16 18:39:09 -07002612extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002613 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002614 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002615 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002616}
2617
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002618// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2619extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2620 REQUIRES_SHARED(Locks::mutator_lock_) {
2621 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2622 DCHECK(!referrer->IsProxyMethod());
2623 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2624 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2625 DCHECK(result == nullptr ||
2626 result->GetDeclaringClass()->IsInterface() ||
2627 result->GetDeclaringClass() ==
2628 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2629 << result->PrettyMethod();
2630 return result;
2631}
2632
Jeff Hao5667f562017-02-27 19:32:01 -08002633// Determine target of interface dispatch. The interface method and this object are known non-null.
2634// The interface method is the method returned by the dex cache in the conflict trampoline.
2635extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002636 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002637 Thread* self,
2638 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002639 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002640 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002641 StackHandleScope<2> hs(self);
2642 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2643 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002644
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002645 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002646 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002647 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002648
Vladimir Marko302f69c2017-07-25 15:27:15 +01002649 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002650 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002651 // Fetch the dex_method_idx of the target interface method from the caller.
2652 uint32_t dex_method_idx;
2653 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002654 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002655 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002656 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2657 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002658 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002659 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002660 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002661 } else {
2662 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002663 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002664 }
2665
Vladimir Marko302f69c2017-07-25 15:27:15 +01002666 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002667 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002668 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2669 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002670 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002671 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002672 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2673 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2674 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002675 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2676 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2677 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002678 visitor.FixupReferences();
2679 }
2680
Vladimir Marko302f69c2017-07-25 15:27:15 +01002681 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002682 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002683 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002684 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002685 }
2686
2687 DCHECK(!interface_method->IsRuntimeMethod());
2688 // Look whether we have a match in the ImtConflictTable.
2689 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2690 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2691 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2692 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2693 DCHECK(current_table != nullptr);
2694 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2695 } else {
2696 // It seems we aren't really a conflict method!
2697 if (kIsDebugBuild) {
2698 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2699 CHECK_EQ(conflict_method, m)
2700 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2701 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2702 }
2703 method = conflict_method;
2704 }
2705 if (method != nullptr) {
2706 return GetTwoWordSuccessValue(
2707 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2708 reinterpret_cast<uintptr_t>(method));
2709 }
2710
2711 // No match, use the IfTable.
2712 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2713 if (UNLIKELY(method == nullptr)) {
2714 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2715 interface_method, this_object.Get(), caller_method);
2716 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002717 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002718
2719 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2720 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002721 DCHECK(conflict_method->IsRuntimeMethod());
2722 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2723 cls.Get(),
2724 conflict_method,
2725 interface_method,
2726 method,
2727 /*force_new_conflict_method*/false);
2728 if (new_conflict_method != conflict_method) {
2729 // Update the IMT if we create a new conflict method. No fence needed here, as the
2730 // data is consistent.
2731 imt->Set(imt_index,
2732 new_conflict_method,
2733 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002734 }
2735
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002736 const void* code = method->GetEntryPointFromQuickCompiledCode();
2737
2738 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002739 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002740 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002741
Andreas Gamped58342c2014-06-05 14:18:08 -07002742 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2743 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002744}
2745
Orion Hodsonac141392017-01-13 11:53:47 +00002746// Returns shorty type so the caller can determine how to put |result|
2747// into expected registers. The shorty type is static so the compiler
2748// could call different flavors of this code path depending on the
2749// shorty type though this would require different entry points for
2750// each type.
2751extern "C" uintptr_t artInvokePolymorphic(
2752 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002753 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002754 Thread* self,
2755 ArtMethod** sp)
2756 REQUIRES_SHARED(Locks::mutator_lock_) {
2757 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002758 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002759
2760 // Start new JNI local reference state
2761 JNIEnvExt* env = self->GetJniEnv();
2762 ScopedObjectAccessUnchecked soa(env);
2763 ScopedJniEnvLocalRefState env_state(env);
2764 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2765
2766 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2767 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2768 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002769 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002770 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2771 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002772 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002773 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002774 const size_t shorty_length = strlen(shorty);
2775 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2776 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002777 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002778
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002779 // Wrap raw_receiver in a Handle for safety.
2780 StackHandleScope<3> hs(self);
2781 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2782 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002783 self->EndAssertNoThreadSuspension(old_cause);
2784
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002785 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002786 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002787 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002788 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002789
2790 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002791 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2792 return static_cast<uintptr_t>('V');
2793 }
2794
Orion Hodsone7732be2017-10-11 14:35:20 +01002795 Handle<mirror::MethodType> method_type(
2796 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2797
Orion Hodsonac141392017-01-13 11:53:47 +00002798 // This implies we couldn't resolve one or more types in this method handle.
2799 if (UNLIKELY(method_type.IsNull())) {
2800 CHECK(self->IsExceptionPending());
2801 return static_cast<uintptr_t>('V');
2802 }
2803
Vladimir Markod7559b72017-09-28 13:50:37 +01002804 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002805 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2806
2807 // Fix references before constructing the shadow frame.
2808 gc_visitor.FixupReferences();
2809
2810 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002811 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2812 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002813 const size_t first_arg = 0;
2814 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2815 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2816 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2817 ScopedStackedShadowFramePusher
2818 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2819 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2820 kMethodIsStatic,
2821 shorty,
2822 strlen(shorty),
2823 shadow_frame,
2824 first_arg);
2825 shadow_frame_builder.VisitArguments();
2826
2827 // Push a transition back into managed code onto the linked list in thread.
2828 ManagedStack fragment;
2829 self->PushManagedStackFragment(&fragment);
2830
2831 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2832 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002833 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002834 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002835 bool success = false;
Orion Hodson537a4fe2018-05-15 13:57:58 +01002836 if (resolved_method->GetDeclaringClass() == mirror::MethodHandle::StaticClass()) {
2837 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2838 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2839 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2840 success = MethodHandleInvokeExact(self,
2841 *shadow_frame,
2842 method_handle,
2843 method_type,
2844 &operands,
2845 result);
2846 } else {
2847 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2848 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2849 success = MethodHandleInvoke(self,
2850 *shadow_frame,
2851 method_handle,
2852 method_type,
2853 &operands,
2854 result);
2855 }
2856 } else {
2857 DCHECK_EQ(mirror::VarHandle::StaticClass(), resolved_method->GetDeclaringClass());
2858 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
2859 ObjPtr<mirror::VarHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2860 mirror::VarHandle::AccessMode access_mode =
2861 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2862 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002863 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002864 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002865 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002866 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002867 &operands,
2868 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002869 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002870
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002871 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002872
2873 // Pop transition record.
2874 self->PopManagedStackFragment(fragment);
2875
2876 return static_cast<uintptr_t>(shorty[0]);
2877}
2878
Ian Rogers848871b2013-08-05 10:56:33 -07002879} // namespace art