blob: d0aec0309db42ae30d7d9d068d2ab8eb0307877d [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "common_throws.h"
Vladimir Marko606adb32018-04-05 14:49:24 +010022#include "debug_print.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070023#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/dex_file-inl.h"
25#include "dex/dex_file_types.h"
26#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070027#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010029#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070031#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070032#include "imt_conflict_table.h"
33#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000034#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070035#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "interpreter/interpreter.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000037#include "jit/jit.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000038#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000039#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070041#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070042#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000043#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010046#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010047#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070048#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070049#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070050#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070051#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070052#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000053#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070054
55namespace art {
56
Andreas Gampe8228cdf2017-05-30 15:03:54 -070057// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070058class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080059 // Number of bytes for each out register in the caller method's frame.
60 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070061 // Frame size in bytes of a callee-save frame for RefsAndArgs.
62 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010063 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
64 // Offset of first GPR arg.
65 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
66 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
67 // Offset of first FPR arg.
68 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
69 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
70 // Offset of return address.
71 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
72 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070073#if defined(__arm__)
74 // The callee save frame is pointed to by SP.
75 // | argN | |
76 // | ... | |
77 // | arg4 | |
78 // | arg3 spill | | Caller's frame
79 // | arg2 spill | |
80 // | arg1 spill | |
81 // | Method* | ---
82 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080083 // | ... | 4x6 bytes callee saves
84 // | R3 |
85 // | R2 |
86 // | R1 |
87 // | S15 |
88 // | : |
89 // | S0 |
90 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070091 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070092 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
93 static constexpr bool kAlignPairRegister = true;
94 static constexpr bool kQuickSoftFloatAbi = false;
95 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000096 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080097 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -070098 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080099 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800100 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000101 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800102 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103#elif defined(__aarch64__)
104 // The callee save frame is pointed to by SP.
105 // | argN | |
106 // | ... | |
107 // | arg4 | |
108 // | arg3 spill | | Caller's frame
109 // | arg2 spill | |
110 // | arg1 spill | |
111 // | Method* | ---
112 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800113 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100115 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000116 // | X7 |
117 // | : |
118 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800119 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 // | : |
121 // | D0 |
122 // | | padding
123 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500124 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000125 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000126 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800127 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000128 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000129 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
130 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800131 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000132 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000133 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000134 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800135#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700136 // The callee save frame is pointed to by SP.
137 // | argN | |
138 // | ... | |
139 // | arg4 | |
140 // | arg3 spill | | Caller's frame
141 // | arg2 spill | |
142 // | arg1 spill | |
143 // | Method* | ---
144 // | RA |
145 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800146 // | T1 | arg5
147 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700148 // | A3 | arg3
149 // | A2 | arg2
150 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800151 // | F19 |
152 // | F18 | f_arg5
153 // | F17 |
154 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000155 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800156 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000157 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800158 // | F12 | f_arg2
159 // | F11 |
160 // | F10 | f_arg1
161 // | F9 |
162 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000163 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700164 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000165 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
166 static constexpr bool kAlignPairRegister = true;
167 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800168 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000169 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800170 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
171 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
172 // passed only in even numbered registers and each
173 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800174 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800175 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000176 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800178#elif defined(__mips__) && defined(__LP64__)
179 // The callee save frame is pointed to by SP.
180 // | argN | |
181 // | ... | |
182 // | arg4 | |
183 // | arg3 spill | | Caller's frame
184 // | arg2 spill | |
185 // | arg1 spill | |
186 // | Method* | ---
187 // | RA |
188 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800189 // | A7 | arg7
190 // | A6 | arg6
191 // | A5 | arg5
192 // | A4 | arg4
193 // | A3 | arg3
194 // | A2 | arg2
195 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000196 // | F19 | f_arg7
197 // | F18 | f_arg6
198 // | F17 | f_arg5
199 // | F16 | f_arg4
200 // | F15 | f_arg3
201 // | F14 | f_arg2
202 // | F13 | f_arg1
203 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800204 // | | padding
205 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800206 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800207 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800208 static constexpr bool kAlignPairRegister = false;
209 static constexpr bool kQuickSoftFloatAbi = false;
210 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000211 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800212 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
213 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
214 static constexpr bool kGprFprLockstep = true;
215
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
217 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
218 }
Ian Rogers848871b2013-08-05 10:56:33 -0700219#elif defined(__i386__)
220 // The callee save frame is pointed to by SP.
221 // | argN | |
222 // | ... | |
223 // | arg4 | |
224 // | arg3 spill | | Caller's frame
225 // | arg2 spill | |
226 // | arg1 spill | |
227 // | Method* | ---
228 // | Return |
229 // | EBP,ESI,EDI | callee saves
230 // | EBX | arg3
231 // | EDX | arg2
232 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000233 // | XMM3 | float arg 4
234 // | XMM2 | float arg 3
235 // | XMM1 | float arg 2
236 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700237 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500238 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000239 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000240 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800241 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000242 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800243 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000244 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800245 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800246 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000247 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800249#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800250 // The callee save frame is pointed to by SP.
251 // | argN | |
252 // | ... | |
253 // | reg. arg spills | | Caller's frame
254 // | Method* | ---
255 // | Return |
256 // | R15 | callee save
257 // | R14 | callee save
258 // | R13 | callee save
259 // | R12 | callee save
260 // | R9 | arg5
261 // | R8 | arg4
262 // | RSI/R6 | arg1
263 // | RBP/R5 | callee save
264 // | RBX/R3 | callee save
265 // | RDX/R2 | arg2
266 // | RCX/R1 | arg3
267 // | XMM7 | float arg 8
268 // | XMM6 | float arg 7
269 // | XMM5 | float arg 6
270 // | XMM4 | float arg 5
271 // | XMM3 | float arg 4
272 // | XMM2 | float arg 3
273 // | XMM1 | float arg 2
274 // | XMM0 | float arg 1
275 // | Padding |
276 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500277 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000278 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800279 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800280 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000281 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700282 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700283 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800284 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
286 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000287 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
288 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
289 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
290 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
291 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800292 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700293 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
294 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 }
296 }
Ian Rogers848871b2013-08-05 10:56:33 -0700297#else
298#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700299#endif
300
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100302 // Special handling for proxy methods. Proxy methods are instance methods so the
303 // 'this' object is the 1st argument. They also have the same frame layout as the
304 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
305 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000306 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700307 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000308 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100309 CHECK_GT(kNumQuickGprArgs, 0u);
310 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
311 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
312 GprIndexToGprOffset(kThisGprIndex);
313 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000314 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100315 }
316
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700317 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700319 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100320 }
321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 uint8_t* previous_sp =
325 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327 }
328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100331 constexpr size_t callee_frame_size =
332 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
334 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100336 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
337 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100339 if (current_code->IsOptimized()) {
340 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000341 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100342 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800344 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100345 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800346 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
347 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100348 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800349 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100350 }
351 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100352 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100353 }
Ian Rogers848871b2013-08-05 10:56:33 -0700354 }
355
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800356 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
357 REQUIRES_SHARED(Locks::mutator_lock_) {
358 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100359 constexpr size_t callee_frame_size =
360 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800361 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
362 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
363 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
364 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
365 if (!current_code->IsOptimized()) {
366 return false;
367 }
368 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
369 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
370 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700371 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800372 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
373 if (invoke.IsValid()) {
374 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700375 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800376 return true;
377 }
378 return false;
379 }
380
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700382 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100384 uint8_t* return_adress_spill =
385 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset;
386 return *reinterpret_cast<uintptr_t*>(return_adress_spill);
Ian Rogers848871b2013-08-05 10:56:33 -0700387 }
388
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700390 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700391 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700392 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
393 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
394 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800396 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
397 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800398 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
399 "Number of Quick FPR arguments unexpected");
400 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
401 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800402 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
403 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800404 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
405 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700406 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800407 }
Ian Rogers848871b2013-08-05 10:56:33 -0700408
409 virtual ~QuickArgumentVisitor() {}
410
411 virtual void Visit() = 0;
412
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 Primitive::Type GetParamPrimitiveType() const {
414 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700415 }
416
Ian Rogers13735952014-10-08 12:43:28 -0700417 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800418 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800419 Primitive::Type type = GetParamPrimitiveType();
420 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
422 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
423 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
424 }
425 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000426 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800427 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700428 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 }
430 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800431 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
433 }
434 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700435 }
436
437 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
439 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 return is_split_long_or_double_;
441 } else {
442 return false; // An optimization for when GPR and FPRs are 64bit.
443 }
Ian Rogers848871b2013-08-05 10:56:33 -0700444 }
445
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700447 return GetParamPrimitiveType() == Primitive::kPrimNot;
448 }
449
Ian Rogers936b37f2014-02-14 00:52:24 -0800450 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700451 Primitive::Type type = GetParamPrimitiveType();
452 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
453 }
454
455 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000456 // The splitted long is always available through the stack.
457 return *reinterpret_cast<uint64_t*>(stack_args_
458 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700459 }
460
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800461 void IncGprIndex() {
462 gpr_index_++;
463 if (kGprFprLockstep) {
464 fpr_index_++;
465 }
466 }
467
468 void IncFprIndex() {
469 fpr_index_++;
470 if (kGprFprLockstep) {
471 gpr_index_++;
472 }
473 }
474
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700475 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800476 // (a) 'stack_args_' should point to the first method's argument
477 // (b) whatever the argument type it is, the 'stack_index_' should
478 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800479 gpr_index_ = 0;
480 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 if (kQuickDoubleRegAlignedFloatBackFilled) {
482 fpr_double_index_ = 0;
483 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800484 stack_index_ = 0;
485 if (!is_static_) { // Handle this.
486 cur_type_ = Primitive::kPrimNot;
487 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700488 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800490 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800491 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 }
Ian Rogers848871b2013-08-05 10:56:33 -0700493 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800494 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
495 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
496 switch (cur_type_) {
497 case Primitive::kPrimNot:
498 case Primitive::kPrimBoolean:
499 case Primitive::kPrimByte:
500 case Primitive::kPrimChar:
501 case Primitive::kPrimShort:
502 case Primitive::kPrimInt:
503 is_split_long_or_double_ = false;
504 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800505 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800506 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800507 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800508 }
509 break;
510 case Primitive::kPrimFloat:
511 is_split_long_or_double_ = false;
512 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800513 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800514 if (kQuickSoftFloatAbi) {
515 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800516 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800517 }
518 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800519 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800521 if (kQuickDoubleRegAlignedFloatBackFilled) {
522 // Double should not overlap with float.
523 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
524 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
525 // Float should not overlap with double.
526 if (fpr_index_ % 2 == 0) {
527 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
528 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000529 } else if (kQuickSkipOddFpRegisters) {
530 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800531 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 }
533 }
534 break;
535 case Primitive::kPrimDouble:
536 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800537 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800538 if (cur_type_ == Primitive::kPrimLong &&
539#if defined(__mips__) && !defined(__LP64__)
540 (gpr_index_ == 0 || gpr_index_ == 2) &&
541#else
542 gpr_index_ == 0 &&
543#endif
544 kAlignPairRegister) {
545 // Currently, this is only for ARM and MIPS, where we align long parameters with
546 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
547 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800548 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000549 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000550 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800551 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500552 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
553 // We don't want to split this. Pass over this register.
554 gpr_index_++;
555 is_split_long_or_double_ = false;
556 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800558 if (kBytesStackArgLocation == 4) {
559 stack_index_+= 2;
560 } else {
561 CHECK_EQ(kBytesStackArgLocation, 8U);
562 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800563 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700564 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800565 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000566 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700567 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800568 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700569 }
570 }
571 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800572 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000573 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800574 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700576 if (kBytesStackArgLocation == 4) {
577 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700579 CHECK_EQ(kBytesStackArgLocation, 8U);
580 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800582 if (kQuickDoubleRegAlignedFloatBackFilled) {
583 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
584 fpr_double_index_ += 2;
585 // Float should not overlap with double.
586 if (fpr_index_ % 2 == 0) {
587 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
588 }
589 }
590 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800591 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800592 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
593 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800594 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 }
596 }
597 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800598 }
599 break;
600 default:
601 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
602 }
Ian Rogers848871b2013-08-05 10:56:33 -0700603 }
604 }
605
Andreas Gampec200a4a2014-06-16 18:39:09 -0700606 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700607 const bool is_static_;
608 const char* const shorty_;
609 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700610
611 private:
Ian Rogers13735952014-10-08 12:43:28 -0700612 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
613 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
614 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800615 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800616 // Index into spilled FPRs.
617 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
618 // holds a higher register number.
619 uint32_t fpr_index_;
620 // Index into spilled FPRs for aligned double.
621 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
622 // terms of singles, may be behind fpr_index.
623 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800624 uint32_t stack_index_; // Index into arguments on the stack.
625 // The current type of argument during VisitArguments.
626 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700627 // Does a 64bit parameter straddle the register and stack arguments?
628 bool is_split_long_or_double_;
629};
630
Sebastien Hertza836bc92014-11-25 16:30:53 +0100631// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
632// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700634 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000635 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
636}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100637
Ian Rogers848871b2013-08-05 10:56:33 -0700638// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800639class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700640 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
642 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700643 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700644
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700645 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700646
647 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800648 ShadowFrame* const sf_;
649 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700650
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700651 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700652};
653
Andreas Gampec200a4a2014-06-16 18:39:09 -0700654void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700655 Primitive::Type type = GetParamPrimitiveType();
656 switch (type) {
657 case Primitive::kPrimLong: // Fall-through.
658 case Primitive::kPrimDouble:
659 if (IsSplitLongOrDouble()) {
660 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
661 } else {
662 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
663 }
664 ++cur_reg_;
665 break;
666 case Primitive::kPrimNot: {
667 StackReference<mirror::Object>* stack_ref =
668 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
669 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
670 }
671 break;
672 case Primitive::kPrimBoolean: // Fall-through.
673 case Primitive::kPrimByte: // Fall-through.
674 case Primitive::kPrimChar: // Fall-through.
675 case Primitive::kPrimShort: // Fall-through.
676 case Primitive::kPrimInt: // Fall-through.
677 case Primitive::kPrimFloat:
678 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
679 break;
680 case Primitive::kPrimVoid:
681 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700682 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700683 }
684 ++cur_reg_;
685}
686
Mingyao Yang417528d2017-09-13 12:10:40 -0700687// Don't inline. See b/65159206.
688NO_INLINE
689static void HandleDeoptimization(JValue* result,
690 ArtMethod* method,
691 ShadowFrame* deopt_frame,
692 ManagedStack* fragment)
693 REQUIRES_SHARED(Locks::mutator_lock_) {
694 // Coming from partial-fragment deopt.
695 Thread* self = Thread::Current();
696 if (kIsDebugBuild) {
697 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
698 // of the call-stack) corresponds to the called method.
699 ShadowFrame* linked = deopt_frame;
700 while (linked->GetLink() != nullptr) {
701 linked = linked->GetLink();
702 }
703 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
704 << ArtMethod::PrettyMethod(linked->GetMethod());
705 }
706
707 if (VLOG_IS_ON(deopt)) {
708 // Print out the stack to verify that it was a partial-fragment deopt.
709 LOG(INFO) << "Continue-ing from deopt. Stack is:";
710 QuickExceptionHandler::DumpFramesWithType(self, true);
711 }
712
713 ObjPtr<mirror::Throwable> pending_exception;
714 bool from_code = false;
715 DeoptimizationMethodType method_type;
716 self->PopDeoptimizationContext(/* out */ result,
717 /* out */ &pending_exception,
718 /* out */ &from_code,
719 /* out */ &method_type);
720
721 // Push a transition back into managed code onto the linked list in thread.
722 self->PushManagedStackFragment(fragment);
723
724 // Ensure that the stack is still in order.
725 if (kIsDebugBuild) {
726 class DummyStackVisitor : public StackVisitor {
727 public:
728 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
729 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
730
731 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
732 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
733 // logic. Just always say we want to continue.
734 return true;
735 }
736 };
737 DummyStackVisitor dsv(self);
738 dsv.WalkStack();
739 }
740
741 // Restore the exception that was pending before deoptimization then interpret the
742 // deoptimized frames.
743 if (pending_exception != nullptr) {
744 self->SetException(pending_exception);
745 }
746 interpreter::EnterInterpreterFromDeoptimize(self,
747 deopt_frame,
748 result,
749 from_code,
750 DeoptimizationMethodType::kDefault);
751}
752
Mathieu Chartiere401d142015-04-22 13:56:20 -0700753extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700754 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700755 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
756 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700757 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700758
Alex Light9139e002015-10-09 15:59:48 -0700759 if (UNLIKELY(!method->IsInvokable())) {
760 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700761 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700762 }
763
764 JValue tmp_value;
765 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700766 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700767 ManagedStack fragment;
768
David Sehr709b0702016-10-13 09:12:37 -0700769 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700770 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700771 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800772 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000773 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700774 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
775
776 JValue result;
777
Mingyao Yang417528d2017-09-13 12:10:40 -0700778 if (UNLIKELY(deopt_frame != nullptr)) {
779 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700780 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700781 const char* old_cause = self->StartAssertNoThreadSuspension(
782 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800783 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700784 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700785 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700786 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700787 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800788 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700789 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800790 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700791 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800792 const bool needs_initialization =
793 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700794 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700795 self->PushManagedStackFragment(&fragment);
796 self->PushShadowFrame(shadow_frame);
797 self->EndAssertNoThreadSuspension(old_cause);
798
Ian Rogerse94652f2014-12-02 11:13:19 -0800799 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700800 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800801 StackHandleScope<1> hs(self);
802 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700803 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700804 DCHECK(Thread::Current()->IsExceptionPending())
805 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700806 self->PopManagedStackFragment(fragment);
807 return 0;
808 }
809 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200810
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800811 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700812 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700813
814 // Pop transition.
815 self->PopManagedStackFragment(fragment);
816
817 // Request a stack deoptimization if needed
818 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700819 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700820 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
821 // should be done and it knows the real return pc.
822 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000823 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
824 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
825 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
826 << caller->PrettyMethod();
827 } else {
828 // Push the context of the deoptimization stack so we can restore the return value and the
829 // exception before executing the deoptimized frames.
830 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700831 result,
832 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
833 self->GetException(),
834 false /* from_code */,
835 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700836
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000837 // Set special exception to cause deoptimization.
838 self->SetException(Thread::GetDeoptimizationException());
839 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700840 }
841
842 // No need to restore the args since the method has already been run by the interpreter.
843 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700844}
845
846// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
847// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800848class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700849 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700851 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700852 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700853
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700854 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700855
856 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700857 ScopedObjectAccessUnchecked* const soa_;
858 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700859
Ian Rogers848871b2013-08-05 10:56:33 -0700860 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
861};
862
Ian Rogers9758f792014-03-13 09:02:55 -0700863void BuildQuickArgumentVisitor::Visit() {
864 jvalue val;
865 Primitive::Type type = GetParamPrimitiveType();
866 switch (type) {
867 case Primitive::kPrimNot: {
868 StackReference<mirror::Object>* stack_ref =
869 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
870 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700871 break;
872 }
873 case Primitive::kPrimLong: // Fall-through.
874 case Primitive::kPrimDouble:
875 if (IsSplitLongOrDouble()) {
876 val.j = ReadSplitLongParam();
877 } else {
878 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
879 }
880 break;
881 case Primitive::kPrimBoolean: // Fall-through.
882 case Primitive::kPrimByte: // Fall-through.
883 case Primitive::kPrimChar: // Fall-through.
884 case Primitive::kPrimShort: // Fall-through.
885 case Primitive::kPrimInt: // Fall-through.
886 case Primitive::kPrimFloat:
887 val.i = *reinterpret_cast<jint*>(GetParamAddress());
888 break;
889 case Primitive::kPrimVoid:
890 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700891 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700892 }
893 args_->push_back(val);
894}
895
Ian Rogers848871b2013-08-05 10:56:33 -0700896// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
897// which is responsible for recording callee save registers. We explicitly place into jobjects the
898// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
899// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900extern "C" uint64_t artQuickProxyInvokeHandler(
901 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700902 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700903 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
904 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700905 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
906 const char* old_cause =
907 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
908 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700909 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700910 self->VerifyStack();
911 // Start new JNI local reference state.
912 JNIEnvExt* env = self->GetJniEnv();
913 ScopedObjectAccessUnchecked soa(env);
914 ScopedJniEnvLocalRefState env_state(env);
915 // Create local ref. copies of proxy method and the receiver.
916 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
917
918 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700919 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700920 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
921 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700922 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700923 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000925 BuildQuickArgumentVisitor local_ref_visitor(
926 sp, /* is_static */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700927
Ian Rogers848871b2013-08-05 10:56:33 -0700928 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700929 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700930 args.erase(args.begin());
931
932 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700933 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700934 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
935 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700936 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700937 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700938 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700939 ObjPtr<mirror::Method> interface_reflect_method =
940 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
941 if (interface_reflect_method == nullptr) {
942 soa.Self()->AssertPendingOOMException();
943 return 0;
944 }
945 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700946
947 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
948 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700949 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Ian Rogers848871b2013-08-05 10:56:33 -0700950 return result.GetJ();
951}
952
Roland Levillainad0777d2018-02-12 20:00:18 +0000953// Visitor returning a reference argument at a given position in a Quick stack frame.
954// NOTE: Only used for testing purposes.
955class GetQuickReferenceArgumentAtVisitor FINAL : public QuickArgumentVisitor {
956 public:
957 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
958 const char* shorty,
959 uint32_t shorty_len,
960 size_t arg_pos)
961 : QuickArgumentVisitor(sp, /* is_static */ false, shorty, shorty_len),
962 cur_pos_(0u),
963 arg_pos_(arg_pos),
964 ref_arg_(nullptr) {
965 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
966 }
967
968 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
969 if (cur_pos_ == arg_pos_) {
970 Primitive::Type type = GetParamPrimitiveType();
971 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
972 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
973 }
974 ++cur_pos_;
975 }
976
977 StackReference<mirror::Object>* GetReferenceArgument() {
978 return ref_arg_;
979 }
980
981 private:
982 // The position of the currently visited argument.
983 size_t cur_pos_;
984 // The position of the searched argument.
985 const size_t arg_pos_;
986 // The reference argument, if found.
987 StackReference<mirror::Object>* ref_arg_;
988
989 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
990};
991
992// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
993// NOTE: Only used for testing purposes.
994extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
995 ArtMethod** sp)
996 REQUIRES_SHARED(Locks::mutator_lock_) {
997 ArtMethod* proxy_method = *sp;
998 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
999 CHECK(!non_proxy_method->IsStatic())
1000 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1001 uint32_t shorty_len = 0;
1002 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1003 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1004 ref_arg_visitor.VisitArguments();
1005 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1006 return ref_arg;
1007}
1008
1009// Visitor returning all the reference arguments in a Quick stack frame.
1010class GetQuickReferenceArgumentsVisitor FINAL : public QuickArgumentVisitor {
1011 public:
1012 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1013 bool is_static,
1014 const char* shorty,
1015 uint32_t shorty_len)
1016 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1017
1018 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE {
1019 Primitive::Type type = GetParamPrimitiveType();
1020 if (type == Primitive::kPrimNot) {
1021 StackReference<mirror::Object>* ref_arg =
1022 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1023 ref_args_.push_back(ref_arg);
1024 }
1025 }
1026
1027 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1028 return ref_args_;
1029 }
1030
1031 private:
1032 // The reference arguments.
1033 std::vector<StackReference<mirror::Object>*> ref_args_;
1034
1035 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1036};
1037
1038// Returning all reference arguments in Quick stack frame at address `sp`.
1039std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1040 REQUIRES_SHARED(Locks::mutator_lock_) {
1041 ArtMethod* proxy_method = *sp;
1042 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1043 CHECK(!non_proxy_method->IsStatic())
1044 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1045 uint32_t shorty_len = 0;
1046 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1047 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /* is_static */ false, shorty, shorty_len);
1048 ref_args_visitor.VisitArguments();
1049 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1050 return ref_args;
1051}
1052
Ian Rogers848871b2013-08-05 10:56:33 -07001053// Read object references held in arguments from quick frames and place in a JNI local references,
1054// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001055class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001056 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1058 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001059 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001060
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001061 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001062
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001063 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001064
1065 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001066 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001067 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001068 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1069
Mathieu Chartier590fee92013-09-13 13:46:47 -07001070 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001071};
1072
Ian Rogers9758f792014-03-13 09:02:55 -07001073void RememberForGcArgumentVisitor::Visit() {
1074 if (IsParamAReference()) {
1075 StackReference<mirror::Object>* stack_ref =
1076 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1077 jobject reference =
1078 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1079 references_.push_back(std::make_pair(reference, stack_ref));
1080 }
1081}
1082
1083void RememberForGcArgumentVisitor::FixupReferences() {
1084 // Fixup any references which may have changed.
1085 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001086 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001087 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001088 }
1089}
1090
Alex Lightb7edcda2017-04-27 13:20:31 -07001091extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1092 mirror::Object* this_object,
1093 Thread* self,
1094 ArtMethod** sp)
1095 REQUIRES_SHARED(Locks::mutator_lock_) {
1096 const void* result;
1097 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1098 // that part.
1099 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1100 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1101 if (instrumentation->IsDeoptimized(method)) {
1102 result = GetQuickToInterpreterBridge();
1103 } else {
1104 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1105 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1106 }
1107
1108 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1109 bool is_static = method->IsStatic();
1110 uint32_t shorty_len;
1111 const char* shorty =
1112 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1113
1114 ScopedObjectAccessUnchecked soa(self);
1115 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1116 visitor.VisitArguments();
1117
1118 instrumentation->PushInstrumentationStackFrame(self,
1119 is_static ? nullptr : this_object,
1120 method,
1121 QuickArgumentVisitor::GetCallingPc(sp),
1122 interpreter_entry);
1123
1124 visitor.FixupReferences();
1125 if (UNLIKELY(self->IsExceptionPending())) {
1126 return nullptr;
1127 }
1128 CHECK(result != nullptr) << method->PrettyMethod();
1129 return result;
1130}
1131
1132extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1133 ArtMethod** sp,
1134 uint64_t* gpr_result,
1135 uint64_t* fpr_result)
1136 REQUIRES_SHARED(Locks::mutator_lock_) {
1137 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1138 CHECK(gpr_result != nullptr);
1139 CHECK(fpr_result != nullptr);
1140 // Instrumentation exit stub must not be entered with a pending exception.
1141 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1142 << self->GetException()->Dump();
1143 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001144 constexpr size_t return_pc_offset =
1145 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001146 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1147 return_pc_offset);
1148 CHECK_EQ(*return_pc, 0U);
1149
1150 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1151 // deoptimization shouldn't be performed with the high-half having the return address. When
1152 // deoptimization should be performed the low half is zero and the high-half the address of the
1153 // deoptimization entry point.
1154 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1155 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1156 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001157 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001158 return GetTwoWordFailureValue();
1159 }
1160 return return_or_deoptimize_pc;
1161}
1162
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001163static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1164 REQUIRES_SHARED(Locks::mutator_lock_) {
1165 if (dex_pc == static_cast<uint32_t>(-1)) {
1166 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1167 return "<native>";
1168 } else {
1169 CodeItemInstructionAccessor accessor = method->DexInstructions();
1170 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1171 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1172 }
1173}
1174
Vladimir Marko606adb32018-04-05 14:49:24 +01001175static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1176 REQUIRES_SHARED(Locks::mutator_lock_) {
1177 std::string storage;
1178 const char* descriptor = klass->GetDescriptor(&storage);
1179 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1180 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1181 if (oat_dex_file != nullptr) {
1182 const OatFile* oat_file = oat_dex_file->GetOatFile();
1183 const char* dex2oat_cmdline =
1184 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1185 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1186 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1187 }
1188}
1189
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001190static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1191 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001192 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001193
1194 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1195 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1196
Vladimir Markod3083dd2018-05-17 08:43:47 +01001197 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001198 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1199 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001200 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001201 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1202 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1203 ArtMethod* outer_method = *caller_sp;
1204
1205 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1206 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1207 << " native pc: " << caller_pc << " Instrumented!";
1208 return;
1209 }
1210
1211 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1212 CHECK(current_code != nullptr);
1213 CHECK(current_code->IsOptimized());
1214 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
1215 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
1216 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
1217 CodeInfoEncoding encoding = code_info.ExtractEncoding();
1218 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
1219 CHECK(stack_map.IsValid());
1220 uint32_t dex_pc = stack_map.GetDexPc(encoding.stack_map.encoding);
1221
1222 // Log the outer method and its associated dex file and class table pointer which can be used
1223 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1224 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1225 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1226 << " native pc: " << caller_pc
1227 << " dex pc: " << dex_pc
1228 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1229 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001230 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001231 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1232
1233 ArtMethod* caller = outer_method;
1234 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
1235 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
1236 const InlineInfoEncoding& inline_info_encoding = encoding.inline_info.encoding;
1237 size_t depth = inline_info.GetDepth(inline_info_encoding);
1238 for (size_t d = 0; d < depth; ++d) {
1239 const char* tag = "";
1240 dex_pc = inline_info.GetDexPcAtDepth(inline_info_encoding, d);
1241 if (inline_info.EncodesArtMethodAtDepth(inline_info_encoding, d)) {
1242 tag = "encoded ";
1243 caller = inline_info.GetArtMethodAtDepth(inline_info_encoding, d);
1244 } else {
1245 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info_encoding,
1246 method_info,
1247 d);
1248 if (dex_pc == static_cast<uint32_t>(-1)) {
1249 tag = "special ";
1250 CHECK_EQ(d + 1u, depth);
1251 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1252 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
1253 } else {
1254 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1255 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1256 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1257 CHECK(caller != nullptr);
1258 }
1259 }
1260 LOG(FATAL_WITHOUT_ABORT) << "Inlined method #" << d << ": " << tag << caller->PrettyMethod()
1261 << " dex pc: " << dex_pc
1262 << " dex file: " << caller->GetDexFile()->GetLocation()
1263 << " class table: "
Vladimir Marko606adb32018-04-05 14:49:24 +01001264 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1265 DumpB74410240ClassData(caller->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001266 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
1267 }
1268 }
1269}
1270
Ian Rogers848871b2013-08-05 10:56:33 -07001271// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001272extern "C" const void* artQuickResolutionTrampoline(
1273 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001274 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001275 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1276 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1277 // does not have the same stack layout as the callee-save method).
1278 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001279 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001280 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001281 ScopedObjectAccessUnchecked soa(env);
1282 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001283 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001284
1285 // Compute details about the called method (avoid GCs)
1286 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001287 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001288 MethodReference called_method(nullptr, 0);
1289 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001290 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001291 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001292 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001293 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001294
1295 InvokeType stack_map_invoke_type;
1296 uint32_t stack_map_dex_method_idx;
1297 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1298 &stack_map_invoke_type,
1299 &stack_map_dex_method_idx);
1300 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1301 // code.
1302 if (!found_stack_map || kIsDebugBuild) {
1303 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001304 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001305 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1306 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001307 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001308 bool is_range;
1309 switch (instr_code) {
1310 case Instruction::INVOKE_DIRECT:
1311 invoke_type = kDirect;
1312 is_range = false;
1313 break;
1314 case Instruction::INVOKE_DIRECT_RANGE:
1315 invoke_type = kDirect;
1316 is_range = true;
1317 break;
1318 case Instruction::INVOKE_STATIC:
1319 invoke_type = kStatic;
1320 is_range = false;
1321 break;
1322 case Instruction::INVOKE_STATIC_RANGE:
1323 invoke_type = kStatic;
1324 is_range = true;
1325 break;
1326 case Instruction::INVOKE_SUPER:
1327 invoke_type = kSuper;
1328 is_range = false;
1329 break;
1330 case Instruction::INVOKE_SUPER_RANGE:
1331 invoke_type = kSuper;
1332 is_range = true;
1333 break;
1334 case Instruction::INVOKE_VIRTUAL:
1335 invoke_type = kVirtual;
1336 is_range = false;
1337 break;
1338 case Instruction::INVOKE_VIRTUAL_RANGE:
1339 invoke_type = kVirtual;
1340 is_range = true;
1341 break;
1342 case Instruction::INVOKE_INTERFACE:
1343 invoke_type = kInterface;
1344 is_range = false;
1345 break;
1346 case Instruction::INVOKE_INTERFACE_RANGE:
1347 invoke_type = kInterface;
1348 is_range = true;
1349 break;
1350 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001351 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001352 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001353 UNREACHABLE();
1354 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001355 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001356 // Check that the invoke matches what we expected, note that this path only happens for debug
1357 // builds.
1358 if (found_stack_map) {
1359 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1360 if (invoke_type != kSuper) {
1361 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001362 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001363 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001364 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001365 }
1366 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001367 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001368 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001369 }
1370 } else {
1371 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001372 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001373 }
Ian Rogers848871b2013-08-05 10:56:33 -07001374 } else {
1375 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001376 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001377 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001378 }
1379 uint32_t shorty_len;
1380 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001381 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001382 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001383 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001384 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001385 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001386 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001387 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001388 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001389 mirror::Object* dummy = nullptr;
1390 HandleWrapper<mirror::Object> h_receiver(
1391 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001392 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001393 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001394 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001395
1396 // Update .bss entry in oat file if any.
1397 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001398 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1399 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1400 called_method.index,
1401 called_method.dex_file->NumMethodIds(),
1402 static_cast<size_t>(kRuntimePointerSize));
1403 if (bss_offset != IndexBssMappingLookup::npos) {
1404 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1405 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1406 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1407 oat_file->BssBegin() + bss_offset));
1408 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1409 DCHECK_LT(method_entry,
1410 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1411 *method_entry = called;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001412 }
1413 }
Ian Rogers848871b2013-08-05 10:56:33 -07001414 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001415 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001416 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001417 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001418 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001419 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001420 if (virtual_or_interface || invoke_type == kSuper) {
1421 // Refine called method based on receiver for kVirtual/kInterface, and
1422 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001423 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001424 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001425 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001426 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001427 } else if (invoke_type == kInterface) {
1428 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001429 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001430 } else {
1431 DCHECK_EQ(invoke_type, kSuper);
1432 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001433 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001434 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001435 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001436 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001437 } else {
1438 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001439 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001440 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001441 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001442
David Sehr709b0702016-10-13 09:12:37 -07001443 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1444 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001445 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001446 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001447
Ian Rogers848871b2013-08-05 10:56:33 -07001448 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001449 StackHandleScope<1> hs(soa.Self());
1450 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001451 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001452 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001453 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1454 // If we are single-stepping or the called method is deoptimized (by a
1455 // breakpoint, for example), then we have to execute the called method
1456 // with the interpreter.
1457 code = GetQuickToInterpreterBridge();
1458 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1459 // If the caller is deoptimized (by a breakpoint, for example), we have to
1460 // continue its execution with interpreter when returning from the called
1461 // method. Because we do not want to execute the called method with the
1462 // interpreter, we wrap its execution into the instrumentation stubs.
1463 // When the called method returns, it will execute the instrumentation
1464 // exit hook that will determine the need of the interpreter with a call
1465 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1466 // it is needed.
1467 code = GetQuickInstrumentationEntryPoint();
1468 } else {
1469 code = called->GetEntryPointFromQuickCompiledCode();
1470 }
Ian Rogers848871b2013-08-05 10:56:33 -07001471 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001472 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1473 // If we are single-stepping or the called method is deoptimized (by a
1474 // breakpoint, for example), then we have to execute the called method
1475 // with the interpreter.
1476 code = GetQuickToInterpreterBridge();
1477 } else if (invoke_type == kStatic) {
Alex Lightfc49fec2018-01-16 22:28:36 +00001478 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1479 // until class is initialized to stop races between threads).
1480 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001481 } else {
1482 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001483 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001484 }
1485 } else {
1486 DCHECK(called_class->IsErroneous());
1487 }
1488 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001489 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001490 // Fixup any locally saved objects may have moved during a GC.
1491 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001492 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001493 *sp = called;
1494
Ian Rogers848871b2013-08-05 10:56:33 -07001495 return code;
1496}
1497
Andreas Gampec147b002014-03-06 18:11:06 -08001498/*
1499 * This class uses a couple of observations to unite the different calling conventions through
1500 * a few constants.
1501 *
1502 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1503 * possible alignment.
1504 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1505 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1506 * when we have to split things
1507 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1508 * and we can use Int handling directly.
1509 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1510 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1511 * extension should be compatible with Aarch64, which mandates copying the available bits
1512 * into LSB and leaving the rest unspecified.
1513 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1514 * the stack.
1515 * 6) There is only little endian.
1516 *
1517 *
1518 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1519 * follows:
1520 *
1521 * void PushGpr(uintptr_t): Add a value for the next GPR
1522 *
1523 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1524 * padding, that is, think the architecture is 32b and aligns 64b.
1525 *
1526 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1527 * split this if necessary. The current state will have aligned, if
1528 * necessary.
1529 *
1530 * void PushStack(uintptr_t): Push a value to the stack.
1531 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001532 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001533 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001534 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001535 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001536 *
1537 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001539 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001540#if defined(__arm__)
1541 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001542 static constexpr bool kNativeSoftFloatAbi = true;
1543 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001544 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1545
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001546 static constexpr size_t kRegistersNeededForLong = 2;
1547 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001548 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001549 static constexpr bool kMultiFPRegistersWidened = false;
1550 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001551 static constexpr bool kAlignLongOnStack = true;
1552 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001553#elif defined(__aarch64__)
1554 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1555 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1556 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1557
1558 static constexpr size_t kRegistersNeededForLong = 1;
1559 static constexpr size_t kRegistersNeededForDouble = 1;
1560 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001561 static constexpr bool kMultiFPRegistersWidened = false;
1562 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001563 static constexpr bool kAlignLongOnStack = false;
1564 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001565#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001566 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001567 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1568 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001569
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001570 static constexpr size_t kRegistersNeededForLong = 2;
1571 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001572 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001573 static constexpr bool kMultiFPRegistersWidened = true;
1574 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001575 static constexpr bool kAlignLongOnStack = true;
1576 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001577#elif defined(__mips__) && defined(__LP64__)
1578 // Let the code prepare GPRs only and we will load the FPRs with same data.
1579 static constexpr bool kNativeSoftFloatAbi = true;
1580 static constexpr size_t kNumNativeGprArgs = 8;
1581 static constexpr size_t kNumNativeFprArgs = 0;
1582
1583 static constexpr size_t kRegistersNeededForLong = 1;
1584 static constexpr size_t kRegistersNeededForDouble = 1;
1585 static constexpr bool kMultiRegistersAligned = false;
1586 static constexpr bool kMultiFPRegistersWidened = false;
1587 static constexpr bool kMultiGPRegistersWidened = true;
1588 static constexpr bool kAlignLongOnStack = false;
1589 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001590#elif defined(__i386__)
1591 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001592 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001593 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1594 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1595
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001596 static constexpr size_t kRegistersNeededForLong = 2;
1597 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001598 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001599 static constexpr bool kMultiFPRegistersWidened = false;
1600 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001601 static constexpr bool kAlignLongOnStack = false;
1602 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001603#elif defined(__x86_64__)
1604 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1605 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1606 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1607
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001608 static constexpr size_t kRegistersNeededForLong = 1;
1609 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001610 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001611 static constexpr bool kMultiFPRegistersWidened = false;
1612 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001613 static constexpr bool kAlignLongOnStack = false;
1614 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001615#else
1616#error "Unsupported architecture"
1617#endif
1618
Andreas Gampec147b002014-03-06 18:11:06 -08001619 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001620 explicit BuildNativeCallFrameStateMachine(T* delegate)
1621 : gpr_index_(kNumNativeGprArgs),
1622 fpr_index_(kNumNativeFprArgs),
1623 stack_entries_(0),
1624 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001625 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1626 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001627 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1628 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001629 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001630
Andreas Gampec200a4a2014-06-16 18:39:09 -07001631 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001632
Ian Rogers1428dce2014-10-21 15:02:15 -07001633 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001634 return gpr_index_ > 0;
1635 }
1636
Andreas Gampec200a4a2014-06-16 18:39:09 -07001637 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001638 if (HavePointerGpr()) {
1639 gpr_index_--;
1640 PushGpr(reinterpret_cast<uintptr_t>(val));
1641 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001643 PushStack(reinterpret_cast<uintptr_t>(val));
1644 gpr_index_ = 0;
1645 }
1646 }
1647
Ian Rogers1428dce2014-10-21 15:02:15 -07001648 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001649 return gpr_index_ > 0;
1650 }
1651
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001652 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001653 uintptr_t handle = PushHandle(ptr);
1654 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001655 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001656 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001657 } else {
1658 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001659 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001660 gpr_index_ = 0;
1661 }
1662 }
1663
Ian Rogers1428dce2014-10-21 15:02:15 -07001664 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001665 return gpr_index_ > 0;
1666 }
1667
1668 void AdvanceInt(uint32_t val) {
1669 if (HaveIntGpr()) {
1670 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001671 if (kMultiGPRegistersWidened) {
1672 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001673 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001674 } else {
1675 PushGpr(val);
1676 }
Andreas Gampec147b002014-03-06 18:11:06 -08001677 } else {
1678 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001679 if (kMultiGPRegistersWidened) {
1680 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001681 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001682 } else {
1683 PushStack(val);
1684 }
Andreas Gampec147b002014-03-06 18:11:06 -08001685 gpr_index_ = 0;
1686 }
1687 }
1688
Ian Rogers1428dce2014-10-21 15:02:15 -07001689 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001690 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1691 }
1692
Ian Rogers1428dce2014-10-21 15:02:15 -07001693 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001694 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1695 kAlignLongOnStack && // and when it needs alignment
1696 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1697 }
1698
Ian Rogers1428dce2014-10-21 15:02:15 -07001699 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001700 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1701 kAlignLongOnStack && // and when it needs 8B alignment
1702 (stack_entries_ & 1) == 1; // counter is odd
1703 }
1704
1705 void AdvanceLong(uint64_t val) {
1706 if (HaveLongGpr()) {
1707 if (LongGprNeedsPadding()) {
1708 PushGpr(0);
1709 gpr_index_--;
1710 }
1711 if (kRegistersNeededForLong == 1) {
1712 PushGpr(static_cast<uintptr_t>(val));
1713 } else {
1714 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1715 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1716 }
1717 gpr_index_ -= kRegistersNeededForLong;
1718 } else {
1719 if (LongStackNeedsPadding()) {
1720 PushStack(0);
1721 stack_entries_++;
1722 }
1723 if (kRegistersNeededForLong == 1) {
1724 PushStack(static_cast<uintptr_t>(val));
1725 stack_entries_++;
1726 } else {
1727 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1728 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1729 stack_entries_ += 2;
1730 }
1731 gpr_index_ = 0;
1732 }
1733 }
1734
Ian Rogers1428dce2014-10-21 15:02:15 -07001735 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001736 return fpr_index_ > 0;
1737 }
1738
Andreas Gampec147b002014-03-06 18:11:06 -08001739 void AdvanceFloat(float val) {
1740 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001741 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001742 } else {
1743 if (HaveFloatFpr()) {
1744 fpr_index_--;
1745 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001746 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001747 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001748 } else {
1749 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001750 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001751 }
1752 } else {
1753 PushFpr4(val);
1754 }
1755 } else {
1756 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001757 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001758 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001759 // Note: We need to jump through those hoops to make the compiler happy.
1760 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001761 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001762 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001763 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001764 }
1765 fpr_index_ = 0;
1766 }
1767 }
1768 }
1769
Ian Rogers1428dce2014-10-21 15:02:15 -07001770 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001771 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1772 }
1773
Ian Rogers1428dce2014-10-21 15:02:15 -07001774 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001775 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1776 kAlignDoubleOnStack && // and when it needs alignment
1777 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1778 }
1779
Ian Rogers1428dce2014-10-21 15:02:15 -07001780 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001781 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1782 kAlignDoubleOnStack && // and when it needs 8B alignment
1783 (stack_entries_ & 1) == 1; // counter is odd
1784 }
1785
1786 void AdvanceDouble(uint64_t val) {
1787 if (kNativeSoftFloatAbi) {
1788 AdvanceLong(val);
1789 } else {
1790 if (HaveDoubleFpr()) {
1791 if (DoubleFprNeedsPadding()) {
1792 PushFpr4(0);
1793 fpr_index_--;
1794 }
1795 PushFpr8(val);
1796 fpr_index_ -= kRegistersNeededForDouble;
1797 } else {
1798 if (DoubleStackNeedsPadding()) {
1799 PushStack(0);
1800 stack_entries_++;
1801 }
1802 if (kRegistersNeededForDouble == 1) {
1803 PushStack(static_cast<uintptr_t>(val));
1804 stack_entries_++;
1805 } else {
1806 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1807 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1808 stack_entries_ += 2;
1809 }
1810 fpr_index_ = 0;
1811 }
1812 }
1813 }
1814
Ian Rogers1428dce2014-10-21 15:02:15 -07001815 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001816 return stack_entries_;
1817 }
1818
Ian Rogers1428dce2014-10-21 15:02:15 -07001819 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001820 return kNumNativeGprArgs - gpr_index_;
1821 }
1822
Ian Rogers1428dce2014-10-21 15:02:15 -07001823 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001824 return kNumNativeFprArgs - fpr_index_;
1825 }
1826
1827 private:
1828 void PushGpr(uintptr_t val) {
1829 delegate_->PushGpr(val);
1830 }
1831 void PushFpr4(float val) {
1832 delegate_->PushFpr4(val);
1833 }
1834 void PushFpr8(uint64_t val) {
1835 delegate_->PushFpr8(val);
1836 }
1837 void PushStack(uintptr_t val) {
1838 delegate_->PushStack(val);
1839 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001840 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001841 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001842 }
1843
1844 uint32_t gpr_index_; // Number of free GPRs
1845 uint32_t fpr_index_; // Number of free FPRs
1846 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1847 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001848 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001849};
1850
Andreas Gampec200a4a2014-06-16 18:39:09 -07001851// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1852// in subclasses.
1853//
1854// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1855// them with handles.
1856class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001857 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001858 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1859
1860 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001861
Ian Rogers1428dce2014-10-21 15:02:15 -07001862 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001863 return num_stack_entries_ * sizeof(uintptr_t);
1864 }
1865
Ian Rogers1428dce2014-10-21 15:02:15 -07001866 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001867 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001868 // Align by kStackAlignment.
1869 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001870 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001871 }
1872
Ian Rogers1428dce2014-10-21 15:02:15 -07001873 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1874 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001875 // Assumption is OK right now, as we have soft-float arm
1876 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1877 sp8 -= fregs * sizeof(uintptr_t);
1878 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1879 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1880 sp8 -= iregs * sizeof(uintptr_t);
1881 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1882 return sp8;
1883 }
Andreas Gampec147b002014-03-06 18:11:06 -08001884
Andreas Gampec200a4a2014-06-16 18:39:09 -07001885 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001886 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001887 // Native call stack.
1888 sp8 = LayoutCallStack(sp8);
1889 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001890
Andreas Gampec200a4a2014-06-16 18:39:09 -07001891 // Put fprs and gprs below.
1892 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001893
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894 // Return the new bottom.
1895 return sp8;
1896 }
1897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001898 virtual void WalkHeader(
1899 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001900 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001901 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001902
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001903 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001904 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1905
1906 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001907
1908 for (uint32_t i = 1; i < shorty_len; ++i) {
1909 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1910 switch (cur_type_) {
1911 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001912 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001913 sm.AdvanceHandleScope(
1914 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001915 break;
1916
1917 case Primitive::kPrimBoolean:
1918 case Primitive::kPrimByte:
1919 case Primitive::kPrimChar:
1920 case Primitive::kPrimShort:
1921 case Primitive::kPrimInt:
1922 sm.AdvanceInt(0);
1923 break;
1924 case Primitive::kPrimFloat:
1925 sm.AdvanceFloat(0);
1926 break;
1927 case Primitive::kPrimDouble:
1928 sm.AdvanceDouble(0);
1929 break;
1930 case Primitive::kPrimLong:
1931 sm.AdvanceLong(0);
1932 break;
1933 default:
1934 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001935 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001936 }
1937 }
1938
Ian Rogers1428dce2014-10-21 15:02:15 -07001939 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001940 }
1941
1942 void PushGpr(uintptr_t /* val */) {
1943 // not optimizing registers, yet
1944 }
1945
1946 void PushFpr4(float /* val */) {
1947 // not optimizing registers, yet
1948 }
1949
1950 void PushFpr8(uint64_t /* val */) {
1951 // not optimizing registers, yet
1952 }
1953
1954 void PushStack(uintptr_t /* val */) {
1955 // counting is already done in the superclass
1956 }
1957
Andreas Gampec200a4a2014-06-16 18:39:09 -07001958 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001959 return reinterpret_cast<uintptr_t>(nullptr);
1960 }
1961
Andreas Gampec200a4a2014-06-16 18:39:09 -07001962 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001963 uint32_t num_stack_entries_;
1964};
1965
Andreas Gampec200a4a2014-06-16 18:39:09 -07001966class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001967 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001968 explicit ComputeGenericJniFrameSize(bool critical_native)
1969 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001970
Andreas Gampec200a4a2014-06-16 18:39:09 -07001971 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1972 // is at *m = sp. Will update to point to the bottom of the save frame.
1973 //
1974 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001975 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001976 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 ArtMethod* method = **m;
1978
Andreas Gampe542451c2016-07-26 09:02:02 -07001979 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001980
Andreas Gampec200a4a2014-06-16 18:39:09 -07001981 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1982
1983 // First, fix up the layout of the callee-save frame.
1984 // We have to squeeze in the HandleScope, and relocate the method pointer.
1985
1986 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001987 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001988
1989 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001990 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001991 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001992
1993 sp8 -= scope_and_method;
1994 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001996
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001998 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1999 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002000
2001 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2002 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002003 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2004 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002005 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002006 }
2007
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002009 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002010 // Reference cookie and padding
2011 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002012 }
2013
Andreas Gampec200a4a2014-06-16 18:39:09 -07002014 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2015 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002017 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002018 // First, fix up the layout of the callee-save frame.
2019 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002020 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002021
2022 // The bottom of the callee-save frame is now where the method is, *m.
2023 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2024
2025 // Add space for cookie.
2026 LayoutCookie(&sp8);
2027
2028 return sp8;
2029 }
2030
2031 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2033 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2034 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002035 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002036 Walk(shorty, shorty_len);
2037
2038 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002039 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002040
2041 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2042
2043 // Return the new bottom.
2044 return sp8;
2045 }
2046
2047 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
2048
2049 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
2050 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002051 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052
2053 private:
2054 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002055 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002056};
2057
2058uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2059 num_handle_scope_references_++;
2060 return reinterpret_cast<uintptr_t>(nullptr);
2061}
2062
2063void ComputeGenericJniFrameSize::WalkHeader(
2064 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002065 // First 2 parameters are always excluded for @CriticalNative.
2066 if (UNLIKELY(critical_native_)) {
2067 return;
2068 }
2069
Andreas Gampec200a4a2014-06-16 18:39:09 -07002070 // JNIEnv
2071 sm->AdvancePointer(nullptr);
2072
2073 // Class object or this as first argument
2074 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2075}
2076
2077// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2078// the template requirements of BuildGenericJniFrameStateMachine.
2079class FillNativeCall {
2080 public:
2081 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2082 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2083
2084 virtual ~FillNativeCall() {}
2085
2086 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2087 cur_gpr_reg_ = gpr_regs;
2088 cur_fpr_reg_ = fpr_regs;
2089 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002090 }
2091
2092 void PushGpr(uintptr_t val) {
2093 *cur_gpr_reg_ = val;
2094 cur_gpr_reg_++;
2095 }
2096
2097 void PushFpr4(float val) {
2098 *cur_fpr_reg_ = val;
2099 cur_fpr_reg_++;
2100 }
2101
2102 void PushFpr8(uint64_t val) {
2103 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2104 *tmp = val;
2105 cur_fpr_reg_ += 2;
2106 }
2107
2108 void PushStack(uintptr_t val) {
2109 *cur_stack_arg_ = val;
2110 cur_stack_arg_++;
2111 }
2112
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002113 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002114 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002115 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002116 }
2117
2118 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002119 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002120 uint32_t* cur_fpr_reg_;
2121 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002122};
Andreas Gampec147b002014-03-06 18:11:06 -08002123
Andreas Gampec200a4a2014-06-16 18:39:09 -07002124// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2125// of transitioning into native code.
2126class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2127 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002128 BuildGenericJniFrameVisitor(Thread* self,
2129 bool is_static,
2130 bool critical_native,
2131 const char* shorty,
2132 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002133 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002134 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002135 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2136 sm_(&jni_call_) {
2137 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002138 uintptr_t* start_gpr_reg;
2139 uint32_t* start_fpr_reg;
2140 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002141 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002142 &handle_scope_,
2143 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002144 &start_gpr_reg, &start_fpr_reg);
2145
Andreas Gampec200a4a2014-06-16 18:39:09 -07002146 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2147
Igor Murashkin06a04e02016-09-13 15:57:37 -07002148 // First 2 parameters are always excluded for CriticalNative methods.
2149 if (LIKELY(!critical_native)) {
2150 // jni environment is always first argument
2151 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002152
Igor Murashkin06a04e02016-09-13 15:57:37 -07002153 if (is_static) {
2154 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2155 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002156 }
2157 }
2158
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002159 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002160
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002161 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002162
Vladimir Markof39745e2016-01-26 12:16:55 +00002163 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002164 return handle_scope_->GetHandle(0).GetReference();
2165 }
2166
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002167 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002168 return handle_scope_->GetHandle(0).ToJObject();
2169 }
2170
Ian Rogers1428dce2014-10-21 15:02:15 -07002171 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002172 return bottom_of_used_area_;
2173 }
2174
2175 private:
2176 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2177 class FillJniCall FINAL : public FillNativeCall {
2178 public:
2179 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002180 HandleScope* handle_scope, bool critical_native)
2181 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2182 handle_scope_(handle_scope),
2183 cur_entry_(0),
2184 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002186 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002187
2188 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2189 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2190 handle_scope_ = scope;
2191 cur_entry_ = 0U;
2192 }
2193
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002194 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002195 // Initialize padding entries.
2196 size_t expected_slots = handle_scope_->NumberOfReferences();
2197 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002198 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002199 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002200
2201 if (!critical_native_) {
2202 // Non-critical natives have at least the self class (jclass) or this (jobject).
2203 DCHECK_NE(cur_entry_, 0U);
2204 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002205 }
2206
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002207 bool CriticalNative() const {
2208 return critical_native_;
2209 }
2210
Andreas Gampec200a4a2014-06-16 18:39:09 -07002211 private:
2212 HandleScope* handle_scope_;
2213 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002214 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002215 };
2216
2217 HandleScope* handle_scope_;
2218 FillJniCall jni_call_;
2219 void* bottom_of_used_area_;
2220
2221 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002222
2223 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2224};
2225
Andreas Gampec200a4a2014-06-16 18:39:09 -07002226uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2227 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002228 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002229 h.Assign(ref);
2230 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2231 cur_entry_++;
2232 return tmp;
2233}
2234
Ian Rogers9758f792014-03-13 09:02:55 -07002235void BuildGenericJniFrameVisitor::Visit() {
2236 Primitive::Type type = GetParamPrimitiveType();
2237 switch (type) {
2238 case Primitive::kPrimLong: {
2239 jlong long_arg;
2240 if (IsSplitLongOrDouble()) {
2241 long_arg = ReadSplitLongParam();
2242 } else {
2243 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2244 }
2245 sm_.AdvanceLong(long_arg);
2246 break;
2247 }
2248 case Primitive::kPrimDouble: {
2249 uint64_t double_arg;
2250 if (IsSplitLongOrDouble()) {
2251 // Read into union so that we don't case to a double.
2252 double_arg = ReadSplitLongParam();
2253 } else {
2254 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2255 }
2256 sm_.AdvanceDouble(double_arg);
2257 break;
2258 }
2259 case Primitive::kPrimNot: {
2260 StackReference<mirror::Object>* stack_ref =
2261 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002262 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002263 break;
2264 }
2265 case Primitive::kPrimFloat:
2266 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2267 break;
2268 case Primitive::kPrimBoolean: // Fall-through.
2269 case Primitive::kPrimByte: // Fall-through.
2270 case Primitive::kPrimChar: // Fall-through.
2271 case Primitive::kPrimShort: // Fall-through.
2272 case Primitive::kPrimInt: // Fall-through.
2273 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2274 break;
2275 case Primitive::kPrimVoid:
2276 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002277 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002278 }
2279}
2280
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002281void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002282 // Clear out rest of the scope.
2283 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002284 if (!jni_call_.CriticalNative()) {
2285 // Install HandleScope.
2286 self->PushHandleScope(handle_scope_);
2287 }
Ian Rogers9758f792014-03-13 09:02:55 -07002288}
2289
Ian Rogers04c31d22014-07-07 21:44:06 -07002290#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002291extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002292#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002293extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002294#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002295
Igor Murashkin06a04e02016-09-13 15:57:37 -07002296static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2297 uint32_t cookie,
2298 bool fast_native ATTRIBUTE_UNUSED,
2299 jobject l,
2300 jobject lock) {
2301 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002302 if (lock != nullptr) {
2303 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2304 } else {
2305 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2306 }
2307}
2308
Igor Murashkin06a04e02016-09-13 15:57:37 -07002309static void artQuickGenericJniEndJNINonRef(Thread* self,
2310 uint32_t cookie,
2311 bool fast_native,
2312 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002313 if (lock != nullptr) {
2314 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002315 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002316 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002317 if (UNLIKELY(fast_native)) {
2318 JniMethodFastEnd(cookie, self);
2319 } else {
2320 JniMethodEnd(cookie, self);
2321 }
Andreas Gampead615172014-04-04 16:20:13 -07002322 }
2323}
2324
Andreas Gampec147b002014-03-06 18:11:06 -08002325/*
2326 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002327 * 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 -08002328 * The final element on the stack is a pointer to the native code.
2329 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002330 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002331 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002332 *
Andreas Gampec147b002014-03-06 18:11:06 -08002333 * The return of this function denotes:
2334 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2335 * 2) An error, if the value is negative.
2336 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002337extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002338 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002339 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002340 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002341 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002342 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002343 uint32_t shorty_len = 0;
2344 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002345 bool critical_native = called->IsCriticalNative();
2346 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002347 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002348
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002349 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002350 BuildGenericJniFrameVisitor visitor(self,
2351 called->IsStatic(),
2352 critical_native,
2353 shorty,
2354 shorty_len,
2355 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002356 {
2357 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2358 visitor.VisitArguments();
2359 // FinalizeHandleScope pushes the handle scope on the thread.
2360 visitor.FinalizeHandleScope(self);
2361 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002362
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002363 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002364 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002365
Ian Rogerse0dcd462014-03-08 15:21:04 -08002366 self->VerifyStack();
2367
Vladimir Markof8655b32018-03-21 17:53:56 +00002368 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2369 jit::Jit* jit = runtime->GetJit();
2370 if (jit != nullptr) {
2371 jit->MethodEntered(self, called);
2372 }
2373
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002374 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002375 uint32_t* sp32;
2376 // Skip calling JniMethodStart for @CriticalNative.
2377 if (LIKELY(!critical_native)) {
2378 // Start JNI, save the cookie.
2379 if (called->IsSynchronized()) {
2380 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2381 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2382 if (self->IsExceptionPending()) {
2383 self->PopHandleScope();
2384 // A negative value denotes an error.
2385 return GetTwoWordFailureValue();
2386 }
2387 } else {
2388 if (fast_native) {
2389 cookie = JniMethodFastStart(self);
2390 } else {
2391 DCHECK(normal_native);
2392 cookie = JniMethodStart(self);
2393 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002394 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002395 sp32 = reinterpret_cast<uint32_t*>(sp);
2396 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002397 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002398
Andreas Gampe90546832014-03-12 18:07:19 -07002399 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002400 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002401
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002402 // There are two cases for the content of nativeCode:
2403 // 1) Pointer to the native function.
2404 // 2) Pointer to the trampoline for native code binding.
2405 // In the second case, we need to execute the binding and continue with the actual native function
2406 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002407 DCHECK(nativeCode != nullptr);
2408 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002409#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002410 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002411#else
2412 nativeCode = artFindNativeMethod(self);
2413#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002414
2415 if (nativeCode == nullptr) {
2416 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002417
Igor Murashkin06a04e02016-09-13 15:57:37 -07002418 // @CriticalNative calls do not need to call back into JniMethodEnd.
2419 if (LIKELY(!critical_native)) {
2420 // End JNI, as the assembly will move to deliver the exception.
2421 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2422 if (shorty[0] == 'L') {
2423 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2424 } else {
2425 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2426 }
Andreas Gampead615172014-04-04 16:20:13 -07002427 }
2428
Andreas Gampec200a4a2014-06-16 18:39:09 -07002429 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002430 }
2431 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002432 }
2433
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002434#if defined(__mips__) && !defined(__LP64__)
2435 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2436 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2437 // and load into floating-point registers.
2438 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2439 // view):
2440 // (1)
2441 // | DOUBLE | DOUBLE | other args, if any
2442 // | F12 | F13 | F14 | F15 |
2443 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2444 // (2)
2445 // | DOUBLE | FLOAT | (PAD) | other args, if any
2446 // | F12 | F13 | F14 | |
2447 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2448 // (3)
2449 // | FLOAT | (PAD) | DOUBLE | other args, if any
2450 // | F12 | | F14 | F15 |
2451 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2452 // (4)
2453 // | FLOAT | FLOAT | other args, if any
2454 // | F12 | F14 |
2455 // | SP+0 | SP+4 | SP+8
2456 // As you can see, only the last case (4) is special. In all others we can just
2457 // load F12/F13 and F14/F15 in the same manner.
2458 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2459 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2460 if (nativeCode != nullptr &&
2461 shorty != nullptr &&
2462 shorty_len >= 3 &&
2463 shorty[1] == 'F' &&
2464 shorty[2] == 'F') {
2465 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2466 }
2467#endif
2468
Andreas Gampec200a4a2014-06-16 18:39:09 -07002469 // Return native code addr(lo) and bottom of alloca address(hi).
2470 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2471 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002472}
2473
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002474// Defined in quick_jni_entrypoints.cc.
2475extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2476 jvalue result, uint64_t result_f, ArtMethod* called,
2477 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002478/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002479 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002480 * unlocking.
2481 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002482extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2483 jvalue result,
2484 uint64_t result_f) {
2485 // We're here just back from a native call. We don't have the shared mutator lock at this point
2486 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2487 // anything that requires a mutator lock before that would cause problems as GC may have the
2488 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002489 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002490 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002491 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002492 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002493 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002494 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2495 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002496}
2497
Andreas Gamped58342c2014-06-05 14:18:08 -07002498// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2499// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002500//
Andreas Gamped58342c2014-06-05 14:18:08 -07002501// 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 -07002502// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002503
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002504template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002505static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2506 ObjPtr<mirror::Object> this_object,
2507 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002508 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002509 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002510 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002511 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002512 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002513 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002514 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2515 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002516 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002517 {
2518 // Remember the args in case a GC happens in FindMethodFromCode.
2519 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2520 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2521 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002522 method = FindMethodFromCode<type, access_check>(method_idx,
2523 &this_object,
2524 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002525 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002526 visitor.FixupReferences();
2527 }
2528
Ian Rogerse0a02da2014-12-02 14:10:53 -08002529 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002530 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002531 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002532 }
2533 }
2534 DCHECK(!self->IsExceptionPending());
2535 const void* code = method->GetEntryPointFromQuickCompiledCode();
2536
2537 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002538 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002539 << " location: "
2540 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002541
Andreas Gamped58342c2014-06-05 14:18:08 -07002542 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2543 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002544}
2545
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002546// Explicit artInvokeCommon template function declarations to please analysis tool.
2547#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002548 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002549 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002550 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002551
2552EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2553EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2554EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2555EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2556EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2557EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2558EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2559EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2560EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2561EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2562#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2563
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002564// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002565extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002566 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002567 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002568 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002569}
2570
Andreas Gampec200a4a2014-06-16 18:39:09 -07002571extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002572 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002573 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002574 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002575}
2576
Andreas Gampec200a4a2014-06-16 18:39:09 -07002577extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002578 uint32_t method_idx,
2579 mirror::Object* this_object ATTRIBUTE_UNUSED,
2580 Thread* self,
2581 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2582 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2583 // it doesn't cause ObjPtr alignment failure check.
2584 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002585}
2586
Andreas Gampec200a4a2014-06-16 18:39:09 -07002587extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002588 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002589 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002590 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002591}
2592
Andreas Gampec200a4a2014-06-16 18:39:09 -07002593extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002594 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002595 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002596 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002597}
2598
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002599// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2600extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2601 REQUIRES_SHARED(Locks::mutator_lock_) {
2602 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2603 DCHECK(!referrer->IsProxyMethod());
2604 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2605 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2606 DCHECK(result == nullptr ||
2607 result->GetDeclaringClass()->IsInterface() ||
2608 result->GetDeclaringClass() ==
2609 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2610 << result->PrettyMethod();
2611 return result;
2612}
2613
Jeff Hao5667f562017-02-27 19:32:01 -08002614// Determine target of interface dispatch. The interface method and this object are known non-null.
2615// The interface method is the method returned by the dex cache in the conflict trampoline.
2616extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002617 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002618 Thread* self,
2619 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002620 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002621 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002622 StackHandleScope<2> hs(self);
2623 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2624 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002625
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002626 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002627 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002628 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002629
Vladimir Marko302f69c2017-07-25 15:27:15 +01002630 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002631 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002632 // Fetch the dex_method_idx of the target interface method from the caller.
2633 uint32_t dex_method_idx;
2634 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002635 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002636 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002637 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2638 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002639 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002640 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002641 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002642 } else {
2643 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002644 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002645 }
2646
Vladimir Marko302f69c2017-07-25 15:27:15 +01002647 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002648 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002649 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2650 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002651 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002652 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002653 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2654 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2655 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2657 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2658 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002659 visitor.FixupReferences();
2660 }
2661
Vladimir Marko302f69c2017-07-25 15:27:15 +01002662 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002663 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002664 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002665 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002666 }
2667
2668 DCHECK(!interface_method->IsRuntimeMethod());
2669 // Look whether we have a match in the ImtConflictTable.
2670 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2671 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2672 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2673 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2674 DCHECK(current_table != nullptr);
2675 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2676 } else {
2677 // It seems we aren't really a conflict method!
2678 if (kIsDebugBuild) {
2679 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2680 CHECK_EQ(conflict_method, m)
2681 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2682 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2683 }
2684 method = conflict_method;
2685 }
2686 if (method != nullptr) {
2687 return GetTwoWordSuccessValue(
2688 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2689 reinterpret_cast<uintptr_t>(method));
2690 }
2691
2692 // No match, use the IfTable.
2693 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2694 if (UNLIKELY(method == nullptr)) {
2695 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2696 interface_method, this_object.Get(), caller_method);
2697 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002698 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002699
2700 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2701 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002702 DCHECK(conflict_method->IsRuntimeMethod());
2703 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2704 cls.Get(),
2705 conflict_method,
2706 interface_method,
2707 method,
2708 /*force_new_conflict_method*/false);
2709 if (new_conflict_method != conflict_method) {
2710 // Update the IMT if we create a new conflict method. No fence needed here, as the
2711 // data is consistent.
2712 imt->Set(imt_index,
2713 new_conflict_method,
2714 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002715 }
2716
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002717 const void* code = method->GetEntryPointFromQuickCompiledCode();
2718
2719 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002720 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002721 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002722
Andreas Gamped58342c2014-06-05 14:18:08 -07002723 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2724 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002725}
2726
Orion Hodsonac141392017-01-13 11:53:47 +00002727// Returns shorty type so the caller can determine how to put |result|
2728// into expected registers. The shorty type is static so the compiler
2729// could call different flavors of this code path depending on the
2730// shorty type though this would require different entry points for
2731// each type.
2732extern "C" uintptr_t artInvokePolymorphic(
2733 JValue* result,
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002734 mirror::Object* raw_receiver,
Orion Hodsonac141392017-01-13 11:53:47 +00002735 Thread* self,
2736 ArtMethod** sp)
2737 REQUIRES_SHARED(Locks::mutator_lock_) {
2738 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002739 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002740
2741 // Start new JNI local reference state
2742 JNIEnvExt* env = self->GetJniEnv();
2743 ScopedObjectAccessUnchecked soa(env);
2744 ScopedJniEnvLocalRefState env_state(env);
2745 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2746
2747 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2748 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2749 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002750 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002751 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2752 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002753 const uint32_t proto_idx = inst.VRegH();
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002754 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002755 const size_t shorty_length = strlen(shorty);
2756 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2757 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002758 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002759
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002760 // Wrap raw_receiver in a Handle for safety.
2761 StackHandleScope<3> hs(self);
2762 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2763 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002764 self->EndAssertNoThreadSuspension(old_cause);
2765
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002766 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002767 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002768 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002769 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002770
2771 if (UNLIKELY(receiver_handle.IsNull())) {
Orion Hodsonac141392017-01-13 11:53:47 +00002772 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2773 return static_cast<uintptr_t>('V');
2774 }
2775
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002776 // TODO(oth): Ensure this path isn't taken for VarHandle accessors (b/65872996).
2777 DCHECK_EQ(resolved_method->GetDeclaringClass(),
2778 WellKnownClasses::ToClass(WellKnownClasses::java_lang_invoke_MethodHandle));
2779
2780 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
2781 ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(receiver_handle.Get()))));
2782
Orion Hodsone7732be2017-10-11 14:35:20 +01002783 Handle<mirror::MethodType> method_type(
2784 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
2785
Orion Hodsonac141392017-01-13 11:53:47 +00002786 // This implies we couldn't resolve one or more types in this method handle.
2787 if (UNLIKELY(method_type.IsNull())) {
2788 CHECK(self->IsExceptionPending());
2789 return static_cast<uintptr_t>('V');
2790 }
2791
Vladimir Markod7559b72017-09-28 13:50:37 +01002792 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002793 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2794
2795 // Fix references before constructing the shadow frame.
2796 gc_visitor.FixupReferences();
2797
2798 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002799 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2800 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002801 const size_t first_arg = 0;
2802 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2803 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2804 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2805 ScopedStackedShadowFramePusher
2806 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2807 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2808 kMethodIsStatic,
2809 shorty,
2810 strlen(shorty),
2811 shadow_frame,
2812 first_arg);
2813 shadow_frame_builder.VisitArguments();
2814
2815 // Push a transition back into managed code onto the linked list in thread.
2816 ManagedStack fragment;
2817 self->PushManagedStackFragment(&fragment);
2818
2819 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2820 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002821 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002822 bool isExact = (jni::EncodeArtMethod(resolved_method) ==
2823 WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact);
2824 bool success = false;
2825 if (isExact) {
Orion Hodson960d4f72017-11-10 15:32:38 +00002826 success = MethodHandleInvokeExact(self,
2827 *shadow_frame,
2828 method_handle,
2829 method_type,
2830 &operands,
2831 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002832 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +00002833 success = MethodHandleInvoke(self,
2834 *shadow_frame,
2835 method_handle,
2836 method_type,
2837 &operands,
2838 result);
Orion Hodsonac141392017-01-13 11:53:47 +00002839 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002840 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002841
2842 // Pop transition record.
2843 self->PopManagedStackFragment(fragment);
2844
2845 return static_cast<uintptr_t>(shorty[0]);
2846}
2847
Ian Rogers848871b2013-08-05 10:56:33 -07002848} // namespace art