blob: 66cec7113e781eced3575449566e4c97c30f8c87 [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 Marko45310912018-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 Sehre6564f42018-03-19 08:39:26 -070027#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070030#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imt_conflict_table.h"
32#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000033#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070034#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "interpreter/interpreter.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000036#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000037#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000038#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070039#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070040#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070041#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000042#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010045#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010046#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070047#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070048#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070049#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070050#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070051#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000052#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070053
54namespace art {
55
Andreas Gampe8228cdf2017-05-30 15:03:54 -070056// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070057class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080058 // Number of bytes for each out register in the caller method's frame.
59 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070060 // Frame size in bytes of a callee-save frame for RefsAndArgs.
61 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070062 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070063#if defined(__arm__)
64 // The callee save frame is pointed to by SP.
65 // | argN | |
66 // | ... | |
67 // | arg4 | |
68 // | arg3 spill | | Caller's frame
69 // | arg2 spill | |
70 // | arg1 spill | |
71 // | Method* | ---
72 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080073 // | ... | 4x6 bytes callee saves
74 // | R3 |
75 // | R2 |
76 // | R1 |
77 // | S15 |
78 // | : |
79 // | S0 |
80 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070081 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070082 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
83 static constexpr bool kAlignPairRegister = true;
84 static constexpr bool kQuickSoftFloatAbi = false;
85 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000086 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080087 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -070088 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080089 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080090 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070091 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070093 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080094 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070095 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080096 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000097 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080098 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000099#elif defined(__aarch64__)
100 // The callee save frame is pointed to by SP.
101 // | argN | |
102 // | ... | |
103 // | arg4 | |
104 // | arg3 spill | | Caller's frame
105 // | arg2 spill | |
106 // | arg1 spill | |
107 // | Method* | ---
108 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800109 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100111 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 // | X7 |
113 // | : |
114 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800115 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000116 // | : |
117 // | D0 |
118 // | | padding
119 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500120 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000121 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800123 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000124 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000125 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
126 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800127 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700128 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800129 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700130 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
131 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800132 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700133 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
134 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800135 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700136 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000138 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000139 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800140#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700141 // The callee save frame is pointed to by SP.
142 // | argN | |
143 // | ... | |
144 // | arg4 | |
145 // | arg3 spill | | Caller's frame
146 // | arg2 spill | |
147 // | arg1 spill | |
148 // | Method* | ---
149 // | RA |
150 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800151 // | T1 | arg5
152 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700153 // | A3 | arg3
154 // | A2 | arg2
155 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800156 // | F19 |
157 // | F18 | f_arg5
158 // | F17 |
159 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800161 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000162 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800163 // | F12 | f_arg2
164 // | F11 |
165 // | F10 | f_arg1
166 // | F9 |
167 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000168 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700169 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000170 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
171 static constexpr bool kAlignPairRegister = true;
172 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800173 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000174 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800175 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
176 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
177 // passed only in even numbered registers and each
178 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800179 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
181 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
182 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800183 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000184 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800185 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800186#elif defined(__mips__) && defined(__LP64__)
187 // The callee save frame is pointed to by SP.
188 // | argN | |
189 // | ... | |
190 // | arg4 | |
191 // | arg3 spill | | Caller's frame
192 // | arg2 spill | |
193 // | arg1 spill | |
194 // | Method* | ---
195 // | RA |
196 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800197 // | A7 | arg7
198 // | A6 | arg6
199 // | A5 | arg5
200 // | A4 | arg4
201 // | A3 | arg3
202 // | A2 | arg2
203 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000204 // | F19 | f_arg7
205 // | F18 | f_arg6
206 // | F17 | f_arg5
207 // | F16 | f_arg4
208 // | F15 | f_arg3
209 // | F14 | f_arg2
210 // | F13 | f_arg1
211 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800212 // | | padding
213 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800214 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800215 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static constexpr bool kAlignPairRegister = false;
217 static constexpr bool kQuickSoftFloatAbi = false;
218 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000219 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800220 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
221 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
222 static constexpr bool kGprFprLockstep = true;
223
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800224 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800225 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
226 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
227 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
228 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
229 }
Ian Rogers848871b2013-08-05 10:56:33 -0700230#elif defined(__i386__)
231 // The callee save frame is pointed to by SP.
232 // | argN | |
233 // | ... | |
234 // | arg4 | |
235 // | arg3 spill | | Caller's frame
236 // | arg2 spill | |
237 // | arg1 spill | |
238 // | Method* | ---
239 // | Return |
240 // | EBP,ESI,EDI | callee saves
241 // | EBX | arg3
242 // | EDX | arg2
243 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000244 // | XMM3 | float arg 4
245 // | XMM2 | float arg 3
246 // | XMM1 | float arg 2
247 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700248 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500249 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000250 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000251 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800252 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000253 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800254 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000255 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800256 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000257 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
259 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000261 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800264 // The callee save frame is pointed to by SP.
265 // | argN | |
266 // | ... | |
267 // | reg. arg spills | | Caller's frame
268 // | Method* | ---
269 // | Return |
270 // | R15 | callee save
271 // | R14 | callee save
272 // | R13 | callee save
273 // | R12 | callee save
274 // | R9 | arg5
275 // | R8 | arg4
276 // | RSI/R6 | arg1
277 // | RBP/R5 | callee save
278 // | RBX/R3 | callee save
279 // | RDX/R2 | arg2
280 // | RCX/R1 | arg3
281 // | XMM7 | float arg 8
282 // | XMM6 | float arg 7
283 // | XMM5 | float arg 6
284 // | XMM4 | float arg 5
285 // | XMM3 | float arg 4
286 // | XMM2 | float arg 3
287 // | XMM1 | float arg 2
288 // | XMM0 | float arg 1
289 // | Padding |
290 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500291 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000292 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800293 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800294 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000295 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700296 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700297 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800298 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700300 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
301 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
303 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000304 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
305 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
306 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
307 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
308 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800309 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700310 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
311 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 }
313 }
Ian Rogers848871b2013-08-05 10:56:33 -0700314#else
315#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700316#endif
317
Ian Rogers936b37f2014-02-14 00:52:24 -0800318 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100319 // Special handling for proxy methods. Proxy methods are instance methods so the
320 // 'this' object is the 1st argument. They also have the same frame layout as the
321 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
322 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000323 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700324 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000325 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100326 CHECK_GT(kNumQuickGprArgs, 0u);
327 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
328 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
329 GprIndexToGprOffset(kThisGprIndex);
330 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000331 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100332 }
333
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700334 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700336 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100337 }
338
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700339 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 uint8_t* previous_sp =
342 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344 }
345
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700346 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700348 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
349 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
351 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100352 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100353 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
354 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100355
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100356 if (current_code->IsOptimized()) {
357 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000358 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100359 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100360 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800361 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100362 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
364 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100365 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800366 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100367 }
368 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100369 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100370 }
Ian Rogers848871b2013-08-05 10:56:33 -0700371 }
372
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800373 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
374 REQUIRES_SHARED(Locks::mutator_lock_) {
375 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700376 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
377 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800378 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
379 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
380 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
381 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
382 if (!current_code->IsOptimized()) {
383 return false;
384 }
385 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
386 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
387 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700388 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800389 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
390 if (invoke.IsValid()) {
391 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700392 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800393 return true;
394 }
395 return false;
396 }
397
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700399 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700401 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700402 return *reinterpret_cast<uintptr_t*>(lr);
403 }
404
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700406 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700407 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700408 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
409 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
410 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800412 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
413 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800414 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
415 "Number of Quick FPR arguments unexpected");
416 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
417 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800418 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
419 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800420 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
421 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700422 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800423 }
Ian Rogers848871b2013-08-05 10:56:33 -0700424
425 virtual ~QuickArgumentVisitor() {}
426
427 virtual void Visit() = 0;
428
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 Primitive::Type GetParamPrimitiveType() const {
430 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700431 }
432
Ian Rogers13735952014-10-08 12:43:28 -0700433 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800434 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800435 Primitive::Type type = GetParamPrimitiveType();
436 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800437 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
438 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
439 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
440 }
441 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000442 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700444 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 }
446 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800447 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800448 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
449 }
450 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700451 }
452
453 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
455 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800456 return is_split_long_or_double_;
457 } else {
458 return false; // An optimization for when GPR and FPRs are 64bit.
459 }
Ian Rogers848871b2013-08-05 10:56:33 -0700460 }
461
Ian Rogers936b37f2014-02-14 00:52:24 -0800462 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700463 return GetParamPrimitiveType() == Primitive::kPrimNot;
464 }
465
Ian Rogers936b37f2014-02-14 00:52:24 -0800466 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700467 Primitive::Type type = GetParamPrimitiveType();
468 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
469 }
470
471 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000472 // The splitted long is always available through the stack.
473 return *reinterpret_cast<uint64_t*>(stack_args_
474 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700475 }
476
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800477 void IncGprIndex() {
478 gpr_index_++;
479 if (kGprFprLockstep) {
480 fpr_index_++;
481 }
482 }
483
484 void IncFprIndex() {
485 fpr_index_++;
486 if (kGprFprLockstep) {
487 gpr_index_++;
488 }
489 }
490
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700491 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800492 // (a) 'stack_args_' should point to the first method's argument
493 // (b) whatever the argument type it is, the 'stack_index_' should
494 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800495 gpr_index_ = 0;
496 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800497 if (kQuickDoubleRegAlignedFloatBackFilled) {
498 fpr_double_index_ = 0;
499 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800500 stack_index_ = 0;
501 if (!is_static_) { // Handle this.
502 cur_type_ = Primitive::kPrimNot;
503 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700504 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800505 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800506 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800507 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800508 }
Ian Rogers848871b2013-08-05 10:56:33 -0700509 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800510 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
511 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
512 switch (cur_type_) {
513 case Primitive::kPrimNot:
514 case Primitive::kPrimBoolean:
515 case Primitive::kPrimByte:
516 case Primitive::kPrimChar:
517 case Primitive::kPrimShort:
518 case Primitive::kPrimInt:
519 is_split_long_or_double_ = false;
520 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800521 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800522 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800523 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800524 }
525 break;
526 case Primitive::kPrimFloat:
527 is_split_long_or_double_ = false;
528 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800529 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800530 if (kQuickSoftFloatAbi) {
531 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800532 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800533 }
534 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800535 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800536 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800537 if (kQuickDoubleRegAlignedFloatBackFilled) {
538 // Double should not overlap with float.
539 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
540 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
541 // Float should not overlap with double.
542 if (fpr_index_ % 2 == 0) {
543 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
544 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000545 } else if (kQuickSkipOddFpRegisters) {
546 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800547 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800548 }
549 }
550 break;
551 case Primitive::kPrimDouble:
552 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800553 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800554 if (cur_type_ == Primitive::kPrimLong &&
555#if defined(__mips__) && !defined(__LP64__)
556 (gpr_index_ == 0 || gpr_index_ == 2) &&
557#else
558 gpr_index_ == 0 &&
559#endif
560 kAlignPairRegister) {
561 // Currently, this is only for ARM and MIPS, where we align long parameters with
562 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
563 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800564 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000565 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000566 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800567 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500568 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
569 // We don't want to split this. Pass over this register.
570 gpr_index_++;
571 is_split_long_or_double_ = false;
572 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800573 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800574 if (kBytesStackArgLocation == 4) {
575 stack_index_+= 2;
576 } else {
577 CHECK_EQ(kBytesStackArgLocation, 8U);
578 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800579 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700580 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800581 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000582 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700583 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800584 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700585 }
586 }
587 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800588 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000589 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800590 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800591 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700592 if (kBytesStackArgLocation == 4) {
593 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800594 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700595 CHECK_EQ(kBytesStackArgLocation, 8U);
596 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800597 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800598 if (kQuickDoubleRegAlignedFloatBackFilled) {
599 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
600 fpr_double_index_ += 2;
601 // Float should not overlap with double.
602 if (fpr_index_ % 2 == 0) {
603 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
604 }
605 }
606 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800607 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800608 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
609 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800610 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800611 }
612 }
613 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800614 }
615 break;
616 default:
617 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
618 }
Ian Rogers848871b2013-08-05 10:56:33 -0700619 }
620 }
621
Andreas Gampec200a4a2014-06-16 18:39:09 -0700622 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700623 const bool is_static_;
624 const char* const shorty_;
625 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700626
627 private:
Ian Rogers13735952014-10-08 12:43:28 -0700628 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
629 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
630 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800631 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800632 // Index into spilled FPRs.
633 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
634 // holds a higher register number.
635 uint32_t fpr_index_;
636 // Index into spilled FPRs for aligned double.
637 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
638 // terms of singles, may be behind fpr_index.
639 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800640 uint32_t stack_index_; // Index into arguments on the stack.
641 // The current type of argument during VisitArguments.
642 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700643 // Does a 64bit parameter straddle the register and stack arguments?
644 bool is_split_long_or_double_;
645};
646
Sebastien Hertza836bc92014-11-25 16:30:53 +0100647// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
648// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700649extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700650 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000651 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
652}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100653
Ian Rogers848871b2013-08-05 10:56:33 -0700654// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800655class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700656 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
658 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700659 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700660
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700661 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700662
663 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800664 ShadowFrame* const sf_;
665 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700666
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700667 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700668};
669
Andreas Gampec200a4a2014-06-16 18:39:09 -0700670void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700671 Primitive::Type type = GetParamPrimitiveType();
672 switch (type) {
673 case Primitive::kPrimLong: // Fall-through.
674 case Primitive::kPrimDouble:
675 if (IsSplitLongOrDouble()) {
676 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
677 } else {
678 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
679 }
680 ++cur_reg_;
681 break;
682 case Primitive::kPrimNot: {
683 StackReference<mirror::Object>* stack_ref =
684 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
685 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
686 }
687 break;
688 case Primitive::kPrimBoolean: // Fall-through.
689 case Primitive::kPrimByte: // Fall-through.
690 case Primitive::kPrimChar: // Fall-through.
691 case Primitive::kPrimShort: // Fall-through.
692 case Primitive::kPrimInt: // Fall-through.
693 case Primitive::kPrimFloat:
694 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
695 break;
696 case Primitive::kPrimVoid:
697 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700698 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700699 }
700 ++cur_reg_;
701}
702
Mingyao Yang417528d2017-09-13 12:10:40 -0700703// Don't inline. See b/65159206.
704NO_INLINE
705static void HandleDeoptimization(JValue* result,
706 ArtMethod* method,
707 ShadowFrame* deopt_frame,
708 ManagedStack* fragment)
709 REQUIRES_SHARED(Locks::mutator_lock_) {
710 // Coming from partial-fragment deopt.
711 Thread* self = Thread::Current();
712 if (kIsDebugBuild) {
713 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
714 // of the call-stack) corresponds to the called method.
715 ShadowFrame* linked = deopt_frame;
716 while (linked->GetLink() != nullptr) {
717 linked = linked->GetLink();
718 }
719 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
720 << ArtMethod::PrettyMethod(linked->GetMethod());
721 }
722
723 if (VLOG_IS_ON(deopt)) {
724 // Print out the stack to verify that it was a partial-fragment deopt.
725 LOG(INFO) << "Continue-ing from deopt. Stack is:";
726 QuickExceptionHandler::DumpFramesWithType(self, true);
727 }
728
729 ObjPtr<mirror::Throwable> pending_exception;
730 bool from_code = false;
731 DeoptimizationMethodType method_type;
732 self->PopDeoptimizationContext(/* out */ result,
733 /* out */ &pending_exception,
734 /* out */ &from_code,
735 /* out */ &method_type);
736
737 // Push a transition back into managed code onto the linked list in thread.
738 self->PushManagedStackFragment(fragment);
739
740 // Ensure that the stack is still in order.
741 if (kIsDebugBuild) {
742 class DummyStackVisitor : public StackVisitor {
743 public:
744 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
745 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
746
747 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
748 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
749 // logic. Just always say we want to continue.
750 return true;
751 }
752 };
753 DummyStackVisitor dsv(self);
754 dsv.WalkStack();
755 }
756
757 // Restore the exception that was pending before deoptimization then interpret the
758 // deoptimized frames.
759 if (pending_exception != nullptr) {
760 self->SetException(pending_exception);
761 }
762 interpreter::EnterInterpreterFromDeoptimize(self,
763 deopt_frame,
764 result,
765 from_code,
766 DeoptimizationMethodType::kDefault);
767}
768
Mathieu Chartiere401d142015-04-22 13:56:20 -0700769extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700770 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700771 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
772 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700773 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700774
Alex Light9139e002015-10-09 15:59:48 -0700775 if (UNLIKELY(!method->IsInvokable())) {
776 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700777 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700778 }
779
780 JValue tmp_value;
781 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700782 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700783 ManagedStack fragment;
784
David Sehr709b0702016-10-13 09:12:37 -0700785 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700786 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700787 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800788 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000789 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700790 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
791
792 JValue result;
793
Mingyao Yang417528d2017-09-13 12:10:40 -0700794 if (UNLIKELY(deopt_frame != nullptr)) {
795 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700796 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700797 const char* old_cause = self->StartAssertNoThreadSuspension(
798 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800799 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700800 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700801 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700802 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700803 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800804 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700805 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800806 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700807 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800808 const bool needs_initialization =
809 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700810 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700811 self->PushManagedStackFragment(&fragment);
812 self->PushShadowFrame(shadow_frame);
813 self->EndAssertNoThreadSuspension(old_cause);
814
Ian Rogerse94652f2014-12-02 11:13:19 -0800815 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700816 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800817 StackHandleScope<1> hs(self);
818 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700819 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700820 DCHECK(Thread::Current()->IsExceptionPending())
821 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700822 self->PopManagedStackFragment(fragment);
823 return 0;
824 }
825 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200826
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800827 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700829
830 // Pop transition.
831 self->PopManagedStackFragment(fragment);
832
833 // Request a stack deoptimization if needed
834 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700835 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700836 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
837 // should be done and it knows the real return pc.
838 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000839 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
840 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
841 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
842 << caller->PrettyMethod();
843 } else {
844 // Push the context of the deoptimization stack so we can restore the return value and the
845 // exception before executing the deoptimized frames.
846 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700847 result,
848 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
849 self->GetException(),
850 false /* from_code */,
851 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700852
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000853 // Set special exception to cause deoptimization.
854 self->SetException(Thread::GetDeoptimizationException());
855 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700856 }
857
858 // No need to restore the args since the method has already been run by the interpreter.
859 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700860}
861
862// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
863// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800864class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700865 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700866 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700867 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700868 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700869
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700870 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700871
872 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700873 ScopedObjectAccessUnchecked* const soa_;
874 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700875
Ian Rogers848871b2013-08-05 10:56:33 -0700876 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
877};
878
Ian Rogers9758f792014-03-13 09:02:55 -0700879void BuildQuickArgumentVisitor::Visit() {
880 jvalue val;
881 Primitive::Type type = GetParamPrimitiveType();
882 switch (type) {
883 case Primitive::kPrimNot: {
884 StackReference<mirror::Object>* stack_ref =
885 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
886 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700887 break;
888 }
889 case Primitive::kPrimLong: // Fall-through.
890 case Primitive::kPrimDouble:
891 if (IsSplitLongOrDouble()) {
892 val.j = ReadSplitLongParam();
893 } else {
894 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
895 }
896 break;
897 case Primitive::kPrimBoolean: // Fall-through.
898 case Primitive::kPrimByte: // Fall-through.
899 case Primitive::kPrimChar: // Fall-through.
900 case Primitive::kPrimShort: // Fall-through.
901 case Primitive::kPrimInt: // Fall-through.
902 case Primitive::kPrimFloat:
903 val.i = *reinterpret_cast<jint*>(GetParamAddress());
904 break;
905 case Primitive::kPrimVoid:
906 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700907 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700908 }
909 args_->push_back(val);
910}
911
Ian Rogers848871b2013-08-05 10:56:33 -0700912// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
913// which is responsible for recording callee save registers. We explicitly place into jobjects the
914// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
915// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700916extern "C" uint64_t artQuickProxyInvokeHandler(
917 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700918 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700919 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
920 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700921 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
922 const char* old_cause =
923 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
924 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700925 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700926 self->VerifyStack();
927 // Start new JNI local reference state.
928 JNIEnvExt* env = self->GetJniEnv();
929 ScopedObjectAccessUnchecked soa(env);
930 ScopedJniEnvLocalRefState env_state(env);
931 // Create local ref. copies of proxy method and the receiver.
932 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
933
934 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700935 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700936 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
937 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700938 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700939 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000941 BuildQuickArgumentVisitor local_ref_visitor(
942 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700943
Ian Rogers848871b2013-08-05 10:56:33 -0700944 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700945 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700946 args.erase(args.begin());
947
948 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700949 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700950 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
951 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700952 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700953 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700954 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700955 ObjPtr<mirror::Method> interface_reflect_method =
956 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
957 if (interface_reflect_method == nullptr) {
958 soa.Self()->AssertPendingOOMException();
959 return 0;
960 }
961 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700962
963 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
964 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700965 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700966 return result.GetJ();
967}
968
Roland Levillainad0777d2018-02-12 20:00:18 +0000969// Visitor returning a reference argument at a given position in a Quick stack frame.
970// NOTE: Only used for testing purposes.
971class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
972 public:
973 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
974 const char* shorty,
975 uint32_t shorty_len,
976 size_t arg_pos)
977 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
978 cur_pos_(0u),
979 arg_pos_(arg_pos),
980 ref_arg_(nullptr) {
981 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
982 }
983
984 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
985 if (cur_pos_ == arg_pos_) {
986 Primitive::Type type = GetParamPrimitiveType();
987 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
988 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
989 }
990 ++cur_pos_;
991 }
992
993 StackReference<mirror::Object>* GetReferenceArgument() {
994 return ref_arg_;
995 }
996
997 private:
998 // The position of the currently visited argument.
999 size_t cur_pos_;
1000 // The position of the searched argument.
1001 const size_t arg_pos_;
1002 // The reference argument, if found.
1003 StackReference<mirror::Object>* ref_arg_;
1004
1005 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
1006};
1007
1008// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
1009// NOTE: Only used for testing purposes.
1010extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
1011 ArtMethod** sp)
1012 REQUIRES_SHARED(Locks::mutator_lock_) {
1013 ArtMethod* proxy_method = *sp;
1014 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1015 CHECK(!non_proxy_method->IsStatic())
1016 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1017 uint32_t shorty_len = 0;
1018 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1019 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1020 ref_arg_visitor.VisitArguments();
1021 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1022 return ref_arg;
1023}
1024
1025// Visitor returning all the reference arguments in a Quick stack frame.
1026class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1027 public:
1028 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1029 bool is_static,
1030 const char* shorty,
1031 uint32_t shorty_len)
1032 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1033
1034 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1035 Primitive::Type type = GetParamPrimitiveType();
1036 if (type == Primitive::kPrimNot) {
1037 StackReference<mirror::Object>* ref_arg =
1038 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1039 ref_args_.push_back(ref_arg);
1040 }
1041 }
1042
1043 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1044 return ref_args_;
1045 }
1046
1047 private:
1048 // The reference arguments.
1049 std::vector<StackReference<mirror::Object>*> ref_args_;
1050
1051 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1052};
1053
1054// Returning all reference arguments in Quick stack frame at address `sp`.
1055std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1056 REQUIRES_SHARED(Locks::mutator_lock_) {
1057 ArtMethod* proxy_method = *sp;
1058 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1059 CHECK(!non_proxy_method->IsStatic())
1060 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1061 uint32_t shorty_len = 0;
1062 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1063 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1064 ref_args_visitor.VisitArguments();
1065 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1066 return ref_args;
1067}
1068
Ian Rogers848871b2013-08-05 10:56:33 -07001069// Read object references held in arguments from quick frames and place in a JNI local references,
1070// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001071class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001072 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001073 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1074 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001075 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001076
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001077 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001078
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001079 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001080
1081 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001082 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001083 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001084 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1085
Mathieu Chartier590fee92013-09-13 13:46:47 -07001086 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001087};
1088
Ian Rogers9758f792014-03-13 09:02:55 -07001089void RememberForGcArgumentVisitor::Visit() {
1090 if (IsParamAReference()) {
1091 StackReference<mirror::Object>* stack_ref =
1092 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1093 jobject reference =
1094 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1095 references_.push_back(std::make_pair(reference, stack_ref));
1096 }
1097}
1098
1099void RememberForGcArgumentVisitor::FixupReferences() {
1100 // Fixup any references which may have changed.
1101 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001102 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001103 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001104 }
1105}
1106
Alex Lightb7edcda2017-04-27 13:20:31 -07001107extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1108 mirror::Object* this_object,
1109 Thread* self,
1110 ArtMethod** sp)
1111 REQUIRES_SHARED(Locks::mutator_lock_) {
1112 const void* result;
1113 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1114 // that part.
1115 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1116 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1117 if (instrumentation->IsDeoptimized(method)) {
1118 result = GetQuickToInterpreterBridge();
1119 } else {
1120 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1121 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1122 }
1123
1124 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1125 bool is_static = method->IsStatic();
1126 uint32_t shorty_len;
1127 const char* shorty =
1128 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1129
1130 ScopedObjectAccessUnchecked soa(self);
1131 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1132 visitor.VisitArguments();
1133
1134 instrumentation->PushInstrumentationStackFrame(self,
1135 is_static ? nullptr : this_object,
1136 method,
1137 QuickArgumentVisitor::GetCallingPc(sp),
1138 interpreter_entry);
1139
1140 visitor.FixupReferences();
1141 if (UNLIKELY(self->IsExceptionPending())) {
1142 return nullptr;
1143 }
1144 CHECK(result != nullptr) << method->PrettyMethod();
1145 return result;
1146}
1147
1148extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1149 ArtMethod** sp,
1150 uint64_t* gpr_result,
1151 uint64_t* fpr_result)
1152 REQUIRES_SHARED(Locks::mutator_lock_) {
1153 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1154 CHECK(gpr_result != nullptr);
1155 CHECK(fpr_result != nullptr);
1156 // Instrumentation exit stub must not be entered with a pending exception.
1157 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1158 << self->GetException()->Dump();
1159 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001160 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1161 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001162 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1163 return_pc_offset);
1164 CHECK_EQ(*return_pc, 0U);
1165
1166 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1167 // deoptimization shouldn't be performed with the high-half having the return address. When
1168 // deoptimization should be performed the low half is zero and the high-half the address of the
1169 // deoptimization entry point.
1170 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1171 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1172 self, return_pc, gpr_result, fpr_result);
1173 if (self->IsExceptionPending()) {
1174 return GetTwoWordFailureValue();
1175 }
1176 return return_or_deoptimize_pc;
1177}
1178
Vladimir Marko338af022018-03-16 09:42:09 +00001179static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1180 REQUIRES_SHARED(Locks::mutator_lock_) {
1181 if (dex_pc == static_cast<uint32_t>(-1)) {
1182 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1183 return "<native>";
1184 } else {
1185 CodeItemInstructionAccessor accessor = method->DexInstructions();
1186 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1187 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1188 }
1189}
1190
Vladimir Marko45310912018-04-05 14:49:24 +01001191static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1192 REQUIRES_SHARED(Locks::mutator_lock_) {
1193 std::string storage;
1194 const char* descriptor = klass->GetDescriptor(&storage);
1195 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1196 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1197 if (oat_dex_file != nullptr) {
1198 const OatFile* oat_file = oat_dex_file->GetOatFile();
1199 const char* dex2oat_cmdline =
1200 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1201 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1202 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1203 }
1204}
1205
Vladimir Marko338af022018-03-16 09:42:09 +00001206static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1207 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko45310912018-04-05 14:49:24 +01001208 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko338af022018-03-16 09:42:09 +00001209
1210 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1211 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1212
1213 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
1214 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1215 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
1216 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
1217 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1218 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1219 ArtMethod* outer_method = *caller_sp;
1220
1221 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1222 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1223 << " native pc: " << caller_pc << " Instrumented!";
1224 return;
1225 }
1226
1227 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1228 CHECK(current_code != nullptr);
1229 CHECK(current_code->IsOptimized());
1230 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
1231 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
1232 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
1233 CodeInfoEncoding encoding = code_info.ExtractEncoding();
1234 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
1235 CHECK(stack_map.IsValid());
1236 uint32_t dex_pc = stack_map.GetDexPc(encoding.stack_map.encoding);
1237
1238 // Log the outer method and its associated dex file and class table pointer which can be used
1239 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1240 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1241 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1242 << " native pc: " << caller_pc
1243 << " dex pc: " << dex_pc
1244 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1245 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko45310912018-04-05 14:49:24 +01001246 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko338af022018-03-16 09:42:09 +00001247 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1248
1249 ArtMethod* caller = outer_method;
1250 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
1251 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
1252 const InlineInfoEncoding& inline_info_encoding = encoding.inline_info.encoding;
1253 size_t depth = inline_info.GetDepth(inline_info_encoding);
1254 for (size_t d = 0; d < depth; ++d) {
1255 const char* tag = "";
1256 dex_pc = inline_info.GetDexPcAtDepth(inline_info_encoding, d);
1257 if (inline_info.EncodesArtMethodAtDepth(inline_info_encoding, d)) {
1258 tag = "encoded ";
1259 caller = inline_info.GetArtMethodAtDepth(inline_info_encoding, d);
1260 } else {
1261 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info_encoding,
1262 method_info,
1263 d);
1264 if (dex_pc == static_cast<uint32_t>(-1)) {
1265 tag = "special ";
1266 CHECK_EQ(d + 1u, depth);
1267 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1268 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1269 } else {
1270 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1271 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1272 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1273 CHECK(caller != nullptr);
1274 }
1275 }
1276 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1277 << " dex pc: " << dex_pc
1278 << " dex file: " << caller->GetDexFile()->GetLocation()
1279 << " class table: "
Vladimir Marko45310912018-04-05 14:49:24 +01001280 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1281 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko338af022018-03-16 09:42:09 +00001282 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1283 }
1284 }
1285}
1286
Ian Rogers848871b2013-08-05 10:56:33 -07001287// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001288extern "C" const void* artQuickResolutionTrampoline(
1289 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001290 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001291 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1292 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1293 // does not have the same stack layout as the callee-save method).
1294 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001295 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001296 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001297 ScopedObjectAccessUnchecked soa(env);
1298 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001299 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001300
1301 // Compute details about the called method (avoid GCs)
1302 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001303 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001304 MethodReference called_method(nullptr, 0);
1305 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001306 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001307 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001308 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001309 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001310
1311 InvokeType stack_map_invoke_type;
1312 uint32_t stack_map_dex_method_idx;
1313 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1314 &stack_map_invoke_type,
1315 &stack_map_dex_method_idx);
1316 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1317 // code.
1318 if (!found_stack_map || kIsDebugBuild) {
1319 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001320 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001321 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1322 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001323 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001324 bool is_range;
1325 switch (instr_code) {
1326 case Instruction::INVOKE_DIRECT:
1327 invoke_type = kDirect;
1328 is_range = false;
1329 break;
1330 case Instruction::INVOKE_DIRECT_RANGE:
1331 invoke_type = kDirect;
1332 is_range = true;
1333 break;
1334 case Instruction::INVOKE_STATIC:
1335 invoke_type = kStatic;
1336 is_range = false;
1337 break;
1338 case Instruction::INVOKE_STATIC_RANGE:
1339 invoke_type = kStatic;
1340 is_range = true;
1341 break;
1342 case Instruction::INVOKE_SUPER:
1343 invoke_type = kSuper;
1344 is_range = false;
1345 break;
1346 case Instruction::INVOKE_SUPER_RANGE:
1347 invoke_type = kSuper;
1348 is_range = true;
1349 break;
1350 case Instruction::INVOKE_VIRTUAL:
1351 invoke_type = kVirtual;
1352 is_range = false;
1353 break;
1354 case Instruction::INVOKE_VIRTUAL_RANGE:
1355 invoke_type = kVirtual;
1356 is_range = true;
1357 break;
1358 case Instruction::INVOKE_INTERFACE:
1359 invoke_type = kInterface;
1360 is_range = false;
1361 break;
1362 case Instruction::INVOKE_INTERFACE_RANGE:
1363 invoke_type = kInterface;
1364 is_range = true;
1365 break;
1366 default:
Vladimir Marko338af022018-03-16 09:42:09 +00001367 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001368 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001369 UNREACHABLE();
1370 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001371 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001372 // Check that the invoke matches what we expected, note that this path only happens for debug
1373 // builds.
1374 if (found_stack_map) {
1375 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1376 if (invoke_type != kSuper) {
1377 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001378 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001379 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001380 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001381 }
1382 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001383 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001384 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001385 }
1386 } else {
1387 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001388 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001389 }
Ian Rogers848871b2013-08-05 10:56:33 -07001390 } else {
1391 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001392 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001393 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001394 }
1395 uint32_t shorty_len;
1396 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001397 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001398 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001399 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001400 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001401 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001402 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001403 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001404 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001405 mirror::Object* dummy = nullptr;
1406 HandleWrapper<mirror::Object> h_receiver(
1407 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001408 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001409 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001410 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001411
1412 // Update .bss entry in oat file if any.
1413 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001414 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1415 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1416 called_method.index,
1417 called_method.dex_file->NumMethodIds(),
1418 static_cast<size_t>(kRuntimePointerSize));
1419 if (bss_offset != IndexBssMappingLookup::npos) {
1420 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1421 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1422 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1423 oat_file->BssBegin() + bss_offset));
1424 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1425 DCHECK_LT(method_entry,
1426 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1427 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001428 }
1429 }
Ian Rogers848871b2013-08-05 10:56:33 -07001430 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001431 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001432 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001433 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001434 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001435 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001436 if (virtual_or_interface || invoke_type == kSuper) {
1437 // Refine called method based on receiver for kVirtual/kInterface, and
1438 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001439 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001440 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001441 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001442 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001443 } else if (invoke_type == kInterface) {
1444 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001445 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001446 } else {
1447 DCHECK_EQ(invoke_type, kSuper);
1448 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001449 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001450 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001451 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001452 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001453 } else {
1454 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001455 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001456 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001457 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001458
David Sehr709b0702016-10-13 09:12:37 -07001459 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1460 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001461 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001462 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001463
Ian Rogers848871b2013-08-05 10:56:33 -07001464 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001465 StackHandleScope<1> hs(soa.Self());
1466 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001467 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001468 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001469 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1470 // If we are single-stepping or the called method is deoptimized (by a
1471 // breakpoint, for example), then we have to execute the called method
1472 // with the interpreter.
1473 code = GetQuickToInterpreterBridge();
1474 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1475 // If the caller is deoptimized (by a breakpoint, for example), we have to
1476 // continue its execution with interpreter when returning from the called
1477 // method. Because we do not want to execute the called method with the
1478 // interpreter, we wrap its execution into the instrumentation stubs.
1479 // When the called method returns, it will execute the instrumentation
1480 // exit hook that will determine the need of the interpreter with a call
1481 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1482 // it is needed.
1483 code = GetQuickInstrumentationEntryPoint();
1484 } else {
1485 code = called->GetEntryPointFromQuickCompiledCode();
1486 }
Ian Rogers848871b2013-08-05 10:56:33 -07001487 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001488 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1489 // If we are single-stepping or the called method is deoptimized (by a
1490 // breakpoint, for example), then we have to execute the called method
1491 // with the interpreter.
1492 code = GetQuickToInterpreterBridge();
1493 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001494 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1495 // until class is initialized to stop races between threads).
1496 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001497 } else {
1498 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001499 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001500 }
1501 } else {
1502 DCHECK(called_class->IsErroneous());
1503 }
1504 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001505 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001506 // Fixup any locally saved objects may have moved during a GC.
1507 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001508 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001509 *sp = called;
1510
Ian Rogers848871b2013-08-05 10:56:33 -07001511 return code;
1512}
1513
Andreas Gampec147b002014-03-06 18:11:06 -08001514/*
1515 * This class uses a couple of observations to unite the different calling conventions through
1516 * a few constants.
1517 *
1518 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1519 * possible alignment.
1520 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1521 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1522 * when we have to split things
1523 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1524 * and we can use Int handling directly.
1525 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1526 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1527 * extension should be compatible with Aarch64, which mandates copying the available bits
1528 * into LSB and leaving the rest unspecified.
1529 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1530 * the stack.
1531 * 6) There is only little endian.
1532 *
1533 *
1534 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1535 * follows:
1536 *
1537 * void PushGpr(uintptr_t): Add a value for the next GPR
1538 *
1539 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1540 * padding, that is, think the architecture is 32b and aligns 64b.
1541 *
1542 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1543 * split this if necessary. The current state will have aligned, if
1544 * necessary.
1545 *
1546 * void PushStack(uintptr_t): Push a value to the stack.
1547 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001548 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001549 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001550 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001551 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001552 *
1553 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001554template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001555 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001556#if defined(__arm__)
1557 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001558 static constexpr bool kNativeSoftFloatAbi = true;
1559 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001560 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1561
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001562 static constexpr size_t kRegistersNeededForLong = 2;
1563 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001564 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001565 static constexpr bool kMultiFPRegistersWidened = false;
1566 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001567 static constexpr bool kAlignLongOnStack = true;
1568 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001569#elif defined(__aarch64__)
1570 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1571 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1572 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1573
1574 static constexpr size_t kRegistersNeededForLong = 1;
1575 static constexpr size_t kRegistersNeededForDouble = 1;
1576 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001577 static constexpr bool kMultiFPRegistersWidened = false;
1578 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001579 static constexpr bool kAlignLongOnStack = false;
1580 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001581#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001582 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001583 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1584 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001585
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001586 static constexpr size_t kRegistersNeededForLong = 2;
1587 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001588 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001589 static constexpr bool kMultiFPRegistersWidened = true;
1590 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001591 static constexpr bool kAlignLongOnStack = true;
1592 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001593#elif defined(__mips__) && defined(__LP64__)
1594 // Let the code prepare GPRs only and we will load the FPRs with same data.
1595 static constexpr bool kNativeSoftFloatAbi = true;
1596 static constexpr size_t kNumNativeGprArgs = 8;
1597 static constexpr size_t kNumNativeFprArgs = 0;
1598
1599 static constexpr size_t kRegistersNeededForLong = 1;
1600 static constexpr size_t kRegistersNeededForDouble = 1;
1601 static constexpr bool kMultiRegistersAligned = false;
1602 static constexpr bool kMultiFPRegistersWidened = false;
1603 static constexpr bool kMultiGPRegistersWidened = true;
1604 static constexpr bool kAlignLongOnStack = false;
1605 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001606#elif defined(__i386__)
1607 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001608 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001609 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1610 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1611
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001612 static constexpr size_t kRegistersNeededForLong = 2;
1613 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001614 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001615 static constexpr bool kMultiFPRegistersWidened = false;
1616 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001617 static constexpr bool kAlignLongOnStack = false;
1618 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001619#elif defined(__x86_64__)
1620 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1621 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1622 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1623
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001624 static constexpr size_t kRegistersNeededForLong = 1;
1625 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001626 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001627 static constexpr bool kMultiFPRegistersWidened = false;
1628 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001629 static constexpr bool kAlignLongOnStack = false;
1630 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001631#else
1632#error "Unsupported architecture"
1633#endif
1634
Andreas Gampec147b002014-03-06 18:11:06 -08001635 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636 explicit BuildNativeCallFrameStateMachine(T* delegate)
1637 : gpr_index_(kNumNativeGprArgs),
1638 fpr_index_(kNumNativeFprArgs),
1639 stack_entries_(0),
1640 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001641 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1642 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001643 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1644 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001645 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001646
Andreas Gampec200a4a2014-06-16 18:39:09 -07001647 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001648
Ian Rogers1428dce2014-10-21 15:02:15 -07001649 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001650 return gpr_index_ > 0;
1651 }
1652
Andreas Gampec200a4a2014-06-16 18:39:09 -07001653 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001654 if (HavePointerGpr()) {
1655 gpr_index_--;
1656 PushGpr(reinterpret_cast<uintptr_t>(val));
1657 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001658 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001659 PushStack(reinterpret_cast<uintptr_t>(val));
1660 gpr_index_ = 0;
1661 }
1662 }
1663
Ian Rogers1428dce2014-10-21 15:02:15 -07001664 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001665 return gpr_index_ > 0;
1666 }
1667
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001668 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001669 uintptr_t handle = PushHandle(ptr);
1670 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001671 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001672 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001673 } else {
1674 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001675 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001676 gpr_index_ = 0;
1677 }
1678 }
1679
Ian Rogers1428dce2014-10-21 15:02:15 -07001680 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001681 return gpr_index_ > 0;
1682 }
1683
1684 void AdvanceInt(uint32_t val) {
1685 if (HaveIntGpr()) {
1686 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001687 if (kMultiGPRegistersWidened) {
1688 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001689 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001690 } else {
1691 PushGpr(val);
1692 }
Andreas Gampec147b002014-03-06 18:11:06 -08001693 } else {
1694 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001695 if (kMultiGPRegistersWidened) {
1696 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001697 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001698 } else {
1699 PushStack(val);
1700 }
Andreas Gampec147b002014-03-06 18:11:06 -08001701 gpr_index_ = 0;
1702 }
1703 }
1704
Ian Rogers1428dce2014-10-21 15:02:15 -07001705 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001706 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1707 }
1708
Ian Rogers1428dce2014-10-21 15:02:15 -07001709 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001710 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1711 kAlignLongOnStack && // and when it needs alignment
1712 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1713 }
1714
Ian Rogers1428dce2014-10-21 15:02:15 -07001715 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001716 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1717 kAlignLongOnStack && // and when it needs 8B alignment
1718 (stack_entries_ & 1) == 1; // counter is odd
1719 }
1720
1721 void AdvanceLong(uint64_t val) {
1722 if (HaveLongGpr()) {
1723 if (LongGprNeedsPadding()) {
1724 PushGpr(0);
1725 gpr_index_--;
1726 }
1727 if (kRegistersNeededForLong == 1) {
1728 PushGpr(static_cast<uintptr_t>(val));
1729 } else {
1730 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1731 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1732 }
1733 gpr_index_ -= kRegistersNeededForLong;
1734 } else {
1735 if (LongStackNeedsPadding()) {
1736 PushStack(0);
1737 stack_entries_++;
1738 }
1739 if (kRegistersNeededForLong == 1) {
1740 PushStack(static_cast<uintptr_t>(val));
1741 stack_entries_++;
1742 } else {
1743 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1744 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1745 stack_entries_ += 2;
1746 }
1747 gpr_index_ = 0;
1748 }
1749 }
1750
Ian Rogers1428dce2014-10-21 15:02:15 -07001751 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001752 return fpr_index_ > 0;
1753 }
1754
Andreas Gampec147b002014-03-06 18:11:06 -08001755 void AdvanceFloat(float val) {
1756 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001757 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001758 } else {
1759 if (HaveFloatFpr()) {
1760 fpr_index_--;
1761 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001762 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001763 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001764 } else {
1765 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001766 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001767 }
1768 } else {
1769 PushFpr4(val);
1770 }
1771 } else {
1772 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001773 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001774 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001775 // Note: We need to jump through those hoops to make the compiler happy.
1776 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001777 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001778 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001779 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001780 }
1781 fpr_index_ = 0;
1782 }
1783 }
1784 }
1785
Ian Rogers1428dce2014-10-21 15:02:15 -07001786 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001787 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1788 }
1789
Ian Rogers1428dce2014-10-21 15:02:15 -07001790 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001791 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1792 kAlignDoubleOnStack && // and when it needs alignment
1793 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1794 }
1795
Ian Rogers1428dce2014-10-21 15:02:15 -07001796 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001797 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1798 kAlignDoubleOnStack && // and when it needs 8B alignment
1799 (stack_entries_ & 1) == 1; // counter is odd
1800 }
1801
1802 void AdvanceDouble(uint64_t val) {
1803 if (kNativeSoftFloatAbi) {
1804 AdvanceLong(val);
1805 } else {
1806 if (HaveDoubleFpr()) {
1807 if (DoubleFprNeedsPadding()) {
1808 PushFpr4(0);
1809 fpr_index_--;
1810 }
1811 PushFpr8(val);
1812 fpr_index_ -= kRegistersNeededForDouble;
1813 } else {
1814 if (DoubleStackNeedsPadding()) {
1815 PushStack(0);
1816 stack_entries_++;
1817 }
1818 if (kRegistersNeededForDouble == 1) {
1819 PushStack(static_cast<uintptr_t>(val));
1820 stack_entries_++;
1821 } else {
1822 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1823 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1824 stack_entries_ += 2;
1825 }
1826 fpr_index_ = 0;
1827 }
1828 }
1829 }
1830
Ian Rogers1428dce2014-10-21 15:02:15 -07001831 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001832 return stack_entries_;
1833 }
1834
Ian Rogers1428dce2014-10-21 15:02:15 -07001835 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001836 return kNumNativeGprArgs - gpr_index_;
1837 }
1838
Ian Rogers1428dce2014-10-21 15:02:15 -07001839 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001840 return kNumNativeFprArgs - fpr_index_;
1841 }
1842
1843 private:
1844 void PushGpr(uintptr_t val) {
1845 delegate_->PushGpr(val);
1846 }
1847 void PushFpr4(float val) {
1848 delegate_->PushFpr4(val);
1849 }
1850 void PushFpr8(uint64_t val) {
1851 delegate_->PushFpr8(val);
1852 }
1853 void PushStack(uintptr_t val) {
1854 delegate_->PushStack(val);
1855 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001856 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001857 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001858 }
1859
1860 uint32_t gpr_index_; // Number of free GPRs
1861 uint32_t fpr_index_; // Number of free FPRs
1862 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1863 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001864 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001865};
1866
Andreas Gampec200a4a2014-06-16 18:39:09 -07001867// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1868// in subclasses.
1869//
1870// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1871// them with handles.
1872class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001873 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1875
1876 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001877
Ian Rogers1428dce2014-10-21 15:02:15 -07001878 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001879 return num_stack_entries_ * sizeof(uintptr_t);
1880 }
1881
Ian Rogers1428dce2014-10-21 15:02:15 -07001882 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001883 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001884 // Align by kStackAlignment.
1885 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001886 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001887 }
1888
Ian Rogers1428dce2014-10-21 15:02:15 -07001889 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1890 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001891 // Assumption is OK right now, as we have soft-float arm
1892 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1893 sp8 -= fregs * sizeof(uintptr_t);
1894 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1895 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1896 sp8 -= iregs * sizeof(uintptr_t);
1897 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1898 return sp8;
1899 }
Andreas Gampec147b002014-03-06 18:11:06 -08001900
Andreas Gampec200a4a2014-06-16 18:39:09 -07001901 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001902 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001903 // Native call stack.
1904 sp8 = LayoutCallStack(sp8);
1905 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001906
Andreas Gampec200a4a2014-06-16 18:39:09 -07001907 // Put fprs and gprs below.
1908 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001909
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 // Return the new bottom.
1911 return sp8;
1912 }
1913
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001914 virtual void WalkHeader(
1915 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001916 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001917 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001918
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001919 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001920 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1921
1922 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001923
1924 for (uint32_t i = 1; i < shorty_len; ++i) {
1925 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1926 switch (cur_type_) {
1927 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001928 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001929 sm.AdvanceHandleScope(
1930 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001931 break;
1932
1933 case Primitive::kPrimBoolean:
1934 case Primitive::kPrimByte:
1935 case Primitive::kPrimChar:
1936 case Primitive::kPrimShort:
1937 case Primitive::kPrimInt:
1938 sm.AdvanceInt(0);
1939 break;
1940 case Primitive::kPrimFloat:
1941 sm.AdvanceFloat(0);
1942 break;
1943 case Primitive::kPrimDouble:
1944 sm.AdvanceDouble(0);
1945 break;
1946 case Primitive::kPrimLong:
1947 sm.AdvanceLong(0);
1948 break;
1949 default:
1950 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001951 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001952 }
1953 }
1954
Ian Rogers1428dce2014-10-21 15:02:15 -07001955 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001956 }
1957
1958 void PushGpr(uintptr_t /* val */) {
1959 // not optimizing registers, yet
1960 }
1961
1962 void PushFpr4(float /* val */) {
1963 // not optimizing registers, yet
1964 }
1965
1966 void PushFpr8(uint64_t /* val */) {
1967 // not optimizing registers, yet
1968 }
1969
1970 void PushStack(uintptr_t /* val */) {
1971 // counting is already done in the superclass
1972 }
1973
Andreas Gampec200a4a2014-06-16 18:39:09 -07001974 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001975 return reinterpret_cast<uintptr_t>(nullptr);
1976 }
1977
Andreas Gampec200a4a2014-06-16 18:39:09 -07001978 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001979 uint32_t num_stack_entries_;
1980};
1981
Andreas Gampec200a4a2014-06-16 18:39:09 -07001982class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001983 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001984 explicit ComputeGenericJniFrameSize(bool critical_native)
1985 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001986
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1988 // is at *m = sp. Will update to point to the bottom of the save frame.
1989 //
1990 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001991 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001992 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 ArtMethod* method = **m;
1994
Andreas Gampe542451c2016-07-26 09:02:02 -07001995 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001996
Andreas Gampec200a4a2014-06-16 18:39:09 -07001997 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1998
1999 // First, fix up the layout of the callee-save frame.
2000 // We have to squeeze in the HandleScope, and relocate the method pointer.
2001
2002 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07002003 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002004
2005 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002006 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002007 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008
2009 sp8 -= scope_and_method;
2010 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002011 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07002012
Mathieu Chartiere401d142015-04-22 13:56:20 -07002013 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07002014 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
2015 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002016
2017 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2018 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002019 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2020 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002021 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002022 }
2023
Andreas Gampec200a4a2014-06-16 18:39:09 -07002024 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002025 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002026 // Reference cookie and padding
2027 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002028 }
2029
Andreas Gampec200a4a2014-06-16 18:39:09 -07002030 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2031 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002033 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002034 // First, fix up the layout of the callee-save frame.
2035 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002036 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037
2038 // The bottom of the callee-save frame is now where the method is, *m.
2039 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2040
2041 // Add space for cookie.
2042 LayoutCookie(&sp8);
2043
2044 return sp8;
2045 }
2046
2047 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2049 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2050 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002051 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052 Walk(shorty, shorty_len);
2053
2054 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002055 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002056
2057 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2058
2059 // Return the new bottom.
2060 return sp8;
2061 }
2062
2063 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2064
2065 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2066 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002067 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068
2069 private:
2070 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002071 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002072};
2073
2074uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2075 num_handle_scope_references_++;
2076 return reinterpret_cast<uintptr_t>(nullptr);
2077}
2078
2079void ComputeGenericJniFrameSize::WalkHeader(
2080 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002081 // First 2 parameters are always excluded for @CriticalNative.
2082 if (UNLIKELY(critical_native_)) {
2083 return;
2084 }
2085
Andreas Gampec200a4a2014-06-16 18:39:09 -07002086 // JNIEnv
2087 sm->AdvancePointer(nullptr);
2088
2089 // Class object or this as first argument
2090 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2091}
2092
2093// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2094// the template requirements of BuildGenericJniFrameStateMachine.
2095class FillNativeCall {
2096 public:
2097 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2098 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2099
2100 virtual ~FillNativeCall() {}
2101
2102 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2103 cur_gpr_reg_ = gpr_regs;
2104 cur_fpr_reg_ = fpr_regs;
2105 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002106 }
2107
2108 void PushGpr(uintptr_t val) {
2109 *cur_gpr_reg_ = val;
2110 cur_gpr_reg_++;
2111 }
2112
2113 void PushFpr4(float val) {
2114 *cur_fpr_reg_ = val;
2115 cur_fpr_reg_++;
2116 }
2117
2118 void PushFpr8(uint64_t val) {
2119 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2120 *tmp = val;
2121 cur_fpr_reg_ += 2;
2122 }
2123
2124 void PushStack(uintptr_t val) {
2125 *cur_stack_arg_ = val;
2126 cur_stack_arg_++;
2127 }
2128
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002129 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002130 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002131 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002132 }
2133
2134 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002135 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002136 uint32_t* cur_fpr_reg_;
2137 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002138};
Andreas Gampec147b002014-03-06 18:11:06 -08002139
Andreas Gampec200a4a2014-06-16 18:39:09 -07002140// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2141// of transitioning into native code.
2142class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2143 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002144 BuildGenericJniFrameVisitor(Thread* self,
2145 bool is_static,
2146 bool critical_native,
2147 const char* shorty,
2148 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002149 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002150 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002151 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2152 sm_(&jni_call_) {
2153 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002154 uintptr_t* start_gpr_reg;
2155 uint32_t* start_fpr_reg;
2156 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002157 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002158 &handle_scope_,
2159 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002160 &start_gpr_reg, &start_fpr_reg);
2161
Andreas Gampec200a4a2014-06-16 18:39:09 -07002162 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2163
Igor Murashkin06a04e02016-09-13 15:57:37 -07002164 // First 2 parameters are always excluded for CriticalNative methods.
2165 if (LIKELY(!critical_native)) {
2166 // jni environment is always first argument
2167 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002168
Igor Murashkin06a04e02016-09-13 15:57:37 -07002169 if (is_static) {
2170 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2171 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002172 }
2173 }
2174
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002175 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002176
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002177 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002178
Vladimir Markof39745e2016-01-26 12:16:55 +00002179 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002180 return handle_scope_->GetHandle(0).GetReference();
2181 }
2182
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002183 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002184 return handle_scope_->GetHandle(0).ToJObject();
2185 }
2186
Ian Rogers1428dce2014-10-21 15:02:15 -07002187 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002188 return bottom_of_used_area_;
2189 }
2190
2191 private:
2192 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2193 class FillJniCall FINAL : public FillNativeCall {
2194 public:
2195 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002196 HandleScope* handle_scope, bool critical_native)
2197 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2198 handle_scope_(handle_scope),
2199 cur_entry_(0),
2200 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002201
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002202 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002203
2204 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2205 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2206 handle_scope_ = scope;
2207 cur_entry_ = 0U;
2208 }
2209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002210 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002211 // Initialize padding entries.
2212 size_t expected_slots = handle_scope_->NumberOfReferences();
2213 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002214 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002215 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002216
2217 if (!critical_native_) {
2218 // Non-critical natives have at least the self class (jclass) or this (jobject).
2219 DCHECK_NE(cur_entry_, 0U);
2220 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002221 }
2222
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002223 bool CriticalNative() const {
2224 return critical_native_;
2225 }
2226
Andreas Gampec200a4a2014-06-16 18:39:09 -07002227 private:
2228 HandleScope* handle_scope_;
2229 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002230 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002231 };
2232
2233 HandleScope* handle_scope_;
2234 FillJniCall jni_call_;
2235 void* bottom_of_used_area_;
2236
2237 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002238
2239 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2240};
2241
Andreas Gampec200a4a2014-06-16 18:39:09 -07002242uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2243 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002244 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002245 h.Assign(ref);
2246 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2247 cur_entry_++;
2248 return tmp;
2249}
2250
Ian Rogers9758f792014-03-13 09:02:55 -07002251void BuildGenericJniFrameVisitor::Visit() {
2252 Primitive::Type type = GetParamPrimitiveType();
2253 switch (type) {
2254 case Primitive::kPrimLong: {
2255 jlong long_arg;
2256 if (IsSplitLongOrDouble()) {
2257 long_arg = ReadSplitLongParam();
2258 } else {
2259 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2260 }
2261 sm_.AdvanceLong(long_arg);
2262 break;
2263 }
2264 case Primitive::kPrimDouble: {
2265 uint64_t double_arg;
2266 if (IsSplitLongOrDouble()) {
2267 // Read into union so that we don't case to a double.
2268 double_arg = ReadSplitLongParam();
2269 } else {
2270 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2271 }
2272 sm_.AdvanceDouble(double_arg);
2273 break;
2274 }
2275 case Primitive::kPrimNot: {
2276 StackReference<mirror::Object>* stack_ref =
2277 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002278 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002279 break;
2280 }
2281 case Primitive::kPrimFloat:
2282 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2283 break;
2284 case Primitive::kPrimBoolean: // Fall-through.
2285 case Primitive::kPrimByte: // Fall-through.
2286 case Primitive::kPrimChar: // Fall-through.
2287 case Primitive::kPrimShort: // Fall-through.
2288 case Primitive::kPrimInt: // Fall-through.
2289 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2290 break;
2291 case Primitive::kPrimVoid:
2292 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002293 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002294 }
2295}
2296
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002297void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002298 // Clear out rest of the scope.
2299 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002300 if (!jni_call_.CriticalNative()) {
2301 // Install HandleScope.
2302 self->PushHandleScope(handle_scope_);
2303 }
Ian Rogers9758f792014-03-13 09:02:55 -07002304}
2305
Ian Rogers04c31d22014-07-07 21:44:06 -07002306#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002307extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002308#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002309extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002310#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002311
Igor Murashkin06a04e02016-09-13 15:57:37 -07002312static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2313 uint32_t cookie,
2314 bool fast_native ATTRIBUTE_UNUSED,
2315 jobject l,
2316 jobject lock) {
2317 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002318 if (lock != nullptr) {
2319 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2320 } else {
2321 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2322 }
2323}
2324
Igor Murashkin06a04e02016-09-13 15:57:37 -07002325static void artQuickGenericJniEndJNINonRef(Thread* self,
2326 uint32_t cookie,
2327 bool fast_native,
2328 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002329 if (lock != nullptr) {
2330 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002331 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002332 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002333 if (UNLIKELY(fast_native)) {
2334 JniMethodFastEnd(cookie, self);
2335 } else {
2336 JniMethodEnd(cookie, self);
2337 }
Andreas Gampead615172014-04-04 16:20:13 -07002338 }
2339}
2340
Andreas Gampec147b002014-03-06 18:11:06 -08002341/*
2342 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002343 * 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 -08002344 * The final element on the stack is a pointer to the native code.
2345 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002346 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002347 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002348 *
Andreas Gampec147b002014-03-06 18:11:06 -08002349 * The return of this function denotes:
2350 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2351 * 2) An error, if the value is negative.
2352 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002354 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002355 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002356 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002357 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002358 Runtime* runtime = Runtime::Current();
2359 jit::Jit* jit = runtime->GetJit();
2360 if (jit != nullptr) {
2361 jit->AddSamples(self, called, 1u, /*with_backedges*/ false);
2362 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002363 uint32_t shorty_len = 0;
2364 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002365 bool critical_native = called->IsCriticalNative();
2366 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002367 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002368
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002369 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002370 BuildGenericJniFrameVisitor visitor(self,
2371 called->IsStatic(),
2372 critical_native,
2373 shorty,
2374 shorty_len,
2375 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002376 {
2377 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2378 visitor.VisitArguments();
2379 // FinalizeHandleScope pushes the handle scope on the thread.
2380 visitor.FinalizeHandleScope(self);
2381 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002382
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002383 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002384 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002385
Ian Rogerse0dcd462014-03-08 15:21:04 -08002386 self->VerifyStack();
2387
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002388 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002389 uint32_t* sp32;
2390 // Skip calling JniMethodStart for @CriticalNative.
2391 if (LIKELY(!critical_native)) {
2392 // Start JNI, save the cookie.
2393 if (called->IsSynchronized()) {
2394 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2395 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2396 if (self->IsExceptionPending()) {
2397 self->PopHandleScope();
2398 // A negative value denotes an error.
2399 return GetTwoWordFailureValue();
2400 }
2401 } else {
2402 if (fast_native) {
2403 cookie = JniMethodFastStart(self);
2404 } else {
2405 DCHECK(normal_native);
2406 cookie = JniMethodStart(self);
2407 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002408 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002409 sp32 = reinterpret_cast<uint32_t*>(sp);
2410 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002411 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002412
Andreas Gampe90546832014-03-12 18:07:19 -07002413 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002414 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002415
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002416 // There are two cases for the content of nativeCode:
2417 // 1) Pointer to the native function.
2418 // 2) Pointer to the trampoline for native code binding.
2419 // In the second case, we need to execute the binding and continue with the actual native function
2420 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002421 DCHECK(nativeCode != nullptr);
2422 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002423#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002424 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002425#else
2426 nativeCode = artFindNativeMethod(self);
2427#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002428
2429 if (nativeCode == nullptr) {
2430 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002431
Igor Murashkin06a04e02016-09-13 15:57:37 -07002432 // @CriticalNative calls do not need to call back into JniMethodEnd.
2433 if (LIKELY(!critical_native)) {
2434 // End JNI, as the assembly will move to deliver the exception.
2435 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2436 if (shorty[0] == 'L') {
2437 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2438 } else {
2439 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2440 }
Andreas Gampead615172014-04-04 16:20:13 -07002441 }
2442
Andreas Gampec200a4a2014-06-16 18:39:09 -07002443 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002444 }
2445 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002446 }
2447
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002448#if defined(__mips__) && !defined(__LP64__)
2449 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2450 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2451 // and load into floating-point registers.
2452 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2453 // view):
2454 // (1)
2455 // | DOUBLE | DOUBLE | other args, if any
2456 // | F12 | F13 | F14 | F15 |
2457 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2458 // (2)
2459 // | DOUBLE | FLOAT | (PAD) | other args, if any
2460 // | F12 | F13 | F14 | |
2461 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2462 // (3)
2463 // | FLOAT | (PAD) | DOUBLE | other args, if any
2464 // | F12 | | F14 | F15 |
2465 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2466 // (4)
2467 // | FLOAT | FLOAT | other args, if any
2468 // | F12 | F14 |
2469 // | SP+0 | SP+4 | SP+8
2470 // As you can see, only the last case (4) is special. In all others we can just
2471 // load F12/F13 and F14/F15 in the same manner.
2472 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2473 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2474 if (nativeCode != nullptr &&
2475 shorty != nullptr &&
2476 shorty_len >= 3 &&
2477 shorty[1] == 'F' &&
2478 shorty[2] == 'F') {
2479 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2480 }
2481#endif
2482
Andreas Gampec200a4a2014-06-16 18:39:09 -07002483 // Return native code addr(lo) and bottom of alloca address(hi).
2484 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2485 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002486}
2487
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002488// Defined in quick_jni_entrypoints.cc.
2489extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2490 jvalue result, uint64_t result_f, ArtMethod* called,
2491 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002492/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002493 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002494 * unlocking.
2495 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002496extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2497 jvalue result,
2498 uint64_t result_f) {
2499 // We're here just back from a native call. We don't have the shared mutator lock at this point
2500 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2501 // anything that requires a mutator lock before that would cause problems as GC may have the
2502 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002503 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002504 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002505 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002506 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002507 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002508 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2509 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002510}
2511
Andreas Gamped58342c2014-06-05 14:18:08 -07002512// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2513// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002514//
Andreas Gamped58342c2014-06-05 14:18:08 -07002515// 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 -07002516// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002517
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002518template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002519static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2520 ObjPtr<mirror::Object> this_object,
2521 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002522 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002523 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002524 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002525 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002526 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002527 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002528 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2529 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002530 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002531 {
2532 // Remember the args in case a GC happens in FindMethodFromCode.
2533 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2534 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2535 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002536 method = FindMethodFromCode<type, access_check>(method_idx,
2537 &this_object,
2538 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002539 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002540 visitor.FixupReferences();
2541 }
2542
Ian Rogerse0a02da2014-12-02 14:10:53 -08002543 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002544 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002545 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002546 }
2547 }
2548 DCHECK(!self->IsExceptionPending());
2549 const void* code = method->GetEntryPointFromQuickCompiledCode();
2550
2551 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002552 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002553 << " location: "
2554 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002555
Andreas Gamped58342c2014-06-05 14:18:08 -07002556 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2557 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002558}
2559
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002560// Explicit artInvokeCommon template function declarations to please analysis tool.
2561#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002562 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002563 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002564 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002565
2566EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2567EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2568EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2569EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2570EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2571EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2572EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2573EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2574EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2575EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2576#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2577
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002578// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002579extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002580 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002581 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002582 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002583}
2584
Andreas Gampec200a4a2014-06-16 18:39:09 -07002585extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002587 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002588 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002589}
2590
Andreas Gampec200a4a2014-06-16 18:39:09 -07002591extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002592 uint32_t method_idx,
2593 mirror::Object* this_object ATTRIBUTE_UNUSED,
2594 Thread* self,
2595 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2596 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2597 // it doesn't cause ObjPtr alignment failure check.
2598 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002599}
2600
Andreas Gampec200a4a2014-06-16 18:39:09 -07002601extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002602 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002603 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002604 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002605}
2606
Andreas Gampec200a4a2014-06-16 18:39:09 -07002607extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002608 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002609 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002610 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002611}
2612
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002613// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2614extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2615 REQUIRES_SHARED(Locks::mutator_lock_) {
2616 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2617 DCHECK(!referrer->IsProxyMethod());
2618 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2619 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2620 DCHECK(result == nullptr ||
2621 result->GetDeclaringClass()->IsInterface() ||
2622 result->GetDeclaringClass() ==
2623 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2624 << result->PrettyMethod();
2625 return result;
2626}
2627
Jeff Hao5667f562017-02-27 19:32:01 -08002628// Determine target of interface dispatch. The interface method and this object are known non-null.
2629// The interface method is the method returned by the dex cache in the conflict trampoline.
2630extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002631 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002632 Thread* self,
2633 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002634 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002635 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002636 StackHandleScope<2> hs(self);
2637 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2638 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002639
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002640 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002641 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002642 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002643
Vladimir Marko302f69c2017-07-25 15:27:15 +01002644 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002645 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002646 // Fetch the dex_method_idx of the target interface method from the caller.
2647 uint32_t dex_method_idx;
2648 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002649 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002650 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002651 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2652 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002653 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002654 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002655 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002656 } else {
2657 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002658 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002659 }
2660
Vladimir Marko302f69c2017-07-25 15:27:15 +01002661 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002662 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002663 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2664 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002665 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002666 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002667 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2668 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2669 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002670 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2671 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2672 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002673 visitor.FixupReferences();
2674 }
2675
Vladimir Marko302f69c2017-07-25 15:27:15 +01002676 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002677 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002678 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002679 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002680 }
2681
2682 DCHECK(!interface_method->IsRuntimeMethod());
2683 // Look whether we have a match in the ImtConflictTable.
2684 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2685 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2686 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2687 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2688 DCHECK(current_table != nullptr);
2689 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2690 } else {
2691 // It seems we aren't really a conflict method!
2692 if (kIsDebugBuild) {
2693 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2694 CHECK_EQ(conflict_method, m)
2695 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2696 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2697 }
2698 method = conflict_method;
2699 }
2700 if (method != nullptr) {
2701 return GetTwoWordSuccessValue(
2702 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2703 reinterpret_cast<uintptr_t>(method));
2704 }
2705
2706 // No match, use the IfTable.
2707 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2708 if (UNLIKELY(method == nullptr)) {
2709 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2710 interface_method, this_object.Get(), caller_method);
2711 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002712 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002713
2714 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2715 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002716 DCHECK(conflict_method->IsRuntimeMethod());
2717 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2718 cls.Get(),
2719 conflict_method,
2720 interface_method,
2721 method,
2722 /*force_new_conflict_method*/false);
2723 if (new_conflict_method != conflict_method) {
2724 // Update the IMT if we create a new conflict method. No fence needed here, as the
2725 // data is consistent.
2726 imt->Set(imt_index,
2727 new_conflict_method,
2728 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002729 }
2730
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002731 const void* code = method->GetEntryPointFromQuickCompiledCode();
2732
2733 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002734 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002735 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002736
Andreas Gamped58342c2014-06-05 14:18:08 -07002737 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2738 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002739}
2740
Orion Hodsonac141392017-01-13 11:53:47 +00002741// Returns shorty type so the caller can determine how to put |result|
2742// into expected registers. The shorty type is static so the compiler
2743// could call different flavors of this code path depending on the
2744// shorty type though this would require different entry points for
2745// each type.
2746extern "C" uintptr_t artInvokePolymorphic(
2747 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002748 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002749 Thread* self,
2750 ArtMethod** sp)
2751 REQUIRES_SHARED(Locks::mutator_lock_) {
2752 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002753 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002754
2755 // Start new JNI local reference state
2756 JNIEnvExt* env = self->GetJniEnv();
2757 ScopedObjectAccessUnchecked soa(env);
2758 ScopedJniEnvLocalRefState env_state(env);
2759 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2760
2761 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2762 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2763 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002764 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002765 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2766 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002767 const uint32_t proto_idx = inst.VRegH();
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002768 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002769 const size_t shorty_length = strlen(shorty);
2770 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2771 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002772 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002773
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002774 // Wrap raw_receiver in a Handle for safety.
2775 StackHandleScope<3> hs(self);
2776 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2777 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002778 self->EndAssertNoThreadSuspension(old_cause);
2779
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002780 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002781 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002782 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002783 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002784
2785 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002786 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2787 return static_cast<uintptr_t>('V');
2788 }
2789
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002790 // TODO(oth): Ensure this path isn't taken for VarHandle accessors (b/65872996).
2791 DCHECK_EQ(resolved_method->GetDeclaringClass(),
2792 WellKnownClasses::ToClass(WellKnownClasses::java_lang_invoke_MethodHandle));
2793
2794 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2795 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2796
Orion Hodsone7732be2017-10-11 14:35:20 +01002797 Handle<mirror::MethodType> method_type(
2798 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2799
Orion Hodsonac141392017-01-13 11:53:47 +00002800 // This implies we couldn't resolve one or more types in this method handle.
2801 if (UNLIKELY(method_type.IsNull())) {
2802 CHECK(self->IsExceptionPending());
2803 return static_cast<uintptr_t>('V');
2804 }
2805
Vladimir Markod7559b72017-09-28 13:50:37 +01002806 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002807 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2808
2809 // Fix references before constructing the shadow frame.
2810 gc_visitor.FixupReferences();
2811
2812 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002813 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2814 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002815 const size_t first_arg = 0;
2816 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2817 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2818 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2819 ScopedStackedShadowFramePusher
2820 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2821 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2822 kMethodIsStatic,
2823 shorty,
2824 strlen(shorty),
2825 shadow_frame,
2826 first_arg);
2827 shadow_frame_builder.VisitArguments();
2828
2829 // Push a transition back into managed code onto the linked list in thread.
2830 ManagedStack fragment;
2831 self->PushManagedStackFragment(&fragment);
2832
2833 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2834 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002835 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002836 bool isExact = (jni::EncodeArtMethod(resolved_method) ==
2837 WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
2838 bool success = false;
2839 if (isExact) {
Orion Hodson960d4f72017-11-10 15:32:38 +00002840 success = MethodHandleInvokeExact(self,
2841 *shadow_frame,
2842 method_handle,
2843 method_type,
2844 &operands,
2845 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002846 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00002847 success = MethodHandleInvoke(self,
2848 *shadow_frame,
2849 method_handle,
2850 method_type,
2851 &operands,
2852 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002853 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002854 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002855
2856 // Pop transition record.
2857 self->PopManagedStackFragment(fragment);
2858
2859 return static_cast<uintptr_t>(shorty[0]);
2860}
2861
Ian Rogers848871b2013-08-05 10:56:33 -07002862} // namespace art