blob: 0985bf216f28bc08308bf561988b713cf0a522a8 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "common_throws.h"
Vladimir Marko606adb32018-04-05 14:49:24 +010022#include "debug_print.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070023#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file-inl.h"
25#include "dex/dex_file_types.h"
26#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010029#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070031#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070032#include "imt_conflict_table.h"
33#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000034#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070035#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "interpreter/interpreter.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010037#include "interpreter/shadow_frame-inl.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000038#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000039#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000040#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070041#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070042#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070043#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000044#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010047#include "mirror/var_handle.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010048#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010049#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070050#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070051#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070053#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070054#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010055#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000056#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070057
58namespace art {
59
Andreas Gampe8228cdf2017-05-30 15:03:54 -070060// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070061class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080062 // Number of bytes for each out register in the caller method's frame.
63 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070064 // Frame size in bytes of a callee-save frame for RefsAndArgs.
65 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010066 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
67 // Offset of first GPR arg.
68 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
69 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
70 // Offset of first FPR arg.
71 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
72 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
73 // Offset of return address.
74 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
75 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070076#if defined(__arm__)
77 // The callee save frame is pointed to by SP.
78 // | argN | |
79 // | ... | |
80 // | arg4 | |
81 // | arg3 spill | | Caller's frame
82 // | arg2 spill | |
83 // | arg1 spill | |
84 // | Method* | ---
85 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080086 // | ... | 4x6 bytes callee saves
87 // | R3 |
88 // | R2 |
89 // | R1 |
90 // | S15 |
91 // | : |
92 // | S0 |
93 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070094 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070095 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
96 static constexpr bool kAlignPairRegister = true;
97 static constexpr bool kQuickSoftFloatAbi = false;
98 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000099 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800100 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -0700101 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800102 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800103 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000104 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800105 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106#elif defined(__aarch64__)
107 // The callee save frame is pointed to by SP.
108 // | argN | |
109 // | ... | |
110 // | arg4 | |
111 // | arg3 spill | | Caller's frame
112 // | arg2 spill | |
113 // | arg1 spill | |
114 // | Method* | ---
115 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800116 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000117 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100118 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 // | X7 |
120 // | : |
121 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800122 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 // | : |
124 // | D0 |
125 // | | padding
126 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500127 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000128 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000129 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800130 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000131 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000132 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
133 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800134 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000135 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000136 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800138#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700139 // The callee save frame is pointed to by SP.
140 // | argN | |
141 // | ... | |
142 // | arg4 | |
143 // | arg3 spill | | Caller's frame
144 // | arg2 spill | |
145 // | arg1 spill | |
146 // | Method* | ---
147 // | RA |
148 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800149 // | T1 | arg5
150 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700151 // | A3 | arg3
152 // | A2 | arg2
153 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800154 // | F19 |
155 // | F18 | f_arg5
156 // | F17 |
157 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000158 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800159 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800161 // | F12 | f_arg2
162 // | F11 |
163 // | F10 | f_arg1
164 // | F9 |
165 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000166 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700167 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000168 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
169 static constexpr bool kAlignPairRegister = true;
170 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800171 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000172 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800173 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
174 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
175 // passed only in even numbered registers and each
176 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800177 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800178 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000179 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800180 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800181#elif defined(__mips__) && defined(__LP64__)
182 // The callee save frame is pointed to by SP.
183 // | argN | |
184 // | ... | |
185 // | arg4 | |
186 // | arg3 spill | | Caller's frame
187 // | arg2 spill | |
188 // | arg1 spill | |
189 // | Method* | ---
190 // | RA |
191 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800192 // | A7 | arg7
193 // | A6 | arg6
194 // | A5 | arg5
195 // | A4 | arg4
196 // | A3 | arg3
197 // | A2 | arg2
198 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000199 // | F19 | f_arg7
200 // | F18 | f_arg6
201 // | F17 | f_arg5
202 // | F16 | f_arg4
203 // | F15 | f_arg3
204 // | F14 | f_arg2
205 // | F13 | f_arg1
206 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800207 // | | padding
208 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800209 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800210 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800211 static constexpr bool kAlignPairRegister = false;
212 static constexpr bool kQuickSoftFloatAbi = false;
213 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000214 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800215 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
216 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
217 static constexpr bool kGprFprLockstep = true;
218
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800219 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
220 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
221 }
Ian Rogers848871b2013-08-05 10:56:33 -0700222#elif defined(__i386__)
223 // The callee save frame is pointed to by SP.
224 // | argN | |
225 // | ... | |
226 // | arg4 | |
227 // | arg3 spill | | Caller's frame
228 // | arg2 spill | |
229 // | arg1 spill | |
230 // | Method* | ---
231 // | Return |
232 // | EBP,ESI,EDI | callee saves
233 // | EBX | arg3
234 // | EDX | arg2
235 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000236 // | XMM3 | float arg 4
237 // | XMM2 | float arg 3
238 // | XMM1 | float arg 2
239 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700240 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500241 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000242 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000243 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800244 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000245 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800246 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800248 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800249 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000250 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800251 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 // The callee save frame is pointed to by SP.
254 // | argN | |
255 // | ... | |
256 // | reg. arg spills | | Caller's frame
257 // | Method* | ---
258 // | Return |
259 // | R15 | callee save
260 // | R14 | callee save
261 // | R13 | callee save
262 // | R12 | callee save
263 // | R9 | arg5
264 // | R8 | arg4
265 // | RSI/R6 | arg1
266 // | RBP/R5 | callee save
267 // | RBX/R3 | callee save
268 // | RDX/R2 | arg2
269 // | RCX/R1 | arg3
270 // | XMM7 | float arg 8
271 // | XMM6 | float arg 7
272 // | XMM5 | float arg 6
273 // | XMM4 | float arg 5
274 // | XMM3 | float arg 4
275 // | XMM2 | float arg 3
276 // | XMM1 | float arg 2
277 // | XMM0 | float arg 1
278 // | Padding |
279 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500280 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000281 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800282 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800283 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000284 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700285 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700286 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800287 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
289 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000290 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
291 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
292 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
293 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700296 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
297 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 }
299 }
Ian Rogers848871b2013-08-05 10:56:33 -0700300#else
301#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700302#endif
303
Ian Rogers936b37f2014-02-14 00:52:24 -0800304 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100305 // Special handling for proxy methods. Proxy methods are instance methods so the
306 // 'this' object is the 1st argument. They also have the same frame layout as the
307 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
308 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000309 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000311 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100312 CHECK_GT(kNumQuickGprArgs, 0u);
313 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
314 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
315 GprIndexToGprOffset(kThisGprIndex);
316 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000317 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100318 }
319
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700320 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700322 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100323 }
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327 uint8_t* previous_sp =
328 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 }
331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100334 constexpr size_t callee_frame_size =
335 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
337 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100339 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
340 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100342 if (current_code->IsOptimized()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100343 CodeInfo code_info(current_code);
344 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100345 DCHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +0100346 if (stack_map.HasInlineInfo()) {
347 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
348 return inline_info.GetDexPcAtDepth(inline_info.GetDepth()-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100350 return stack_map.GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100351 }
352 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100353 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100354 }
Ian Rogers848871b2013-08-05 10:56:33 -0700355 }
356
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800357 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
358 REQUIRES_SHARED(Locks::mutator_lock_) {
359 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100360 constexpr size_t callee_frame_size =
361 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800362 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
363 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
364 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
365 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
366 if (!current_code->IsOptimized()) {
367 return false;
368 }
369 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +0100370 CodeInfo code_info(current_code);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700371 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
David Srbecky052f8ca2018-04-26 15:42:54 +0100372 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset));
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800373 if (invoke.IsValid()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100374 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType());
375 *dex_method_index = invoke.GetMethodIndex(method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800376 return true;
377 }
378 return false;
379 }
380
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700382 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100384 uint8_t* return_adress_spill =
385 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset;
386 return *reinterpret_cast<uintptr_t*>(return_adress_spill);
Ian Rogers848871b2013-08-05 10:56:33 -0700387 }
388
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700391 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700392 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
393 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
394 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800396 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
397 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800398 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
399 "Number of Quick FPR arguments unexpected");
400 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
401 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800402 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
403 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800404 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
405 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700406 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800407 }
Ian Rogers848871b2013-08-05 10:56:33 -0700408
409 virtual ~QuickArgumentVisitor() {}
410
411 virtual void Visit() = 0;
412
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 Primitive::Type GetParamPrimitiveType() const {
414 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700415 }
416
Ian Rogers13735952014-10-08 12:43:28 -0700417 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800418 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800419 Primitive::Type type = GetParamPrimitiveType();
420 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
422 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
423 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
424 }
425 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000426 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800427 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700428 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 }
430 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800431 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
433 }
434 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700435 }
436
437 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
439 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 return is_split_long_or_double_;
441 } else {
442 return false; // An optimization for when GPR and FPRs are 64bit.
443 }
Ian Rogers848871b2013-08-05 10:56:33 -0700444 }
445
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700447 return GetParamPrimitiveType() == Primitive::kPrimNot;
448 }
449
Ian Rogers936b37f2014-02-14 00:52:24 -0800450 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700451 Primitive::Type type = GetParamPrimitiveType();
452 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
453 }
454
455 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000456 // The splitted long is always available through the stack.
457 return *reinterpret_cast<uint64_t*>(stack_args_
458 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700459 }
460
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800461 void IncGprIndex() {
462 gpr_index_++;
463 if (kGprFprLockstep) {
464 fpr_index_++;
465 }
466 }
467
468 void IncFprIndex() {
469 fpr_index_++;
470 if (kGprFprLockstep) {
471 gpr_index_++;
472 }
473 }
474
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700475 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800476 // (a) 'stack_args_' should point to the first method's argument
477 // (b) whatever the argument type it is, the 'stack_index_' should
478 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800479 gpr_index_ = 0;
480 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 if (kQuickDoubleRegAlignedFloatBackFilled) {
482 fpr_double_index_ = 0;
483 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800484 stack_index_ = 0;
485 if (!is_static_) { // Handle this.
486 cur_type_ = Primitive::kPrimNot;
487 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700488 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800490 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800491 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 }
Ian Rogers848871b2013-08-05 10:56:33 -0700493 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800494 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
495 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
496 switch (cur_type_) {
497 case Primitive::kPrimNot:
498 case Primitive::kPrimBoolean:
499 case Primitive::kPrimByte:
500 case Primitive::kPrimChar:
501 case Primitive::kPrimShort:
502 case Primitive::kPrimInt:
503 is_split_long_or_double_ = false;
504 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800505 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800506 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800507 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800508 }
509 break;
510 case Primitive::kPrimFloat:
511 is_split_long_or_double_ = false;
512 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800513 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800514 if (kQuickSoftFloatAbi) {
515 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800516 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800517 }
518 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800519 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800521 if (kQuickDoubleRegAlignedFloatBackFilled) {
522 // Double should not overlap with float.
523 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
524 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
525 // Float should not overlap with double.
526 if (fpr_index_ % 2 == 0) {
527 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
528 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000529 } else if (kQuickSkipOddFpRegisters) {
530 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800531 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 }
533 }
534 break;
535 case Primitive::kPrimDouble:
536 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800537 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800538 if (cur_type_ == Primitive::kPrimLong &&
539#if defined(__mips__) && !defined(__LP64__)
540 (gpr_index_ == 0 || gpr_index_ == 2) &&
541#else
542 gpr_index_ == 0 &&
543#endif
544 kAlignPairRegister) {
545 // Currently, this is only for ARM and MIPS, where we align long parameters with
546 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
547 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800548 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000549 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000550 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800551 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500552 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
553 // We don't want to split this. Pass over this register.
554 gpr_index_++;
555 is_split_long_or_double_ = false;
556 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800558 if (kBytesStackArgLocation == 4) {
559 stack_index_+= 2;
560 } else {
561 CHECK_EQ(kBytesStackArgLocation, 8U);
562 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800563 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700564 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800565 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000566 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700567 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800568 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700569 }
570 }
571 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800572 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000573 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800574 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700576 if (kBytesStackArgLocation == 4) {
577 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700579 CHECK_EQ(kBytesStackArgLocation, 8U);
580 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800582 if (kQuickDoubleRegAlignedFloatBackFilled) {
583 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
584 fpr_double_index_ += 2;
585 // Float should not overlap with double.
586 if (fpr_index_ % 2 == 0) {
587 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
588 }
589 }
590 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800591 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800592 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
593 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800594 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 }
596 }
597 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800598 }
599 break;
600 default:
601 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
602 }
Ian Rogers848871b2013-08-05 10:56:33 -0700603 }
604 }
605
Andreas Gampec200a4a2014-06-16 18:39:09 -0700606 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700607 const bool is_static_;
608 const char* const shorty_;
609 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700610
611 private:
Ian Rogers13735952014-10-08 12:43:28 -0700612 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
613 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
614 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800615 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800616 // Index into spilled FPRs.
617 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
618 // holds a higher register number.
619 uint32_t fpr_index_;
620 // Index into spilled FPRs for aligned double.
621 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
622 // terms of singles, may be behind fpr_index.
623 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800624 uint32_t stack_index_; // Index into arguments on the stack.
625 // The current type of argument during VisitArguments.
626 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700627 // Does a 64bit parameter straddle the register and stack arguments?
628 bool is_split_long_or_double_;
629};
630
Sebastien Hertza836bc92014-11-25 16:30:53 +0100631// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
632// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700634 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000635 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
636}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100637
Ian Rogers848871b2013-08-05 10:56:33 -0700638// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800639class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700640 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
642 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700643 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700644
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700645 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700646
647 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800648 ShadowFrame* const sf_;
649 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700650
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700651 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700652};
653
Andreas Gampec200a4a2014-06-16 18:39:09 -0700654void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700655 Primitive::Type type = GetParamPrimitiveType();
656 switch (type) {
657 case Primitive::kPrimLong: // Fall-through.
658 case Primitive::kPrimDouble:
659 if (IsSplitLongOrDouble()) {
660 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
661 } else {
662 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
663 }
664 ++cur_reg_;
665 break;
666 case Primitive::kPrimNot: {
667 StackReference<mirror::Object>* stack_ref =
668 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
669 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
670 }
671 break;
672 case Primitive::kPrimBoolean: // Fall-through.
673 case Primitive::kPrimByte: // Fall-through.
674 case Primitive::kPrimChar: // Fall-through.
675 case Primitive::kPrimShort: // Fall-through.
676 case Primitive::kPrimInt: // Fall-through.
677 case Primitive::kPrimFloat:
678 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
679 break;
680 case Primitive::kPrimVoid:
681 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700682 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700683 }
684 ++cur_reg_;
685}
686
Mingyao Yang417528d2017-09-13 12:10:40 -0700687// Don't inline. See b/65159206.
688NO_INLINE
689static void HandleDeoptimization(JValue* result,
690 ArtMethod* method,
691 ShadowFrame* deopt_frame,
692 ManagedStack* fragment)
693 REQUIRES_SHARED(Locks::mutator_lock_) {
694 // Coming from partial-fragment deopt.
695 Thread* self = Thread::Current();
696 if (kIsDebugBuild) {
697 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
698 // of the call-stack) corresponds to the called method.
699 ShadowFrame* linked = deopt_frame;
700 while (linked->GetLink() != nullptr) {
701 linked = linked->GetLink();
702 }
703 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
704 << ArtMethod::PrettyMethod(linked->GetMethod());
705 }
706
707 if (VLOG_IS_ON(deopt)) {
708 // Print out the stack to verify that it was a partial-fragment deopt.
709 LOG(INFO) << "Continue-ing from deopt. Stack is:";
710 QuickExceptionHandler::DumpFramesWithType(self, true);
711 }
712
713 ObjPtr<mirror::Throwable> pending_exception;
714 bool from_code = false;
715 DeoptimizationMethodType method_type;
716 self->PopDeoptimizationContext(/* out */ result,
717 /* out */ &pending_exception,
718 /* out */ &from_code,
719 /* out */ &method_type);
720
721 // Push a transition back into managed code onto the linked list in thread.
722 self->PushManagedStackFragment(fragment);
723
724 // Ensure that the stack is still in order.
725 if (kIsDebugBuild) {
726 class DummyStackVisitor : public StackVisitor {
727 public:
728 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
729 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
730
731 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
732 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
733 // logic. Just always say we want to continue.
734 return true;
735 }
736 };
737 DummyStackVisitor dsv(self);
738 dsv.WalkStack();
739 }
740
741 // Restore the exception that was pending before deoptimization then interpret the
742 // deoptimized frames.
743 if (pending_exception != nullptr) {
744 self->SetException(pending_exception);
745 }
746 interpreter::EnterInterpreterFromDeoptimize(self,
747 deopt_frame,
748 result,
749 from_code,
750 DeoptimizationMethodType::kDefault);
751}
752
Mathieu Chartiere401d142015-04-22 13:56:20 -0700753extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700754 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700755 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
756 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700757 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700758
Alex Light9139e002015-10-09 15:59:48 -0700759 if (UNLIKELY(!method->IsInvokable())) {
760 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700761 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700762 }
763
764 JValue tmp_value;
765 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700766 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700767 ManagedStack fragment;
768
David Sehr709b0702016-10-13 09:12:37 -0700769 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700770 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700771 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800772 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000773 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700774 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
775
776 JValue result;
777
Mingyao Yang417528d2017-09-13 12:10:40 -0700778 if (UNLIKELY(deopt_frame != nullptr)) {
779 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700780 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700781 const char* old_cause = self->StartAssertNoThreadSuspension(
782 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800783 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700784 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700785 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700786 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700787 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800788 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700789 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800790 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700791 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800792 const bool needs_initialization =
793 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700794 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700795 self->PushManagedStackFragment(&fragment);
796 self->PushShadowFrame(shadow_frame);
797 self->EndAssertNoThreadSuspension(old_cause);
798
Ian Rogerse94652f2014-12-02 11:13:19 -0800799 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700800 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800801 StackHandleScope<1> hs(self);
802 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700803 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700804 DCHECK(Thread::Current()->IsExceptionPending())
805 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700806 self->PopManagedStackFragment(fragment);
807 return 0;
808 }
809 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200810
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800811 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700812 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700813
814 // Pop transition.
815 self->PopManagedStackFragment(fragment);
816
817 // Request a stack deoptimization if needed
818 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700819 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700820 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
821 // should be done and it knows the real return pc.
822 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000823 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
824 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
825 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
826 << caller->PrettyMethod();
827 } else {
828 // Push the context of the deoptimization stack so we can restore the return value and the
829 // exception before executing the deoptimized frames.
830 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700831 result,
832 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
833 self->GetException(),
834 false /* from_code */,
835 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700836
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000837 // Set special exception to cause deoptimization.
838 self->SetException(Thread::GetDeoptimizationException());
839 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700840 }
841
842 // No need to restore the args since the method has already been run by the interpreter.
843 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700844}
845
846// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
847// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800848class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700849 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700851 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700852 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700853
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700854 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700855
856 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700857 ScopedObjectAccessUnchecked* const soa_;
858 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700859
Ian Rogers848871b2013-08-05 10:56:33 -0700860 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
861};
862
Ian Rogers9758f792014-03-13 09:02:55 -0700863void BuildQuickArgumentVisitor::Visit() {
864 jvalue val;
865 Primitive::Type type = GetParamPrimitiveType();
866 switch (type) {
867 case Primitive::kPrimNot: {
868 StackReference<mirror::Object>* stack_ref =
869 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
870 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700871 break;
872 }
873 case Primitive::kPrimLong: // Fall-through.
874 case Primitive::kPrimDouble:
875 if (IsSplitLongOrDouble()) {
876 val.j = ReadSplitLongParam();
877 } else {
878 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
879 }
880 break;
881 case Primitive::kPrimBoolean: // Fall-through.
882 case Primitive::kPrimByte: // Fall-through.
883 case Primitive::kPrimChar: // Fall-through.
884 case Primitive::kPrimShort: // Fall-through.
885 case Primitive::kPrimInt: // Fall-through.
886 case Primitive::kPrimFloat:
887 val.i = *reinterpret_cast<jint*>(GetParamAddress());
888 break;
889 case Primitive::kPrimVoid:
890 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700891 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700892 }
893 args_->push_back(val);
894}
895
Ian Rogers848871b2013-08-05 10:56:33 -0700896// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
897// which is responsible for recording callee save registers. We explicitly place into jobjects the
898// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
899// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900extern "C" uint64_t artQuickProxyInvokeHandler(
901 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700902 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700903 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
904 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700905 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
906 const char* old_cause =
907 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
908 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700909 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700910 self->VerifyStack();
911 // Start new JNI local reference state.
912 JNIEnvExt* env = self->GetJniEnv();
913 ScopedObjectAccessUnchecked soa(env);
914 ScopedJniEnvLocalRefState env_state(env);
915 // Create local ref. copies of proxy method and the receiver.
916 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
917
918 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700919 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700920 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
921 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700922 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700923 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000925 BuildQuickArgumentVisitor local_ref_visitor(
926 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700927
Ian Rogers848871b2013-08-05 10:56:33 -0700928 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700929 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700930 args.erase(args.begin());
931
932 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700933 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700934 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
935 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700936 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700937 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700938 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700939 ObjPtr<mirror::Method> interface_reflect_method =
940 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
941 if (interface_reflect_method == nullptr) {
942 soa.Self()->AssertPendingOOMException();
943 return 0;
944 }
945 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700946
947 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
948 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700949 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700950 return result.GetJ();
951}
952
Roland Levillainad0777d2018-02-12 20:00:18 +0000953// Visitor returning a reference argument at a given position in a Quick stack frame.
954// NOTE: Only used for testing purposes.
955class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
956 public:
957 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
958 const char* shorty,
959 uint32_t shorty_len,
960 size_t arg_pos)
961 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
962 cur_pos_(0u),
963 arg_pos_(arg_pos),
964 ref_arg_(nullptr) {
965 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
966 }
967
968 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
969 if (cur_pos_ == arg_pos_) {
970 Primitive::Type type = GetParamPrimitiveType();
971 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
972 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
973 }
974 ++cur_pos_;
975 }
976
977 StackReference<mirror::Object>* GetReferenceArgument() {
978 return ref_arg_;
979 }
980
981 private:
982 // The position of the currently visited argument.
983 size_t cur_pos_;
984 // The position of the searched argument.
985 const size_t arg_pos_;
986 // The reference argument, if found.
987 StackReference<mirror::Object>* ref_arg_;
988
989 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
990};
991
992// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
993// NOTE: Only used for testing purposes.
994extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
995 ArtMethod** sp)
996 REQUIRES_SHARED(Locks::mutator_lock_) {
997 ArtMethod* proxy_method = *sp;
998 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
999 CHECK(!non_proxy_method->IsStatic())
1000 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1001 uint32_t shorty_len = 0;
1002 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1003 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1004 ref_arg_visitor.VisitArguments();
1005 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1006 return ref_arg;
1007}
1008
1009// Visitor returning all the reference arguments in a Quick stack frame.
1010class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1011 public:
1012 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1013 bool is_static,
1014 const char* shorty,
1015 uint32_t shorty_len)
1016 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1017
1018 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1019 Primitive::Type type = GetParamPrimitiveType();
1020 if (type == Primitive::kPrimNot) {
1021 StackReference<mirror::Object>* ref_arg =
1022 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1023 ref_args_.push_back(ref_arg);
1024 }
1025 }
1026
1027 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1028 return ref_args_;
1029 }
1030
1031 private:
1032 // The reference arguments.
1033 std::vector<StackReference<mirror::Object>*> ref_args_;
1034
1035 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1036};
1037
1038// Returning all reference arguments in Quick stack frame at address `sp`.
1039std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1040 REQUIRES_SHARED(Locks::mutator_lock_) {
1041 ArtMethod* proxy_method = *sp;
1042 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1043 CHECK(!non_proxy_method->IsStatic())
1044 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1045 uint32_t shorty_len = 0;
1046 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1047 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1048 ref_args_visitor.VisitArguments();
1049 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1050 return ref_args;
1051}
1052
Ian Rogers848871b2013-08-05 10:56:33 -07001053// Read object references held in arguments from quick frames and place in a JNI local references,
1054// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001055class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001056 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1058 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001059 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001060
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001061 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001062
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001063 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001064
1065 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001066 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001067 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001068 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1069
Mathieu Chartier590fee92013-09-13 13:46:47 -07001070 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001071};
1072
Ian Rogers9758f792014-03-13 09:02:55 -07001073void RememberForGcArgumentVisitor::Visit() {
1074 if (IsParamAReference()) {
1075 StackReference<mirror::Object>* stack_ref =
1076 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1077 jobject reference =
1078 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1079 references_.push_back(std::make_pair(reference, stack_ref));
1080 }
1081}
1082
1083void RememberForGcArgumentVisitor::FixupReferences() {
1084 // Fixup any references which may have changed.
1085 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001086 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001087 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001088 }
1089}
1090
Alex Lightb7edcda2017-04-27 13:20:31 -07001091extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1092 mirror::Object* this_object,
1093 Thread* self,
1094 ArtMethod** sp)
1095 REQUIRES_SHARED(Locks::mutator_lock_) {
1096 const void* result;
1097 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1098 // that part.
1099 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1100 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1101 if (instrumentation->IsDeoptimized(method)) {
1102 result = GetQuickToInterpreterBridge();
1103 } else {
1104 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1105 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1106 }
1107
1108 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1109 bool is_static = method->IsStatic();
1110 uint32_t shorty_len;
1111 const char* shorty =
1112 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1113
1114 ScopedObjectAccessUnchecked soa(self);
1115 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1116 visitor.VisitArguments();
1117
1118 instrumentation->PushInstrumentationStackFrame(self,
1119 is_static ? nullptr : this_object,
1120 method,
1121 QuickArgumentVisitor::GetCallingPc(sp),
1122 interpreter_entry);
1123
1124 visitor.FixupReferences();
1125 if (UNLIKELY(self->IsExceptionPending())) {
1126 return nullptr;
1127 }
1128 CHECK(result != nullptr) << method->PrettyMethod();
1129 return result;
1130}
1131
1132extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1133 ArtMethod** sp,
1134 uint64_t* gpr_result,
1135 uint64_t* fpr_result)
1136 REQUIRES_SHARED(Locks::mutator_lock_) {
1137 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1138 CHECK(gpr_result != nullptr);
1139 CHECK(fpr_result != nullptr);
1140 // Instrumentation exit stub must not be entered with a pending exception.
1141 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1142 << self->GetException()->Dump();
1143 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001144 constexpr size_t return_pc_offset =
1145 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001146 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1147 return_pc_offset);
1148 CHECK_EQ(*return_pc, 0U);
1149
1150 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1151 // deoptimization shouldn't be performed with the high-half having the return address. When
1152 // deoptimization should be performed the low half is zero and the high-half the address of the
1153 // deoptimization entry point.
1154 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1155 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1156 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001157 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001158 return GetTwoWordFailureValue();
1159 }
1160 return return_or_deoptimize_pc;
1161}
1162
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001163static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1164 REQUIRES_SHARED(Locks::mutator_lock_) {
1165 if (dex_pc == static_cast<uint32_t>(-1)) {
1166 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1167 return "<native>";
1168 } else {
1169 CodeItemInstructionAccessor accessor = method->DexInstructions();
1170 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1171 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1172 }
1173}
1174
Vladimir Marko606adb32018-04-05 14:49:24 +01001175static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1176 REQUIRES_SHARED(Locks::mutator_lock_) {
1177 std::string storage;
1178 const char* descriptor = klass->GetDescriptor(&storage);
1179 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1180 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1181 if (oat_dex_file != nullptr) {
1182 const OatFile* oat_file = oat_dex_file->GetOatFile();
1183 const char* dex2oat_cmdline =
1184 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1185 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1186 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1187 }
1188}
1189
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001190static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1191 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001192 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001193
1194 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1195 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1196
Vladimir Markod3083dd2018-05-17 08:43:47 +01001197 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001198 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1199 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001200 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001201 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1202 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1203 ArtMethod* outer_method = *caller_sp;
1204
1205 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1206 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1207 << " native pc: " << caller_pc << " Instrumented!";
1208 return;
1209 }
1210
1211 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1212 CHECK(current_code != nullptr);
1213 CHECK(current_code->IsOptimized());
1214 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +01001215 CodeInfo code_info(current_code);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001216 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
David Srbecky052f8ca2018-04-26 15:42:54 +01001217 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001218 CHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +01001219 uint32_t dex_pc = stack_map.GetDexPc();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001220
1221 // Log the outer method and its associated dex file and class table pointer which can be used
1222 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1223 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1224 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1225 << " native pc: " << caller_pc
1226 << " dex pc: " << dex_pc
1227 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1228 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001229 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001230 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1231
1232 ArtMethod* caller = outer_method;
David Srbecky052f8ca2018-04-26 15:42:54 +01001233 if (stack_map.HasInlineInfo()) {
1234 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
1235 size_t depth = inline_info.GetDepth();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001236 for (size_t d = 0; d < depth; ++d) {
1237 const char* tag = "";
David Srbecky052f8ca2018-04-26 15:42:54 +01001238 dex_pc = inline_info.GetDexPcAtDepth(d);
1239 if (inline_info.EncodesArtMethodAtDepth(d)) {
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001240 tag = "encoded ";
David Srbecky052f8ca2018-04-26 15:42:54 +01001241 caller = inline_info.GetArtMethodAtDepth(d);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001242 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +01001243 uint32_t method_index = inline_info.GetMethodIndexAtDepth(method_info, d);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001244 if (dex_pc == static_cast<uint32_t>(-1)) {
1245 tag = "special ";
1246 CHECK_EQ(d + 1u, depth);
1247 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1248 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1249 } else {
1250 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1251 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1252 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1253 CHECK(caller != nullptr);
1254 }
1255 }
1256 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1257 << " dex pc: " << dex_pc
1258 << " dex file: " << caller->GetDexFile()->GetLocation()
1259 << " class table: "
Vladimir Marko606adb32018-04-05 14:49:24 +01001260 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1261 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001262 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1263 }
1264 }
1265}
1266
Ian Rogers848871b2013-08-05 10:56:33 -07001267// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001268extern "C" const void* artQuickResolutionTrampoline(
1269 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001270 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001271 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1272 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1273 // does not have the same stack layout as the callee-save method).
1274 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001275 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001276 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001277 ScopedObjectAccessUnchecked soa(env);
1278 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001279 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001280
1281 // Compute details about the called method (avoid GCs)
1282 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001283 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001284 MethodReference called_method(nullptr, 0);
1285 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001286 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001287 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001288 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001289 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001290
1291 InvokeType stack_map_invoke_type;
1292 uint32_t stack_map_dex_method_idx;
1293 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1294 &stack_map_invoke_type,
1295 &stack_map_dex_method_idx);
1296 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1297 // code.
1298 if (!found_stack_map || kIsDebugBuild) {
1299 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001300 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001301 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1302 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001303 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001304 bool is_range;
1305 switch (instr_code) {
1306 case Instruction::INVOKE_DIRECT:
1307 invoke_type = kDirect;
1308 is_range = false;
1309 break;
1310 case Instruction::INVOKE_DIRECT_RANGE:
1311 invoke_type = kDirect;
1312 is_range = true;
1313 break;
1314 case Instruction::INVOKE_STATIC:
1315 invoke_type = kStatic;
1316 is_range = false;
1317 break;
1318 case Instruction::INVOKE_STATIC_RANGE:
1319 invoke_type = kStatic;
1320 is_range = true;
1321 break;
1322 case Instruction::INVOKE_SUPER:
1323 invoke_type = kSuper;
1324 is_range = false;
1325 break;
1326 case Instruction::INVOKE_SUPER_RANGE:
1327 invoke_type = kSuper;
1328 is_range = true;
1329 break;
1330 case Instruction::INVOKE_VIRTUAL:
1331 invoke_type = kVirtual;
1332 is_range = false;
1333 break;
1334 case Instruction::INVOKE_VIRTUAL_RANGE:
1335 invoke_type = kVirtual;
1336 is_range = true;
1337 break;
1338 case Instruction::INVOKE_INTERFACE:
1339 invoke_type = kInterface;
1340 is_range = false;
1341 break;
1342 case Instruction::INVOKE_INTERFACE_RANGE:
1343 invoke_type = kInterface;
1344 is_range = true;
1345 break;
1346 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001347 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001348 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001349 UNREACHABLE();
1350 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001351 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001352 // Check that the invoke matches what we expected, note that this path only happens for debug
1353 // builds.
1354 if (found_stack_map) {
1355 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1356 if (invoke_type != kSuper) {
1357 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001358 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001359 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001360 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001361 }
1362 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001363 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001364 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001365 }
1366 } else {
1367 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001368 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001369 }
Ian Rogers848871b2013-08-05 10:56:33 -07001370 } else {
1371 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001372 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001373 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001374 }
1375 uint32_t shorty_len;
1376 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001377 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001378 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001379 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001380 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001381 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001382 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001383 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001384 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001385 mirror::Object* dummy = nullptr;
1386 HandleWrapper<mirror::Object> h_receiver(
1387 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001388 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001389 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001390 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001391
1392 // Update .bss entry in oat file if any.
1393 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001394 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1395 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1396 called_method.index,
1397 called_method.dex_file->NumMethodIds(),
1398 static_cast<size_t>(kRuntimePointerSize));
1399 if (bss_offset != IndexBssMappingLookup::npos) {
1400 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1401 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1402 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1403 oat_file->BssBegin() + bss_offset));
1404 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1405 DCHECK_LT(method_entry,
1406 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1407 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001408 }
1409 }
Ian Rogers848871b2013-08-05 10:56:33 -07001410 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001411 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001412 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001413 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001414 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001415 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001416 if (virtual_or_interface || invoke_type == kSuper) {
1417 // Refine called method based on receiver for kVirtual/kInterface, and
1418 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001419 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001420 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001421 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001422 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001423 } else if (invoke_type == kInterface) {
1424 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001425 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001426 } else {
1427 DCHECK_EQ(invoke_type, kSuper);
1428 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001429 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001430 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001431 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001432 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001433 } else {
1434 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001435 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001436 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001437 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001438
David Sehr709b0702016-10-13 09:12:37 -07001439 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1440 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001441 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001442 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001443
Ian Rogers848871b2013-08-05 10:56:33 -07001444 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001445 StackHandleScope<1> hs(soa.Self());
1446 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001447 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001448 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001449 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1450 // If we are single-stepping or the called method is deoptimized (by a
1451 // breakpoint, for example), then we have to execute the called method
1452 // with the interpreter.
1453 code = GetQuickToInterpreterBridge();
1454 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1455 // If the caller is deoptimized (by a breakpoint, for example), we have to
1456 // continue its execution with interpreter when returning from the called
1457 // method. Because we do not want to execute the called method with the
1458 // interpreter, we wrap its execution into the instrumentation stubs.
1459 // When the called method returns, it will execute the instrumentation
1460 // exit hook that will determine the need of the interpreter with a call
1461 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1462 // it is needed.
1463 code = GetQuickInstrumentationEntryPoint();
1464 } else {
1465 code = called->GetEntryPointFromQuickCompiledCode();
1466 }
Ian Rogers848871b2013-08-05 10:56:33 -07001467 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001468 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1469 // If we are single-stepping or the called method is deoptimized (by a
1470 // breakpoint, for example), then we have to execute the called method
1471 // with the interpreter.
1472 code = GetQuickToInterpreterBridge();
1473 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001474 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1475 // until class is initialized to stop races between threads).
1476 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001477 } else {
1478 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001479 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001480 }
1481 } else {
1482 DCHECK(called_class->IsErroneous());
1483 }
1484 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001485 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001486 // Fixup any locally saved objects may have moved during a GC.
1487 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001488 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001489 *sp = called;
1490
Ian Rogers848871b2013-08-05 10:56:33 -07001491 return code;
1492}
1493
Andreas Gampec147b002014-03-06 18:11:06 -08001494/*
1495 * This class uses a couple of observations to unite the different calling conventions through
1496 * a few constants.
1497 *
1498 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1499 * possible alignment.
1500 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1501 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1502 * when we have to split things
1503 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1504 * and we can use Int handling directly.
1505 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1506 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1507 * extension should be compatible with Aarch64, which mandates copying the available bits
1508 * into LSB and leaving the rest unspecified.
1509 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1510 * the stack.
1511 * 6) There is only little endian.
1512 *
1513 *
1514 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1515 * follows:
1516 *
1517 * void PushGpr(uintptr_t): Add a value for the next GPR
1518 *
1519 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1520 * padding, that is, think the architecture is 32b and aligns 64b.
1521 *
1522 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1523 * split this if necessary. The current state will have aligned, if
1524 * necessary.
1525 *
1526 * void PushStack(uintptr_t): Push a value to the stack.
1527 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001528 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001529 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001530 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001531 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001532 *
1533 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001534template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001535 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001536#if defined(__arm__)
1537 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001538 static constexpr bool kNativeSoftFloatAbi = true;
1539 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001540 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1541
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001542 static constexpr size_t kRegistersNeededForLong = 2;
1543 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001544 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001545 static constexpr bool kMultiFPRegistersWidened = false;
1546 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001547 static constexpr bool kAlignLongOnStack = true;
1548 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001549#elif defined(__aarch64__)
1550 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1551 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1552 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1553
1554 static constexpr size_t kRegistersNeededForLong = 1;
1555 static constexpr size_t kRegistersNeededForDouble = 1;
1556 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001557 static constexpr bool kMultiFPRegistersWidened = false;
1558 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001559 static constexpr bool kAlignLongOnStack = false;
1560 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001561#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001562 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001563 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1564 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001565
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001566 static constexpr size_t kRegistersNeededForLong = 2;
1567 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001568 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001569 static constexpr bool kMultiFPRegistersWidened = true;
1570 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001571 static constexpr bool kAlignLongOnStack = true;
1572 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001573#elif defined(__mips__) && defined(__LP64__)
1574 // Let the code prepare GPRs only and we will load the FPRs with same data.
1575 static constexpr bool kNativeSoftFloatAbi = true;
1576 static constexpr size_t kNumNativeGprArgs = 8;
1577 static constexpr size_t kNumNativeFprArgs = 0;
1578
1579 static constexpr size_t kRegistersNeededForLong = 1;
1580 static constexpr size_t kRegistersNeededForDouble = 1;
1581 static constexpr bool kMultiRegistersAligned = false;
1582 static constexpr bool kMultiFPRegistersWidened = false;
1583 static constexpr bool kMultiGPRegistersWidened = true;
1584 static constexpr bool kAlignLongOnStack = false;
1585 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001586#elif defined(__i386__)
1587 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001588 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001589 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1590 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1591
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001592 static constexpr size_t kRegistersNeededForLong = 2;
1593 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001594 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001595 static constexpr bool kMultiFPRegistersWidened = false;
1596 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001597 static constexpr bool kAlignLongOnStack = false;
1598 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001599#elif defined(__x86_64__)
1600 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1601 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1602 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1603
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001604 static constexpr size_t kRegistersNeededForLong = 1;
1605 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001606 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001607 static constexpr bool kMultiFPRegistersWidened = false;
1608 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001609 static constexpr bool kAlignLongOnStack = false;
1610 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001611#else
1612#error "Unsupported architecture"
1613#endif
1614
Andreas Gampec147b002014-03-06 18:11:06 -08001615 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616 explicit BuildNativeCallFrameStateMachine(T* delegate)
1617 : gpr_index_(kNumNativeGprArgs),
1618 fpr_index_(kNumNativeFprArgs),
1619 stack_entries_(0),
1620 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001621 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1622 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001623 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1624 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001625 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001626
Andreas Gampec200a4a2014-06-16 18:39:09 -07001627 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001628
Ian Rogers1428dce2014-10-21 15:02:15 -07001629 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001630 return gpr_index_ > 0;
1631 }
1632
Andreas Gampec200a4a2014-06-16 18:39:09 -07001633 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001634 if (HavePointerGpr()) {
1635 gpr_index_--;
1636 PushGpr(reinterpret_cast<uintptr_t>(val));
1637 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001639 PushStack(reinterpret_cast<uintptr_t>(val));
1640 gpr_index_ = 0;
1641 }
1642 }
1643
Ian Rogers1428dce2014-10-21 15:02:15 -07001644 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001645 return gpr_index_ > 0;
1646 }
1647
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001648 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001649 uintptr_t handle = PushHandle(ptr);
1650 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001651 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001652 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001653 } else {
1654 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001655 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001656 gpr_index_ = 0;
1657 }
1658 }
1659
Ian Rogers1428dce2014-10-21 15:02:15 -07001660 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001661 return gpr_index_ > 0;
1662 }
1663
1664 void AdvanceInt(uint32_t val) {
1665 if (HaveIntGpr()) {
1666 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001667 if (kMultiGPRegistersWidened) {
1668 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001669 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001670 } else {
1671 PushGpr(val);
1672 }
Andreas Gampec147b002014-03-06 18:11:06 -08001673 } else {
1674 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001675 if (kMultiGPRegistersWidened) {
1676 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001677 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001678 } else {
1679 PushStack(val);
1680 }
Andreas Gampec147b002014-03-06 18:11:06 -08001681 gpr_index_ = 0;
1682 }
1683 }
1684
Ian Rogers1428dce2014-10-21 15:02:15 -07001685 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001686 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1687 }
1688
Ian Rogers1428dce2014-10-21 15:02:15 -07001689 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001690 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1691 kAlignLongOnStack && // and when it needs alignment
1692 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1693 }
1694
Ian Rogers1428dce2014-10-21 15:02:15 -07001695 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001696 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1697 kAlignLongOnStack && // and when it needs 8B alignment
1698 (stack_entries_ & 1) == 1; // counter is odd
1699 }
1700
1701 void AdvanceLong(uint64_t val) {
1702 if (HaveLongGpr()) {
1703 if (LongGprNeedsPadding()) {
1704 PushGpr(0);
1705 gpr_index_--;
1706 }
1707 if (kRegistersNeededForLong == 1) {
1708 PushGpr(static_cast<uintptr_t>(val));
1709 } else {
1710 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1711 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1712 }
1713 gpr_index_ -= kRegistersNeededForLong;
1714 } else {
1715 if (LongStackNeedsPadding()) {
1716 PushStack(0);
1717 stack_entries_++;
1718 }
1719 if (kRegistersNeededForLong == 1) {
1720 PushStack(static_cast<uintptr_t>(val));
1721 stack_entries_++;
1722 } else {
1723 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1724 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1725 stack_entries_ += 2;
1726 }
1727 gpr_index_ = 0;
1728 }
1729 }
1730
Ian Rogers1428dce2014-10-21 15:02:15 -07001731 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001732 return fpr_index_ > 0;
1733 }
1734
Andreas Gampec147b002014-03-06 18:11:06 -08001735 void AdvanceFloat(float val) {
1736 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001737 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001738 } else {
1739 if (HaveFloatFpr()) {
1740 fpr_index_--;
1741 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001742 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001743 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001744 } else {
1745 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001746 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001747 }
1748 } else {
1749 PushFpr4(val);
1750 }
1751 } else {
1752 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001753 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001754 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001755 // Note: We need to jump through those hoops to make the compiler happy.
1756 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001757 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001758 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001759 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001760 }
1761 fpr_index_ = 0;
1762 }
1763 }
1764 }
1765
Ian Rogers1428dce2014-10-21 15:02:15 -07001766 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001767 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1768 }
1769
Ian Rogers1428dce2014-10-21 15:02:15 -07001770 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001771 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1772 kAlignDoubleOnStack && // and when it needs alignment
1773 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1774 }
1775
Ian Rogers1428dce2014-10-21 15:02:15 -07001776 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001777 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1778 kAlignDoubleOnStack && // and when it needs 8B alignment
1779 (stack_entries_ & 1) == 1; // counter is odd
1780 }
1781
1782 void AdvanceDouble(uint64_t val) {
1783 if (kNativeSoftFloatAbi) {
1784 AdvanceLong(val);
1785 } else {
1786 if (HaveDoubleFpr()) {
1787 if (DoubleFprNeedsPadding()) {
1788 PushFpr4(0);
1789 fpr_index_--;
1790 }
1791 PushFpr8(val);
1792 fpr_index_ -= kRegistersNeededForDouble;
1793 } else {
1794 if (DoubleStackNeedsPadding()) {
1795 PushStack(0);
1796 stack_entries_++;
1797 }
1798 if (kRegistersNeededForDouble == 1) {
1799 PushStack(static_cast<uintptr_t>(val));
1800 stack_entries_++;
1801 } else {
1802 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1803 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1804 stack_entries_ += 2;
1805 }
1806 fpr_index_ = 0;
1807 }
1808 }
1809 }
1810
Ian Rogers1428dce2014-10-21 15:02:15 -07001811 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001812 return stack_entries_;
1813 }
1814
Ian Rogers1428dce2014-10-21 15:02:15 -07001815 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001816 return kNumNativeGprArgs - gpr_index_;
1817 }
1818
Ian Rogers1428dce2014-10-21 15:02:15 -07001819 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001820 return kNumNativeFprArgs - fpr_index_;
1821 }
1822
1823 private:
1824 void PushGpr(uintptr_t val) {
1825 delegate_->PushGpr(val);
1826 }
1827 void PushFpr4(float val) {
1828 delegate_->PushFpr4(val);
1829 }
1830 void PushFpr8(uint64_t val) {
1831 delegate_->PushFpr8(val);
1832 }
1833 void PushStack(uintptr_t val) {
1834 delegate_->PushStack(val);
1835 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001836 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001837 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001838 }
1839
1840 uint32_t gpr_index_; // Number of free GPRs
1841 uint32_t fpr_index_; // Number of free FPRs
1842 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1843 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001844 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001845};
1846
Andreas Gampec200a4a2014-06-16 18:39:09 -07001847// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1848// in subclasses.
1849//
1850// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1851// them with handles.
1852class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001853 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001854 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1855
1856 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001857
Ian Rogers1428dce2014-10-21 15:02:15 -07001858 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001859 return num_stack_entries_ * sizeof(uintptr_t);
1860 }
1861
Ian Rogers1428dce2014-10-21 15:02:15 -07001862 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001863 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001864 // Align by kStackAlignment.
1865 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001866 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001867 }
1868
Ian Rogers1428dce2014-10-21 15:02:15 -07001869 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1870 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001871 // Assumption is OK right now, as we have soft-float arm
1872 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1873 sp8 -= fregs * sizeof(uintptr_t);
1874 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1875 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1876 sp8 -= iregs * sizeof(uintptr_t);
1877 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1878 return sp8;
1879 }
Andreas Gampec147b002014-03-06 18:11:06 -08001880
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001882 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 // Native call stack.
1884 sp8 = LayoutCallStack(sp8);
1885 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001886
Andreas Gampec200a4a2014-06-16 18:39:09 -07001887 // Put fprs and gprs below.
1888 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890 // Return the new bottom.
1891 return sp8;
1892 }
1893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001894 virtual void WalkHeader(
1895 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001896 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001897 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001898
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001899 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1901
1902 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001903
1904 for (uint32_t i = 1; i < shorty_len; ++i) {
1905 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1906 switch (cur_type_) {
1907 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001908 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001909 sm.AdvanceHandleScope(
1910 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001911 break;
1912
1913 case Primitive::kPrimBoolean:
1914 case Primitive::kPrimByte:
1915 case Primitive::kPrimChar:
1916 case Primitive::kPrimShort:
1917 case Primitive::kPrimInt:
1918 sm.AdvanceInt(0);
1919 break;
1920 case Primitive::kPrimFloat:
1921 sm.AdvanceFloat(0);
1922 break;
1923 case Primitive::kPrimDouble:
1924 sm.AdvanceDouble(0);
1925 break;
1926 case Primitive::kPrimLong:
1927 sm.AdvanceLong(0);
1928 break;
1929 default:
1930 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001931 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001932 }
1933 }
1934
Ian Rogers1428dce2014-10-21 15:02:15 -07001935 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001936 }
1937
1938 void PushGpr(uintptr_t /* val */) {
1939 // not optimizing registers, yet
1940 }
1941
1942 void PushFpr4(float /* val */) {
1943 // not optimizing registers, yet
1944 }
1945
1946 void PushFpr8(uint64_t /* val */) {
1947 // not optimizing registers, yet
1948 }
1949
1950 void PushStack(uintptr_t /* val */) {
1951 // counting is already done in the superclass
1952 }
1953
Andreas Gampec200a4a2014-06-16 18:39:09 -07001954 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001955 return reinterpret_cast<uintptr_t>(nullptr);
1956 }
1957
Andreas Gampec200a4a2014-06-16 18:39:09 -07001958 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001959 uint32_t num_stack_entries_;
1960};
1961
Andreas Gampec200a4a2014-06-16 18:39:09 -07001962class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001963 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001964 explicit ComputeGenericJniFrameSize(bool critical_native)
1965 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001966
Andreas Gampec200a4a2014-06-16 18:39:09 -07001967 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1968 // is at *m = sp. Will update to point to the bottom of the save frame.
1969 //
1970 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001971 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001972 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001973 ArtMethod* method = **m;
1974
Andreas Gampe542451c2016-07-26 09:02:02 -07001975 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001976
Andreas Gampec200a4a2014-06-16 18:39:09 -07001977 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1978
1979 // First, fix up the layout of the callee-save frame.
1980 // We have to squeeze in the HandleScope, and relocate the method pointer.
1981
1982 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001983 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984
1985 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001986 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001988
1989 sp8 -= scope_and_method;
1990 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001991 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001992
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001994 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1995 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001996
1997 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1998 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2000 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002001 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002002 }
2003
Andreas Gampec200a4a2014-06-16 18:39:09 -07002004 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002005 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002006 // Reference cookie and padding
2007 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002008 }
2009
Andreas Gampec200a4a2014-06-16 18:39:09 -07002010 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2011 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002012 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002013 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002014 // First, fix up the layout of the callee-save frame.
2015 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002016 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002017
2018 // The bottom of the callee-save frame is now where the method is, *m.
2019 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2020
2021 // Add space for cookie.
2022 LayoutCookie(&sp8);
2023
2024 return sp8;
2025 }
2026
2027 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002028 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2029 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2030 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002031 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002032 Walk(shorty, shorty_len);
2033
2034 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002035 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002036
2037 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2038
2039 // Return the new bottom.
2040 return sp8;
2041 }
2042
2043 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2044
2045 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2046 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002047 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002048
2049 private:
2050 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002051 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052};
2053
2054uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2055 num_handle_scope_references_++;
2056 return reinterpret_cast<uintptr_t>(nullptr);
2057}
2058
2059void ComputeGenericJniFrameSize::WalkHeader(
2060 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002061 // First 2 parameters are always excluded for @CriticalNative.
2062 if (UNLIKELY(critical_native_)) {
2063 return;
2064 }
2065
Andreas Gampec200a4a2014-06-16 18:39:09 -07002066 // JNIEnv
2067 sm->AdvancePointer(nullptr);
2068
2069 // Class object or this as first argument
2070 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2071}
2072
2073// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2074// the template requirements of BuildGenericJniFrameStateMachine.
2075class FillNativeCall {
2076 public:
2077 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2078 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2079
2080 virtual ~FillNativeCall() {}
2081
2082 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2083 cur_gpr_reg_ = gpr_regs;
2084 cur_fpr_reg_ = fpr_regs;
2085 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002086 }
2087
2088 void PushGpr(uintptr_t val) {
2089 *cur_gpr_reg_ = val;
2090 cur_gpr_reg_++;
2091 }
2092
2093 void PushFpr4(float val) {
2094 *cur_fpr_reg_ = val;
2095 cur_fpr_reg_++;
2096 }
2097
2098 void PushFpr8(uint64_t val) {
2099 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2100 *tmp = val;
2101 cur_fpr_reg_ += 2;
2102 }
2103
2104 void PushStack(uintptr_t val) {
2105 *cur_stack_arg_ = val;
2106 cur_stack_arg_++;
2107 }
2108
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002109 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002110 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002111 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002112 }
2113
2114 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002115 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002116 uint32_t* cur_fpr_reg_;
2117 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002118};
Andreas Gampec147b002014-03-06 18:11:06 -08002119
Andreas Gampec200a4a2014-06-16 18:39:09 -07002120// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2121// of transitioning into native code.
2122class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2123 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002124 BuildGenericJniFrameVisitor(Thread* self,
2125 bool is_static,
2126 bool critical_native,
2127 const char* shorty,
2128 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002129 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002130 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002131 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2132 sm_(&jni_call_) {
2133 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002134 uintptr_t* start_gpr_reg;
2135 uint32_t* start_fpr_reg;
2136 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002137 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002138 &handle_scope_,
2139 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002140 &start_gpr_reg, &start_fpr_reg);
2141
Andreas Gampec200a4a2014-06-16 18:39:09 -07002142 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2143
Igor Murashkin06a04e02016-09-13 15:57:37 -07002144 // First 2 parameters are always excluded for CriticalNative methods.
2145 if (LIKELY(!critical_native)) {
2146 // jni environment is always first argument
2147 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002148
Igor Murashkin06a04e02016-09-13 15:57:37 -07002149 if (is_static) {
2150 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2151 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002152 }
2153 }
2154
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002155 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002156
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002157 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002158
Vladimir Markof39745e2016-01-26 12:16:55 +00002159 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002160 return handle_scope_->GetHandle(0).GetReference();
2161 }
2162
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002163 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002164 return handle_scope_->GetHandle(0).ToJObject();
2165 }
2166
Ian Rogers1428dce2014-10-21 15:02:15 -07002167 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002168 return bottom_of_used_area_;
2169 }
2170
2171 private:
2172 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2173 class FillJniCall FINAL : public FillNativeCall {
2174 public:
2175 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002176 HandleScope* handle_scope, bool critical_native)
2177 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2178 handle_scope_(handle_scope),
2179 cur_entry_(0),
2180 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002181
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002182 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002183
2184 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2185 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2186 handle_scope_ = scope;
2187 cur_entry_ = 0U;
2188 }
2189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002190 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002191 // Initialize padding entries.
2192 size_t expected_slots = handle_scope_->NumberOfReferences();
2193 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002194 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002195 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002196
2197 if (!critical_native_) {
2198 // Non-critical natives have at least the self class (jclass) or this (jobject).
2199 DCHECK_NE(cur_entry_, 0U);
2200 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002201 }
2202
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002203 bool CriticalNative() const {
2204 return critical_native_;
2205 }
2206
Andreas Gampec200a4a2014-06-16 18:39:09 -07002207 private:
2208 HandleScope* handle_scope_;
2209 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002210 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002211 };
2212
2213 HandleScope* handle_scope_;
2214 FillJniCall jni_call_;
2215 void* bottom_of_used_area_;
2216
2217 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002218
2219 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2220};
2221
Andreas Gampec200a4a2014-06-16 18:39:09 -07002222uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2223 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002224 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002225 h.Assign(ref);
2226 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2227 cur_entry_++;
2228 return tmp;
2229}
2230
Ian Rogers9758f792014-03-13 09:02:55 -07002231void BuildGenericJniFrameVisitor::Visit() {
2232 Primitive::Type type = GetParamPrimitiveType();
2233 switch (type) {
2234 case Primitive::kPrimLong: {
2235 jlong long_arg;
2236 if (IsSplitLongOrDouble()) {
2237 long_arg = ReadSplitLongParam();
2238 } else {
2239 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2240 }
2241 sm_.AdvanceLong(long_arg);
2242 break;
2243 }
2244 case Primitive::kPrimDouble: {
2245 uint64_t double_arg;
2246 if (IsSplitLongOrDouble()) {
2247 // Read into union so that we don't case to a double.
2248 double_arg = ReadSplitLongParam();
2249 } else {
2250 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2251 }
2252 sm_.AdvanceDouble(double_arg);
2253 break;
2254 }
2255 case Primitive::kPrimNot: {
2256 StackReference<mirror::Object>* stack_ref =
2257 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002258 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002259 break;
2260 }
2261 case Primitive::kPrimFloat:
2262 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2263 break;
2264 case Primitive::kPrimBoolean: // Fall-through.
2265 case Primitive::kPrimByte: // Fall-through.
2266 case Primitive::kPrimChar: // Fall-through.
2267 case Primitive::kPrimShort: // Fall-through.
2268 case Primitive::kPrimInt: // Fall-through.
2269 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2270 break;
2271 case Primitive::kPrimVoid:
2272 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002273 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002274 }
2275}
2276
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002277void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002278 // Clear out rest of the scope.
2279 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002280 if (!jni_call_.CriticalNative()) {
2281 // Install HandleScope.
2282 self->PushHandleScope(handle_scope_);
2283 }
Ian Rogers9758f792014-03-13 09:02:55 -07002284}
2285
Ian Rogers04c31d22014-07-07 21:44:06 -07002286#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002287extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002288#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002289extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002290#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002291
Igor Murashkin06a04e02016-09-13 15:57:37 -07002292static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2293 uint32_t cookie,
2294 bool fast_native ATTRIBUTE_UNUSED,
2295 jobject l,
2296 jobject lock) {
2297 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002298 if (lock != nullptr) {
2299 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2300 } else {
2301 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2302 }
2303}
2304
Igor Murashkin06a04e02016-09-13 15:57:37 -07002305static void artQuickGenericJniEndJNINonRef(Thread* self,
2306 uint32_t cookie,
2307 bool fast_native,
2308 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002309 if (lock != nullptr) {
2310 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002311 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002312 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002313 if (UNLIKELY(fast_native)) {
2314 JniMethodFastEnd(cookie, self);
2315 } else {
2316 JniMethodEnd(cookie, self);
2317 }
Andreas Gampead615172014-04-04 16:20:13 -07002318 }
2319}
2320
Andreas Gampec147b002014-03-06 18:11:06 -08002321/*
2322 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002323 * 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 -08002324 * The final element on the stack is a pointer to the native code.
2325 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002326 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002327 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002328 *
Andreas Gampec147b002014-03-06 18:11:06 -08002329 * The return of this function denotes:
2330 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2331 * 2) An error, if the value is negative.
2332 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002334 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002335 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002337 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002338 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002339 uint32_t shorty_len = 0;
2340 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002341 bool critical_native = called->IsCriticalNative();
2342 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002343 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002344
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002345 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002346 BuildGenericJniFrameVisitor visitor(self,
2347 called->IsStatic(),
2348 critical_native,
2349 shorty,
2350 shorty_len,
2351 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002352 {
2353 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2354 visitor.VisitArguments();
2355 // FinalizeHandleScope pushes the handle scope on the thread.
2356 visitor.FinalizeHandleScope(self);
2357 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002358
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002359 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002360 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002361
Ian Rogerse0dcd462014-03-08 15:21:04 -08002362 self->VerifyStack();
2363
Vladimir Markof8655b32018-03-21 17:53:56 +00002364 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2365 jit::Jit* jit = runtime->GetJit();
2366 if (jit != nullptr) {
2367 jit->MethodEntered(self, called);
2368 }
2369
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002370 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002371 uint32_t* sp32;
2372 // Skip calling JniMethodStart for @CriticalNative.
2373 if (LIKELY(!critical_native)) {
2374 // Start JNI, save the cookie.
2375 if (called->IsSynchronized()) {
2376 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2377 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2378 if (self->IsExceptionPending()) {
2379 self->PopHandleScope();
2380 // A negative value denotes an error.
2381 return GetTwoWordFailureValue();
2382 }
2383 } else {
2384 if (fast_native) {
2385 cookie = JniMethodFastStart(self);
2386 } else {
2387 DCHECK(normal_native);
2388 cookie = JniMethodStart(self);
2389 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002390 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002391 sp32 = reinterpret_cast<uint32_t*>(sp);
2392 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002393 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002394
Andreas Gampe90546832014-03-12 18:07:19 -07002395 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002396 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002397
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002398 // There are two cases for the content of nativeCode:
2399 // 1) Pointer to the native function.
2400 // 2) Pointer to the trampoline for native code binding.
2401 // In the second case, we need to execute the binding and continue with the actual native function
2402 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002403 DCHECK(nativeCode != nullptr);
2404 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002405#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002406 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002407#else
2408 nativeCode = artFindNativeMethod(self);
2409#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002410
2411 if (nativeCode == nullptr) {
2412 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002413
Igor Murashkin06a04e02016-09-13 15:57:37 -07002414 // @CriticalNative calls do not need to call back into JniMethodEnd.
2415 if (LIKELY(!critical_native)) {
2416 // End JNI, as the assembly will move to deliver the exception.
2417 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2418 if (shorty[0] == 'L') {
2419 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2420 } else {
2421 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2422 }
Andreas Gampead615172014-04-04 16:20:13 -07002423 }
2424
Andreas Gampec200a4a2014-06-16 18:39:09 -07002425 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002426 }
2427 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002428 }
2429
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002430#if defined(__mips__) && !defined(__LP64__)
2431 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2432 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2433 // and load into floating-point registers.
2434 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2435 // view):
2436 // (1)
2437 // | DOUBLE | DOUBLE | other args, if any
2438 // | F12 | F13 | F14 | F15 |
2439 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2440 // (2)
2441 // | DOUBLE | FLOAT | (PAD) | other args, if any
2442 // | F12 | F13 | F14 | |
2443 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2444 // (3)
2445 // | FLOAT | (PAD) | DOUBLE | other args, if any
2446 // | F12 | | F14 | F15 |
2447 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2448 // (4)
2449 // | FLOAT | FLOAT | other args, if any
2450 // | F12 | F14 |
2451 // | SP+0 | SP+4 | SP+8
2452 // As you can see, only the last case (4) is special. In all others we can just
2453 // load F12/F13 and F14/F15 in the same manner.
2454 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2455 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2456 if (nativeCode != nullptr &&
2457 shorty != nullptr &&
2458 shorty_len >= 3 &&
2459 shorty[1] == 'F' &&
2460 shorty[2] == 'F') {
2461 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2462 }
2463#endif
2464
Andreas Gampec200a4a2014-06-16 18:39:09 -07002465 // Return native code addr(lo) and bottom of alloca address(hi).
2466 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2467 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002468}
2469
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002470// Defined in quick_jni_entrypoints.cc.
2471extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2472 jvalue result, uint64_t result_f, ArtMethod* called,
2473 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002474/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002475 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002476 * unlocking.
2477 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002478extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2479 jvalue result,
2480 uint64_t result_f) {
2481 // We're here just back from a native call. We don't have the shared mutator lock at this point
2482 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2483 // anything that requires a mutator lock before that would cause problems as GC may have the
2484 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002485 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002486 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002487 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002488 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002489 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002490 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2491 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002492}
2493
Andreas Gamped58342c2014-06-05 14:18:08 -07002494// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2495// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002496//
Andreas Gamped58342c2014-06-05 14:18:08 -07002497// 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 -07002498// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002499
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002500template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002501static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2502 ObjPtr<mirror::Object> this_object,
2503 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002504 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002505 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002506 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002507 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002508 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002509 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002510 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2511 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002512 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002513 {
2514 // Remember the args in case a GC happens in FindMethodFromCode.
2515 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2516 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2517 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002518 method = FindMethodFromCode<type, access_check>(method_idx,
2519 &this_object,
2520 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002521 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002522 visitor.FixupReferences();
2523 }
2524
Ian Rogerse0a02da2014-12-02 14:10:53 -08002525 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002526 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002527 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002528 }
2529 }
2530 DCHECK(!self->IsExceptionPending());
2531 const void* code = method->GetEntryPointFromQuickCompiledCode();
2532
2533 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002534 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002535 << " location: "
2536 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002537
Andreas Gamped58342c2014-06-05 14:18:08 -07002538 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2539 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002540}
2541
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002542// Explicit artInvokeCommon template function declarations to please analysis tool.
2543#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002544 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002545 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002546 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002547
2548EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2549EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2550EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2551EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2552EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2553EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2554EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2555EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2556EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2557EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2558#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2559
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002560// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002561extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002562 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002563 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002564 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002565}
2566
Andreas Gampec200a4a2014-06-16 18:39:09 -07002567extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002568 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002569 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002570 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002571}
2572
Andreas Gampec200a4a2014-06-16 18:39:09 -07002573extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002574 uint32_t method_idx,
2575 mirror::Object* this_object ATTRIBUTE_UNUSED,
2576 Thread* self,
2577 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2578 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2579 // it doesn't cause ObjPtr alignment failure check.
2580 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002581}
2582
Andreas Gampec200a4a2014-06-16 18:39:09 -07002583extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002584 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002585 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002586 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002587}
2588
Andreas Gampec200a4a2014-06-16 18:39:09 -07002589extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002590 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002591 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002592 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002593}
2594
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002595// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2596extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2597 REQUIRES_SHARED(Locks::mutator_lock_) {
2598 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2599 DCHECK(!referrer->IsProxyMethod());
2600 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2601 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2602 DCHECK(result == nullptr ||
2603 result->GetDeclaringClass()->IsInterface() ||
2604 result->GetDeclaringClass() ==
2605 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2606 << result->PrettyMethod();
2607 return result;
2608}
2609
Jeff Hao5667f562017-02-27 19:32:01 -08002610// Determine target of interface dispatch. The interface method and this object are known non-null.
2611// The interface method is the method returned by the dex cache in the conflict trampoline.
2612extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002613 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002614 Thread* self,
2615 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002616 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002617 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002618 StackHandleScope<2> hs(self);
2619 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2620 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002621
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002622 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002623 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002624 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002625
Vladimir Marko302f69c2017-07-25 15:27:15 +01002626 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002627 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002628 // Fetch the dex_method_idx of the target interface method from the caller.
2629 uint32_t dex_method_idx;
2630 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002631 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002632 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002633 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2634 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002635 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002636 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002637 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002638 } else {
2639 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002640 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002641 }
2642
Vladimir Marko302f69c2017-07-25 15:27:15 +01002643 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002644 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002645 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2646 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002647 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002648 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002649 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2650 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2651 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002652 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2653 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2654 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002655 visitor.FixupReferences();
2656 }
2657
Vladimir Marko302f69c2017-07-25 15:27:15 +01002658 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002659 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002660 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002661 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002662 }
2663
2664 DCHECK(!interface_method->IsRuntimeMethod());
2665 // Look whether we have a match in the ImtConflictTable.
2666 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2667 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2668 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2669 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2670 DCHECK(current_table != nullptr);
2671 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2672 } else {
2673 // It seems we aren't really a conflict method!
2674 if (kIsDebugBuild) {
2675 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2676 CHECK_EQ(conflict_method, m)
2677 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2678 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2679 }
2680 method = conflict_method;
2681 }
2682 if (method != nullptr) {
2683 return GetTwoWordSuccessValue(
2684 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2685 reinterpret_cast<uintptr_t>(method));
2686 }
2687
2688 // No match, use the IfTable.
2689 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2690 if (UNLIKELY(method == nullptr)) {
2691 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2692 interface_method, this_object.Get(), caller_method);
2693 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002694 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002695
2696 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2697 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002698 DCHECK(conflict_method->IsRuntimeMethod());
2699 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2700 cls.Get(),
2701 conflict_method,
2702 interface_method,
2703 method,
2704 /*force_new_conflict_method*/false);
2705 if (new_conflict_method != conflict_method) {
2706 // Update the IMT if we create a new conflict method. No fence needed here, as the
2707 // data is consistent.
2708 imt->Set(imt_index,
2709 new_conflict_method,
2710 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002711 }
2712
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002713 const void* code = method->GetEntryPointFromQuickCompiledCode();
2714
2715 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002716 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002717 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002718
Andreas Gamped58342c2014-06-05 14:18:08 -07002719 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2720 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002721}
2722
Orion Hodsonac141392017-01-13 11:53:47 +00002723// Returns shorty type so the caller can determine how to put |result|
2724// into expected registers. The shorty type is static so the compiler
2725// could call different flavors of this code path depending on the
2726// shorty type though this would require different entry points for
2727// each type.
2728extern "C" uintptr_t artInvokePolymorphic(
2729 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002730 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002731 Thread* self,
2732 ArtMethod** sp)
2733 REQUIRES_SHARED(Locks::mutator_lock_) {
2734 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002735 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002736
2737 // Start new JNI local reference state
2738 JNIEnvExt* env = self->GetJniEnv();
2739 ScopedObjectAccessUnchecked soa(env);
2740 ScopedJniEnvLocalRefState env_state(env);
2741 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2742
2743 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2744 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2745 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002746 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002747 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2748 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002749 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002750 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002751 const size_t shorty_length = strlen(shorty);
2752 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2753 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002754 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002755
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002756 // Wrap raw_receiver in a Handle for safety.
2757 StackHandleScope<3> hs(self);
2758 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2759 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002760 self->EndAssertNoThreadSuspension(old_cause);
2761
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002762 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002763 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002764 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002765 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002766
2767 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002768 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2769 return static_cast<uintptr_t>('V');
2770 }
2771
Orion Hodsone7732be2017-10-11 14:35:20 +01002772 Handle<mirror::MethodType> method_type(
2773 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2774
Orion Hodsonac141392017-01-13 11:53:47 +00002775 // This implies we couldn't resolve one or more types in this method handle.
2776 if (UNLIKELY(method_type.IsNull())) {
2777 CHECK(self->IsExceptionPending());
2778 return static_cast<uintptr_t>('V');
2779 }
2780
Vladimir Markod7559b72017-09-28 13:50:37 +01002781 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002782 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2783
2784 // Fix references before constructing the shadow frame.
2785 gc_visitor.FixupReferences();
2786
2787 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002788 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2789 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002790 const size_t first_arg = 0;
2791 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2792 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2793 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2794 ScopedStackedShadowFramePusher
2795 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2796 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2797 kMethodIsStatic,
2798 shorty,
2799 strlen(shorty),
2800 shadow_frame,
2801 first_arg);
2802 shadow_frame_builder.VisitArguments();
2803
2804 // Push a transition back into managed code onto the linked list in thread.
2805 ManagedStack fragment;
2806 self->PushManagedStackFragment(&fragment);
2807
2808 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2809 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002810 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002811 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002812 bool success = false;
Orion Hodson537a4fe2018-05-15 13:57:58 +01002813 if (resolved_method->GetDeclaringClass() == mirror::MethodHandle::StaticClass()) {
2814 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2815 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2816 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2817 success = MethodHandleInvokeExact(self,
2818 *shadow_frame,
2819 method_handle,
2820 method_type,
2821 &operands,
2822 result);
2823 } else {
2824 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2825 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2826 success = MethodHandleInvoke(self,
2827 *shadow_frame,
2828 method_handle,
2829 method_type,
2830 &operands,
2831 result);
2832 }
2833 } else {
2834 DCHECK_EQ(mirror::VarHandle::StaticClass(), resolved_method->GetDeclaringClass());
2835 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
2836 ObjPtr<mirror::VarHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2837 mirror::VarHandle::AccessMode access_mode =
2838 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2839 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002840 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002841 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002842 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002843 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002844 &operands,
2845 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002846 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002847
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002848 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002849
2850 // Pop transition record.
2851 self->PopManagedStackFragment(fragment);
2852
2853 return static_cast<uintptr_t>(shorty[0]);
2854}
2855
Ian Rogers848871b2013-08-05 10:56:33 -07002856} // namespace art