blob: af6a936d408170bdc957bda12db949bd3a19303f [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 Markoc7aa87e2018-05-24 15:19:52 +010022#include "class_root.h"
Vladimir Marko606adb32018-04-05 14:49:24 +010023#include "debug_print.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
26#include "dex/dex_file_types.h"
27#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070028#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010030#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070031#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070032#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070033#include "imt_conflict_table.h"
34#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000035#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070036#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "interpreter/interpreter.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010038#include "interpreter/shadow_frame-inl.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000039#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000040#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000041#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070042#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070043#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070044#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000045#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010048#include "mirror/var_handle.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010049#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010050#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070051#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070052#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070053#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070054#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070055#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010056#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000057#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070058
59namespace art {
60
Andreas Gampe8228cdf2017-05-30 15:03:54 -070061// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070062class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080063 // Number of bytes for each out register in the caller method's frame.
64 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070065 // Frame size in bytes of a callee-save frame for RefsAndArgs.
66 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010067 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
68 // Offset of first GPR arg.
69 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
70 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
71 // Offset of first FPR arg.
72 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
73 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
74 // Offset of return address.
75 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
76 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070077#if defined(__arm__)
78 // The callee save frame is pointed to by SP.
79 // | argN | |
80 // | ... | |
81 // | arg4 | |
82 // | arg3 spill | | Caller's frame
83 // | arg2 spill | |
84 // | arg1 spill | |
85 // | Method* | ---
86 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080087 // | ... | 4x6 bytes callee saves
88 // | R3 |
89 // | R2 |
90 // | R1 |
91 // | S15 |
92 // | : |
93 // | S0 |
94 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070095 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070096 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
97 static constexpr bool kAlignPairRegister = true;
98 static constexpr bool kQuickSoftFloatAbi = false;
99 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000100 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800101 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -0700102 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800103 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800104 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000105 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800106 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107#elif defined(__aarch64__)
108 // The callee save frame is pointed to by SP.
109 // | argN | |
110 // | ... | |
111 // | arg4 | |
112 // | arg3 spill | | Caller's frame
113 // | arg2 spill | |
114 // | arg1 spill | |
115 // | Method* | ---
116 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800117 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100119 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 // | X7 |
121 // | : |
122 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800123 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000124 // | : |
125 // | D0 |
126 // | | padding
127 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500128 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000129 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000130 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800131 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000132 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000133 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
134 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800135 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000136 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000137 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000138 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800139#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700140 // The callee save frame is pointed to by SP.
141 // | argN | |
142 // | ... | |
143 // | arg4 | |
144 // | arg3 spill | | Caller's frame
145 // | arg2 spill | |
146 // | arg1 spill | |
147 // | Method* | ---
148 // | RA |
149 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800150 // | T1 | arg5
151 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700152 // | A3 | arg3
153 // | A2 | arg2
154 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800155 // | F19 |
156 // | F18 | f_arg5
157 // | F17 |
158 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000159 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800160 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000161 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800162 // | F12 | f_arg2
163 // | F11 |
164 // | F10 | f_arg1
165 // | F9 |
166 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000167 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700168 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000169 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
170 static constexpr bool kAlignPairRegister = true;
171 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800172 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000173 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800174 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
175 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
176 // passed only in even numbered registers and each
177 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800178 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800179 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000180 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800181 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800182#elif defined(__mips__) && defined(__LP64__)
183 // The callee save frame is pointed to by SP.
184 // | argN | |
185 // | ... | |
186 // | arg4 | |
187 // | arg3 spill | | Caller's frame
188 // | arg2 spill | |
189 // | arg1 spill | |
190 // | Method* | ---
191 // | RA |
192 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800193 // | A7 | arg7
194 // | A6 | arg6
195 // | A5 | arg5
196 // | A4 | arg4
197 // | A3 | arg3
198 // | A2 | arg2
199 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000200 // | F19 | f_arg7
201 // | F18 | f_arg6
202 // | F17 | f_arg5
203 // | F16 | f_arg4
204 // | F15 | f_arg3
205 // | F14 | f_arg2
206 // | F13 | f_arg1
207 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800208 // | | padding
209 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800210 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800211 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800212 static constexpr bool kAlignPairRegister = false;
213 static constexpr bool kQuickSoftFloatAbi = false;
214 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000215 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
217 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
218 static constexpr bool kGprFprLockstep = true;
219
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800220 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
221 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
222 }
Ian Rogers848871b2013-08-05 10:56:33 -0700223#elif defined(__i386__)
224 // The callee save frame is pointed to by SP.
225 // | argN | |
226 // | ... | |
227 // | arg4 | |
228 // | arg3 spill | | Caller's frame
229 // | arg2 spill | |
230 // | arg1 spill | |
231 // | Method* | ---
232 // | Return |
233 // | EBP,ESI,EDI | callee saves
234 // | EBX | arg3
235 // | EDX | arg2
236 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000237 // | XMM3 | float arg 4
238 // | XMM2 | float arg 3
239 // | XMM1 | float arg 2
240 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700241 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500242 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000243 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000244 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800245 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000246 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800247 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000248 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800249 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800250 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000251 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800252 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 // The callee save frame is pointed to by SP.
255 // | argN | |
256 // | ... | |
257 // | reg. arg spills | | Caller's frame
258 // | Method* | ---
259 // | Return |
260 // | R15 | callee save
261 // | R14 | callee save
262 // | R13 | callee save
263 // | R12 | callee save
264 // | R9 | arg5
265 // | R8 | arg4
266 // | RSI/R6 | arg1
267 // | RBP/R5 | callee save
268 // | RBX/R3 | callee save
269 // | RDX/R2 | arg2
270 // | RCX/R1 | arg3
271 // | XMM7 | float arg 8
272 // | XMM6 | float arg 7
273 // | XMM5 | float arg 6
274 // | XMM4 | float arg 5
275 // | XMM3 | float arg 4
276 // | XMM2 | float arg 3
277 // | XMM1 | float arg 2
278 // | XMM0 | float arg 1
279 // | Padding |
280 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500281 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000282 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800283 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800284 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000285 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700286 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700287 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800288 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
290 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000291 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
292 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
293 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700297 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
298 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 }
300 }
Ian Rogers848871b2013-08-05 10:56:33 -0700301#else
302#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700303#endif
304
Ian Rogers936b37f2014-02-14 00:52:24 -0800305 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100306 // Special handling for proxy methods. Proxy methods are instance methods so the
307 // 'this' object is the 1st argument. They also have the same frame layout as the
308 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
309 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000310 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700311 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000312 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100313 CHECK_GT(kNumQuickGprArgs, 0u);
314 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
315 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
316 GprIndexToGprOffset(kThisGprIndex);
317 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000318 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100319 }
320
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700321 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700323 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100324 }
325
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100328 uint8_t* previous_sp =
329 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 }
332
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700333 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100335 constexpr size_t callee_frame_size =
336 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
338 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100340 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
341 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100343 if (current_code->IsOptimized()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100344 CodeInfo code_info(current_code);
345 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100346 DCHECK(stack_map.IsValid());
David Srbecky6e69e522018-06-03 12:00:14 +0100347 uint32_t depth = code_info.GetInlineDepthOf(stack_map);
348 if (depth != 0) {
349 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(stack_map, depth - 1);
350 return inline_info.GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100351 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100352 return stack_map.GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100353 }
354 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100355 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100356 }
Ian Rogers848871b2013-08-05 10:56:33 -0700357 }
358
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800359 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
360 REQUIRES_SHARED(Locks::mutator_lock_) {
361 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100362 constexpr size_t callee_frame_size =
363 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800364 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
365 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
366 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
367 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
368 if (!current_code->IsOptimized()) {
369 return false;
370 }
371 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +0100372 CodeInfo code_info(current_code);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700373 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
David Srbecky052f8ca2018-04-26 15:42:54 +0100374 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset));
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800375 if (invoke.IsValid()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100376 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType());
377 *dex_method_index = invoke.GetMethodIndex(method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800378 return true;
379 }
380 return false;
381 }
382
Ian Rogers936b37f2014-02-14 00:52:24 -0800383 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700384 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100386 uint8_t* return_adress_spill =
387 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset;
388 return *reinterpret_cast<uintptr_t*>(return_adress_spill);
Ian Rogers848871b2013-08-05 10:56:33 -0700389 }
390
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700392 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700393 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700394 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
395 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
396 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800398 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
399 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800400 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
401 "Number of Quick FPR arguments unexpected");
402 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
403 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800404 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
405 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800406 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
407 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700408 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800409 }
Ian Rogers848871b2013-08-05 10:56:33 -0700410
411 virtual ~QuickArgumentVisitor() {}
412
413 virtual void Visit() = 0;
414
Ian Rogers936b37f2014-02-14 00:52:24 -0800415 Primitive::Type GetParamPrimitiveType() const {
416 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700417 }
418
Ian Rogers13735952014-10-08 12:43:28 -0700419 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800420 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800421 Primitive::Type type = GetParamPrimitiveType();
422 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800423 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
424 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
425 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
426 }
427 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000428 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700430 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 }
432 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800433 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800434 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
435 }
436 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700437 }
438
439 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
441 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800442 return is_split_long_or_double_;
443 } else {
444 return false; // An optimization for when GPR and FPRs are 64bit.
445 }
Ian Rogers848871b2013-08-05 10:56:33 -0700446 }
447
Ian Rogers936b37f2014-02-14 00:52:24 -0800448 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700449 return GetParamPrimitiveType() == Primitive::kPrimNot;
450 }
451
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700453 Primitive::Type type = GetParamPrimitiveType();
454 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
455 }
456
457 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000458 // The splitted long is always available through the stack.
459 return *reinterpret_cast<uint64_t*>(stack_args_
460 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700461 }
462
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800463 void IncGprIndex() {
464 gpr_index_++;
465 if (kGprFprLockstep) {
466 fpr_index_++;
467 }
468 }
469
470 void IncFprIndex() {
471 fpr_index_++;
472 if (kGprFprLockstep) {
473 gpr_index_++;
474 }
475 }
476
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700477 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800478 // (a) 'stack_args_' should point to the first method's argument
479 // (b) whatever the argument type it is, the 'stack_index_' should
480 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 gpr_index_ = 0;
482 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800483 if (kQuickDoubleRegAlignedFloatBackFilled) {
484 fpr_double_index_ = 0;
485 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800486 stack_index_ = 0;
487 if (!is_static_) { // Handle this.
488 cur_type_ = Primitive::kPrimNot;
489 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700490 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800491 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800492 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800493 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800494 }
Ian Rogers848871b2013-08-05 10:56:33 -0700495 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
497 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
498 switch (cur_type_) {
499 case Primitive::kPrimNot:
500 case Primitive::kPrimBoolean:
501 case Primitive::kPrimByte:
502 case Primitive::kPrimChar:
503 case Primitive::kPrimShort:
504 case Primitive::kPrimInt:
505 is_split_long_or_double_ = false;
506 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800507 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800508 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800509 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800510 }
511 break;
512 case Primitive::kPrimFloat:
513 is_split_long_or_double_ = false;
514 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800515 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800516 if (kQuickSoftFloatAbi) {
517 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800518 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800519 }
520 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800521 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800522 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800523 if (kQuickDoubleRegAlignedFloatBackFilled) {
524 // Double should not overlap with float.
525 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
526 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
527 // Float should not overlap with double.
528 if (fpr_index_ % 2 == 0) {
529 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
530 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000531 } else if (kQuickSkipOddFpRegisters) {
532 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800533 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800534 }
535 }
536 break;
537 case Primitive::kPrimDouble:
538 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800539 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800540 if (cur_type_ == Primitive::kPrimLong &&
541#if defined(__mips__) && !defined(__LP64__)
542 (gpr_index_ == 0 || gpr_index_ == 2) &&
543#else
544 gpr_index_ == 0 &&
545#endif
546 kAlignPairRegister) {
547 // Currently, this is only for ARM and MIPS, where we align long parameters with
548 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
549 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800550 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000551 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000552 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800553 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500554 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
555 // We don't want to split this. Pass over this register.
556 gpr_index_++;
557 is_split_long_or_double_ = false;
558 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800559 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800560 if (kBytesStackArgLocation == 4) {
561 stack_index_+= 2;
562 } else {
563 CHECK_EQ(kBytesStackArgLocation, 8U);
564 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800565 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700566 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800567 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000568 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700569 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800570 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700571 }
572 }
573 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800574 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000575 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800576 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800577 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700578 if (kBytesStackArgLocation == 4) {
579 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800580 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700581 CHECK_EQ(kBytesStackArgLocation, 8U);
582 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800583 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800584 if (kQuickDoubleRegAlignedFloatBackFilled) {
585 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
586 fpr_double_index_ += 2;
587 // Float should not overlap with double.
588 if (fpr_index_ % 2 == 0) {
589 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
590 }
591 }
592 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800593 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800594 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
595 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800596 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800597 }
598 }
599 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800600 }
601 break;
602 default:
603 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
604 }
Ian Rogers848871b2013-08-05 10:56:33 -0700605 }
606 }
607
Andreas Gampec200a4a2014-06-16 18:39:09 -0700608 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700609 const bool is_static_;
610 const char* const shorty_;
611 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700612
613 private:
Ian Rogers13735952014-10-08 12:43:28 -0700614 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
615 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
616 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800617 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800618 // Index into spilled FPRs.
619 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
620 // holds a higher register number.
621 uint32_t fpr_index_;
622 // Index into spilled FPRs for aligned double.
623 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
624 // terms of singles, may be behind fpr_index.
625 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800626 uint32_t stack_index_; // Index into arguments on the stack.
627 // The current type of argument during VisitArguments.
628 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700629 // Does a 64bit parameter straddle the register and stack arguments?
630 bool is_split_long_or_double_;
631};
632
Sebastien Hertza836bc92014-11-25 16:30:53 +0100633// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
634// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700636 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000637 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
638}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100639
Ian Rogers848871b2013-08-05 10:56:33 -0700640// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800641class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700642 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
644 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700645 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700646
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700647 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700648
649 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800650 ShadowFrame* const sf_;
651 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700652
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700653 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700654};
655
Andreas Gampec200a4a2014-06-16 18:39:09 -0700656void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700657 Primitive::Type type = GetParamPrimitiveType();
658 switch (type) {
659 case Primitive::kPrimLong: // Fall-through.
660 case Primitive::kPrimDouble:
661 if (IsSplitLongOrDouble()) {
662 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
663 } else {
664 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
665 }
666 ++cur_reg_;
667 break;
668 case Primitive::kPrimNot: {
669 StackReference<mirror::Object>* stack_ref =
670 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
671 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
672 }
673 break;
674 case Primitive::kPrimBoolean: // Fall-through.
675 case Primitive::kPrimByte: // Fall-through.
676 case Primitive::kPrimChar: // Fall-through.
677 case Primitive::kPrimShort: // Fall-through.
678 case Primitive::kPrimInt: // Fall-through.
679 case Primitive::kPrimFloat:
680 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
681 break;
682 case Primitive::kPrimVoid:
683 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700684 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700685 }
686 ++cur_reg_;
687}
688
Mingyao Yang417528d2017-09-13 12:10:40 -0700689// Don't inline. See b/65159206.
690NO_INLINE
691static void HandleDeoptimization(JValue* result,
692 ArtMethod* method,
693 ShadowFrame* deopt_frame,
694 ManagedStack* fragment)
695 REQUIRES_SHARED(Locks::mutator_lock_) {
696 // Coming from partial-fragment deopt.
697 Thread* self = Thread::Current();
698 if (kIsDebugBuild) {
699 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
700 // of the call-stack) corresponds to the called method.
701 ShadowFrame* linked = deopt_frame;
702 while (linked->GetLink() != nullptr) {
703 linked = linked->GetLink();
704 }
705 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
706 << ArtMethod::PrettyMethod(linked->GetMethod());
707 }
708
709 if (VLOG_IS_ON(deopt)) {
710 // Print out the stack to verify that it was a partial-fragment deopt.
711 LOG(INFO) << "Continue-ing from deopt. Stack is:";
712 QuickExceptionHandler::DumpFramesWithType(self, true);
713 }
714
715 ObjPtr<mirror::Throwable> pending_exception;
716 bool from_code = false;
717 DeoptimizationMethodType method_type;
718 self->PopDeoptimizationContext(/* out */ result,
719 /* out */ &pending_exception,
720 /* out */ &from_code,
721 /* out */ &method_type);
722
723 // Push a transition back into managed code onto the linked list in thread.
724 self->PushManagedStackFragment(fragment);
725
726 // Ensure that the stack is still in order.
727 if (kIsDebugBuild) {
728 class DummyStackVisitor : public StackVisitor {
729 public:
730 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
731 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
732
733 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
734 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
735 // logic. Just always say we want to continue.
736 return true;
737 }
738 };
739 DummyStackVisitor dsv(self);
740 dsv.WalkStack();
741 }
742
743 // Restore the exception that was pending before deoptimization then interpret the
744 // deoptimized frames.
745 if (pending_exception != nullptr) {
746 self->SetException(pending_exception);
747 }
748 interpreter::EnterInterpreterFromDeoptimize(self,
749 deopt_frame,
750 result,
751 from_code,
752 DeoptimizationMethodType::kDefault);
753}
754
Mathieu Chartiere401d142015-04-22 13:56:20 -0700755extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700756 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700757 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
758 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700759 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700760
Alex Light9139e002015-10-09 15:59:48 -0700761 if (UNLIKELY(!method->IsInvokable())) {
762 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700763 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700764 }
765
766 JValue tmp_value;
767 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700768 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700769 ManagedStack fragment;
770
David Sehr709b0702016-10-13 09:12:37 -0700771 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700772 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700773 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800774 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000775 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700776 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
777
778 JValue result;
779
Mingyao Yang417528d2017-09-13 12:10:40 -0700780 if (UNLIKELY(deopt_frame != nullptr)) {
781 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700782 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700783 const char* old_cause = self->StartAssertNoThreadSuspension(
784 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800785 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700786 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700787 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700788 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700789 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800790 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700791 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800792 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700793 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800794 const bool needs_initialization =
795 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700796 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700797 self->PushManagedStackFragment(&fragment);
798 self->PushShadowFrame(shadow_frame);
799 self->EndAssertNoThreadSuspension(old_cause);
800
Ian Rogerse94652f2014-12-02 11:13:19 -0800801 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700802 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800803 StackHandleScope<1> hs(self);
804 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700805 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700806 DCHECK(Thread::Current()->IsExceptionPending())
807 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700808 self->PopManagedStackFragment(fragment);
809 return 0;
810 }
811 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200812
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800813 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700814 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700815
816 // Pop transition.
817 self->PopManagedStackFragment(fragment);
818
819 // Request a stack deoptimization if needed
820 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700821 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700822 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
823 // should be done and it knows the real return pc.
824 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000825 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
826 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
827 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
828 << caller->PrettyMethod();
829 } else {
830 // Push the context of the deoptimization stack so we can restore the return value and the
831 // exception before executing the deoptimized frames.
832 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700833 result,
834 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
835 self->GetException(),
836 false /* from_code */,
837 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700838
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000839 // Set special exception to cause deoptimization.
840 self->SetException(Thread::GetDeoptimizationException());
841 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700842 }
843
844 // No need to restore the args since the method has already been run by the interpreter.
845 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700846}
847
848// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
849// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800850class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700851 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700852 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700853 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700854 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700855
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700856 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700857
858 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700859 ScopedObjectAccessUnchecked* const soa_;
860 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700861
Ian Rogers848871b2013-08-05 10:56:33 -0700862 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
863};
864
Ian Rogers9758f792014-03-13 09:02:55 -0700865void BuildQuickArgumentVisitor::Visit() {
866 jvalue val;
867 Primitive::Type type = GetParamPrimitiveType();
868 switch (type) {
869 case Primitive::kPrimNot: {
870 StackReference<mirror::Object>* stack_ref =
871 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
872 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700873 break;
874 }
875 case Primitive::kPrimLong: // Fall-through.
876 case Primitive::kPrimDouble:
877 if (IsSplitLongOrDouble()) {
878 val.j = ReadSplitLongParam();
879 } else {
880 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
881 }
882 break;
883 case Primitive::kPrimBoolean: // Fall-through.
884 case Primitive::kPrimByte: // Fall-through.
885 case Primitive::kPrimChar: // Fall-through.
886 case Primitive::kPrimShort: // Fall-through.
887 case Primitive::kPrimInt: // Fall-through.
888 case Primitive::kPrimFloat:
889 val.i = *reinterpret_cast<jint*>(GetParamAddress());
890 break;
891 case Primitive::kPrimVoid:
892 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700893 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700894 }
895 args_->push_back(val);
896}
897
Ian Rogers848871b2013-08-05 10:56:33 -0700898// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
899// which is responsible for recording callee save registers. We explicitly place into jobjects the
900// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
901// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902extern "C" uint64_t artQuickProxyInvokeHandler(
903 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700904 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700905 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
906 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700907 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
908 const char* old_cause =
909 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
910 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700911 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700912 self->VerifyStack();
913 // Start new JNI local reference state.
914 JNIEnvExt* env = self->GetJniEnv();
915 ScopedObjectAccessUnchecked soa(env);
916 ScopedJniEnvLocalRefState env_state(env);
917 // Create local ref. copies of proxy method and the receiver.
918 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
919
920 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700921 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700922 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
923 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700924 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700925 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000927 BuildQuickArgumentVisitor local_ref_visitor(
928 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700929
Ian Rogers848871b2013-08-05 10:56:33 -0700930 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700931 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700932 args.erase(args.begin());
933
934 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700935 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700936 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
937 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700938 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700939 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700940 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700941 ObjPtr<mirror::Method> interface_reflect_method =
942 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
943 if (interface_reflect_method == nullptr) {
944 soa.Self()->AssertPendingOOMException();
945 return 0;
946 }
947 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700948
949 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
950 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700951 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700952 return result.GetJ();
953}
954
Roland Levillainad0777d2018-02-12 20:00:18 +0000955// Visitor returning a reference argument at a given position in a Quick stack frame.
956// NOTE: Only used for testing purposes.
957class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
958 public:
959 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
960 const char* shorty,
961 uint32_t shorty_len,
962 size_t arg_pos)
963 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
964 cur_pos_(0u),
965 arg_pos_(arg_pos),
966 ref_arg_(nullptr) {
967 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
968 }
969
970 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
971 if (cur_pos_ == arg_pos_) {
972 Primitive::Type type = GetParamPrimitiveType();
973 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
974 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
975 }
976 ++cur_pos_;
977 }
978
979 StackReference<mirror::Object>* GetReferenceArgument() {
980 return ref_arg_;
981 }
982
983 private:
984 // The position of the currently visited argument.
985 size_t cur_pos_;
986 // The position of the searched argument.
987 const size_t arg_pos_;
988 // The reference argument, if found.
989 StackReference<mirror::Object>* ref_arg_;
990
991 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
992};
993
994// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
995// NOTE: Only used for testing purposes.
996extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
997 ArtMethod** sp)
998 REQUIRES_SHARED(Locks::mutator_lock_) {
999 ArtMethod* proxy_method = *sp;
1000 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1001 CHECK(!non_proxy_method->IsStatic())
1002 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1003 uint32_t shorty_len = 0;
1004 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1005 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1006 ref_arg_visitor.VisitArguments();
1007 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1008 return ref_arg;
1009}
1010
1011// Visitor returning all the reference arguments in a Quick stack frame.
1012class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1013 public:
1014 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1015 bool is_static,
1016 const char* shorty,
1017 uint32_t shorty_len)
1018 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1019
1020 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1021 Primitive::Type type = GetParamPrimitiveType();
1022 if (type == Primitive::kPrimNot) {
1023 StackReference<mirror::Object>* ref_arg =
1024 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1025 ref_args_.push_back(ref_arg);
1026 }
1027 }
1028
1029 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1030 return ref_args_;
1031 }
1032
1033 private:
1034 // The reference arguments.
1035 std::vector<StackReference<mirror::Object>*> ref_args_;
1036
1037 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1038};
1039
1040// Returning all reference arguments in Quick stack frame at address `sp`.
1041std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1042 REQUIRES_SHARED(Locks::mutator_lock_) {
1043 ArtMethod* proxy_method = *sp;
1044 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1045 CHECK(!non_proxy_method->IsStatic())
1046 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1047 uint32_t shorty_len = 0;
1048 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1049 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1050 ref_args_visitor.VisitArguments();
1051 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1052 return ref_args;
1053}
1054
Ian Rogers848871b2013-08-05 10:56:33 -07001055// Read object references held in arguments from quick frames and place in a JNI local references,
1056// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001057class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001058 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1060 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001061 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001062
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001063 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001064
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001065 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001066
1067 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001068 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001069 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001070 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1071
Mathieu Chartier590fee92013-09-13 13:46:47 -07001072 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001073};
1074
Ian Rogers9758f792014-03-13 09:02:55 -07001075void RememberForGcArgumentVisitor::Visit() {
1076 if (IsParamAReference()) {
1077 StackReference<mirror::Object>* stack_ref =
1078 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1079 jobject reference =
1080 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1081 references_.push_back(std::make_pair(reference, stack_ref));
1082 }
1083}
1084
1085void RememberForGcArgumentVisitor::FixupReferences() {
1086 // Fixup any references which may have changed.
1087 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001088 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001089 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001090 }
1091}
1092
Alex Lightb7edcda2017-04-27 13:20:31 -07001093extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1094 mirror::Object* this_object,
1095 Thread* self,
1096 ArtMethod** sp)
1097 REQUIRES_SHARED(Locks::mutator_lock_) {
1098 const void* result;
1099 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1100 // that part.
1101 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1102 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1103 if (instrumentation->IsDeoptimized(method)) {
1104 result = GetQuickToInterpreterBridge();
1105 } else {
1106 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1107 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1108 }
1109
1110 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1111 bool is_static = method->IsStatic();
1112 uint32_t shorty_len;
1113 const char* shorty =
1114 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1115
1116 ScopedObjectAccessUnchecked soa(self);
1117 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1118 visitor.VisitArguments();
1119
1120 instrumentation->PushInstrumentationStackFrame(self,
1121 is_static ? nullptr : this_object,
1122 method,
1123 QuickArgumentVisitor::GetCallingPc(sp),
1124 interpreter_entry);
1125
1126 visitor.FixupReferences();
1127 if (UNLIKELY(self->IsExceptionPending())) {
1128 return nullptr;
1129 }
1130 CHECK(result != nullptr) << method->PrettyMethod();
1131 return result;
1132}
1133
1134extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1135 ArtMethod** sp,
1136 uint64_t* gpr_result,
1137 uint64_t* fpr_result)
1138 REQUIRES_SHARED(Locks::mutator_lock_) {
1139 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1140 CHECK(gpr_result != nullptr);
1141 CHECK(fpr_result != nullptr);
1142 // Instrumentation exit stub must not be entered with a pending exception.
1143 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1144 << self->GetException()->Dump();
1145 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001146 constexpr size_t return_pc_offset =
1147 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001148 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1149 return_pc_offset);
1150 CHECK_EQ(*return_pc, 0U);
1151
1152 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1153 // deoptimization shouldn't be performed with the high-half having the return address. When
1154 // deoptimization should be performed the low half is zero and the high-half the address of the
1155 // deoptimization entry point.
1156 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1157 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1158 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001159 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001160 return GetTwoWordFailureValue();
1161 }
1162 return return_or_deoptimize_pc;
1163}
1164
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001165static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1166 REQUIRES_SHARED(Locks::mutator_lock_) {
1167 if (dex_pc == static_cast<uint32_t>(-1)) {
1168 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1169 return "<native>";
1170 } else {
1171 CodeItemInstructionAccessor accessor = method->DexInstructions();
1172 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1173 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1174 }
1175}
1176
Vladimir Marko606adb32018-04-05 14:49:24 +01001177static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1178 REQUIRES_SHARED(Locks::mutator_lock_) {
1179 std::string storage;
1180 const char* descriptor = klass->GetDescriptor(&storage);
1181 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1182 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1183 if (oat_dex_file != nullptr) {
1184 const OatFile* oat_file = oat_dex_file->GetOatFile();
1185 const char* dex2oat_cmdline =
1186 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1187 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1188 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1189 }
1190}
1191
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001192static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1193 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001194 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001195
1196 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1197 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1198
Vladimir Markod3083dd2018-05-17 08:43:47 +01001199 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001200 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1201 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001202 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001203 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1204 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1205 ArtMethod* outer_method = *caller_sp;
1206
1207 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1208 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1209 << " native pc: " << caller_pc << " Instrumented!";
1210 return;
1211 }
1212
1213 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1214 CHECK(current_code != nullptr);
1215 CHECK(current_code->IsOptimized());
1216 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +01001217 CodeInfo code_info(current_code);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001218 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
David Srbecky052f8ca2018-04-26 15:42:54 +01001219 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001220 CHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +01001221 uint32_t dex_pc = stack_map.GetDexPc();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001222
1223 // Log the outer method and its associated dex file and class table pointer which can be used
1224 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1225 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1226 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1227 << " native pc: " << caller_pc
1228 << " dex pc: " << dex_pc
1229 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1230 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001231 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001232 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1233
1234 ArtMethod* caller = outer_method;
David Srbecky6e69e522018-06-03 12:00:14 +01001235 uint32_t depth = code_info.GetInlineDepthOf(stack_map);
1236 if (depth != 0) {
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001237 for (size_t d = 0; d < depth; ++d) {
David Srbecky6e69e522018-06-03 12:00:14 +01001238 InlineInfo inline_info = code_info.GetInlineInfoAtDepth(stack_map, d);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001239 const char* tag = "";
David Srbecky6e69e522018-06-03 12:00:14 +01001240 dex_pc = inline_info.GetDexPc();
1241 if (inline_info.EncodesArtMethod()) {
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001242 tag = "encoded ";
David Srbecky6e69e522018-06-03 12:00:14 +01001243 caller = inline_info.GetArtMethod();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001244 } else {
David Srbecky6e69e522018-06-03 12:00:14 +01001245 uint32_t method_index = inline_info.GetMethodIndex(method_info);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001246 if (dex_pc == static_cast<uint32_t>(-1)) {
1247 tag = "special ";
1248 CHECK_EQ(d + 1u, depth);
1249 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1250 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1251 } else {
1252 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1253 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1254 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1255 CHECK(caller != nullptr);
1256 }
1257 }
1258 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1259 << " dex pc: " << dex_pc
1260 << " dex file: " << caller->GetDexFile()->GetLocation()
1261 << " class table: "
Vladimir Marko606adb32018-04-05 14:49:24 +01001262 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1263 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001264 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1265 }
1266 }
1267}
1268
Ian Rogers848871b2013-08-05 10:56:33 -07001269// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001270extern "C" const void* artQuickResolutionTrampoline(
1271 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001272 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001273 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1274 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1275 // does not have the same stack layout as the callee-save method).
1276 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001277 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001278 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001279 ScopedObjectAccessUnchecked soa(env);
1280 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001281 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001282
1283 // Compute details about the called method (avoid GCs)
1284 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001285 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001286 MethodReference called_method(nullptr, 0);
1287 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001288 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001289 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001290 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001291 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001292
1293 InvokeType stack_map_invoke_type;
1294 uint32_t stack_map_dex_method_idx;
1295 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1296 &stack_map_invoke_type,
1297 &stack_map_dex_method_idx);
1298 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1299 // code.
1300 if (!found_stack_map || kIsDebugBuild) {
1301 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001302 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001303 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1304 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001305 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001306 bool is_range;
1307 switch (instr_code) {
1308 case Instruction::INVOKE_DIRECT:
1309 invoke_type = kDirect;
1310 is_range = false;
1311 break;
1312 case Instruction::INVOKE_DIRECT_RANGE:
1313 invoke_type = kDirect;
1314 is_range = true;
1315 break;
1316 case Instruction::INVOKE_STATIC:
1317 invoke_type = kStatic;
1318 is_range = false;
1319 break;
1320 case Instruction::INVOKE_STATIC_RANGE:
1321 invoke_type = kStatic;
1322 is_range = true;
1323 break;
1324 case Instruction::INVOKE_SUPER:
1325 invoke_type = kSuper;
1326 is_range = false;
1327 break;
1328 case Instruction::INVOKE_SUPER_RANGE:
1329 invoke_type = kSuper;
1330 is_range = true;
1331 break;
1332 case Instruction::INVOKE_VIRTUAL:
1333 invoke_type = kVirtual;
1334 is_range = false;
1335 break;
1336 case Instruction::INVOKE_VIRTUAL_RANGE:
1337 invoke_type = kVirtual;
1338 is_range = true;
1339 break;
1340 case Instruction::INVOKE_INTERFACE:
1341 invoke_type = kInterface;
1342 is_range = false;
1343 break;
1344 case Instruction::INVOKE_INTERFACE_RANGE:
1345 invoke_type = kInterface;
1346 is_range = true;
1347 break;
1348 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001349 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001350 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001351 UNREACHABLE();
1352 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001353 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001354 // Check that the invoke matches what we expected, note that this path only happens for debug
1355 // builds.
1356 if (found_stack_map) {
1357 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1358 if (invoke_type != kSuper) {
1359 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001360 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001361 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001362 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001363 }
1364 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001365 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001366 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001367 }
1368 } else {
1369 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001370 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001371 }
Ian Rogers848871b2013-08-05 10:56:33 -07001372 } else {
1373 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001374 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001375 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001376 }
1377 uint32_t shorty_len;
1378 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001379 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001380 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001381 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001382 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001383 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001384 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001385 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001386 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001387 mirror::Object* dummy = nullptr;
1388 HandleWrapper<mirror::Object> h_receiver(
1389 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001390 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001391 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001392 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001393
1394 // Update .bss entry in oat file if any.
1395 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001396 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1397 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1398 called_method.index,
1399 called_method.dex_file->NumMethodIds(),
1400 static_cast<size_t>(kRuntimePointerSize));
1401 if (bss_offset != IndexBssMappingLookup::npos) {
1402 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1403 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1404 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1405 oat_file->BssBegin() + bss_offset));
1406 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1407 DCHECK_LT(method_entry,
1408 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1409 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001410 }
1411 }
Ian Rogers848871b2013-08-05 10:56:33 -07001412 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001413 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001414 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001415 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001416 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001417 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001418 if (virtual_or_interface || invoke_type == kSuper) {
1419 // Refine called method based on receiver for kVirtual/kInterface, and
1420 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001421 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001422 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001423 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001424 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001425 } else if (invoke_type == kInterface) {
1426 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001427 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001428 } else {
1429 DCHECK_EQ(invoke_type, kSuper);
1430 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001431 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001432 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001433 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001434 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001435 } else {
1436 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001437 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001438 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001439 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001440
David Sehr709b0702016-10-13 09:12:37 -07001441 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1442 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001443 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001444 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001445
Ian Rogers848871b2013-08-05 10:56:33 -07001446 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001447 StackHandleScope<1> hs(soa.Self());
1448 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001449 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001450 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001451 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1452 // If we are single-stepping or the called method is deoptimized (by a
1453 // breakpoint, for example), then we have to execute the called method
1454 // with the interpreter.
1455 code = GetQuickToInterpreterBridge();
1456 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1457 // If the caller is deoptimized (by a breakpoint, for example), we have to
1458 // continue its execution with interpreter when returning from the called
1459 // method. Because we do not want to execute the called method with the
1460 // interpreter, we wrap its execution into the instrumentation stubs.
1461 // When the called method returns, it will execute the instrumentation
1462 // exit hook that will determine the need of the interpreter with a call
1463 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1464 // it is needed.
1465 code = GetQuickInstrumentationEntryPoint();
1466 } else {
1467 code = called->GetEntryPointFromQuickCompiledCode();
1468 }
Ian Rogers848871b2013-08-05 10:56:33 -07001469 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001470 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1471 // If we are single-stepping or the called method is deoptimized (by a
1472 // breakpoint, for example), then we have to execute the called method
1473 // with the interpreter.
1474 code = GetQuickToInterpreterBridge();
1475 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001476 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1477 // until class is initialized to stop races between threads).
1478 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001479 } else {
1480 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001481 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001482 }
1483 } else {
1484 DCHECK(called_class->IsErroneous());
1485 }
1486 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001487 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001488 // Fixup any locally saved objects may have moved during a GC.
1489 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001490 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001491 *sp = called;
1492
Ian Rogers848871b2013-08-05 10:56:33 -07001493 return code;
1494}
1495
Andreas Gampec147b002014-03-06 18:11:06 -08001496/*
1497 * This class uses a couple of observations to unite the different calling conventions through
1498 * a few constants.
1499 *
1500 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1501 * possible alignment.
1502 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1503 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1504 * when we have to split things
1505 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1506 * and we can use Int handling directly.
1507 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1508 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1509 * extension should be compatible with Aarch64, which mandates copying the available bits
1510 * into LSB and leaving the rest unspecified.
1511 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1512 * the stack.
1513 * 6) There is only little endian.
1514 *
1515 *
1516 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1517 * follows:
1518 *
1519 * void PushGpr(uintptr_t): Add a value for the next GPR
1520 *
1521 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1522 * padding, that is, think the architecture is 32b and aligns 64b.
1523 *
1524 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1525 * split this if necessary. The current state will have aligned, if
1526 * necessary.
1527 *
1528 * void PushStack(uintptr_t): Push a value to the stack.
1529 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001530 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001531 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001532 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001533 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001534 *
1535 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001536template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001537 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001538#if defined(__arm__)
1539 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001540 static constexpr bool kNativeSoftFloatAbi = true;
1541 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001542 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1543
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001544 static constexpr size_t kRegistersNeededForLong = 2;
1545 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001546 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001547 static constexpr bool kMultiFPRegistersWidened = false;
1548 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001549 static constexpr bool kAlignLongOnStack = true;
1550 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001551#elif defined(__aarch64__)
1552 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1553 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1554 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1555
1556 static constexpr size_t kRegistersNeededForLong = 1;
1557 static constexpr size_t kRegistersNeededForDouble = 1;
1558 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001559 static constexpr bool kMultiFPRegistersWidened = false;
1560 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001561 static constexpr bool kAlignLongOnStack = false;
1562 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001563#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001564 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001565 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1566 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001567
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001568 static constexpr size_t kRegistersNeededForLong = 2;
1569 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001570 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001571 static constexpr bool kMultiFPRegistersWidened = true;
1572 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001573 static constexpr bool kAlignLongOnStack = true;
1574 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001575#elif defined(__mips__) && defined(__LP64__)
1576 // Let the code prepare GPRs only and we will load the FPRs with same data.
1577 static constexpr bool kNativeSoftFloatAbi = true;
1578 static constexpr size_t kNumNativeGprArgs = 8;
1579 static constexpr size_t kNumNativeFprArgs = 0;
1580
1581 static constexpr size_t kRegistersNeededForLong = 1;
1582 static constexpr size_t kRegistersNeededForDouble = 1;
1583 static constexpr bool kMultiRegistersAligned = false;
1584 static constexpr bool kMultiFPRegistersWidened = false;
1585 static constexpr bool kMultiGPRegistersWidened = true;
1586 static constexpr bool kAlignLongOnStack = false;
1587 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001588#elif defined(__i386__)
1589 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001590 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001591 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1592 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1593
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001594 static constexpr size_t kRegistersNeededForLong = 2;
1595 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001596 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001597 static constexpr bool kMultiFPRegistersWidened = false;
1598 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001599 static constexpr bool kAlignLongOnStack = false;
1600 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001601#elif defined(__x86_64__)
1602 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1603 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1604 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1605
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001606 static constexpr size_t kRegistersNeededForLong = 1;
1607 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001608 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001609 static constexpr bool kMultiFPRegistersWidened = false;
1610 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001611 static constexpr bool kAlignLongOnStack = false;
1612 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001613#else
1614#error "Unsupported architecture"
1615#endif
1616
Andreas Gampec147b002014-03-06 18:11:06 -08001617 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618 explicit BuildNativeCallFrameStateMachine(T* delegate)
1619 : gpr_index_(kNumNativeGprArgs),
1620 fpr_index_(kNumNativeFprArgs),
1621 stack_entries_(0),
1622 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001623 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1624 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001625 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1626 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001627 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001628
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001630
Ian Rogers1428dce2014-10-21 15:02:15 -07001631 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001632 return gpr_index_ > 0;
1633 }
1634
Andreas Gampec200a4a2014-06-16 18:39:09 -07001635 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001636 if (HavePointerGpr()) {
1637 gpr_index_--;
1638 PushGpr(reinterpret_cast<uintptr_t>(val));
1639 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001641 PushStack(reinterpret_cast<uintptr_t>(val));
1642 gpr_index_ = 0;
1643 }
1644 }
1645
Ian Rogers1428dce2014-10-21 15:02:15 -07001646 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001647 return gpr_index_ > 0;
1648 }
1649
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001650 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001651 uintptr_t handle = PushHandle(ptr);
1652 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001653 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001654 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001655 } else {
1656 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001657 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001658 gpr_index_ = 0;
1659 }
1660 }
1661
Ian Rogers1428dce2014-10-21 15:02:15 -07001662 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001663 return gpr_index_ > 0;
1664 }
1665
1666 void AdvanceInt(uint32_t val) {
1667 if (HaveIntGpr()) {
1668 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001669 if (kMultiGPRegistersWidened) {
1670 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001671 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001672 } else {
1673 PushGpr(val);
1674 }
Andreas Gampec147b002014-03-06 18:11:06 -08001675 } else {
1676 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001677 if (kMultiGPRegistersWidened) {
1678 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001679 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001680 } else {
1681 PushStack(val);
1682 }
Andreas Gampec147b002014-03-06 18:11:06 -08001683 gpr_index_ = 0;
1684 }
1685 }
1686
Ian Rogers1428dce2014-10-21 15:02:15 -07001687 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001688 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1689 }
1690
Ian Rogers1428dce2014-10-21 15:02:15 -07001691 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001692 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1693 kAlignLongOnStack && // and when it needs alignment
1694 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1695 }
1696
Ian Rogers1428dce2014-10-21 15:02:15 -07001697 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001698 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1699 kAlignLongOnStack && // and when it needs 8B alignment
1700 (stack_entries_ & 1) == 1; // counter is odd
1701 }
1702
1703 void AdvanceLong(uint64_t val) {
1704 if (HaveLongGpr()) {
1705 if (LongGprNeedsPadding()) {
1706 PushGpr(0);
1707 gpr_index_--;
1708 }
1709 if (kRegistersNeededForLong == 1) {
1710 PushGpr(static_cast<uintptr_t>(val));
1711 } else {
1712 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1713 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1714 }
1715 gpr_index_ -= kRegistersNeededForLong;
1716 } else {
1717 if (LongStackNeedsPadding()) {
1718 PushStack(0);
1719 stack_entries_++;
1720 }
1721 if (kRegistersNeededForLong == 1) {
1722 PushStack(static_cast<uintptr_t>(val));
1723 stack_entries_++;
1724 } else {
1725 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1726 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1727 stack_entries_ += 2;
1728 }
1729 gpr_index_ = 0;
1730 }
1731 }
1732
Ian Rogers1428dce2014-10-21 15:02:15 -07001733 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001734 return fpr_index_ > 0;
1735 }
1736
Andreas Gampec147b002014-03-06 18:11:06 -08001737 void AdvanceFloat(float val) {
1738 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001739 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001740 } else {
1741 if (HaveFloatFpr()) {
1742 fpr_index_--;
1743 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001744 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001745 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001746 } else {
1747 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001748 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001749 }
1750 } else {
1751 PushFpr4(val);
1752 }
1753 } else {
1754 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001755 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001756 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001757 // Note: We need to jump through those hoops to make the compiler happy.
1758 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001759 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001760 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001761 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001762 }
1763 fpr_index_ = 0;
1764 }
1765 }
1766 }
1767
Ian Rogers1428dce2014-10-21 15:02:15 -07001768 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001769 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1770 }
1771
Ian Rogers1428dce2014-10-21 15:02:15 -07001772 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001773 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1774 kAlignDoubleOnStack && // and when it needs alignment
1775 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1776 }
1777
Ian Rogers1428dce2014-10-21 15:02:15 -07001778 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001779 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1780 kAlignDoubleOnStack && // and when it needs 8B alignment
1781 (stack_entries_ & 1) == 1; // counter is odd
1782 }
1783
1784 void AdvanceDouble(uint64_t val) {
1785 if (kNativeSoftFloatAbi) {
1786 AdvanceLong(val);
1787 } else {
1788 if (HaveDoubleFpr()) {
1789 if (DoubleFprNeedsPadding()) {
1790 PushFpr4(0);
1791 fpr_index_--;
1792 }
1793 PushFpr8(val);
1794 fpr_index_ -= kRegistersNeededForDouble;
1795 } else {
1796 if (DoubleStackNeedsPadding()) {
1797 PushStack(0);
1798 stack_entries_++;
1799 }
1800 if (kRegistersNeededForDouble == 1) {
1801 PushStack(static_cast<uintptr_t>(val));
1802 stack_entries_++;
1803 } else {
1804 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1805 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1806 stack_entries_ += 2;
1807 }
1808 fpr_index_ = 0;
1809 }
1810 }
1811 }
1812
Ian Rogers1428dce2014-10-21 15:02:15 -07001813 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001814 return stack_entries_;
1815 }
1816
Ian Rogers1428dce2014-10-21 15:02:15 -07001817 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001818 return kNumNativeGprArgs - gpr_index_;
1819 }
1820
Ian Rogers1428dce2014-10-21 15:02:15 -07001821 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001822 return kNumNativeFprArgs - fpr_index_;
1823 }
1824
1825 private:
1826 void PushGpr(uintptr_t val) {
1827 delegate_->PushGpr(val);
1828 }
1829 void PushFpr4(float val) {
1830 delegate_->PushFpr4(val);
1831 }
1832 void PushFpr8(uint64_t val) {
1833 delegate_->PushFpr8(val);
1834 }
1835 void PushStack(uintptr_t val) {
1836 delegate_->PushStack(val);
1837 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001838 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001839 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001840 }
1841
1842 uint32_t gpr_index_; // Number of free GPRs
1843 uint32_t fpr_index_; // Number of free FPRs
1844 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1845 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001846 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001847};
1848
Andreas Gampec200a4a2014-06-16 18:39:09 -07001849// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1850// in subclasses.
1851//
1852// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1853// them with handles.
1854class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001855 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001856 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1857
1858 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001859
Ian Rogers1428dce2014-10-21 15:02:15 -07001860 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001861 return num_stack_entries_ * sizeof(uintptr_t);
1862 }
1863
Ian Rogers1428dce2014-10-21 15:02:15 -07001864 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001865 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001866 // Align by kStackAlignment.
1867 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001868 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001869 }
1870
Ian Rogers1428dce2014-10-21 15:02:15 -07001871 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1872 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001873 // Assumption is OK right now, as we have soft-float arm
1874 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1875 sp8 -= fregs * sizeof(uintptr_t);
1876 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1877 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1878 sp8 -= iregs * sizeof(uintptr_t);
1879 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1880 return sp8;
1881 }
Andreas Gampec147b002014-03-06 18:11:06 -08001882
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001884 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001885 // Native call stack.
1886 sp8 = LayoutCallStack(sp8);
1887 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001888
Andreas Gampec200a4a2014-06-16 18:39:09 -07001889 // Put fprs and gprs below.
1890 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001891
Andreas Gampec200a4a2014-06-16 18:39:09 -07001892 // Return the new bottom.
1893 return sp8;
1894 }
1895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001896 virtual void WalkHeader(
1897 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001898 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001899 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001901 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001902 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1903
1904 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001905
1906 for (uint32_t i = 1; i < shorty_len; ++i) {
1907 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1908 switch (cur_type_) {
1909 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001910 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001911 sm.AdvanceHandleScope(
1912 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001913 break;
1914
1915 case Primitive::kPrimBoolean:
1916 case Primitive::kPrimByte:
1917 case Primitive::kPrimChar:
1918 case Primitive::kPrimShort:
1919 case Primitive::kPrimInt:
1920 sm.AdvanceInt(0);
1921 break;
1922 case Primitive::kPrimFloat:
1923 sm.AdvanceFloat(0);
1924 break;
1925 case Primitive::kPrimDouble:
1926 sm.AdvanceDouble(0);
1927 break;
1928 case Primitive::kPrimLong:
1929 sm.AdvanceLong(0);
1930 break;
1931 default:
1932 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001933 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001934 }
1935 }
1936
Ian Rogers1428dce2014-10-21 15:02:15 -07001937 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001938 }
1939
1940 void PushGpr(uintptr_t /* val */) {
1941 // not optimizing registers, yet
1942 }
1943
1944 void PushFpr4(float /* val */) {
1945 // not optimizing registers, yet
1946 }
1947
1948 void PushFpr8(uint64_t /* val */) {
1949 // not optimizing registers, yet
1950 }
1951
1952 void PushStack(uintptr_t /* val */) {
1953 // counting is already done in the superclass
1954 }
1955
Andreas Gampec200a4a2014-06-16 18:39:09 -07001956 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001957 return reinterpret_cast<uintptr_t>(nullptr);
1958 }
1959
Andreas Gampec200a4a2014-06-16 18:39:09 -07001960 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001961 uint32_t num_stack_entries_;
1962};
1963
Andreas Gampec200a4a2014-06-16 18:39:09 -07001964class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001965 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001966 explicit ComputeGenericJniFrameSize(bool critical_native)
1967 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001968
Andreas Gampec200a4a2014-06-16 18:39:09 -07001969 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1970 // is at *m = sp. Will update to point to the bottom of the save frame.
1971 //
1972 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001973 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001974 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001975 ArtMethod* method = **m;
1976
Andreas Gampe542451c2016-07-26 09:02:02 -07001977 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001978
Andreas Gampec200a4a2014-06-16 18:39:09 -07001979 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1980
1981 // First, fix up the layout of the callee-save frame.
1982 // We have to squeeze in the HandleScope, and relocate the method pointer.
1983
1984 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001985 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001986
1987 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001988 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001990
1991 sp8 -= scope_and_method;
1992 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001994
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001996 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1997 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001998
1999 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2000 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002001 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2002 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002003 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002004 }
2005
Andreas Gampec200a4a2014-06-16 18:39:09 -07002006 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002007 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008 // Reference cookie and padding
2009 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002010 }
2011
Andreas Gampec200a4a2014-06-16 18:39:09 -07002012 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2013 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002015 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002016 // First, fix up the layout of the callee-save frame.
2017 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002018 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019
2020 // The bottom of the callee-save frame is now where the method is, *m.
2021 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2022
2023 // Add space for cookie.
2024 LayoutCookie(&sp8);
2025
2026 return sp8;
2027 }
2028
2029 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2031 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2032 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002033 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002034 Walk(shorty, shorty_len);
2035
2036 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002037 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002038
2039 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2040
2041 // Return the new bottom.
2042 return sp8;
2043 }
2044
2045 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2046
2047 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2048 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002049 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002050
2051 private:
2052 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002053 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002054};
2055
2056uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2057 num_handle_scope_references_++;
2058 return reinterpret_cast<uintptr_t>(nullptr);
2059}
2060
2061void ComputeGenericJniFrameSize::WalkHeader(
2062 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002063 // First 2 parameters are always excluded for @CriticalNative.
2064 if (UNLIKELY(critical_native_)) {
2065 return;
2066 }
2067
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068 // JNIEnv
2069 sm->AdvancePointer(nullptr);
2070
2071 // Class object or this as first argument
2072 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2073}
2074
2075// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2076// the template requirements of BuildGenericJniFrameStateMachine.
2077class FillNativeCall {
2078 public:
2079 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2080 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2081
2082 virtual ~FillNativeCall() {}
2083
2084 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2085 cur_gpr_reg_ = gpr_regs;
2086 cur_fpr_reg_ = fpr_regs;
2087 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002088 }
2089
2090 void PushGpr(uintptr_t val) {
2091 *cur_gpr_reg_ = val;
2092 cur_gpr_reg_++;
2093 }
2094
2095 void PushFpr4(float val) {
2096 *cur_fpr_reg_ = val;
2097 cur_fpr_reg_++;
2098 }
2099
2100 void PushFpr8(uint64_t val) {
2101 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2102 *tmp = val;
2103 cur_fpr_reg_ += 2;
2104 }
2105
2106 void PushStack(uintptr_t val) {
2107 *cur_stack_arg_ = val;
2108 cur_stack_arg_++;
2109 }
2110
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002111 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002112 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002113 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002114 }
2115
2116 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002117 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002118 uint32_t* cur_fpr_reg_;
2119 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002120};
Andreas Gampec147b002014-03-06 18:11:06 -08002121
Andreas Gampec200a4a2014-06-16 18:39:09 -07002122// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2123// of transitioning into native code.
2124class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2125 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002126 BuildGenericJniFrameVisitor(Thread* self,
2127 bool is_static,
2128 bool critical_native,
2129 const char* shorty,
2130 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002131 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002132 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002133 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2134 sm_(&jni_call_) {
2135 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002136 uintptr_t* start_gpr_reg;
2137 uint32_t* start_fpr_reg;
2138 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002139 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002140 &handle_scope_,
2141 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002142 &start_gpr_reg, &start_fpr_reg);
2143
Andreas Gampec200a4a2014-06-16 18:39:09 -07002144 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2145
Igor Murashkin06a04e02016-09-13 15:57:37 -07002146 // First 2 parameters are always excluded for CriticalNative methods.
2147 if (LIKELY(!critical_native)) {
2148 // jni environment is always first argument
2149 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002150
Igor Murashkin06a04e02016-09-13 15:57:37 -07002151 if (is_static) {
2152 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2153 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002154 }
2155 }
2156
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002157 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002158
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002159 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002160
Vladimir Markof39745e2016-01-26 12:16:55 +00002161 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002162 return handle_scope_->GetHandle(0).GetReference();
2163 }
2164
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002165 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002166 return handle_scope_->GetHandle(0).ToJObject();
2167 }
2168
Ian Rogers1428dce2014-10-21 15:02:15 -07002169 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002170 return bottom_of_used_area_;
2171 }
2172
2173 private:
2174 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2175 class FillJniCall FINAL : public FillNativeCall {
2176 public:
2177 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002178 HandleScope* handle_scope, bool critical_native)
2179 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2180 handle_scope_(handle_scope),
2181 cur_entry_(0),
2182 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002183
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002184 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002185
2186 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2187 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2188 handle_scope_ = scope;
2189 cur_entry_ = 0U;
2190 }
2191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002192 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002193 // Initialize padding entries.
2194 size_t expected_slots = handle_scope_->NumberOfReferences();
2195 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002196 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002197 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002198
2199 if (!critical_native_) {
2200 // Non-critical natives have at least the self class (jclass) or this (jobject).
2201 DCHECK_NE(cur_entry_, 0U);
2202 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002203 }
2204
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002205 bool CriticalNative() const {
2206 return critical_native_;
2207 }
2208
Andreas Gampec200a4a2014-06-16 18:39:09 -07002209 private:
2210 HandleScope* handle_scope_;
2211 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002212 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002213 };
2214
2215 HandleScope* handle_scope_;
2216 FillJniCall jni_call_;
2217 void* bottom_of_used_area_;
2218
2219 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002220
2221 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2222};
2223
Andreas Gampec200a4a2014-06-16 18:39:09 -07002224uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2225 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002226 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002227 h.Assign(ref);
2228 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2229 cur_entry_++;
2230 return tmp;
2231}
2232
Ian Rogers9758f792014-03-13 09:02:55 -07002233void BuildGenericJniFrameVisitor::Visit() {
2234 Primitive::Type type = GetParamPrimitiveType();
2235 switch (type) {
2236 case Primitive::kPrimLong: {
2237 jlong long_arg;
2238 if (IsSplitLongOrDouble()) {
2239 long_arg = ReadSplitLongParam();
2240 } else {
2241 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2242 }
2243 sm_.AdvanceLong(long_arg);
2244 break;
2245 }
2246 case Primitive::kPrimDouble: {
2247 uint64_t double_arg;
2248 if (IsSplitLongOrDouble()) {
2249 // Read into union so that we don't case to a double.
2250 double_arg = ReadSplitLongParam();
2251 } else {
2252 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2253 }
2254 sm_.AdvanceDouble(double_arg);
2255 break;
2256 }
2257 case Primitive::kPrimNot: {
2258 StackReference<mirror::Object>* stack_ref =
2259 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002260 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002261 break;
2262 }
2263 case Primitive::kPrimFloat:
2264 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2265 break;
2266 case Primitive::kPrimBoolean: // Fall-through.
2267 case Primitive::kPrimByte: // Fall-through.
2268 case Primitive::kPrimChar: // Fall-through.
2269 case Primitive::kPrimShort: // Fall-through.
2270 case Primitive::kPrimInt: // Fall-through.
2271 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2272 break;
2273 case Primitive::kPrimVoid:
2274 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002275 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002276 }
2277}
2278
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002279void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002280 // Clear out rest of the scope.
2281 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002282 if (!jni_call_.CriticalNative()) {
2283 // Install HandleScope.
2284 self->PushHandleScope(handle_scope_);
2285 }
Ian Rogers9758f792014-03-13 09:02:55 -07002286}
2287
Ian Rogers04c31d22014-07-07 21:44:06 -07002288#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002289extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002290#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002291extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002292#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002293
Igor Murashkin06a04e02016-09-13 15:57:37 -07002294static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2295 uint32_t cookie,
2296 bool fast_native ATTRIBUTE_UNUSED,
2297 jobject l,
2298 jobject lock) {
2299 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002300 if (lock != nullptr) {
2301 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2302 } else {
2303 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2304 }
2305}
2306
Igor Murashkin06a04e02016-09-13 15:57:37 -07002307static void artQuickGenericJniEndJNINonRef(Thread* self,
2308 uint32_t cookie,
2309 bool fast_native,
2310 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002311 if (lock != nullptr) {
2312 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002313 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002314 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002315 if (UNLIKELY(fast_native)) {
2316 JniMethodFastEnd(cookie, self);
2317 } else {
2318 JniMethodEnd(cookie, self);
2319 }
Andreas Gampead615172014-04-04 16:20:13 -07002320 }
2321}
2322
Andreas Gampec147b002014-03-06 18:11:06 -08002323/*
2324 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002325 * 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 -08002326 * The final element on the stack is a pointer to the native code.
2327 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002328 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002329 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002330 *
Andreas Gampec147b002014-03-06 18:11:06 -08002331 * The return of this function denotes:
2332 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2333 * 2) An error, if the value is negative.
2334 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002335extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002336 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002337 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002338 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002339 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002340 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002341 uint32_t shorty_len = 0;
2342 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002343 bool critical_native = called->IsCriticalNative();
2344 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002345 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002346
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002347 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002348 BuildGenericJniFrameVisitor visitor(self,
2349 called->IsStatic(),
2350 critical_native,
2351 shorty,
2352 shorty_len,
2353 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002354 {
2355 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2356 visitor.VisitArguments();
2357 // FinalizeHandleScope pushes the handle scope on the thread.
2358 visitor.FinalizeHandleScope(self);
2359 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002360
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002361 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002362 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002363
Ian Rogerse0dcd462014-03-08 15:21:04 -08002364 self->VerifyStack();
2365
Vladimir Markof8655b32018-03-21 17:53:56 +00002366 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2367 jit::Jit* jit = runtime->GetJit();
2368 if (jit != nullptr) {
2369 jit->MethodEntered(self, called);
2370 }
2371
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002372 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002373 uint32_t* sp32;
2374 // Skip calling JniMethodStart for @CriticalNative.
2375 if (LIKELY(!critical_native)) {
2376 // Start JNI, save the cookie.
2377 if (called->IsSynchronized()) {
2378 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2379 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2380 if (self->IsExceptionPending()) {
2381 self->PopHandleScope();
2382 // A negative value denotes an error.
2383 return GetTwoWordFailureValue();
2384 }
2385 } else {
2386 if (fast_native) {
2387 cookie = JniMethodFastStart(self);
2388 } else {
2389 DCHECK(normal_native);
2390 cookie = JniMethodStart(self);
2391 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002392 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002393 sp32 = reinterpret_cast<uint32_t*>(sp);
2394 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002395 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002396
Andreas Gampe90546832014-03-12 18:07:19 -07002397 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002398 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002399
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002400 // There are two cases for the content of nativeCode:
2401 // 1) Pointer to the native function.
2402 // 2) Pointer to the trampoline for native code binding.
2403 // In the second case, we need to execute the binding and continue with the actual native function
2404 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002405 DCHECK(nativeCode != nullptr);
2406 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002407#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002408 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002409#else
2410 nativeCode = artFindNativeMethod(self);
2411#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002412
2413 if (nativeCode == nullptr) {
2414 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002415
Igor Murashkin06a04e02016-09-13 15:57:37 -07002416 // @CriticalNative calls do not need to call back into JniMethodEnd.
2417 if (LIKELY(!critical_native)) {
2418 // End JNI, as the assembly will move to deliver the exception.
2419 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2420 if (shorty[0] == 'L') {
2421 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2422 } else {
2423 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2424 }
Andreas Gampead615172014-04-04 16:20:13 -07002425 }
2426
Andreas Gampec200a4a2014-06-16 18:39:09 -07002427 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002428 }
2429 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002430 }
2431
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002432#if defined(__mips__) && !defined(__LP64__)
2433 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2434 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2435 // and load into floating-point registers.
2436 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2437 // view):
2438 // (1)
2439 // | DOUBLE | DOUBLE | other args, if any
2440 // | F12 | F13 | F14 | F15 |
2441 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2442 // (2)
2443 // | DOUBLE | FLOAT | (PAD) | other args, if any
2444 // | F12 | F13 | F14 | |
2445 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2446 // (3)
2447 // | FLOAT | (PAD) | DOUBLE | other args, if any
2448 // | F12 | | F14 | F15 |
2449 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2450 // (4)
2451 // | FLOAT | FLOAT | other args, if any
2452 // | F12 | F14 |
2453 // | SP+0 | SP+4 | SP+8
2454 // As you can see, only the last case (4) is special. In all others we can just
2455 // load F12/F13 and F14/F15 in the same manner.
2456 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2457 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2458 if (nativeCode != nullptr &&
2459 shorty != nullptr &&
2460 shorty_len >= 3 &&
2461 shorty[1] == 'F' &&
2462 shorty[2] == 'F') {
2463 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2464 }
2465#endif
2466
Andreas Gampec200a4a2014-06-16 18:39:09 -07002467 // Return native code addr(lo) and bottom of alloca address(hi).
2468 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2469 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002470}
2471
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002472// Defined in quick_jni_entrypoints.cc.
2473extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2474 jvalue result, uint64_t result_f, ArtMethod* called,
2475 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002476/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002477 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002478 * unlocking.
2479 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002480extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2481 jvalue result,
2482 uint64_t result_f) {
2483 // We're here just back from a native call. We don't have the shared mutator lock at this point
2484 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2485 // anything that requires a mutator lock before that would cause problems as GC may have the
2486 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002487 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002488 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002489 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002490 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002491 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002492 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2493 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002494}
2495
Andreas Gamped58342c2014-06-05 14:18:08 -07002496// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2497// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002498//
Andreas Gamped58342c2014-06-05 14:18:08 -07002499// 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 -07002500// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002501
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002502template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002503static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2504 ObjPtr<mirror::Object> this_object,
2505 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002506 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002507 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002508 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002509 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002510 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002511 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002512 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2513 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002514 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002515 {
2516 // Remember the args in case a GC happens in FindMethodFromCode.
2517 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2518 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2519 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002520 method = FindMethodFromCode<type, access_check>(method_idx,
2521 &this_object,
2522 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002523 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002524 visitor.FixupReferences();
2525 }
2526
Ian Rogerse0a02da2014-12-02 14:10:53 -08002527 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002528 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002529 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002530 }
2531 }
2532 DCHECK(!self->IsExceptionPending());
2533 const void* code = method->GetEntryPointFromQuickCompiledCode();
2534
2535 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002536 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002537 << " location: "
2538 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002539
Andreas Gamped58342c2014-06-05 14:18:08 -07002540 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2541 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002542}
2543
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002544// Explicit artInvokeCommon template function declarations to please analysis tool.
2545#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002546 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002547 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002548 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002549
2550EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2551EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2552EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2553EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2554EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2555EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2556EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2557EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2558EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2559EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2560#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2561
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002562// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002563extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002564 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002565 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002566 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002567}
2568
Andreas Gampec200a4a2014-06-16 18:39:09 -07002569extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002570 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002571 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002572 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002573}
2574
Andreas Gampec200a4a2014-06-16 18:39:09 -07002575extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002576 uint32_t method_idx,
2577 mirror::Object* this_object ATTRIBUTE_UNUSED,
2578 Thread* self,
2579 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2580 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2581 // it doesn't cause ObjPtr alignment failure check.
2582 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002583}
2584
Andreas Gampec200a4a2014-06-16 18:39:09 -07002585extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002587 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002588 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002589}
2590
Andreas Gampec200a4a2014-06-16 18:39:09 -07002591extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002592 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002593 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002594 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002595}
2596
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002597// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2598extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2599 REQUIRES_SHARED(Locks::mutator_lock_) {
2600 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2601 DCHECK(!referrer->IsProxyMethod());
2602 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2603 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2604 DCHECK(result == nullptr ||
2605 result->GetDeclaringClass()->IsInterface() ||
2606 result->GetDeclaringClass() ==
2607 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2608 << result->PrettyMethod();
2609 return result;
2610}
2611
Jeff Hao5667f562017-02-27 19:32:01 -08002612// Determine target of interface dispatch. The interface method and this object are known non-null.
2613// The interface method is the method returned by the dex cache in the conflict trampoline.
2614extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002615 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002616 Thread* self,
2617 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002618 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002619 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002620 StackHandleScope<2> hs(self);
2621 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2622 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002623
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002624 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002625 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002626 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002627
Vladimir Marko302f69c2017-07-25 15:27:15 +01002628 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002629 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002630 // Fetch the dex_method_idx of the target interface method from the caller.
2631 uint32_t dex_method_idx;
2632 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002633 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002634 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002635 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2636 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002637 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002638 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002639 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002640 } else {
2641 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002642 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002643 }
2644
Vladimir Marko302f69c2017-07-25 15:27:15 +01002645 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002646 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002647 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2648 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002649 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002650 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002651 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2652 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2653 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002654 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2655 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2656 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002657 visitor.FixupReferences();
2658 }
2659
Vladimir Marko302f69c2017-07-25 15:27:15 +01002660 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002661 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002662 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002663 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002664 }
2665
2666 DCHECK(!interface_method->IsRuntimeMethod());
2667 // Look whether we have a match in the ImtConflictTable.
2668 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2669 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2670 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2671 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2672 DCHECK(current_table != nullptr);
2673 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2674 } else {
2675 // It seems we aren't really a conflict method!
2676 if (kIsDebugBuild) {
2677 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2678 CHECK_EQ(conflict_method, m)
2679 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2680 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2681 }
2682 method = conflict_method;
2683 }
2684 if (method != nullptr) {
2685 return GetTwoWordSuccessValue(
2686 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2687 reinterpret_cast<uintptr_t>(method));
2688 }
2689
2690 // No match, use the IfTable.
2691 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2692 if (UNLIKELY(method == nullptr)) {
2693 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2694 interface_method, this_object.Get(), caller_method);
2695 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002696 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002697
2698 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2699 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002700 DCHECK(conflict_method->IsRuntimeMethod());
2701 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2702 cls.Get(),
2703 conflict_method,
2704 interface_method,
2705 method,
2706 /*force_new_conflict_method*/false);
2707 if (new_conflict_method != conflict_method) {
2708 // Update the IMT if we create a new conflict method. No fence needed here, as the
2709 // data is consistent.
2710 imt->Set(imt_index,
2711 new_conflict_method,
2712 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002713 }
2714
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002715 const void* code = method->GetEntryPointFromQuickCompiledCode();
2716
2717 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002718 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002719 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002720
Andreas Gamped58342c2014-06-05 14:18:08 -07002721 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2722 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002723}
2724
Orion Hodsonac141392017-01-13 11:53:47 +00002725// Returns shorty type so the caller can determine how to put |result|
2726// into expected registers. The shorty type is static so the compiler
2727// could call different flavors of this code path depending on the
2728// shorty type though this would require different entry points for
2729// each type.
2730extern "C" uintptr_t artInvokePolymorphic(
2731 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002732 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002733 Thread* self,
2734 ArtMethod** sp)
2735 REQUIRES_SHARED(Locks::mutator_lock_) {
2736 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002737 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002738
2739 // Start new JNI local reference state
2740 JNIEnvExt* env = self->GetJniEnv();
2741 ScopedObjectAccessUnchecked soa(env);
2742 ScopedJniEnvLocalRefState env_state(env);
2743 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2744
2745 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2746 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2747 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002748 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002749 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2750 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002751 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002752 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002753 const size_t shorty_length = strlen(shorty);
2754 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2755 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002756 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002757
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002758 // Wrap raw_receiver in a Handle for safety.
2759 StackHandleScope<3> hs(self);
2760 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2761 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002762 self->EndAssertNoThreadSuspension(old_cause);
2763
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002764 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002765 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002766 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002767 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002768
2769 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002770 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2771 return static_cast<uintptr_t>('V');
2772 }
2773
Orion Hodsone7732be2017-10-11 14:35:20 +01002774 Handle<mirror::MethodType> method_type(
2775 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2776
Orion Hodsonac141392017-01-13 11:53:47 +00002777 // This implies we couldn't resolve one or more types in this method handle.
2778 if (UNLIKELY(method_type.IsNull())) {
2779 CHECK(self->IsExceptionPending());
2780 return static_cast<uintptr_t>('V');
2781 }
2782
Vladimir Markod7559b72017-09-28 13:50:37 +01002783 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002784 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2785
2786 // Fix references before constructing the shadow frame.
2787 gc_visitor.FixupReferences();
2788
2789 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002790 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2791 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002792 const size_t first_arg = 0;
2793 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2794 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2795 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2796 ScopedStackedShadowFramePusher
2797 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2798 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2799 kMethodIsStatic,
2800 shorty,
2801 strlen(shorty),
2802 shadow_frame,
2803 first_arg);
2804 shadow_frame_builder.VisitArguments();
2805
2806 // Push a transition back into managed code onto the linked list in thread.
2807 ManagedStack fragment;
2808 self->PushManagedStackFragment(&fragment);
2809
2810 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2811 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002812 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002813 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002814 bool success = false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002815 if (resolved_method->GetDeclaringClass() == GetClassRoot<mirror::MethodHandle>(linker)) {
Orion Hodson537a4fe2018-05-15 13:57:58 +01002816 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2817 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2818 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2819 success = MethodHandleInvokeExact(self,
2820 *shadow_frame,
2821 method_handle,
2822 method_type,
2823 &operands,
2824 result);
2825 } else {
2826 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2827 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2828 success = MethodHandleInvoke(self,
2829 *shadow_frame,
2830 method_handle,
2831 method_type,
2832 &operands,
2833 result);
2834 }
2835 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002836 DCHECK_EQ(GetClassRoot<mirror::VarHandle>(linker), resolved_method->GetDeclaringClass());
Orion Hodson537a4fe2018-05-15 13:57:58 +01002837 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
2838 ObjPtr<mirror::VarHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2839 mirror::VarHandle::AccessMode access_mode =
2840 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2841 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002842 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002843 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002844 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002845 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002846 &operands,
2847 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002848 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002849
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002850 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002851
2852 // Pop transition record.
2853 self->PopManagedStackFragment(fragment);
2854
2855 return static_cast<uintptr_t>(shorty[0]);
2856}
2857
Ian Rogers848871b2013-08-05 10:56:33 -07002858} // namespace art