blob: c894406e9886a1c87b0ca6749acef1600ee17590 [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"
Orion Hodson4c8e12e2018-05-18 08:33:20 +010038#include "interpreter/interpreter_common.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010039#include "interpreter/shadow_frame-inl.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000040#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000041#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000042#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070043#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070044#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070045#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000046#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010049#include "mirror/var_handle.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010050#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010051#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070052#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070053#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070054#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070055#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070056#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010057#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000058#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070059
60namespace art {
61
Andreas Gampe8228cdf2017-05-30 15:03:54 -070062// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070063class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080064 // Number of bytes for each out register in the caller method's frame.
65 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070066 // Frame size in bytes of a callee-save frame for RefsAndArgs.
67 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010068 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
69 // Offset of first GPR arg.
70 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
71 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
72 // Offset of first FPR arg.
73 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
74 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
75 // Offset of return address.
76 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
77 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070078#if defined(__arm__)
79 // The callee save frame is pointed to by SP.
80 // | argN | |
81 // | ... | |
82 // | arg4 | |
83 // | arg3 spill | | Caller's frame
84 // | arg2 spill | |
85 // | arg1 spill | |
86 // | Method* | ---
87 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080088 // | ... | 4x6 bytes callee saves
89 // | R3 |
90 // | R2 |
91 // | R1 |
92 // | S15 |
93 // | : |
94 // | S0 |
95 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070096 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070097 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
98 static constexpr bool kAlignPairRegister = true;
99 static constexpr bool kQuickSoftFloatAbi = false;
100 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000101 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800102 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -0700103 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800104 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800105 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000106 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800107 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108#elif defined(__aarch64__)
109 // The callee save frame is pointed to by SP.
110 // | argN | |
111 // | ... | |
112 // | arg4 | |
113 // | arg3 spill | | Caller's frame
114 // | arg2 spill | |
115 // | arg1 spill | |
116 // | Method* | ---
117 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800118 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100120 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000121 // | X7 |
122 // | : |
123 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800124 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000125 // | : |
126 // | D0 |
127 // | | padding
128 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500129 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000130 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000131 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800132 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000133 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000134 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
135 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800136 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000138 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000139 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800140#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700141 // The callee save frame is pointed to by SP.
142 // | argN | |
143 // | ... | |
144 // | arg4 | |
145 // | arg3 spill | | Caller's frame
146 // | arg2 spill | |
147 // | arg1 spill | |
148 // | Method* | ---
149 // | RA |
150 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800151 // | T1 | arg5
152 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700153 // | A3 | arg3
154 // | A2 | arg2
155 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800156 // | F19 |
157 // | F18 | f_arg5
158 // | F17 |
159 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800161 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000162 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800163 // | F12 | f_arg2
164 // | F11 |
165 // | F10 | f_arg1
166 // | F9 |
167 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000168 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700169 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000170 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
171 static constexpr bool kAlignPairRegister = true;
172 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800173 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000174 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800175 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
176 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
177 // passed only in even numbered registers and each
178 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800179 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800180 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000181 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800182 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800183#elif defined(__mips__) && defined(__LP64__)
184 // The callee save frame is pointed to by SP.
185 // | argN | |
186 // | ... | |
187 // | arg4 | |
188 // | arg3 spill | | Caller's frame
189 // | arg2 spill | |
190 // | arg1 spill | |
191 // | Method* | ---
192 // | RA |
193 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800194 // | A7 | arg7
195 // | A6 | arg6
196 // | A5 | arg5
197 // | A4 | arg4
198 // | A3 | arg3
199 // | A2 | arg2
200 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000201 // | F19 | f_arg7
202 // | F18 | f_arg6
203 // | F17 | f_arg5
204 // | F16 | f_arg4
205 // | F15 | f_arg3
206 // | F14 | f_arg2
207 // | F13 | f_arg1
208 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800209 // | | padding
210 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800211 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800212 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800213 static constexpr bool kAlignPairRegister = false;
214 static constexpr bool kQuickSoftFloatAbi = false;
215 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000216 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800217 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
218 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
219 static constexpr bool kGprFprLockstep = true;
220
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800221 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
222 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
223 }
Ian Rogers848871b2013-08-05 10:56:33 -0700224#elif defined(__i386__)
225 // The callee save frame is pointed to by SP.
226 // | argN | |
227 // | ... | |
228 // | arg4 | |
229 // | arg3 spill | | Caller's frame
230 // | arg2 spill | |
231 // | arg1 spill | |
232 // | Method* | ---
233 // | Return |
234 // | EBP,ESI,EDI | callee saves
235 // | EBX | arg3
236 // | EDX | arg2
237 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000238 // | XMM3 | float arg 4
239 // | XMM2 | float arg 3
240 // | XMM1 | float arg 2
241 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700242 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500243 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000244 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000245 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800246 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000247 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800248 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000249 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800250 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800251 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000252 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800255 // The callee save frame is pointed to by SP.
256 // | argN | |
257 // | ... | |
258 // | reg. arg spills | | Caller's frame
259 // | Method* | ---
260 // | Return |
261 // | R15 | callee save
262 // | R14 | callee save
263 // | R13 | callee save
264 // | R12 | callee save
265 // | R9 | arg5
266 // | R8 | arg4
267 // | RSI/R6 | arg1
268 // | RBP/R5 | callee save
269 // | RBX/R3 | callee save
270 // | RDX/R2 | arg2
271 // | RCX/R1 | arg3
272 // | XMM7 | float arg 8
273 // | XMM6 | float arg 7
274 // | XMM5 | float arg 6
275 // | XMM4 | float arg 5
276 // | XMM3 | float arg 4
277 // | XMM2 | float arg 3
278 // | XMM1 | float arg 2
279 // | XMM0 | float arg 1
280 // | Padding |
281 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500282 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000283 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800284 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800285 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000286 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700287 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700288 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800289 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800290 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
291 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000292 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
293 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
296 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800297 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700298 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
299 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800300 }
301 }
Ian Rogers848871b2013-08-05 10:56:33 -0700302#else
303#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700304#endif
305
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100307 // Special handling for proxy methods. Proxy methods are instance methods so the
308 // 'this' object is the 1st argument. They also have the same frame layout as the
309 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
310 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000311 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700312 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000313 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100314 CHECK_GT(kNumQuickGprArgs, 0u);
315 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
316 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
317 GprIndexToGprOffset(kThisGprIndex);
318 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000319 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100320 }
321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700324 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100325 }
326
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700327 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100329 uint8_t* previous_sp =
330 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700331 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 }
333
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700334 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100336 constexpr size_t callee_frame_size =
337 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
339 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100341 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
342 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100344 if (current_code->IsOptimized()) {
David Srbecky052f8ca2018-04-26 15:42:54 +0100345 CodeInfo code_info(current_code);
346 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100347 DCHECK(stack_map.IsValid());
David Srbecky93bd3612018-07-02 19:30:18 +0100348 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
349 if (!inline_infos.empty()) {
350 return inline_infos.back().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();
Alex Light6cae5ea2018-06-07 17:07:02 -07001103 DCHECK(!method->IsProxyMethod())
1104 << "Proxy method " << method->PrettyMethod()
1105 << " (declaring class: " << method->GetDeclaringClass()->PrettyClass() << ")"
1106 << " should not hit instrumentation entrypoint.";
Alex Lightb7edcda2017-04-27 13:20:31 -07001107 if (instrumentation->IsDeoptimized(method)) {
1108 result = GetQuickToInterpreterBridge();
1109 } else {
1110 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
Alex Lightb7edcda2017-04-27 13:20:31 -07001111 }
1112
1113 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1114 bool is_static = method->IsStatic();
1115 uint32_t shorty_len;
1116 const char* shorty =
1117 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1118
1119 ScopedObjectAccessUnchecked soa(self);
1120 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1121 visitor.VisitArguments();
1122
1123 instrumentation->PushInstrumentationStackFrame(self,
1124 is_static ? nullptr : this_object,
1125 method,
1126 QuickArgumentVisitor::GetCallingPc(sp),
1127 interpreter_entry);
1128
1129 visitor.FixupReferences();
1130 if (UNLIKELY(self->IsExceptionPending())) {
1131 return nullptr;
1132 }
1133 CHECK(result != nullptr) << method->PrettyMethod();
1134 return result;
1135}
1136
1137extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1138 ArtMethod** sp,
1139 uint64_t* gpr_result,
1140 uint64_t* fpr_result)
1141 REQUIRES_SHARED(Locks::mutator_lock_) {
1142 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1143 CHECK(gpr_result != nullptr);
1144 CHECK(fpr_result != nullptr);
1145 // Instrumentation exit stub must not be entered with a pending exception.
1146 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1147 << self->GetException()->Dump();
1148 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001149 constexpr size_t return_pc_offset =
1150 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001151 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1152 return_pc_offset);
1153 CHECK_EQ(*return_pc, 0U);
1154
1155 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1156 // deoptimization shouldn't be performed with the high-half having the return address. When
1157 // deoptimization should be performed the low half is zero and the high-half the address of the
1158 // deoptimization entry point.
1159 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1160 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1161 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001162 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001163 return GetTwoWordFailureValue();
1164 }
1165 return return_or_deoptimize_pc;
1166}
1167
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001168static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1169 REQUIRES_SHARED(Locks::mutator_lock_) {
1170 if (dex_pc == static_cast<uint32_t>(-1)) {
1171 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1172 return "<native>";
1173 } else {
1174 CodeItemInstructionAccessor accessor = method->DexInstructions();
1175 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1176 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1177 }
1178}
1179
Vladimir Marko606adb32018-04-05 14:49:24 +01001180static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1181 REQUIRES_SHARED(Locks::mutator_lock_) {
1182 std::string storage;
1183 const char* descriptor = klass->GetDescriptor(&storage);
1184 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1185 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1186 if (oat_dex_file != nullptr) {
1187 const OatFile* oat_file = oat_dex_file->GetOatFile();
1188 const char* dex2oat_cmdline =
1189 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1190 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1191 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1192 }
1193}
1194
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001195static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1196 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001197 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001198
1199 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1200 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1201
Vladimir Markod3083dd2018-05-17 08:43:47 +01001202 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001203 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1204 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001205 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001206 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1207 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1208 ArtMethod* outer_method = *caller_sp;
1209
1210 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1211 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1212 << " native pc: " << caller_pc << " Instrumented!";
1213 return;
1214 }
1215
1216 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1217 CHECK(current_code != nullptr);
1218 CHECK(current_code->IsOptimized());
1219 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +01001220 CodeInfo code_info(current_code);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001221 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
David Srbecky052f8ca2018-04-26 15:42:54 +01001222 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001223 CHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +01001224 uint32_t dex_pc = stack_map.GetDexPc();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001225
1226 // Log the outer method and its associated dex file and class table pointer which can be used
1227 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1228 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1229 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1230 << " native pc: " << caller_pc
1231 << " dex pc: " << dex_pc
1232 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1233 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001234 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001235 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1236
1237 ArtMethod* caller = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +01001238 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
1239 for (InlineInfo inline_info : inline_infos) {
1240 const char* tag = "";
1241 dex_pc = inline_info.GetDexPc();
1242 if (inline_info.EncodesArtMethod()) {
1243 tag = "encoded ";
1244 caller = inline_info.GetArtMethod();
1245 } else {
1246 uint32_t method_index = inline_info.GetMethodIndex(method_info);
1247 if (dex_pc == static_cast<uint32_t>(-1)) {
1248 tag = "special ";
1249 CHECK(inline_info.Equals(inline_infos.back()));
1250 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1251 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001252 } else {
David Srbecky93bd3612018-07-02 19:30:18 +01001253 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1254 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1255 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1256 CHECK(caller != nullptr);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001257 }
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001258 }
David Srbecky93bd3612018-07-02 19:30:18 +01001259 LOG(FATAL_WITHOUT_ABORT) << "InlineInfo #" << inline_info.Row()
1260 << ": " << tag << caller->PrettyMethod()
1261 << " dex pc: " << dex_pc
1262 << " dex file: " << caller->GetDexFile()->GetLocation()
1263 << " class table: "
1264 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1265 DumpB74410240ClassData(caller->GetDeclaringClass());
1266 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001267 }
1268}
1269
Ian Rogers848871b2013-08-05 10:56:33 -07001270// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001271extern "C" const void* artQuickResolutionTrampoline(
1272 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001273 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001274 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1275 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1276 // does not have the same stack layout as the callee-save method).
1277 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001278 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001279 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001280 ScopedObjectAccessUnchecked soa(env);
1281 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001282 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001283
1284 // Compute details about the called method (avoid GCs)
1285 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001286 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001287 MethodReference called_method(nullptr, 0);
1288 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001289 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001290 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001291 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001292 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001293
1294 InvokeType stack_map_invoke_type;
1295 uint32_t stack_map_dex_method_idx;
1296 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1297 &stack_map_invoke_type,
1298 &stack_map_dex_method_idx);
1299 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1300 // code.
1301 if (!found_stack_map || kIsDebugBuild) {
1302 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001303 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001304 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1305 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001306 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001307 bool is_range;
1308 switch (instr_code) {
1309 case Instruction::INVOKE_DIRECT:
1310 invoke_type = kDirect;
1311 is_range = false;
1312 break;
1313 case Instruction::INVOKE_DIRECT_RANGE:
1314 invoke_type = kDirect;
1315 is_range = true;
1316 break;
1317 case Instruction::INVOKE_STATIC:
1318 invoke_type = kStatic;
1319 is_range = false;
1320 break;
1321 case Instruction::INVOKE_STATIC_RANGE:
1322 invoke_type = kStatic;
1323 is_range = true;
1324 break;
1325 case Instruction::INVOKE_SUPER:
1326 invoke_type = kSuper;
1327 is_range = false;
1328 break;
1329 case Instruction::INVOKE_SUPER_RANGE:
1330 invoke_type = kSuper;
1331 is_range = true;
1332 break;
1333 case Instruction::INVOKE_VIRTUAL:
1334 invoke_type = kVirtual;
1335 is_range = false;
1336 break;
1337 case Instruction::INVOKE_VIRTUAL_RANGE:
1338 invoke_type = kVirtual;
1339 is_range = true;
1340 break;
1341 case Instruction::INVOKE_INTERFACE:
1342 invoke_type = kInterface;
1343 is_range = false;
1344 break;
1345 case Instruction::INVOKE_INTERFACE_RANGE:
1346 invoke_type = kInterface;
1347 is_range = true;
1348 break;
1349 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001350 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001351 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001352 UNREACHABLE();
1353 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001354 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001355 // Check that the invoke matches what we expected, note that this path only happens for debug
1356 // builds.
1357 if (found_stack_map) {
1358 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1359 if (invoke_type != kSuper) {
1360 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001361 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001362 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001363 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001364 }
1365 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001366 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001367 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001368 }
1369 } else {
1370 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001371 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001372 }
Ian Rogers848871b2013-08-05 10:56:33 -07001373 } else {
1374 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001375 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001376 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001377 }
1378 uint32_t shorty_len;
1379 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001380 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001381 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001382 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001383 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001384 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001385 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001386 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001387 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001388 mirror::Object* dummy = nullptr;
1389 HandleWrapper<mirror::Object> h_receiver(
1390 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001391 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001392 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001393 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001394
1395 // Update .bss entry in oat file if any.
1396 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001397 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1398 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1399 called_method.index,
1400 called_method.dex_file->NumMethodIds(),
1401 static_cast<size_t>(kRuntimePointerSize));
1402 if (bss_offset != IndexBssMappingLookup::npos) {
1403 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1404 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1405 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1406 oat_file->BssBegin() + bss_offset));
1407 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1408 DCHECK_LT(method_entry,
1409 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1410 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001411 }
1412 }
Ian Rogers848871b2013-08-05 10:56:33 -07001413 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001414 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001415 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001416 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001417 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001418 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001419 if (virtual_or_interface || invoke_type == kSuper) {
1420 // Refine called method based on receiver for kVirtual/kInterface, and
1421 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001422 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001423 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001424 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001425 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001426 } else if (invoke_type == kInterface) {
1427 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001428 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001429 } else {
1430 DCHECK_EQ(invoke_type, kSuper);
1431 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001432 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001433 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001434 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001435 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001436 } else {
1437 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001438 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001439 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001440 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001441
David Sehr709b0702016-10-13 09:12:37 -07001442 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1443 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001444 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001445 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001446
Ian Rogers848871b2013-08-05 10:56:33 -07001447 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001448 StackHandleScope<1> hs(soa.Self());
1449 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001450 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001451 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001452 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1453 // If we are single-stepping or the called method is deoptimized (by a
1454 // breakpoint, for example), then we have to execute the called method
1455 // with the interpreter.
1456 code = GetQuickToInterpreterBridge();
1457 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1458 // If the caller is deoptimized (by a breakpoint, for example), we have to
1459 // continue its execution with interpreter when returning from the called
1460 // method. Because we do not want to execute the called method with the
1461 // interpreter, we wrap its execution into the instrumentation stubs.
1462 // When the called method returns, it will execute the instrumentation
1463 // exit hook that will determine the need of the interpreter with a call
1464 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1465 // it is needed.
1466 code = GetQuickInstrumentationEntryPoint();
1467 } else {
1468 code = called->GetEntryPointFromQuickCompiledCode();
1469 }
Ian Rogers848871b2013-08-05 10:56:33 -07001470 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001471 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1472 // If we are single-stepping or the called method is deoptimized (by a
1473 // breakpoint, for example), then we have to execute the called method
1474 // with the interpreter.
1475 code = GetQuickToInterpreterBridge();
1476 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001477 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1478 // until class is initialized to stop races between threads).
1479 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001480 } else {
1481 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001482 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001483 }
1484 } else {
1485 DCHECK(called_class->IsErroneous());
1486 }
1487 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001488 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001489 // Fixup any locally saved objects may have moved during a GC.
1490 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001491 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001492 *sp = called;
1493
Ian Rogers848871b2013-08-05 10:56:33 -07001494 return code;
1495}
1496
Andreas Gampec147b002014-03-06 18:11:06 -08001497/*
1498 * This class uses a couple of observations to unite the different calling conventions through
1499 * a few constants.
1500 *
1501 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1502 * possible alignment.
1503 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1504 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1505 * when we have to split things
1506 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1507 * and we can use Int handling directly.
1508 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1509 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1510 * extension should be compatible with Aarch64, which mandates copying the available bits
1511 * into LSB and leaving the rest unspecified.
1512 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1513 * the stack.
1514 * 6) There is only little endian.
1515 *
1516 *
1517 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1518 * follows:
1519 *
1520 * void PushGpr(uintptr_t): Add a value for the next GPR
1521 *
1522 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1523 * padding, that is, think the architecture is 32b and aligns 64b.
1524 *
1525 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1526 * split this if necessary. The current state will have aligned, if
1527 * necessary.
1528 *
1529 * void PushStack(uintptr_t): Push a value to the stack.
1530 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001531 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001532 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001533 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001534 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001535 *
1536 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001537template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001538 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001539#if defined(__arm__)
1540 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001541 static constexpr bool kNativeSoftFloatAbi = true;
1542 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001543 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1544
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001545 static constexpr size_t kRegistersNeededForLong = 2;
1546 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001547 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001548 static constexpr bool kMultiFPRegistersWidened = false;
1549 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001550 static constexpr bool kAlignLongOnStack = true;
1551 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001552#elif defined(__aarch64__)
1553 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1554 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1555 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1556
1557 static constexpr size_t kRegistersNeededForLong = 1;
1558 static constexpr size_t kRegistersNeededForDouble = 1;
1559 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001560 static constexpr bool kMultiFPRegistersWidened = false;
1561 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001562 static constexpr bool kAlignLongOnStack = false;
1563 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001564#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001565 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001566 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1567 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001568
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001569 static constexpr size_t kRegistersNeededForLong = 2;
1570 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001571 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001572 static constexpr bool kMultiFPRegistersWidened = true;
1573 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001574 static constexpr bool kAlignLongOnStack = true;
1575 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001576#elif defined(__mips__) && defined(__LP64__)
1577 // Let the code prepare GPRs only and we will load the FPRs with same data.
1578 static constexpr bool kNativeSoftFloatAbi = true;
1579 static constexpr size_t kNumNativeGprArgs = 8;
1580 static constexpr size_t kNumNativeFprArgs = 0;
1581
1582 static constexpr size_t kRegistersNeededForLong = 1;
1583 static constexpr size_t kRegistersNeededForDouble = 1;
1584 static constexpr bool kMultiRegistersAligned = false;
1585 static constexpr bool kMultiFPRegistersWidened = false;
1586 static constexpr bool kMultiGPRegistersWidened = true;
1587 static constexpr bool kAlignLongOnStack = false;
1588 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001589#elif defined(__i386__)
1590 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001591 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001592 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1593 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1594
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001595 static constexpr size_t kRegistersNeededForLong = 2;
1596 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001597 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001598 static constexpr bool kMultiFPRegistersWidened = false;
1599 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001600 static constexpr bool kAlignLongOnStack = false;
1601 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001602#elif defined(__x86_64__)
1603 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1604 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1605 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1606
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001607 static constexpr size_t kRegistersNeededForLong = 1;
1608 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001609 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001610 static constexpr bool kMultiFPRegistersWidened = false;
1611 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001612 static constexpr bool kAlignLongOnStack = false;
1613 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001614#else
1615#error "Unsupported architecture"
1616#endif
1617
Andreas Gampec147b002014-03-06 18:11:06 -08001618 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001619 explicit BuildNativeCallFrameStateMachine(T* delegate)
1620 : gpr_index_(kNumNativeGprArgs),
1621 fpr_index_(kNumNativeFprArgs),
1622 stack_entries_(0),
1623 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001624 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1625 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001626 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1627 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001628 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001629
Andreas Gampec200a4a2014-06-16 18:39:09 -07001630 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001631
Ian Rogers1428dce2014-10-21 15:02:15 -07001632 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001633 return gpr_index_ > 0;
1634 }
1635
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001637 if (HavePointerGpr()) {
1638 gpr_index_--;
1639 PushGpr(reinterpret_cast<uintptr_t>(val));
1640 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001641 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001642 PushStack(reinterpret_cast<uintptr_t>(val));
1643 gpr_index_ = 0;
1644 }
1645 }
1646
Ian Rogers1428dce2014-10-21 15:02:15 -07001647 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001648 return gpr_index_ > 0;
1649 }
1650
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001651 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001652 uintptr_t handle = PushHandle(ptr);
1653 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001654 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001655 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001656 } else {
1657 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001658 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001659 gpr_index_ = 0;
1660 }
1661 }
1662
Ian Rogers1428dce2014-10-21 15:02:15 -07001663 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001664 return gpr_index_ > 0;
1665 }
1666
1667 void AdvanceInt(uint32_t val) {
1668 if (HaveIntGpr()) {
1669 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001670 if (kMultiGPRegistersWidened) {
1671 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001672 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001673 } else {
1674 PushGpr(val);
1675 }
Andreas Gampec147b002014-03-06 18:11:06 -08001676 } else {
1677 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001678 if (kMultiGPRegistersWidened) {
1679 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001680 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001681 } else {
1682 PushStack(val);
1683 }
Andreas Gampec147b002014-03-06 18:11:06 -08001684 gpr_index_ = 0;
1685 }
1686 }
1687
Ian Rogers1428dce2014-10-21 15:02:15 -07001688 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001689 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1690 }
1691
Ian Rogers1428dce2014-10-21 15:02:15 -07001692 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001693 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1694 kAlignLongOnStack && // and when it needs alignment
1695 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1696 }
1697
Ian Rogers1428dce2014-10-21 15:02:15 -07001698 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001699 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1700 kAlignLongOnStack && // and when it needs 8B alignment
1701 (stack_entries_ & 1) == 1; // counter is odd
1702 }
1703
1704 void AdvanceLong(uint64_t val) {
1705 if (HaveLongGpr()) {
1706 if (LongGprNeedsPadding()) {
1707 PushGpr(0);
1708 gpr_index_--;
1709 }
1710 if (kRegistersNeededForLong == 1) {
1711 PushGpr(static_cast<uintptr_t>(val));
1712 } else {
1713 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1714 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1715 }
1716 gpr_index_ -= kRegistersNeededForLong;
1717 } else {
1718 if (LongStackNeedsPadding()) {
1719 PushStack(0);
1720 stack_entries_++;
1721 }
1722 if (kRegistersNeededForLong == 1) {
1723 PushStack(static_cast<uintptr_t>(val));
1724 stack_entries_++;
1725 } else {
1726 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1727 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1728 stack_entries_ += 2;
1729 }
1730 gpr_index_ = 0;
1731 }
1732 }
1733
Ian Rogers1428dce2014-10-21 15:02:15 -07001734 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001735 return fpr_index_ > 0;
1736 }
1737
Andreas Gampec147b002014-03-06 18:11:06 -08001738 void AdvanceFloat(float val) {
1739 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001740 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001741 } else {
1742 if (HaveFloatFpr()) {
1743 fpr_index_--;
1744 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001745 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001746 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001747 } else {
1748 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001749 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001750 }
1751 } else {
1752 PushFpr4(val);
1753 }
1754 } else {
1755 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001756 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001757 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001758 // Note: We need to jump through those hoops to make the compiler happy.
1759 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001760 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001761 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001762 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001763 }
1764 fpr_index_ = 0;
1765 }
1766 }
1767 }
1768
Ian Rogers1428dce2014-10-21 15:02:15 -07001769 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001770 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1771 }
1772
Ian Rogers1428dce2014-10-21 15:02:15 -07001773 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001774 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1775 kAlignDoubleOnStack && // and when it needs alignment
1776 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1777 }
1778
Ian Rogers1428dce2014-10-21 15:02:15 -07001779 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001780 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1781 kAlignDoubleOnStack && // and when it needs 8B alignment
1782 (stack_entries_ & 1) == 1; // counter is odd
1783 }
1784
1785 void AdvanceDouble(uint64_t val) {
1786 if (kNativeSoftFloatAbi) {
1787 AdvanceLong(val);
1788 } else {
1789 if (HaveDoubleFpr()) {
1790 if (DoubleFprNeedsPadding()) {
1791 PushFpr4(0);
1792 fpr_index_--;
1793 }
1794 PushFpr8(val);
1795 fpr_index_ -= kRegistersNeededForDouble;
1796 } else {
1797 if (DoubleStackNeedsPadding()) {
1798 PushStack(0);
1799 stack_entries_++;
1800 }
1801 if (kRegistersNeededForDouble == 1) {
1802 PushStack(static_cast<uintptr_t>(val));
1803 stack_entries_++;
1804 } else {
1805 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1806 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1807 stack_entries_ += 2;
1808 }
1809 fpr_index_ = 0;
1810 }
1811 }
1812 }
1813
Ian Rogers1428dce2014-10-21 15:02:15 -07001814 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001815 return stack_entries_;
1816 }
1817
Ian Rogers1428dce2014-10-21 15:02:15 -07001818 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001819 return kNumNativeGprArgs - gpr_index_;
1820 }
1821
Ian Rogers1428dce2014-10-21 15:02:15 -07001822 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001823 return kNumNativeFprArgs - fpr_index_;
1824 }
1825
1826 private:
1827 void PushGpr(uintptr_t val) {
1828 delegate_->PushGpr(val);
1829 }
1830 void PushFpr4(float val) {
1831 delegate_->PushFpr4(val);
1832 }
1833 void PushFpr8(uint64_t val) {
1834 delegate_->PushFpr8(val);
1835 }
1836 void PushStack(uintptr_t val) {
1837 delegate_->PushStack(val);
1838 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001839 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001840 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001841 }
1842
1843 uint32_t gpr_index_; // Number of free GPRs
1844 uint32_t fpr_index_; // Number of free FPRs
1845 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1846 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001847 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001848};
1849
Andreas Gampec200a4a2014-06-16 18:39:09 -07001850// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1851// in subclasses.
1852//
1853// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1854// them with handles.
1855class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001856 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001857 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1858
1859 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001860
Ian Rogers1428dce2014-10-21 15:02:15 -07001861 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001862 return num_stack_entries_ * sizeof(uintptr_t);
1863 }
1864
Ian Rogers1428dce2014-10-21 15:02:15 -07001865 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001866 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001867 // Align by kStackAlignment.
1868 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001869 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001870 }
1871
Ian Rogers1428dce2014-10-21 15:02:15 -07001872 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1873 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874 // Assumption is OK right now, as we have soft-float arm
1875 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1876 sp8 -= fregs * sizeof(uintptr_t);
1877 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1878 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1879 sp8 -= iregs * sizeof(uintptr_t);
1880 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1881 return sp8;
1882 }
Andreas Gampec147b002014-03-06 18:11:06 -08001883
Andreas Gampec200a4a2014-06-16 18:39:09 -07001884 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001885 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001886 // Native call stack.
1887 sp8 = LayoutCallStack(sp8);
1888 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890 // Put fprs and gprs below.
1891 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001892
Andreas Gampec200a4a2014-06-16 18:39:09 -07001893 // Return the new bottom.
1894 return sp8;
1895 }
1896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897 virtual void WalkHeader(
1898 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001899 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001900 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001901
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001902 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001903 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1904
1905 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001906
1907 for (uint32_t i = 1; i < shorty_len; ++i) {
1908 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1909 switch (cur_type_) {
1910 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001911 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001912 sm.AdvanceHandleScope(
1913 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001914 break;
1915
1916 case Primitive::kPrimBoolean:
1917 case Primitive::kPrimByte:
1918 case Primitive::kPrimChar:
1919 case Primitive::kPrimShort:
1920 case Primitive::kPrimInt:
1921 sm.AdvanceInt(0);
1922 break;
1923 case Primitive::kPrimFloat:
1924 sm.AdvanceFloat(0);
1925 break;
1926 case Primitive::kPrimDouble:
1927 sm.AdvanceDouble(0);
1928 break;
1929 case Primitive::kPrimLong:
1930 sm.AdvanceLong(0);
1931 break;
1932 default:
1933 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001934 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001935 }
1936 }
1937
Ian Rogers1428dce2014-10-21 15:02:15 -07001938 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001939 }
1940
1941 void PushGpr(uintptr_t /* val */) {
1942 // not optimizing registers, yet
1943 }
1944
1945 void PushFpr4(float /* val */) {
1946 // not optimizing registers, yet
1947 }
1948
1949 void PushFpr8(uint64_t /* val */) {
1950 // not optimizing registers, yet
1951 }
1952
1953 void PushStack(uintptr_t /* val */) {
1954 // counting is already done in the superclass
1955 }
1956
Andreas Gampec200a4a2014-06-16 18:39:09 -07001957 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001958 return reinterpret_cast<uintptr_t>(nullptr);
1959 }
1960
Andreas Gampec200a4a2014-06-16 18:39:09 -07001961 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001962 uint32_t num_stack_entries_;
1963};
1964
Andreas Gampec200a4a2014-06-16 18:39:09 -07001965class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001966 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001967 explicit ComputeGenericJniFrameSize(bool critical_native)
1968 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001969
Andreas Gampec200a4a2014-06-16 18:39:09 -07001970 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1971 // is at *m = sp. Will update to point to the bottom of the save frame.
1972 //
1973 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001974 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001975 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001976 ArtMethod* method = **m;
1977
Andreas Gampe542451c2016-07-26 09:02:02 -07001978 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001979
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1981
1982 // First, fix up the layout of the callee-save frame.
1983 // We have to squeeze in the HandleScope, and relocate the method pointer.
1984
1985 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001986 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987
1988 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001989 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001991
1992 sp8 -= scope_and_method;
1993 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001994 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001995
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001997 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1998 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001999
2000 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2001 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002002 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2003 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002004 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002005 }
2006
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002008 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009 // Reference cookie and padding
2010 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002011 }
2012
Andreas Gampec200a4a2014-06-16 18:39:09 -07002013 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2014 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002016 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002017 // First, fix up the layout of the callee-save frame.
2018 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002019 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002020
2021 // The bottom of the callee-save frame is now where the method is, *m.
2022 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2023
2024 // Add space for cookie.
2025 LayoutCookie(&sp8);
2026
2027 return sp8;
2028 }
2029
2030 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2032 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2033 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002034 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002035 Walk(shorty, shorty_len);
2036
2037 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002038 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002039
2040 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2041
2042 // Return the new bottom.
2043 return sp8;
2044 }
2045
2046 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2047
2048 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2049 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002050 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002051
2052 private:
2053 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002054 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002055};
2056
2057uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2058 num_handle_scope_references_++;
2059 return reinterpret_cast<uintptr_t>(nullptr);
2060}
2061
2062void ComputeGenericJniFrameSize::WalkHeader(
2063 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002064 // First 2 parameters are always excluded for @CriticalNative.
2065 if (UNLIKELY(critical_native_)) {
2066 return;
2067 }
2068
Andreas Gampec200a4a2014-06-16 18:39:09 -07002069 // JNIEnv
2070 sm->AdvancePointer(nullptr);
2071
2072 // Class object or this as first argument
2073 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2074}
2075
2076// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2077// the template requirements of BuildGenericJniFrameStateMachine.
2078class FillNativeCall {
2079 public:
2080 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2081 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2082
2083 virtual ~FillNativeCall() {}
2084
2085 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2086 cur_gpr_reg_ = gpr_regs;
2087 cur_fpr_reg_ = fpr_regs;
2088 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002089 }
2090
2091 void PushGpr(uintptr_t val) {
2092 *cur_gpr_reg_ = val;
2093 cur_gpr_reg_++;
2094 }
2095
2096 void PushFpr4(float val) {
2097 *cur_fpr_reg_ = val;
2098 cur_fpr_reg_++;
2099 }
2100
2101 void PushFpr8(uint64_t val) {
2102 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2103 *tmp = val;
2104 cur_fpr_reg_ += 2;
2105 }
2106
2107 void PushStack(uintptr_t val) {
2108 *cur_stack_arg_ = val;
2109 cur_stack_arg_++;
2110 }
2111
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002112 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002113 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002114 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002115 }
2116
2117 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002118 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002119 uint32_t* cur_fpr_reg_;
2120 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002121};
Andreas Gampec147b002014-03-06 18:11:06 -08002122
Andreas Gampec200a4a2014-06-16 18:39:09 -07002123// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2124// of transitioning into native code.
2125class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2126 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002127 BuildGenericJniFrameVisitor(Thread* self,
2128 bool is_static,
2129 bool critical_native,
2130 const char* shorty,
2131 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002132 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002133 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002134 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2135 sm_(&jni_call_) {
2136 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002137 uintptr_t* start_gpr_reg;
2138 uint32_t* start_fpr_reg;
2139 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002140 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002141 &handle_scope_,
2142 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002143 &start_gpr_reg, &start_fpr_reg);
2144
Andreas Gampec200a4a2014-06-16 18:39:09 -07002145 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2146
Igor Murashkin06a04e02016-09-13 15:57:37 -07002147 // First 2 parameters are always excluded for CriticalNative methods.
2148 if (LIKELY(!critical_native)) {
2149 // jni environment is always first argument
2150 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002151
Igor Murashkin06a04e02016-09-13 15:57:37 -07002152 if (is_static) {
2153 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2154 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002155 }
2156 }
2157
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002158 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002159
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002160 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002161
Vladimir Markof39745e2016-01-26 12:16:55 +00002162 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002163 return handle_scope_->GetHandle(0).GetReference();
2164 }
2165
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002166 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002167 return handle_scope_->GetHandle(0).ToJObject();
2168 }
2169
Ian Rogers1428dce2014-10-21 15:02:15 -07002170 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002171 return bottom_of_used_area_;
2172 }
2173
2174 private:
2175 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2176 class FillJniCall FINAL : public FillNativeCall {
2177 public:
2178 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002179 HandleScope* handle_scope, bool critical_native)
2180 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2181 handle_scope_(handle_scope),
2182 cur_entry_(0),
2183 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002184
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002185 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002186
2187 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2188 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2189 handle_scope_ = scope;
2190 cur_entry_ = 0U;
2191 }
2192
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002193 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002194 // Initialize padding entries.
2195 size_t expected_slots = handle_scope_->NumberOfReferences();
2196 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002197 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002198 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002199
2200 if (!critical_native_) {
2201 // Non-critical natives have at least the self class (jclass) or this (jobject).
2202 DCHECK_NE(cur_entry_, 0U);
2203 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002204 }
2205
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002206 bool CriticalNative() const {
2207 return critical_native_;
2208 }
2209
Andreas Gampec200a4a2014-06-16 18:39:09 -07002210 private:
2211 HandleScope* handle_scope_;
2212 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002213 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002214 };
2215
2216 HandleScope* handle_scope_;
2217 FillJniCall jni_call_;
2218 void* bottom_of_used_area_;
2219
2220 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002221
2222 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2223};
2224
Andreas Gampec200a4a2014-06-16 18:39:09 -07002225uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2226 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002227 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002228 h.Assign(ref);
2229 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2230 cur_entry_++;
2231 return tmp;
2232}
2233
Ian Rogers9758f792014-03-13 09:02:55 -07002234void BuildGenericJniFrameVisitor::Visit() {
2235 Primitive::Type type = GetParamPrimitiveType();
2236 switch (type) {
2237 case Primitive::kPrimLong: {
2238 jlong long_arg;
2239 if (IsSplitLongOrDouble()) {
2240 long_arg = ReadSplitLongParam();
2241 } else {
2242 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2243 }
2244 sm_.AdvanceLong(long_arg);
2245 break;
2246 }
2247 case Primitive::kPrimDouble: {
2248 uint64_t double_arg;
2249 if (IsSplitLongOrDouble()) {
2250 // Read into union so that we don't case to a double.
2251 double_arg = ReadSplitLongParam();
2252 } else {
2253 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2254 }
2255 sm_.AdvanceDouble(double_arg);
2256 break;
2257 }
2258 case Primitive::kPrimNot: {
2259 StackReference<mirror::Object>* stack_ref =
2260 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002261 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002262 break;
2263 }
2264 case Primitive::kPrimFloat:
2265 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2266 break;
2267 case Primitive::kPrimBoolean: // Fall-through.
2268 case Primitive::kPrimByte: // Fall-through.
2269 case Primitive::kPrimChar: // Fall-through.
2270 case Primitive::kPrimShort: // Fall-through.
2271 case Primitive::kPrimInt: // Fall-through.
2272 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2273 break;
2274 case Primitive::kPrimVoid:
2275 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002276 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002277 }
2278}
2279
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002280void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002281 // Clear out rest of the scope.
2282 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002283 if (!jni_call_.CriticalNative()) {
2284 // Install HandleScope.
2285 self->PushHandleScope(handle_scope_);
2286 }
Ian Rogers9758f792014-03-13 09:02:55 -07002287}
2288
Ian Rogers04c31d22014-07-07 21:44:06 -07002289#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002290extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002291#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002292extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002293#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002294
Igor Murashkin06a04e02016-09-13 15:57:37 -07002295static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2296 uint32_t cookie,
2297 bool fast_native ATTRIBUTE_UNUSED,
2298 jobject l,
2299 jobject lock) {
2300 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002301 if (lock != nullptr) {
2302 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2303 } else {
2304 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2305 }
2306}
2307
Igor Murashkin06a04e02016-09-13 15:57:37 -07002308static void artQuickGenericJniEndJNINonRef(Thread* self,
2309 uint32_t cookie,
2310 bool fast_native,
2311 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002312 if (lock != nullptr) {
2313 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002314 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002315 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002316 if (UNLIKELY(fast_native)) {
2317 JniMethodFastEnd(cookie, self);
2318 } else {
2319 JniMethodEnd(cookie, self);
2320 }
Andreas Gampead615172014-04-04 16:20:13 -07002321 }
2322}
2323
Andreas Gampec147b002014-03-06 18:11:06 -08002324/*
2325 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002326 * 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 -08002327 * The final element on the stack is a pointer to the native code.
2328 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002329 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002330 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002331 *
Andreas Gampec147b002014-03-06 18:11:06 -08002332 * The return of this function denotes:
2333 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2334 * 2) An error, if the value is negative.
2335 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002337 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002338 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002340 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002341 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002342 uint32_t shorty_len = 0;
2343 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002344 bool critical_native = called->IsCriticalNative();
2345 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002346 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002347
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002348 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002349 BuildGenericJniFrameVisitor visitor(self,
2350 called->IsStatic(),
2351 critical_native,
2352 shorty,
2353 shorty_len,
2354 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002355 {
2356 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2357 visitor.VisitArguments();
2358 // FinalizeHandleScope pushes the handle scope on the thread.
2359 visitor.FinalizeHandleScope(self);
2360 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002361
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002362 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002363 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002364
Ian Rogerse0dcd462014-03-08 15:21:04 -08002365 self->VerifyStack();
2366
Vladimir Markof8655b32018-03-21 17:53:56 +00002367 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2368 jit::Jit* jit = runtime->GetJit();
2369 if (jit != nullptr) {
2370 jit->MethodEntered(self, called);
2371 }
2372
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002373 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002374 uint32_t* sp32;
2375 // Skip calling JniMethodStart for @CriticalNative.
2376 if (LIKELY(!critical_native)) {
2377 // Start JNI, save the cookie.
2378 if (called->IsSynchronized()) {
2379 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2380 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2381 if (self->IsExceptionPending()) {
2382 self->PopHandleScope();
2383 // A negative value denotes an error.
2384 return GetTwoWordFailureValue();
2385 }
2386 } else {
2387 if (fast_native) {
2388 cookie = JniMethodFastStart(self);
2389 } else {
2390 DCHECK(normal_native);
2391 cookie = JniMethodStart(self);
2392 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002393 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002394 sp32 = reinterpret_cast<uint32_t*>(sp);
2395 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002396 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002397
Andreas Gampe90546832014-03-12 18:07:19 -07002398 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002399 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002400
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002401 // There are two cases for the content of nativeCode:
2402 // 1) Pointer to the native function.
2403 // 2) Pointer to the trampoline for native code binding.
2404 // In the second case, we need to execute the binding and continue with the actual native function
2405 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002406 DCHECK(nativeCode != nullptr);
2407 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002408#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002409 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002410#else
2411 nativeCode = artFindNativeMethod(self);
2412#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002413
2414 if (nativeCode == nullptr) {
2415 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002416
Igor Murashkin06a04e02016-09-13 15:57:37 -07002417 // @CriticalNative calls do not need to call back into JniMethodEnd.
2418 if (LIKELY(!critical_native)) {
2419 // End JNI, as the assembly will move to deliver the exception.
2420 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2421 if (shorty[0] == 'L') {
2422 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2423 } else {
2424 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2425 }
Andreas Gampead615172014-04-04 16:20:13 -07002426 }
2427
Andreas Gampec200a4a2014-06-16 18:39:09 -07002428 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002429 }
2430 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002431 }
2432
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002433#if defined(__mips__) && !defined(__LP64__)
2434 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2435 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2436 // and load into floating-point registers.
2437 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2438 // view):
2439 // (1)
2440 // | DOUBLE | DOUBLE | other args, if any
2441 // | F12 | F13 | F14 | F15 |
2442 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2443 // (2)
2444 // | DOUBLE | FLOAT | (PAD) | other args, if any
2445 // | F12 | F13 | F14 | |
2446 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2447 // (3)
2448 // | FLOAT | (PAD) | DOUBLE | other args, if any
2449 // | F12 | | F14 | F15 |
2450 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2451 // (4)
2452 // | FLOAT | FLOAT | other args, if any
2453 // | F12 | F14 |
2454 // | SP+0 | SP+4 | SP+8
2455 // As you can see, only the last case (4) is special. In all others we can just
2456 // load F12/F13 and F14/F15 in the same manner.
2457 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2458 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2459 if (nativeCode != nullptr &&
2460 shorty != nullptr &&
2461 shorty_len >= 3 &&
2462 shorty[1] == 'F' &&
2463 shorty[2] == 'F') {
2464 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2465 }
2466#endif
2467
Andreas Gampec200a4a2014-06-16 18:39:09 -07002468 // Return native code addr(lo) and bottom of alloca address(hi).
2469 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2470 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002471}
2472
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002473// Defined in quick_jni_entrypoints.cc.
2474extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2475 jvalue result, uint64_t result_f, ArtMethod* called,
2476 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002477/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002478 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002479 * unlocking.
2480 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002481extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2482 jvalue result,
2483 uint64_t result_f) {
2484 // We're here just back from a native call. We don't have the shared mutator lock at this point
2485 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2486 // anything that requires a mutator lock before that would cause problems as GC may have the
2487 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002488 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002489 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002490 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002491 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002492 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002493 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2494 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002495}
2496
Andreas Gamped58342c2014-06-05 14:18:08 -07002497// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2498// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002499//
Andreas Gamped58342c2014-06-05 14:18:08 -07002500// 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 -07002501// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002502
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002503template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002504static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2505 ObjPtr<mirror::Object> this_object,
2506 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002507 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002508 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002509 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002510 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002511 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002512 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002513 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2514 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002515 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002516 {
2517 // Remember the args in case a GC happens in FindMethodFromCode.
2518 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2519 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2520 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002521 method = FindMethodFromCode<type, access_check>(method_idx,
2522 &this_object,
2523 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002524 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002525 visitor.FixupReferences();
2526 }
2527
Ian Rogerse0a02da2014-12-02 14:10:53 -08002528 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002529 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002530 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002531 }
2532 }
2533 DCHECK(!self->IsExceptionPending());
2534 const void* code = method->GetEntryPointFromQuickCompiledCode();
2535
2536 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002537 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002538 << " location: "
2539 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002540
Andreas Gamped58342c2014-06-05 14:18:08 -07002541 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2542 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002543}
2544
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002545// Explicit artInvokeCommon template function declarations to please analysis tool.
2546#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002547 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002548 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002549 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002550
2551EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2552EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2553EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2554EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2555EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2556EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2557EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2558EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2559EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2560EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2561#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2562
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002563// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002564extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002565 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002566 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002567 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002568}
2569
Andreas Gampec200a4a2014-06-16 18:39:09 -07002570extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002571 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002572 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002573 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002574}
2575
Andreas Gampec200a4a2014-06-16 18:39:09 -07002576extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002577 uint32_t method_idx,
2578 mirror::Object* this_object ATTRIBUTE_UNUSED,
2579 Thread* self,
2580 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2581 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2582 // it doesn't cause ObjPtr alignment failure check.
2583 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002584}
2585
Andreas Gampec200a4a2014-06-16 18:39:09 -07002586extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002587 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002588 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002589 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002590}
2591
Andreas Gampec200a4a2014-06-16 18:39:09 -07002592extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002593 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002594 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002595 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002596}
2597
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002598// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2599extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2600 REQUIRES_SHARED(Locks::mutator_lock_) {
2601 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2602 DCHECK(!referrer->IsProxyMethod());
2603 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2604 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2605 DCHECK(result == nullptr ||
2606 result->GetDeclaringClass()->IsInterface() ||
2607 result->GetDeclaringClass() ==
2608 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2609 << result->PrettyMethod();
2610 return result;
2611}
2612
Jeff Hao5667f562017-02-27 19:32:01 -08002613// Determine target of interface dispatch. The interface method and this object are known non-null.
2614// The interface method is the method returned by the dex cache in the conflict trampoline.
2615extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002616 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002617 Thread* self,
2618 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002619 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002620 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002621 StackHandleScope<2> hs(self);
2622 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2623 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002624
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002625 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002626 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002627 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002628
Vladimir Marko302f69c2017-07-25 15:27:15 +01002629 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002630 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002631 // Fetch the dex_method_idx of the target interface method from the caller.
2632 uint32_t dex_method_idx;
2633 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002634 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002635 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002636 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2637 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002638 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002639 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002640 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002641 } else {
2642 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002643 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002644 }
2645
Vladimir Marko302f69c2017-07-25 15:27:15 +01002646 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002647 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002648 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2649 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002650 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002651 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002652 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2653 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2654 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002655 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2656 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2657 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002658 visitor.FixupReferences();
2659 }
2660
Vladimir Marko302f69c2017-07-25 15:27:15 +01002661 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002662 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002663 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002664 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002665 }
2666
2667 DCHECK(!interface_method->IsRuntimeMethod());
2668 // Look whether we have a match in the ImtConflictTable.
2669 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2670 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2671 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2672 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2673 DCHECK(current_table != nullptr);
2674 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2675 } else {
2676 // It seems we aren't really a conflict method!
2677 if (kIsDebugBuild) {
2678 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2679 CHECK_EQ(conflict_method, m)
2680 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2681 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2682 }
2683 method = conflict_method;
2684 }
2685 if (method != nullptr) {
2686 return GetTwoWordSuccessValue(
2687 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2688 reinterpret_cast<uintptr_t>(method));
2689 }
2690
2691 // No match, use the IfTable.
2692 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2693 if (UNLIKELY(method == nullptr)) {
2694 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2695 interface_method, this_object.Get(), caller_method);
2696 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002697 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002698
2699 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2700 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002701 DCHECK(conflict_method->IsRuntimeMethod());
2702 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2703 cls.Get(),
2704 conflict_method,
2705 interface_method,
2706 method,
2707 /*force_new_conflict_method*/false);
2708 if (new_conflict_method != conflict_method) {
2709 // Update the IMT if we create a new conflict method. No fence needed here, as the
2710 // data is consistent.
2711 imt->Set(imt_index,
2712 new_conflict_method,
2713 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002714 }
2715
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002716 const void* code = method->GetEntryPointFromQuickCompiledCode();
2717
2718 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002719 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002720 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002721
Andreas Gamped58342c2014-06-05 14:18:08 -07002722 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2723 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002724}
2725
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002726// Returns uint64_t representing raw bits from JValue.
2727extern "C" uint64_t artInvokePolymorphic(mirror::Object* raw_receiver, Thread* self, ArtMethod** sp)
Orion Hodsonac141392017-01-13 11:53:47 +00002728 REQUIRES_SHARED(Locks::mutator_lock_) {
2729 ScopedQuickEntrypointChecks sqec(self);
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002730 DCHECK(raw_receiver != nullptr);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002731 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002732
2733 // Start new JNI local reference state
2734 JNIEnvExt* env = self->GetJniEnv();
2735 ScopedObjectAccessUnchecked soa(env);
2736 ScopedJniEnvLocalRefState env_state(env);
2737 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2738
2739 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2740 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2741 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002742 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002743 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2744 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002745 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002746 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002747 const size_t shorty_length = strlen(shorty);
2748 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2749 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002750 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002751
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002752 // Wrap raw_receiver in a Handle for safety.
2753 StackHandleScope<3> hs(self);
2754 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2755 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002756 self->EndAssertNoThreadSuspension(old_cause);
2757
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002758 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002759 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002760 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002761 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002762
Orion Hodsone7732be2017-10-11 14:35:20 +01002763 Handle<mirror::MethodType> method_type(
2764 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
Orion Hodsonac141392017-01-13 11:53:47 +00002765 if (UNLIKELY(method_type.IsNull())) {
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002766 // This implies we couldn't resolve one or more types in this method handle.
Orion Hodsonac141392017-01-13 11:53:47 +00002767 CHECK(self->IsExceptionPending());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002768 return 0UL;
Orion Hodsonac141392017-01-13 11:53:47 +00002769 }
2770
Vladimir Markod7559b72017-09-28 13:50:37 +01002771 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002772 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2773
2774 // Fix references before constructing the shadow frame.
2775 gc_visitor.FixupReferences();
2776
2777 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002778 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2779 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002780 const size_t first_arg = 0;
2781 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2782 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2783 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2784 ScopedStackedShadowFramePusher
2785 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2786 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2787 kMethodIsStatic,
2788 shorty,
2789 strlen(shorty),
2790 shadow_frame,
2791 first_arg);
2792 shadow_frame_builder.VisitArguments();
2793
2794 // Push a transition back into managed code onto the linked list in thread.
2795 ManagedStack fragment;
2796 self->PushManagedStackFragment(&fragment);
2797
2798 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2799 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002800 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002801 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002802 JValue result;
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002803 bool success = false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002804 if (resolved_method->GetDeclaringClass() == GetClassRoot<mirror::MethodHandle>(linker)) {
Orion Hodson537a4fe2018-05-15 13:57:58 +01002805 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2806 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2807 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2808 success = MethodHandleInvokeExact(self,
2809 *shadow_frame,
2810 method_handle,
2811 method_type,
2812 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002813 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002814 } else {
2815 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2816 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2817 success = MethodHandleInvoke(self,
2818 *shadow_frame,
2819 method_handle,
2820 method_type,
2821 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002822 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002823 }
2824 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002825 DCHECK_EQ(GetClassRoot<mirror::VarHandle>(linker), resolved_method->GetDeclaringClass());
Orion Hodson537a4fe2018-05-15 13:57:58 +01002826 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
2827 ObjPtr<mirror::VarHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2828 mirror::VarHandle::AccessMode access_mode =
2829 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2830 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002831 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002832 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002833 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002834 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002835 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002836 &result);
Orion Hodsonac141392017-01-13 11:53:47 +00002837 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002838
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002839 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002840
2841 // Pop transition record.
2842 self->PopManagedStackFragment(fragment);
2843
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002844 return result.GetJ();
Orion Hodsonac141392017-01-13 11:53:47 +00002845}
2846
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002847// Returns uint64_t representing raw bits from JValue.
2848extern "C" uint64_t artInvokeCustom(uint32_t call_site_idx, Thread* self, ArtMethod** sp)
2849 REQUIRES_SHARED(Locks::mutator_lock_) {
2850 ScopedQuickEntrypointChecks sqec(self);
2851 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
2852
2853 // invoke-custom is effectively a static call (no receiver).
2854 static constexpr bool kMethodIsStatic = true;
2855
2856 // Start new JNI local reference state
2857 JNIEnvExt* env = self->GetJniEnv();
2858 ScopedObjectAccessUnchecked soa(env);
2859 ScopedJniEnvLocalRefState env_state(env);
2860
2861 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2862
2863 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2864 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2865 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2866 const DexFile* dex_file = caller_method->GetDexFile();
2867 const dex::ProtoIndex proto_idx(dex_file->GetProtoIndexForCallSite(call_site_idx));
2868 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
2869 const uint32_t shorty_len = strlen(shorty);
2870
2871 // Construct the shadow frame placing arguments consecutively from |first_arg|.
2872 const size_t first_arg = 0;
2873 const size_t num_vregs = ArtMethod::NumArgRegisters(shorty);
2874 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2875 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, caller_method, dex_pc);
2876 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2877 ScopedStackedShadowFramePusher
2878 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2879 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2880 kMethodIsStatic,
2881 shorty,
2882 shorty_len,
2883 shadow_frame,
2884 first_arg);
2885 shadow_frame_builder.VisitArguments();
2886
2887 // Push a transition back into managed code onto the linked list in thread.
2888 ManagedStack fragment;
2889 self->PushManagedStackFragment(&fragment);
2890 self->EndAssertNoThreadSuspension(old_cause);
2891
2892 // Perform the invoke-custom operation.
2893 RangeInstructionOperands operands(first_arg, num_vregs);
2894 JValue result;
2895 bool success =
2896 interpreter::DoInvokeCustom(self, *shadow_frame, call_site_idx, &operands, &result);
2897 DCHECK(success || self->IsExceptionPending());
2898
2899 // Pop transition record.
2900 self->PopManagedStackFragment(fragment);
2901
2902 return result.GetJ();
2903}
2904
Ian Rogers848871b2013-08-05 10:56:33 -07002905} // namespace art