blob: e7eff0d25e0b058f99532f043896a95a6d23f2f9 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "common_throws.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010022#include "class_root.h"
Vladimir Marko606adb32018-04-05 14:49:24 +010023#include "debug_print.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
26#include "dex/dex_file_types.h"
27#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070028#include "dex/method_reference.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "entrypoints/entrypoint_utils-inl.h"
Vladimir Markod3083dd2018-05-17 08:43:47 +010030#include "entrypoints/quick/callee_save_frame.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070031#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070032#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070033#include "imt_conflict_table.h"
34#include "imtable-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000035#include "index_bss_mapping.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070036#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "interpreter/interpreter.h"
Orion Hodson4c8e12e2018-05-18 08:33:20 +010038#include "interpreter/interpreter_common.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010039#include "interpreter/shadow_frame-inl.h"
Vladimir Marko2196c652017-11-30 16:16:07 +000040#include "jit/jit.h"
Alex Light2d441b12018-06-08 15:33:21 -070041#include "jit/jit_code_cache.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000042#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000043#include "method_handles.h"
Ian Rogers848871b2013-08-05 10:56:33 -070044#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070045#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070046#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000047#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010050#include "mirror/var_handle.h"
Vladimir Markod3d00c02019-11-07 15:09:07 +000051#include "oat.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010052#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010053#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070054#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070055#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070056#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070057#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070058#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010059#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000060#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070061
62namespace art {
63
Andreas Gampe8228cdf2017-05-30 15:03:54 -070064// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070065class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080066 // Number of bytes for each out register in the caller method's frame.
67 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070068 // Frame size in bytes of a callee-save frame for RefsAndArgs.
69 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010070 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
71 // Offset of first GPR arg.
72 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
73 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
74 // Offset of first FPR arg.
75 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
76 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
77 // Offset of return address.
78 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
79 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070080#if defined(__arm__)
81 // The callee save frame is pointed to by SP.
82 // | argN | |
83 // | ... | |
84 // | arg4 | |
85 // | arg3 spill | | Caller's frame
86 // | arg2 spill | |
87 // | arg1 spill | |
88 // | Method* | ---
89 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080090 // | ... | 4x6 bytes callee saves
91 // | R3 |
92 // | R2 |
93 // | R1 |
94 // | S15 |
95 // | : |
96 // | S0 |
97 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070098 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070099 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
100 static constexpr bool kAlignPairRegister = true;
101 static constexpr bool kQuickSoftFloatAbi = false;
102 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000103 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800104 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -0700105 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800106 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800107 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000108 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800109 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110#elif defined(__aarch64__)
111 // The callee save frame is pointed to by SP.
112 // | argN | |
113 // | ... | |
114 // | arg4 | |
115 // | arg3 spill | | Caller's frame
116 // | arg2 spill | |
117 // | arg1 spill | |
118 // | Method* | ---
119 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800120 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000121 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100122 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 // | X7 |
124 // | : |
125 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800126 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000127 // | : |
128 // | D0 |
129 // | | padding
130 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500131 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000132 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000133 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800134 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000135 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000136 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
137 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800138 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000139 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000140 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000141 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800142#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700143 // The callee save frame is pointed to by SP.
144 // | argN | |
145 // | ... | |
146 // | arg4 | |
147 // | arg3 spill | | Caller's frame
148 // | arg2 spill | |
149 // | arg1 spill | |
150 // | Method* | ---
151 // | RA |
152 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | T1 | arg5
154 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700155 // | A3 | arg3
156 // | A2 | arg2
157 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800158 // | F19 |
159 // | F18 | f_arg5
160 // | F17 |
161 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000162 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800163 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000164 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800165 // | F12 | f_arg2
166 // | F11 |
167 // | F10 | f_arg1
168 // | F9 |
169 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000170 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700171 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000172 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
173 static constexpr bool kAlignPairRegister = true;
174 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800175 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000176 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800177 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
178 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
179 // passed only in even numbered registers and each
180 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800181 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800182 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000183 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800184 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800185#elif defined(__mips__) && defined(__LP64__)
186 // The callee save frame is pointed to by SP.
187 // | argN | |
188 // | ... | |
189 // | arg4 | |
190 // | arg3 spill | | Caller's frame
191 // | arg2 spill | |
192 // | arg1 spill | |
193 // | Method* | ---
194 // | RA |
195 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800196 // | A7 | arg7
197 // | A6 | arg6
198 // | A5 | arg5
199 // | A4 | arg4
200 // | A3 | arg3
201 // | A2 | arg2
202 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000203 // | F19 | f_arg7
204 // | F18 | f_arg6
205 // | F17 | f_arg5
206 // | F16 | f_arg4
207 // | F15 | f_arg3
208 // | F14 | f_arg2
209 // | F13 | f_arg1
210 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800211 // | | padding
212 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800213 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800214 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800215 static constexpr bool kAlignPairRegister = false;
216 static constexpr bool kQuickSoftFloatAbi = false;
217 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000218 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800219 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
220 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
221 static constexpr bool kGprFprLockstep = true;
222
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800223 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
224 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
225 }
Ian Rogers848871b2013-08-05 10:56:33 -0700226#elif defined(__i386__)
227 // The callee save frame is pointed to by SP.
228 // | argN | |
229 // | ... | |
230 // | arg4 | |
231 // | arg3 spill | | Caller's frame
232 // | arg2 spill | |
233 // | arg1 spill | |
234 // | Method* | ---
235 // | Return |
236 // | EBP,ESI,EDI | callee saves
237 // | EBX | arg3
238 // | EDX | arg2
239 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000240 // | XMM3 | float arg 4
241 // | XMM2 | float arg 3
242 // | XMM1 | float arg 2
243 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700244 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500245 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000246 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800248 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000249 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800250 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000251 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800252 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000254 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800255 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 // The callee save frame is pointed to by SP.
258 // | argN | |
259 // | ... | |
260 // | reg. arg spills | | Caller's frame
261 // | Method* | ---
262 // | Return |
263 // | R15 | callee save
264 // | R14 | callee save
265 // | R13 | callee save
266 // | R12 | callee save
267 // | R9 | arg5
268 // | R8 | arg4
269 // | RSI/R6 | arg1
270 // | RBP/R5 | callee save
271 // | RBX/R3 | callee save
272 // | RDX/R2 | arg2
273 // | RCX/R1 | arg3
274 // | XMM7 | float arg 8
275 // | XMM6 | float arg 7
276 // | XMM5 | float arg 6
277 // | XMM4 | float arg 5
278 // | XMM3 | float arg 4
279 // | XMM2 | float arg 3
280 // | XMM1 | float arg 2
281 // | XMM0 | float arg 1
282 // | Padding |
283 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500284 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000285 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800286 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800287 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000288 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700289 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700290 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800291 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800292 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
293 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000294 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
296 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
297 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
298 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700300 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
Elliott Hughesc1896c92018-11-29 11:33:18 -0800301 UNREACHABLE();
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 }
303 }
Ian Rogers848871b2013-08-05 10:56:33 -0700304#else
305#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700306#endif
307
Ian Rogers936b37f2014-02-14 00:52:24 -0800308 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100309 // Special handling for proxy methods. Proxy methods are instance methods so the
310 // 'this' object is the 1st argument. They also have the same frame layout as the
311 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
312 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000313 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700314 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000315 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100316 CHECK_GT(kNumQuickGprArgs, 0u);
317 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
318 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
319 GprIndexToGprOffset(kThisGprIndex);
320 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000321 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100322 }
323
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700324 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700326 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100327 }
328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 uint8_t* previous_sp =
332 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100338 constexpr size_t callee_frame_size =
339 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
341 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100343 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
344 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100345
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100346 if (current_code->IsOptimized()) {
David Srbecky0d4567f2019-05-30 22:45:40 +0100347 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code);
David Srbecky052f8ca2018-04-26 15:42:54 +0100348 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 DCHECK(stack_map.IsValid());
David Srbecky93bd3612018-07-02 19:30:18 +0100350 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
351 if (!inline_infos.empty()) {
352 return inline_infos.back().GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100353 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100354 return stack_map.GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100355 }
356 } else {
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +0000357 return current_code->ToDexPc(caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100358 }
Ian Rogers848871b2013-08-05 10:56:33 -0700359 }
360
Ian Rogers936b37f2014-02-14 00:52:24 -0800361 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100364 uint8_t* return_adress_spill =
365 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset;
366 return *reinterpret_cast<uintptr_t*>(return_adress_spill);
Ian Rogers848871b2013-08-05 10:56:33 -0700367 }
368
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700370 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700371 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700372 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
373 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
374 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800376 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
377 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800378 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
379 "Number of Quick FPR arguments unexpected");
380 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
381 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800382 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
383 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800384 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
385 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700386 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800387 }
Ian Rogers848871b2013-08-05 10:56:33 -0700388
389 virtual ~QuickArgumentVisitor() {}
390
391 virtual void Visit() = 0;
392
Ian Rogers936b37f2014-02-14 00:52:24 -0800393 Primitive::Type GetParamPrimitiveType() const {
394 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700395 }
396
Ian Rogers13735952014-10-08 12:43:28 -0700397 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800398 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800399 Primitive::Type type = GetParamPrimitiveType();
400 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800401 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
402 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
403 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
404 }
405 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000406 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800407 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700408 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800409 }
410 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800411 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800412 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
413 }
414 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700415 }
416
417 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
419 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800420 return is_split_long_or_double_;
421 } else {
422 return false; // An optimization for when GPR and FPRs are 64bit.
423 }
Ian Rogers848871b2013-08-05 10:56:33 -0700424 }
425
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700427 return GetParamPrimitiveType() == Primitive::kPrimNot;
428 }
429
Ian Rogers936b37f2014-02-14 00:52:24 -0800430 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700431 Primitive::Type type = GetParamPrimitiveType();
432 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
433 }
434
435 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000436 // The splitted long is always available through the stack.
437 return *reinterpret_cast<uint64_t*>(stack_args_
438 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700439 }
440
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800441 void IncGprIndex() {
442 gpr_index_++;
443 if (kGprFprLockstep) {
444 fpr_index_++;
445 }
446 }
447
448 void IncFprIndex() {
449 fpr_index_++;
450 if (kGprFprLockstep) {
451 gpr_index_++;
452 }
453 }
454
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700455 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800456 // (a) 'stack_args_' should point to the first method's argument
457 // (b) whatever the argument type it is, the 'stack_index_' should
458 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800459 gpr_index_ = 0;
460 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800461 if (kQuickDoubleRegAlignedFloatBackFilled) {
462 fpr_double_index_ = 0;
463 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800464 stack_index_ = 0;
465 if (!is_static_) { // Handle this.
466 cur_type_ = Primitive::kPrimNot;
467 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700468 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800469 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800470 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800471 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800472 }
Ian Rogers848871b2013-08-05 10:56:33 -0700473 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800474 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
475 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
476 switch (cur_type_) {
477 case Primitive::kPrimNot:
478 case Primitive::kPrimBoolean:
479 case Primitive::kPrimByte:
480 case Primitive::kPrimChar:
481 case Primitive::kPrimShort:
482 case Primitive::kPrimInt:
483 is_split_long_or_double_ = false;
484 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800485 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800486 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800487 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800488 }
489 break;
490 case Primitive::kPrimFloat:
491 is_split_long_or_double_ = false;
492 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800493 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800494 if (kQuickSoftFloatAbi) {
495 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800496 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 }
498 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800499 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800500 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800501 if (kQuickDoubleRegAlignedFloatBackFilled) {
502 // Double should not overlap with float.
503 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
504 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
505 // Float should not overlap with double.
506 if (fpr_index_ % 2 == 0) {
507 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
508 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000509 } else if (kQuickSkipOddFpRegisters) {
510 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800511 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 }
513 }
514 break;
515 case Primitive::kPrimDouble:
516 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800517 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800518 if (cur_type_ == Primitive::kPrimLong &&
519#if defined(__mips__) && !defined(__LP64__)
520 (gpr_index_ == 0 || gpr_index_ == 2) &&
521#else
522 gpr_index_ == 0 &&
523#endif
524 kAlignPairRegister) {
525 // Currently, this is only for ARM and MIPS, where we align long parameters with
526 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
527 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800528 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000529 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000530 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800531 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500532 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
533 // We don't want to split this. Pass over this register.
534 gpr_index_++;
535 is_split_long_or_double_ = false;
536 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800537 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800538 if (kBytesStackArgLocation == 4) {
539 stack_index_+= 2;
540 } else {
541 CHECK_EQ(kBytesStackArgLocation, 8U);
542 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800543 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700544 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800545 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000546 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700547 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800548 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700549 }
550 }
551 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800552 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000553 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800554 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800555 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700556 if (kBytesStackArgLocation == 4) {
557 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800558 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700559 CHECK_EQ(kBytesStackArgLocation, 8U);
560 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800561 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800562 if (kQuickDoubleRegAlignedFloatBackFilled) {
563 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
564 fpr_double_index_ += 2;
565 // Float should not overlap with double.
566 if (fpr_index_ % 2 == 0) {
567 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
568 }
569 }
570 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800571 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800572 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
573 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800574 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800575 }
576 }
577 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 }
579 break;
580 default:
581 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
582 }
Ian Rogers848871b2013-08-05 10:56:33 -0700583 }
584 }
585
Andreas Gampec200a4a2014-06-16 18:39:09 -0700586 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700587 const bool is_static_;
588 const char* const shorty_;
589 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700590
591 private:
Ian Rogers13735952014-10-08 12:43:28 -0700592 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
593 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
594 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800595 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800596 // Index into spilled FPRs.
597 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
598 // holds a higher register number.
599 uint32_t fpr_index_;
600 // Index into spilled FPRs for aligned double.
601 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
602 // terms of singles, may be behind fpr_index.
603 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800604 uint32_t stack_index_; // Index into arguments on the stack.
605 // The current type of argument during VisitArguments.
606 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700607 // Does a 64bit parameter straddle the register and stack arguments?
608 bool is_split_long_or_double_;
609};
610
Sebastien Hertza836bc92014-11-25 16:30:53 +0100611// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
612// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700614 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000615 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
616}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100617
Ian Rogers848871b2013-08-05 10:56:33 -0700618// Visits arguments on the stack placing them into the shadow frame.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100619class BuildQuickShadowFrameVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700620 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
622 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700623 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700624
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100625 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Ian Rogers848871b2013-08-05 10:56:33 -0700626
627 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800628 ShadowFrame* const sf_;
629 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700630
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700631 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700632};
633
Andreas Gampec200a4a2014-06-16 18:39:09 -0700634void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700635 Primitive::Type type = GetParamPrimitiveType();
636 switch (type) {
637 case Primitive::kPrimLong: // Fall-through.
638 case Primitive::kPrimDouble:
639 if (IsSplitLongOrDouble()) {
640 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
641 } else {
642 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
643 }
644 ++cur_reg_;
645 break;
646 case Primitive::kPrimNot: {
647 StackReference<mirror::Object>* stack_ref =
648 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
649 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
650 }
651 break;
652 case Primitive::kPrimBoolean: // Fall-through.
653 case Primitive::kPrimByte: // Fall-through.
654 case Primitive::kPrimChar: // Fall-through.
655 case Primitive::kPrimShort: // Fall-through.
656 case Primitive::kPrimInt: // Fall-through.
657 case Primitive::kPrimFloat:
658 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
659 break;
660 case Primitive::kPrimVoid:
661 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700662 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700663 }
664 ++cur_reg_;
665}
666
Mingyao Yang417528d2017-09-13 12:10:40 -0700667// Don't inline. See b/65159206.
668NO_INLINE
669static void HandleDeoptimization(JValue* result,
670 ArtMethod* method,
671 ShadowFrame* deopt_frame,
672 ManagedStack* fragment)
673 REQUIRES_SHARED(Locks::mutator_lock_) {
674 // Coming from partial-fragment deopt.
675 Thread* self = Thread::Current();
676 if (kIsDebugBuild) {
677 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
678 // of the call-stack) corresponds to the called method.
679 ShadowFrame* linked = deopt_frame;
680 while (linked->GetLink() != nullptr) {
681 linked = linked->GetLink();
682 }
683 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
684 << ArtMethod::PrettyMethod(linked->GetMethod());
685 }
686
687 if (VLOG_IS_ON(deopt)) {
688 // Print out the stack to verify that it was a partial-fragment deopt.
689 LOG(INFO) << "Continue-ing from deopt. Stack is:";
690 QuickExceptionHandler::DumpFramesWithType(self, true);
691 }
692
693 ObjPtr<mirror::Throwable> pending_exception;
694 bool from_code = false;
695 DeoptimizationMethodType method_type;
696 self->PopDeoptimizationContext(/* out */ result,
697 /* out */ &pending_exception,
698 /* out */ &from_code,
699 /* out */ &method_type);
700
701 // Push a transition back into managed code onto the linked list in thread.
702 self->PushManagedStackFragment(fragment);
703
704 // Ensure that the stack is still in order.
705 if (kIsDebugBuild) {
706 class DummyStackVisitor : public StackVisitor {
707 public:
708 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
709 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
710
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100711 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang417528d2017-09-13 12:10:40 -0700712 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
713 // logic. Just always say we want to continue.
714 return true;
715 }
716 };
717 DummyStackVisitor dsv(self);
718 dsv.WalkStack();
719 }
720
721 // Restore the exception that was pending before deoptimization then interpret the
722 // deoptimized frames.
723 if (pending_exception != nullptr) {
724 self->SetException(pending_exception);
725 }
726 interpreter::EnterInterpreterFromDeoptimize(self,
727 deopt_frame,
728 result,
729 from_code,
730 DeoptimizationMethodType::kDefault);
731}
732
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700734 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700735 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
736 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700737 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700738
Alex Light9139e002015-10-09 15:59:48 -0700739 if (UNLIKELY(!method->IsInvokable())) {
740 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700741 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700742 }
743
744 JValue tmp_value;
745 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700746 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700747 ManagedStack fragment;
748
David Sehr709b0702016-10-13 09:12:37 -0700749 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700750 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700751 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800752 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000753 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700754 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
755
756 JValue result;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000757 bool force_frame_pop = false;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700758
Mingyao Yang417528d2017-09-13 12:10:40 -0700759 if (UNLIKELY(deopt_frame != nullptr)) {
760 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700761 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700762 const char* old_cause = self->StartAssertNoThreadSuspension(
763 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800764 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700765 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700766 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700767 CREATE_SHADOW_FRAME(num_regs, /* link= */ nullptr, method, /* dex_pc= */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700768 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800769 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700770 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800771 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700772 shadow_frame_builder.VisitArguments();
773 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700774 self->PushManagedStackFragment(&fragment);
775 self->PushShadowFrame(shadow_frame);
776 self->EndAssertNoThreadSuspension(old_cause);
777
Vladimir Marko5115a4d2019-10-17 14:56:47 +0100778 if (NeedsClinitCheckBeforeCall(method)) {
Vladimir Markobf121912019-06-04 13:49:05 +0100779 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Vladimir Marko8e110652019-07-30 10:14:41 +0100780 if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
Vladimir Markobf121912019-06-04 13:49:05 +0100781 // Ensure static method's class is initialized.
782 StackHandleScope<1> hs(self);
Vladimir Marko8e110652019-07-30 10:14:41 +0100783 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Vladimir Markobf121912019-06-04 13:49:05 +0100784 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Vladimir Marko8e110652019-07-30 10:14:41 +0100785 DCHECK(Thread::Current()->IsExceptionPending()) << method->PrettyMethod();
Vladimir Markobf121912019-06-04 13:49:05 +0100786 self->PopManagedStackFragment(fragment);
787 return 0;
788 }
Ian Rogers848871b2013-08-05 10:56:33 -0700789 }
790 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200791
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800792 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000793 force_frame_pop = shadow_frame->GetForcePopFrame();
Ian Rogers848871b2013-08-05 10:56:33 -0700794 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700795
796 // Pop transition.
797 self->PopManagedStackFragment(fragment);
798
799 // Request a stack deoptimization if needed
800 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700801 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700802 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
Alex Light3dacdd62019-03-12 15:45:47 +0000803 // should be done and it knows the real return pc. NB If the upcall is null we don't need to do
804 // anything. This can happen during shutdown or early startup.
805 if (UNLIKELY(
806 caller != nullptr &&
807 caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
808 (self->IsForceInterpreter() || Dbg::IsForcedInterpreterNeededForUpcall(self, caller)))) {
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000809 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
810 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
811 << caller->PrettyMethod();
812 } else {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000813 VLOG(deopt) << "Forcing deoptimization on return from method " << method->PrettyMethod()
814 << " to " << caller->PrettyMethod()
815 << (force_frame_pop ? " for frame-pop" : "");
816 DCHECK(!force_frame_pop || result.GetJ() == 0) << "Force frame pop should have no result.";
817 if (force_frame_pop && self->GetException() != nullptr) {
818 LOG(WARNING) << "Suppressing exception for instruction-retry: "
819 << self->GetException()->Dump();
820 }
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000821 // Push the context of the deoptimization stack so we can restore the return value and the
822 // exception before executing the deoptimized frames.
823 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700824 result,
825 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
Alex Light0aa7a5a2018-10-10 15:58:14 +0000826 force_frame_pop ? nullptr : self->GetException(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700827 /* from_code= */ false,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700828 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700829
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000830 // Set special exception to cause deoptimization.
831 self->SetException(Thread::GetDeoptimizationException());
832 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700833 }
834
835 // No need to restore the args since the method has already been run by the interpreter.
836 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700837}
838
839// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
840// to jobjects.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100841class BuildQuickArgumentVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700842 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700844 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700845 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100847 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Ian Rogers848871b2013-08-05 10:56:33 -0700848
849 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700850 ScopedObjectAccessUnchecked* const soa_;
851 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700852
Ian Rogers848871b2013-08-05 10:56:33 -0700853 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
854};
855
Ian Rogers9758f792014-03-13 09:02:55 -0700856void BuildQuickArgumentVisitor::Visit() {
857 jvalue val;
858 Primitive::Type type = GetParamPrimitiveType();
859 switch (type) {
860 case Primitive::kPrimNot: {
861 StackReference<mirror::Object>* stack_ref =
862 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
863 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700864 break;
865 }
866 case Primitive::kPrimLong: // Fall-through.
867 case Primitive::kPrimDouble:
868 if (IsSplitLongOrDouble()) {
869 val.j = ReadSplitLongParam();
870 } else {
871 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
872 }
873 break;
874 case Primitive::kPrimBoolean: // Fall-through.
875 case Primitive::kPrimByte: // Fall-through.
876 case Primitive::kPrimChar: // Fall-through.
877 case Primitive::kPrimShort: // Fall-through.
878 case Primitive::kPrimInt: // Fall-through.
879 case Primitive::kPrimFloat:
880 val.i = *reinterpret_cast<jint*>(GetParamAddress());
881 break;
882 case Primitive::kPrimVoid:
883 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700884 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700885 }
886 args_->push_back(val);
887}
888
Ian Rogers848871b2013-08-05 10:56:33 -0700889// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
890// which is responsible for recording callee save registers. We explicitly place into jobjects the
891// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
892// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893extern "C" uint64_t artQuickProxyInvokeHandler(
894 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700895 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700896 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
897 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700898 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
899 const char* old_cause =
900 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
901 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700902 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700903 self->VerifyStack();
904 // Start new JNI local reference state.
905 JNIEnvExt* env = self->GetJniEnv();
906 ScopedObjectAccessUnchecked soa(env);
907 ScopedJniEnvLocalRefState env_state(env);
908 // Create local ref. copies of proxy method and the receiver.
909 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
910
911 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700912 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700913 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
914 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700915 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700916 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000918 BuildQuickArgumentVisitor local_ref_visitor(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700919 sp, /* is_static= */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700920
Ian Rogers848871b2013-08-05 10:56:33 -0700921 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700922 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700923 args.erase(args.begin());
924
925 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700926 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700927 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
928 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700929 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700930 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700931 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700932 ObjPtr<mirror::Method> interface_reflect_method =
933 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
934 if (interface_reflect_method == nullptr) {
935 soa.Self()->AssertPendingOOMException();
936 return 0;
937 }
938 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700939
940 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
Alex Lightc9167362018-06-11 16:46:43 -0700941 // that performs allocations or instrumentation events.
942 instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation();
943 if (instr->HasMethodEntryListeners()) {
944 instr->MethodEnterEvent(soa.Self(),
Vladimir Marko19711d42019-04-12 14:05:34 +0100945 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700946 proxy_method,
947 0);
948 if (soa.Self()->IsExceptionPending()) {
949 instr->MethodUnwindEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100950 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700951 proxy_method,
952 0);
953 return 0;
954 }
955 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700956 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Alex Lightc9167362018-06-11 16:46:43 -0700957 if (soa.Self()->IsExceptionPending()) {
958 if (instr->HasMethodUnwindListeners()) {
959 instr->MethodUnwindEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100960 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700961 proxy_method,
962 0);
963 }
964 } else if (instr->HasMethodExitListeners()) {
965 instr->MethodExitEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100966 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700967 proxy_method,
968 0,
Alex Lightb7c640d2019-03-20 15:52:13 -0700969 {},
Alex Lightc9167362018-06-11 16:46:43 -0700970 result);
971 }
Ian Rogers848871b2013-08-05 10:56:33 -0700972 return result.GetJ();
973}
974
Roland Levillainad0777d2018-02-12 20:00:18 +0000975// Visitor returning a reference argument at a given position in a Quick stack frame.
976// NOTE: Only used for testing purposes.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100977class GetQuickReferenceArgumentAtVisitor final : public QuickArgumentVisitor {
Roland Levillainad0777d2018-02-12 20:00:18 +0000978 public:
979 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
980 const char* shorty,
981 uint32_t shorty_len,
982 size_t arg_pos)
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700983 : QuickArgumentVisitor(sp, /* is_static= */ false, shorty, shorty_len),
Roland Levillainad0777d2018-02-12 20:00:18 +0000984 cur_pos_(0u),
985 arg_pos_(arg_pos),
986 ref_arg_(nullptr) {
987 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
988 }
989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100990 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override {
Roland Levillainad0777d2018-02-12 20:00:18 +0000991 if (cur_pos_ == arg_pos_) {
992 Primitive::Type type = GetParamPrimitiveType();
993 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
994 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
995 }
996 ++cur_pos_;
997 }
998
999 StackReference<mirror::Object>* GetReferenceArgument() {
1000 return ref_arg_;
1001 }
1002
1003 private:
1004 // The position of the currently visited argument.
1005 size_t cur_pos_;
1006 // The position of the searched argument.
1007 const size_t arg_pos_;
1008 // The reference argument, if found.
1009 StackReference<mirror::Object>* ref_arg_;
1010
1011 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
1012};
1013
1014// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
1015// NOTE: Only used for testing purposes.
1016extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
1017 ArtMethod** sp)
1018 REQUIRES_SHARED(Locks::mutator_lock_) {
1019 ArtMethod* proxy_method = *sp;
1020 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1021 CHECK(!non_proxy_method->IsStatic())
1022 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1023 uint32_t shorty_len = 0;
1024 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1025 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1026 ref_arg_visitor.VisitArguments();
1027 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1028 return ref_arg;
1029}
1030
1031// Visitor returning all the reference arguments in a Quick stack frame.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001032class GetQuickReferenceArgumentsVisitor final : public QuickArgumentVisitor {
Roland Levillainad0777d2018-02-12 20:00:18 +00001033 public:
1034 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1035 bool is_static,
1036 const char* shorty,
1037 uint32_t shorty_len)
1038 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001040 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override {
Roland Levillainad0777d2018-02-12 20:00:18 +00001041 Primitive::Type type = GetParamPrimitiveType();
1042 if (type == Primitive::kPrimNot) {
1043 StackReference<mirror::Object>* ref_arg =
1044 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1045 ref_args_.push_back(ref_arg);
1046 }
1047 }
1048
1049 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1050 return ref_args_;
1051 }
1052
1053 private:
1054 // The reference arguments.
1055 std::vector<StackReference<mirror::Object>*> ref_args_;
1056
1057 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1058};
1059
1060// Returning all reference arguments in Quick stack frame at address `sp`.
1061std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1062 REQUIRES_SHARED(Locks::mutator_lock_) {
1063 ArtMethod* proxy_method = *sp;
1064 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1065 CHECK(!non_proxy_method->IsStatic())
1066 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1067 uint32_t shorty_len = 0;
1068 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001069 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /*is_static=*/ false, shorty, shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +00001070 ref_args_visitor.VisitArguments();
1071 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1072 return ref_args;
1073}
1074
Ian Rogers848871b2013-08-05 10:56:33 -07001075// Read object references held in arguments from quick frames and place in a JNI local references,
1076// so they don't get garbage collected.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001077class RememberForGcArgumentVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001078 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001079 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1080 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001081 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001083 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001084
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001085 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001086
1087 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001088 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001089 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001090 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1091
Mathieu Chartier590fee92013-09-13 13:46:47 -07001092 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001093};
1094
Ian Rogers9758f792014-03-13 09:02:55 -07001095void RememberForGcArgumentVisitor::Visit() {
1096 if (IsParamAReference()) {
1097 StackReference<mirror::Object>* stack_ref =
1098 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1099 jobject reference =
1100 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1101 references_.push_back(std::make_pair(reference, stack_ref));
1102 }
1103}
1104
1105void RememberForGcArgumentVisitor::FixupReferences() {
1106 // Fixup any references which may have changed.
1107 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001108 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001109 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001110 }
1111}
1112
Alex Lightb7edcda2017-04-27 13:20:31 -07001113extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1114 mirror::Object* this_object,
1115 Thread* self,
1116 ArtMethod** sp)
1117 REQUIRES_SHARED(Locks::mutator_lock_) {
1118 const void* result;
1119 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1120 // that part.
1121 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1122 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Alex Light6cae5ea2018-06-07 17:07:02 -07001123 DCHECK(!method->IsProxyMethod())
1124 << "Proxy method " << method->PrettyMethod()
1125 << " (declaring class: " << method->GetDeclaringClass()->PrettyClass() << ")"
1126 << " should not hit instrumentation entrypoint.";
Alex Lightb7edcda2017-04-27 13:20:31 -07001127 if (instrumentation->IsDeoptimized(method)) {
1128 result = GetQuickToInterpreterBridge();
1129 } else {
Alex Light2d441b12018-06-08 15:33:21 -07001130 // This will get the entry point either from the oat file, the JIT or the appropriate bridge
1131 // method if none of those can be found.
1132 result = instrumentation->GetCodeForInvoke(method);
1133 jit::Jit* jit = Runtime::Current()->GetJit();
1134 DCHECK_NE(result, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod();
1135 DCHECK(jit == nullptr ||
1136 // Native methods come through here in Interpreter entrypoints. We might not have
1137 // disabled jit-gc but that is fine since we won't return jit-code for native methods.
1138 method->IsNative() ||
1139 !jit->GetCodeCache()->GetGarbageCollectCode());
1140 DCHECK(!method->IsNative() ||
1141 jit == nullptr ||
1142 !jit->GetCodeCache()->ContainsPc(result))
1143 << method->PrettyMethod() << " code will jump to possibly cleaned up jit code!";
Alex Lightb7edcda2017-04-27 13:20:31 -07001144 }
1145
1146 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1147 bool is_static = method->IsStatic();
1148 uint32_t shorty_len;
1149 const char* shorty =
1150 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1151
1152 ScopedObjectAccessUnchecked soa(self);
1153 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1154 visitor.VisitArguments();
1155
1156 instrumentation->PushInstrumentationStackFrame(self,
1157 is_static ? nullptr : this_object,
1158 method,
1159 QuickArgumentVisitor::GetCallingPc(sp),
1160 interpreter_entry);
1161
1162 visitor.FixupReferences();
1163 if (UNLIKELY(self->IsExceptionPending())) {
1164 return nullptr;
1165 }
1166 CHECK(result != nullptr) << method->PrettyMethod();
1167 return result;
1168}
1169
1170extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1171 ArtMethod** sp,
1172 uint64_t* gpr_result,
1173 uint64_t* fpr_result)
1174 REQUIRES_SHARED(Locks::mutator_lock_) {
1175 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1176 CHECK(gpr_result != nullptr);
1177 CHECK(fpr_result != nullptr);
1178 // Instrumentation exit stub must not be entered with a pending exception.
1179 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1180 << self->GetException()->Dump();
1181 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001182 constexpr size_t return_pc_offset =
1183 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001184 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1185 return_pc_offset);
1186 CHECK_EQ(*return_pc, 0U);
1187
1188 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1189 // deoptimization shouldn't be performed with the high-half having the return address. When
1190 // deoptimization should be performed the low half is zero and the high-half the address of the
1191 // deoptimization entry point.
1192 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1193 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1194 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001195 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001196 return GetTwoWordFailureValue();
1197 }
1198 return return_or_deoptimize_pc;
1199}
1200
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001201static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1202 REQUIRES_SHARED(Locks::mutator_lock_) {
1203 if (dex_pc == static_cast<uint32_t>(-1)) {
1204 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1205 return "<native>";
1206 } else {
1207 CodeItemInstructionAccessor accessor = method->DexInstructions();
1208 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1209 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1210 }
1211}
1212
Vladimir Marko606adb32018-04-05 14:49:24 +01001213static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1214 REQUIRES_SHARED(Locks::mutator_lock_) {
1215 std::string storage;
1216 const char* descriptor = klass->GetDescriptor(&storage);
1217 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1218 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1219 if (oat_dex_file != nullptr) {
1220 const OatFile* oat_file = oat_dex_file->GetOatFile();
1221 const char* dex2oat_cmdline =
1222 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1223 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1224 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1225 }
1226}
1227
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001228static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1229 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001230 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001231
1232 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1233 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1234
Vladimir Markod3083dd2018-05-17 08:43:47 +01001235 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001236 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1237 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001238 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001239 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1240 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1241 ArtMethod* outer_method = *caller_sp;
1242
1243 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1244 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1245 << " native pc: " << caller_pc << " Instrumented!";
1246 return;
1247 }
1248
1249 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1250 CHECK(current_code != nullptr);
1251 CHECK(current_code->IsOptimized());
1252 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +01001253 CodeInfo code_info(current_code);
David Srbecky052f8ca2018-04-26 15:42:54 +01001254 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001255 CHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +01001256 uint32_t dex_pc = stack_map.GetDexPc();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001257
1258 // Log the outer method and its associated dex file and class table pointer which can be used
1259 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1260 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1261 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1262 << " native pc: " << caller_pc
1263 << " dex pc: " << dex_pc
1264 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1265 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001266 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001267 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1268
1269 ArtMethod* caller = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +01001270 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
1271 for (InlineInfo inline_info : inline_infos) {
1272 const char* tag = "";
1273 dex_pc = inline_info.GetDexPc();
1274 if (inline_info.EncodesArtMethod()) {
1275 tag = "encoded ";
1276 caller = inline_info.GetArtMethod();
1277 } else {
David Srbecky8cd54542018-07-15 23:58:44 +01001278 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
David Srbecky93bd3612018-07-02 19:30:18 +01001279 if (dex_pc == static_cast<uint32_t>(-1)) {
1280 tag = "special ";
1281 CHECK(inline_info.Equals(inline_infos.back()));
1282 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1283 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001284 } else {
David Srbecky93bd3612018-07-02 19:30:18 +01001285 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1286 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1287 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1288 CHECK(caller != nullptr);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001289 }
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001290 }
David Srbecky93bd3612018-07-02 19:30:18 +01001291 LOG(FATAL_WITHOUT_ABORT) << "InlineInfo #" << inline_info.Row()
1292 << ": " << tag << caller->PrettyMethod()
1293 << " dex pc: " << dex_pc
1294 << " dex file: " << caller->GetDexFile()->GetLocation()
1295 << " class table: "
1296 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1297 DumpB74410240ClassData(caller->GetDeclaringClass());
1298 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001299 }
1300}
1301
Ian Rogers848871b2013-08-05 10:56:33 -07001302// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001303extern "C" const void* artQuickResolutionTrampoline(
1304 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001305 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001306 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1307 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1308 // does not have the same stack layout as the callee-save method).
1309 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001310 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001311 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001312 ScopedObjectAccessUnchecked soa(env);
1313 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001314 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001315
1316 // Compute details about the called method (avoid GCs)
1317 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001318 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001319 MethodReference called_method(nullptr, 0);
1320 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001321 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001322 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001323 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001324 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001325
David Srbecky2c762572018-06-27 10:09:11 +01001326 {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001327 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001328 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001329 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1330 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001331 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001332 bool is_range;
1333 switch (instr_code) {
1334 case Instruction::INVOKE_DIRECT:
1335 invoke_type = kDirect;
1336 is_range = false;
1337 break;
1338 case Instruction::INVOKE_DIRECT_RANGE:
1339 invoke_type = kDirect;
1340 is_range = true;
1341 break;
1342 case Instruction::INVOKE_STATIC:
1343 invoke_type = kStatic;
1344 is_range = false;
1345 break;
1346 case Instruction::INVOKE_STATIC_RANGE:
1347 invoke_type = kStatic;
1348 is_range = true;
1349 break;
1350 case Instruction::INVOKE_SUPER:
1351 invoke_type = kSuper;
1352 is_range = false;
1353 break;
1354 case Instruction::INVOKE_SUPER_RANGE:
1355 invoke_type = kSuper;
1356 is_range = true;
1357 break;
1358 case Instruction::INVOKE_VIRTUAL:
1359 invoke_type = kVirtual;
1360 is_range = false;
1361 break;
1362 case Instruction::INVOKE_VIRTUAL_RANGE:
1363 invoke_type = kVirtual;
1364 is_range = true;
1365 break;
1366 case Instruction::INVOKE_INTERFACE:
1367 invoke_type = kInterface;
1368 is_range = false;
1369 break;
1370 case Instruction::INVOKE_INTERFACE_RANGE:
1371 invoke_type = kInterface;
1372 is_range = true;
1373 break;
1374 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001375 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001376 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001377 UNREACHABLE();
1378 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001379 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
David Srbecky2c762572018-06-27 10:09:11 +01001380 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
1381 << called_method.index;
Ian Rogers848871b2013-08-05 10:56:33 -07001382 }
Ian Rogers848871b2013-08-05 10:56:33 -07001383 } else {
1384 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001385 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001386 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001387 }
1388 uint32_t shorty_len;
1389 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001390 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001391 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001392 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001393 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001394 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001395 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001396 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001397 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001398 mirror::Object* dummy = nullptr;
1399 HandleWrapper<mirror::Object> h_receiver(
1400 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001401 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001402 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001403 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001404
1405 // Update .bss entry in oat file if any.
1406 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001407 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1408 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1409 called_method.index,
1410 called_method.dex_file->NumMethodIds(),
1411 static_cast<size_t>(kRuntimePointerSize));
1412 if (bss_offset != IndexBssMappingLookup::npos) {
1413 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1414 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1415 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1416 oat_file->BssBegin() + bss_offset));
1417 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1418 DCHECK_LT(method_entry,
1419 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
Vladimir Markod5fd5c32019-07-02 14:46:32 +01001420 std::atomic<ArtMethod*>* atomic_entry =
1421 reinterpret_cast<std::atomic<ArtMethod*>*>(method_entry);
1422 static_assert(sizeof(*method_entry) == sizeof(*atomic_entry), "Size check.");
1423 atomic_entry->store(called, std::memory_order_release);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001424 }
1425 }
Ian Rogers848871b2013-08-05 10:56:33 -07001426 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001427 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001428 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001429 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001430 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001431 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001432 if (virtual_or_interface || invoke_type == kSuper) {
1433 // Refine called method based on receiver for kVirtual/kInterface, and
1434 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001435 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001436 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001437 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001438 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001439 } else if (invoke_type == kInterface) {
1440 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001441 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001442 } else {
1443 DCHECK_EQ(invoke_type, kSuper);
1444 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001445 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001446 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001447 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001448 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001449 } else {
1450 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001451 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001452 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001453 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001454
David Sehr709b0702016-10-13 09:12:37 -07001455 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1456 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001457 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001458 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001459
Vladimir Marko5115a4d2019-10-17 14:56:47 +01001460 ObjPtr<mirror::Class> called_class = called->GetDeclaringClass();
1461 if (NeedsClinitCheckBeforeCall(called) && !called_class->IsVisiblyInitialized()) {
1462 // Ensure that the called method's class is initialized.
1463 StackHandleScope<1> hs(soa.Self());
1464 HandleWrapperObjPtr<mirror::Class> h_called_class(hs.NewHandleWrapper(&called_class));
1465 linker->EnsureInitialized(soa.Self(), h_called_class, true, true);
1466 }
Alex Light3dacdd62019-03-12 15:45:47 +00001467 bool force_interpreter = self->IsForceInterpreter() && !called->IsNative();
Nicolas Geoffray623d4f12019-09-30 13:45:51 +01001468 if (called_class->IsInitialized() || called_class->IsInitializing()) {
Alex Light3dacdd62019-03-12 15:45:47 +00001469 if (UNLIKELY(force_interpreter ||
1470 Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001471 // If we are single-stepping or the called method is deoptimized (by a
1472 // breakpoint, for example), then we have to execute the called method
1473 // with the interpreter.
1474 code = GetQuickToInterpreterBridge();
1475 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1476 // If the caller is deoptimized (by a breakpoint, for example), we have to
1477 // continue its execution with interpreter when returning from the called
1478 // method. Because we do not want to execute the called method with the
1479 // interpreter, we wrap its execution into the instrumentation stubs.
1480 // When the called method returns, it will execute the instrumentation
1481 // exit hook that will determine the need of the interpreter with a call
1482 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1483 // it is needed.
1484 code = GetQuickInstrumentationEntryPoint();
1485 } else {
1486 code = called->GetEntryPointFromQuickCompiledCode();
Nicolas Geoffray623d4f12019-09-30 13:45:51 +01001487 if (linker->IsQuickResolutionStub(code)) {
1488 DCHECK_EQ(invoke_type, kStatic);
1489 // Go to JIT or oat and grab code.
1490 code = linker->GetQuickOatCodeFor(called);
1491 if (called_class->IsInitialized()) {
1492 // Only update the entrypoint once the class is initialized. Other
1493 // threads still need to go through the resolution stub.
1494 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(called, code);
1495 }
1496 }
Ian Rogers848871b2013-08-05 10:56:33 -07001497 }
1498 } else {
1499 DCHECK(called_class->IsErroneous());
1500 }
1501 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001502 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001503 // Fixup any locally saved objects may have moved during a GC.
1504 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001505 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001506 *sp = called;
1507
Ian Rogers848871b2013-08-05 10:56:33 -07001508 return code;
1509}
1510
Andreas Gampec147b002014-03-06 18:11:06 -08001511/*
1512 * This class uses a couple of observations to unite the different calling conventions through
1513 * a few constants.
1514 *
1515 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1516 * possible alignment.
1517 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1518 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1519 * when we have to split things
1520 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1521 * and we can use Int handling directly.
1522 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1523 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1524 * extension should be compatible with Aarch64, which mandates copying the available bits
1525 * into LSB and leaving the rest unspecified.
1526 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1527 * the stack.
1528 * 6) There is only little endian.
1529 *
1530 *
1531 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1532 * follows:
1533 *
1534 * void PushGpr(uintptr_t): Add a value for the next GPR
1535 *
1536 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1537 * padding, that is, think the architecture is 32b and aligns 64b.
1538 *
1539 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1540 * split this if necessary. The current state will have aligned, if
1541 * necessary.
1542 *
1543 * void PushStack(uintptr_t): Push a value to the stack.
1544 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001545 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001546 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001547 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001548 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001549 *
1550 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001552 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001553#if defined(__arm__)
1554 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001555 static constexpr bool kNativeSoftFloatAbi = true;
1556 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001557 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1558
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001559 static constexpr size_t kRegistersNeededForLong = 2;
1560 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001561 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001562 static constexpr bool kMultiFPRegistersWidened = false;
1563 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001564 static constexpr bool kAlignLongOnStack = true;
1565 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001566#elif defined(__aarch64__)
1567 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1568 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1569 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1570
1571 static constexpr size_t kRegistersNeededForLong = 1;
1572 static constexpr size_t kRegistersNeededForDouble = 1;
1573 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001574 static constexpr bool kMultiFPRegistersWidened = false;
1575 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001576 static constexpr bool kAlignLongOnStack = false;
1577 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001578#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001579 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001580 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1581 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001582
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001583 static constexpr size_t kRegistersNeededForLong = 2;
1584 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001585 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001586 static constexpr bool kMultiFPRegistersWidened = true;
1587 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001588 static constexpr bool kAlignLongOnStack = true;
1589 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001590#elif defined(__mips__) && defined(__LP64__)
1591 // Let the code prepare GPRs only and we will load the FPRs with same data.
1592 static constexpr bool kNativeSoftFloatAbi = true;
1593 static constexpr size_t kNumNativeGprArgs = 8;
1594 static constexpr size_t kNumNativeFprArgs = 0;
1595
1596 static constexpr size_t kRegistersNeededForLong = 1;
1597 static constexpr size_t kRegistersNeededForDouble = 1;
1598 static constexpr bool kMultiRegistersAligned = false;
1599 static constexpr bool kMultiFPRegistersWidened = false;
1600 static constexpr bool kMultiGPRegistersWidened = true;
1601 static constexpr bool kAlignLongOnStack = false;
1602 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001603#elif defined(__i386__)
1604 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001605 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001606 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1607 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1608
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001609 static constexpr size_t kRegistersNeededForLong = 2;
1610 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001611 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001612 static constexpr bool kMultiFPRegistersWidened = false;
1613 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001614 static constexpr bool kAlignLongOnStack = false;
1615 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001616#elif defined(__x86_64__)
1617 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1618 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1619 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1620
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001621 static constexpr size_t kRegistersNeededForLong = 1;
1622 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001623 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001624 static constexpr bool kMultiFPRegistersWidened = false;
1625 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001626 static constexpr bool kAlignLongOnStack = false;
1627 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001628#else
1629#error "Unsupported architecture"
1630#endif
1631
Andreas Gampec147b002014-03-06 18:11:06 -08001632 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001633 explicit BuildNativeCallFrameStateMachine(T* delegate)
1634 : gpr_index_(kNumNativeGprArgs),
1635 fpr_index_(kNumNativeFprArgs),
1636 stack_entries_(0),
1637 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001638 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1639 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001640 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1641 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001642 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001643
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001645
Ian Rogers1428dce2014-10-21 15:02:15 -07001646 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001647 return gpr_index_ > 0;
1648 }
1649
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001651 if (HavePointerGpr()) {
1652 gpr_index_--;
1653 PushGpr(reinterpret_cast<uintptr_t>(val));
1654 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001655 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001656 PushStack(reinterpret_cast<uintptr_t>(val));
1657 gpr_index_ = 0;
1658 }
1659 }
1660
Ian Rogers1428dce2014-10-21 15:02:15 -07001661 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001662 return gpr_index_ > 0;
1663 }
1664
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001665 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001666 uintptr_t handle = PushHandle(ptr);
1667 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001668 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001669 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001670 } else {
1671 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001672 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001673 gpr_index_ = 0;
1674 }
1675 }
1676
Ian Rogers1428dce2014-10-21 15:02:15 -07001677 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001678 return gpr_index_ > 0;
1679 }
1680
1681 void AdvanceInt(uint32_t val) {
1682 if (HaveIntGpr()) {
1683 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001684 if (kMultiGPRegistersWidened) {
1685 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001686 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001687 } else {
1688 PushGpr(val);
1689 }
Andreas Gampec147b002014-03-06 18:11:06 -08001690 } else {
1691 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001692 if (kMultiGPRegistersWidened) {
1693 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001694 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001695 } else {
1696 PushStack(val);
1697 }
Andreas Gampec147b002014-03-06 18:11:06 -08001698 gpr_index_ = 0;
1699 }
1700 }
1701
Ian Rogers1428dce2014-10-21 15:02:15 -07001702 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001703 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1704 }
1705
Ian Rogers1428dce2014-10-21 15:02:15 -07001706 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001707 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1708 kAlignLongOnStack && // and when it needs alignment
1709 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1710 }
1711
Ian Rogers1428dce2014-10-21 15:02:15 -07001712 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001713 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1714 kAlignLongOnStack && // and when it needs 8B alignment
1715 (stack_entries_ & 1) == 1; // counter is odd
1716 }
1717
1718 void AdvanceLong(uint64_t val) {
1719 if (HaveLongGpr()) {
1720 if (LongGprNeedsPadding()) {
1721 PushGpr(0);
1722 gpr_index_--;
1723 }
1724 if (kRegistersNeededForLong == 1) {
1725 PushGpr(static_cast<uintptr_t>(val));
1726 } else {
1727 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1728 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1729 }
1730 gpr_index_ -= kRegistersNeededForLong;
1731 } else {
1732 if (LongStackNeedsPadding()) {
1733 PushStack(0);
1734 stack_entries_++;
1735 }
1736 if (kRegistersNeededForLong == 1) {
1737 PushStack(static_cast<uintptr_t>(val));
1738 stack_entries_++;
1739 } else {
1740 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1741 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1742 stack_entries_ += 2;
1743 }
1744 gpr_index_ = 0;
1745 }
1746 }
1747
Ian Rogers1428dce2014-10-21 15:02:15 -07001748 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001749 return fpr_index_ > 0;
1750 }
1751
Andreas Gampec147b002014-03-06 18:11:06 -08001752 void AdvanceFloat(float val) {
1753 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001754 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001755 } else {
1756 if (HaveFloatFpr()) {
1757 fpr_index_--;
1758 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001759 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001760 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001761 } else {
1762 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001763 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001764 }
1765 } else {
1766 PushFpr4(val);
1767 }
1768 } else {
1769 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001770 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001771 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772 // Note: We need to jump through those hoops to make the compiler happy.
1773 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001774 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001775 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001776 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001777 }
1778 fpr_index_ = 0;
1779 }
1780 }
1781 }
1782
Ian Rogers1428dce2014-10-21 15:02:15 -07001783 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001784 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1785 }
1786
Ian Rogers1428dce2014-10-21 15:02:15 -07001787 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001788 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1789 kAlignDoubleOnStack && // and when it needs alignment
1790 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1791 }
1792
Ian Rogers1428dce2014-10-21 15:02:15 -07001793 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001794 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1795 kAlignDoubleOnStack && // and when it needs 8B alignment
1796 (stack_entries_ & 1) == 1; // counter is odd
1797 }
1798
1799 void AdvanceDouble(uint64_t val) {
1800 if (kNativeSoftFloatAbi) {
1801 AdvanceLong(val);
1802 } else {
1803 if (HaveDoubleFpr()) {
1804 if (DoubleFprNeedsPadding()) {
1805 PushFpr4(0);
1806 fpr_index_--;
1807 }
1808 PushFpr8(val);
1809 fpr_index_ -= kRegistersNeededForDouble;
1810 } else {
1811 if (DoubleStackNeedsPadding()) {
1812 PushStack(0);
1813 stack_entries_++;
1814 }
1815 if (kRegistersNeededForDouble == 1) {
1816 PushStack(static_cast<uintptr_t>(val));
1817 stack_entries_++;
1818 } else {
1819 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1820 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1821 stack_entries_ += 2;
1822 }
1823 fpr_index_ = 0;
1824 }
1825 }
1826 }
1827
Ian Rogers1428dce2014-10-21 15:02:15 -07001828 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001829 return stack_entries_;
1830 }
1831
Ian Rogers1428dce2014-10-21 15:02:15 -07001832 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001833 return kNumNativeGprArgs - gpr_index_;
1834 }
1835
Ian Rogers1428dce2014-10-21 15:02:15 -07001836 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001837 return kNumNativeFprArgs - fpr_index_;
1838 }
1839
1840 private:
1841 void PushGpr(uintptr_t val) {
1842 delegate_->PushGpr(val);
1843 }
1844 void PushFpr4(float val) {
1845 delegate_->PushFpr4(val);
1846 }
1847 void PushFpr8(uint64_t val) {
1848 delegate_->PushFpr8(val);
1849 }
1850 void PushStack(uintptr_t val) {
1851 delegate_->PushStack(val);
1852 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001853 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001854 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001855 }
1856
1857 uint32_t gpr_index_; // Number of free GPRs
1858 uint32_t fpr_index_; // Number of free FPRs
1859 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1860 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001861 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001862};
1863
Andreas Gampec200a4a2014-06-16 18:39:09 -07001864// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1865// in subclasses.
1866//
1867// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1868// them with handles.
1869class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001870 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001871 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1872
1873 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001874
Ian Rogers1428dce2014-10-21 15:02:15 -07001875 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001876 return num_stack_entries_ * sizeof(uintptr_t);
1877 }
1878
Ian Rogers1428dce2014-10-21 15:02:15 -07001879 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001880 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001881 // Align by kStackAlignment.
1882 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001884 }
1885
Ian Rogers1428dce2014-10-21 15:02:15 -07001886 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1887 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888 // Assumption is OK right now, as we have soft-float arm
1889 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1890 sp8 -= fregs * sizeof(uintptr_t);
1891 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1892 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1893 sp8 -= iregs * sizeof(uintptr_t);
1894 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1895 return sp8;
1896 }
Andreas Gampec147b002014-03-06 18:11:06 -08001897
Andreas Gampec200a4a2014-06-16 18:39:09 -07001898 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001899 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900 // Native call stack.
1901 sp8 = LayoutCallStack(sp8);
1902 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001903
Andreas Gampec200a4a2014-06-16 18:39:09 -07001904 // Put fprs and gprs below.
1905 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001906
Andreas Gampec200a4a2014-06-16 18:39:09 -07001907 // Return the new bottom.
1908 return sp8;
1909 }
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911 virtual void WalkHeader(
1912 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001913 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001914 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001916 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001917 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1918
1919 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001920
1921 for (uint32_t i = 1; i < shorty_len; ++i) {
1922 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1923 switch (cur_type_) {
1924 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001925 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001926 sm.AdvanceHandleScope(
1927 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001928 break;
1929
1930 case Primitive::kPrimBoolean:
1931 case Primitive::kPrimByte:
1932 case Primitive::kPrimChar:
1933 case Primitive::kPrimShort:
1934 case Primitive::kPrimInt:
1935 sm.AdvanceInt(0);
1936 break;
1937 case Primitive::kPrimFloat:
1938 sm.AdvanceFloat(0);
1939 break;
1940 case Primitive::kPrimDouble:
1941 sm.AdvanceDouble(0);
1942 break;
1943 case Primitive::kPrimLong:
1944 sm.AdvanceLong(0);
1945 break;
1946 default:
1947 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001948 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001949 }
1950 }
1951
Ian Rogers1428dce2014-10-21 15:02:15 -07001952 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001953 }
1954
1955 void PushGpr(uintptr_t /* val */) {
1956 // not optimizing registers, yet
1957 }
1958
1959 void PushFpr4(float /* val */) {
1960 // not optimizing registers, yet
1961 }
1962
1963 void PushFpr8(uint64_t /* val */) {
1964 // not optimizing registers, yet
1965 }
1966
1967 void PushStack(uintptr_t /* val */) {
1968 // counting is already done in the superclass
1969 }
1970
Andreas Gampec200a4a2014-06-16 18:39:09 -07001971 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001972 return reinterpret_cast<uintptr_t>(nullptr);
1973 }
1974
Andreas Gampec200a4a2014-06-16 18:39:09 -07001975 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001976 uint32_t num_stack_entries_;
1977};
1978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001979class ComputeGenericJniFrameSize final : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001980 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001981 explicit ComputeGenericJniFrameSize(bool critical_native)
1982 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001983
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1985 // is at *m = sp. Will update to point to the bottom of the save frame.
1986 //
1987 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001988 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001989 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990 ArtMethod* method = **m;
1991
Andreas Gampe542451c2016-07-26 09:02:02 -07001992 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001993
Andreas Gampec200a4a2014-06-16 18:39:09 -07001994 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1995
1996 // First, fix up the layout of the callee-save frame.
1997 // We have to squeeze in the HandleScope, and relocate the method pointer.
1998
1999 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07002000 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002001
2002 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002003 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002004 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002005
2006 sp8 -= scope_and_method;
2007 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07002011 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
2012 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002013
2014 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2015 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2017 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002018 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002019 }
2020
Andreas Gampec200a4a2014-06-16 18:39:09 -07002021 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002022 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023 // Reference cookie and padding
2024 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002025 }
2026
Andreas Gampec200a4a2014-06-16 18:39:09 -07002027 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2028 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002030 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002031 // First, fix up the layout of the callee-save frame.
2032 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002033 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002034
2035 // The bottom of the callee-save frame is now where the method is, *m.
2036 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2037
2038 // Add space for cookie.
2039 LayoutCookie(&sp8);
2040
2041 return sp8;
2042 }
2043
2044 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2046 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2047 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002048 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002049 Walk(shorty, shorty_len);
2050
2051 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002052 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002053
2054 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2055
2056 // Return the new bottom.
2057 return sp8;
2058 }
2059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002060 uintptr_t PushHandle(mirror::Object* /* ptr */) override;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002061
2062 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002063 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002064 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002065
2066 private:
2067 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002068 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002069};
2070
2071uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2072 num_handle_scope_references_++;
2073 return reinterpret_cast<uintptr_t>(nullptr);
2074}
2075
2076void ComputeGenericJniFrameSize::WalkHeader(
2077 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002078 // First 2 parameters are always excluded for @CriticalNative.
2079 if (UNLIKELY(critical_native_)) {
2080 return;
2081 }
2082
Andreas Gampec200a4a2014-06-16 18:39:09 -07002083 // JNIEnv
2084 sm->AdvancePointer(nullptr);
2085
2086 // Class object or this as first argument
2087 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2088}
2089
2090// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2091// the template requirements of BuildGenericJniFrameStateMachine.
2092class FillNativeCall {
2093 public:
2094 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2095 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2096
2097 virtual ~FillNativeCall() {}
2098
2099 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2100 cur_gpr_reg_ = gpr_regs;
2101 cur_fpr_reg_ = fpr_regs;
2102 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002103 }
2104
2105 void PushGpr(uintptr_t val) {
2106 *cur_gpr_reg_ = val;
2107 cur_gpr_reg_++;
2108 }
2109
2110 void PushFpr4(float val) {
2111 *cur_fpr_reg_ = val;
2112 cur_fpr_reg_++;
2113 }
2114
2115 void PushFpr8(uint64_t val) {
2116 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2117 *tmp = val;
2118 cur_fpr_reg_ += 2;
2119 }
2120
2121 void PushStack(uintptr_t val) {
2122 *cur_stack_arg_ = val;
2123 cur_stack_arg_++;
2124 }
2125
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002126 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002127 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002128 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002129 }
2130
2131 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002132 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002133 uint32_t* cur_fpr_reg_;
2134 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002135};
Andreas Gampec147b002014-03-06 18:11:06 -08002136
Andreas Gampec200a4a2014-06-16 18:39:09 -07002137// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2138// of transitioning into native code.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002139class BuildGenericJniFrameVisitor final : public QuickArgumentVisitor {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002140 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002141 BuildGenericJniFrameVisitor(Thread* self,
2142 bool is_static,
2143 bool critical_native,
2144 const char* shorty,
2145 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002146 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002147 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002148 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2149 sm_(&jni_call_) {
2150 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002151 uintptr_t* start_gpr_reg;
2152 uint32_t* start_fpr_reg;
2153 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002154 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002155 &handle_scope_,
2156 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002157 &start_gpr_reg, &start_fpr_reg);
2158
Andreas Gampec200a4a2014-06-16 18:39:09 -07002159 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2160
Igor Murashkin06a04e02016-09-13 15:57:37 -07002161 // First 2 parameters are always excluded for CriticalNative methods.
2162 if (LIKELY(!critical_native)) {
2163 // jni environment is always first argument
2164 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002165
Igor Murashkin06a04e02016-09-13 15:57:37 -07002166 if (is_static) {
Vladimir Markod93e3742018-07-18 10:58:13 +01002167 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass().Ptr());
Igor Murashkin06a04e02016-09-13 15:57:37 -07002168 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002169 }
2170 }
2171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002172 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002173
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002174 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002175
Vladimir Markof39745e2016-01-26 12:16:55 +00002176 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002177 return handle_scope_->GetHandle(0).GetReference();
2178 }
2179
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002180 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002181 return handle_scope_->GetHandle(0).ToJObject();
2182 }
2183
Ian Rogers1428dce2014-10-21 15:02:15 -07002184 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002185 return bottom_of_used_area_;
2186 }
2187
2188 private:
2189 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002190 class FillJniCall final : public FillNativeCall {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002191 public:
2192 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002193 HandleScope* handle_scope, bool critical_native)
2194 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2195 handle_scope_(handle_scope),
2196 cur_entry_(0),
2197 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002199 uintptr_t PushHandle(mirror::Object* ref) override REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002200
2201 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2202 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2203 handle_scope_ = scope;
2204 cur_entry_ = 0U;
2205 }
2206
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002207 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002208 // Initialize padding entries.
2209 size_t expected_slots = handle_scope_->NumberOfReferences();
2210 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002211 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002212 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002213
2214 if (!critical_native_) {
2215 // Non-critical natives have at least the self class (jclass) or this (jobject).
2216 DCHECK_NE(cur_entry_, 0U);
2217 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002218 }
2219
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002220 bool CriticalNative() const {
2221 return critical_native_;
2222 }
2223
Andreas Gampec200a4a2014-06-16 18:39:09 -07002224 private:
2225 HandleScope* handle_scope_;
2226 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002227 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002228 };
2229
2230 HandleScope* handle_scope_;
2231 FillJniCall jni_call_;
2232 void* bottom_of_used_area_;
2233
2234 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002235
2236 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2237};
2238
Andreas Gampec200a4a2014-06-16 18:39:09 -07002239uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2240 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002241 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002242 h.Assign(ref);
2243 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2244 cur_entry_++;
2245 return tmp;
2246}
2247
Ian Rogers9758f792014-03-13 09:02:55 -07002248void BuildGenericJniFrameVisitor::Visit() {
2249 Primitive::Type type = GetParamPrimitiveType();
2250 switch (type) {
2251 case Primitive::kPrimLong: {
2252 jlong long_arg;
2253 if (IsSplitLongOrDouble()) {
2254 long_arg = ReadSplitLongParam();
2255 } else {
2256 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2257 }
2258 sm_.AdvanceLong(long_arg);
2259 break;
2260 }
2261 case Primitive::kPrimDouble: {
2262 uint64_t double_arg;
2263 if (IsSplitLongOrDouble()) {
2264 // Read into union so that we don't case to a double.
2265 double_arg = ReadSplitLongParam();
2266 } else {
2267 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2268 }
2269 sm_.AdvanceDouble(double_arg);
2270 break;
2271 }
2272 case Primitive::kPrimNot: {
2273 StackReference<mirror::Object>* stack_ref =
2274 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002275 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002276 break;
2277 }
2278 case Primitive::kPrimFloat:
2279 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2280 break;
2281 case Primitive::kPrimBoolean: // Fall-through.
2282 case Primitive::kPrimByte: // Fall-through.
2283 case Primitive::kPrimChar: // Fall-through.
2284 case Primitive::kPrimShort: // Fall-through.
2285 case Primitive::kPrimInt: // Fall-through.
2286 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2287 break;
2288 case Primitive::kPrimVoid:
2289 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002290 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002291 }
2292}
2293
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002294void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002295 // Clear out rest of the scope.
2296 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002297 if (!jni_call_.CriticalNative()) {
2298 // Install HandleScope.
2299 self->PushHandleScope(handle_scope_);
2300 }
Ian Rogers9758f792014-03-13 09:02:55 -07002301}
2302
Andreas Gampec147b002014-03-06 18:11:06 -08002303/*
2304 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002305 * 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 -08002306 * The final element on the stack is a pointer to the native code.
2307 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002308 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002309 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002310 *
Andreas Gampec147b002014-03-06 18:11:06 -08002311 * The return of this function denotes:
2312 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2313 * 2) An error, if the value is negative.
2314 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002316 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002317 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002318 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002319 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002320 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002321 uint32_t shorty_len = 0;
2322 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002323 bool critical_native = called->IsCriticalNative();
2324 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002325 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002326
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002327 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002328 BuildGenericJniFrameVisitor visitor(self,
2329 called->IsStatic(),
2330 critical_native,
2331 shorty,
2332 shorty_len,
2333 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002334 {
2335 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2336 visitor.VisitArguments();
2337 // FinalizeHandleScope pushes the handle scope on the thread.
2338 visitor.FinalizeHandleScope(self);
2339 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002340
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002341 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002342 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002343
Ian Rogerse0dcd462014-03-08 15:21:04 -08002344 self->VerifyStack();
2345
Vladimir Markof8655b32018-03-21 17:53:56 +00002346 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2347 jit::Jit* jit = runtime->GetJit();
2348 if (jit != nullptr) {
2349 jit->MethodEntered(self, called);
2350 }
2351
Nicolas Geoffray5a0b6722019-09-24 15:09:40 +01002352 // We can set the entrypoint of a native method to generic JNI even when the
2353 // class hasn't been initialized, so we need to do the initialization check
2354 // before invoking the native code.
Vladimir Marko5115a4d2019-10-17 14:56:47 +01002355 if (NeedsClinitCheckBeforeCall(called)) {
2356 ObjPtr<mirror::Class> declaring_class = called->GetDeclaringClass();
2357 if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
2358 // Ensure static method's class is initialized.
2359 StackHandleScope<1> hs(self);
2360 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
Vladimir Marko7dac8642019-11-06 17:09:30 +00002361 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Vladimir Marko5115a4d2019-10-17 14:56:47 +01002362 DCHECK(Thread::Current()->IsExceptionPending()) << called->PrettyMethod();
2363 self->PopHandleScope();
2364 // A negative value denotes an error.
2365 return GetTwoWordFailureValue();
2366 }
Nicolas Geoffray5a0b6722019-09-24 15:09:40 +01002367 }
2368 }
2369
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002370 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002371 uint32_t* sp32;
2372 // Skip calling JniMethodStart for @CriticalNative.
2373 if (LIKELY(!critical_native)) {
2374 // Start JNI, save the cookie.
2375 if (called->IsSynchronized()) {
2376 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2377 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2378 if (self->IsExceptionPending()) {
2379 self->PopHandleScope();
2380 // A negative value denotes an error.
2381 return GetTwoWordFailureValue();
2382 }
2383 } else {
2384 if (fast_native) {
2385 cookie = JniMethodFastStart(self);
2386 } else {
2387 DCHECK(normal_native);
2388 cookie = JniMethodStart(self);
2389 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002390 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002391 sp32 = reinterpret_cast<uint32_t*>(sp);
2392 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002393 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002394
Andreas Gampe90546832014-03-12 18:07:19 -07002395 // Retrieve the stored native code.
Vladimir Marko08d09842019-12-02 12:38:49 +00002396 // Note that it may point to the lookup stub or trampoline.
2397 // FIXME: This is broken for @CriticalNative as the art_jni_dlsym_lookup_stub
2398 // does not handle that case. Calls from compiled stubs are also broken.
Alex Lightd78ddec2017-04-18 15:20:38 -07002399 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002400
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002401#if defined(__mips__) && !defined(__LP64__)
2402 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2403 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2404 // and load into floating-point registers.
2405 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2406 // view):
2407 // (1)
2408 // | DOUBLE | DOUBLE | other args, if any
2409 // | F12 | F13 | F14 | F15 |
2410 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2411 // (2)
2412 // | DOUBLE | FLOAT | (PAD) | other args, if any
2413 // | F12 | F13 | F14 | |
2414 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2415 // (3)
2416 // | FLOAT | (PAD) | DOUBLE | other args, if any
2417 // | F12 | | F14 | F15 |
2418 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2419 // (4)
2420 // | FLOAT | FLOAT | other args, if any
2421 // | F12 | F14 |
2422 // | SP+0 | SP+4 | SP+8
2423 // As you can see, only the last case (4) is special. In all others we can just
2424 // load F12/F13 and F14/F15 in the same manner.
2425 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2426 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2427 if (nativeCode != nullptr &&
2428 shorty != nullptr &&
2429 shorty_len >= 3 &&
2430 shorty[1] == 'F' &&
2431 shorty[2] == 'F') {
2432 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2433 }
2434#endif
2435
Andreas Gampe21a6ec52019-03-28 10:55:50 -07002436 VLOG(third_party_jni) << "GenericJNI: "
2437 << called->PrettyMethod()
2438 << " -> "
2439 << std::hex << reinterpret_cast<uintptr_t>(nativeCode);
2440
Andreas Gampec200a4a2014-06-16 18:39:09 -07002441 // Return native code addr(lo) and bottom of alloca address(hi).
2442 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2443 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002444}
2445
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002446// Defined in quick_jni_entrypoints.cc.
2447extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2448 jvalue result, uint64_t result_f, ArtMethod* called,
2449 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002450/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002451 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002452 * unlocking.
2453 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002454extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2455 jvalue result,
2456 uint64_t result_f) {
2457 // We're here just back from a native call. We don't have the shared mutator lock at this point
2458 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2459 // anything that requires a mutator lock before that would cause problems as GC may have the
2460 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002461 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002462 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002463 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002464 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002465 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002466 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2467 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002468}
2469
Andreas Gamped58342c2014-06-05 14:18:08 -07002470// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2471// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002472//
Andreas Gamped58342c2014-06-05 14:18:08 -07002473// 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 -07002474// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002475
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002476template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002477static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2478 ObjPtr<mirror::Object> this_object,
2479 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002481 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002482 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002483 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002484 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002485 if (UNLIKELY(method == nullptr)) {
Vladimir Marko7e097372018-11-28 16:40:59 +00002486 const DexFile* dex_file = caller_method->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002487 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002488 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002489 {
2490 // Remember the args in case a GC happens in FindMethodFromCode.
2491 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2492 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2493 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002494 method = FindMethodFromCode<type, access_check>(method_idx,
2495 &this_object,
2496 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002497 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002498 visitor.FixupReferences();
2499 }
2500
Ian Rogerse0a02da2014-12-02 14:10:53 -08002501 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002502 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002503 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002504 }
2505 }
2506 DCHECK(!self->IsExceptionPending());
2507 const void* code = method->GetEntryPointFromQuickCompiledCode();
2508
2509 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002510 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002511 << " location: "
2512 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002513
Andreas Gamped58342c2014-06-05 14:18:08 -07002514 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2515 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002516}
2517
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002518// Explicit artInvokeCommon template function declarations to please analysis tool.
2519#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002520 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002521 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002522 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002523
2524EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2525EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2526EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2527EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2528EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2529EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2530EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2531EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2532EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2533EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2534#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2535
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002536// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002537extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002538 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002539 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002540 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002541}
2542
Andreas Gampec200a4a2014-06-16 18:39:09 -07002543extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002544 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002545 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002546 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002547}
2548
Andreas Gampec200a4a2014-06-16 18:39:09 -07002549extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002550 uint32_t method_idx,
2551 mirror::Object* this_object ATTRIBUTE_UNUSED,
2552 Thread* self,
2553 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2554 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2555 // it doesn't cause ObjPtr alignment failure check.
2556 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002557}
2558
Andreas Gampec200a4a2014-06-16 18:39:09 -07002559extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002560 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002561 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002562 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002563}
2564
Andreas Gampec200a4a2014-06-16 18:39:09 -07002565extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
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<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002569}
2570
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002571// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2572extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2573 REQUIRES_SHARED(Locks::mutator_lock_) {
2574 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2575 DCHECK(!referrer->IsProxyMethod());
2576 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2577 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2578 DCHECK(result == nullptr ||
2579 result->GetDeclaringClass()->IsInterface() ||
2580 result->GetDeclaringClass() ==
2581 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2582 << result->PrettyMethod();
2583 return result;
2584}
2585
Jeff Hao5667f562017-02-27 19:32:01 -08002586// Determine target of interface dispatch. The interface method and this object are known non-null.
2587// The interface method is the method returned by the dex cache in the conflict trampoline.
2588extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002589 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002590 Thread* self,
2591 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002592 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002593 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002594 StackHandleScope<2> hs(self);
2595 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2596 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002597
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002598 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002599 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002600 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002601
Vladimir Marko302f69c2017-07-25 15:27:15 +01002602 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002603 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002604 // Fetch the dex_method_idx of the target interface method from the caller.
2605 uint32_t dex_method_idx;
2606 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002607 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002608 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002609 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2610 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002611 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002612 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002613 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002614 } else {
2615 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002616 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002617 }
2618
Vladimir Marko7e097372018-11-28 16:40:59 +00002619 const DexFile& dex_file = *caller_method->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002620 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002621 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2622 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002623 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002624 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002625 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2626 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2627 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002628 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2629 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2630 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002631 visitor.FixupReferences();
2632 }
2633
Vladimir Marko302f69c2017-07-25 15:27:15 +01002634 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002635 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002636 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002637 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002638 }
2639
2640 DCHECK(!interface_method->IsRuntimeMethod());
2641 // Look whether we have a match in the ImtConflictTable.
David Srbeckye36e7f22018-11-14 14:21:23 +00002642 uint32_t imt_index = interface_method->GetImtIndex();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002643 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2644 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2645 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2646 DCHECK(current_table != nullptr);
2647 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2648 } else {
2649 // It seems we aren't really a conflict method!
2650 if (kIsDebugBuild) {
2651 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2652 CHECK_EQ(conflict_method, m)
2653 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2654 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2655 }
2656 method = conflict_method;
2657 }
2658 if (method != nullptr) {
2659 return GetTwoWordSuccessValue(
2660 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2661 reinterpret_cast<uintptr_t>(method));
2662 }
2663
2664 // No match, use the IfTable.
2665 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2666 if (UNLIKELY(method == nullptr)) {
2667 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2668 interface_method, this_object.Get(), caller_method);
2669 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002670 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002671
2672 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2673 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002674 DCHECK(conflict_method->IsRuntimeMethod());
2675 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2676 cls.Get(),
2677 conflict_method,
2678 interface_method,
2679 method,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002680 /*force_new_conflict_method=*/false);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002681 if (new_conflict_method != conflict_method) {
2682 // Update the IMT if we create a new conflict method. No fence needed here, as the
2683 // data is consistent.
2684 imt->Set(imt_index,
2685 new_conflict_method,
2686 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002687 }
2688
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002689 const void* code = method->GetEntryPointFromQuickCompiledCode();
2690
2691 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002692 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002693 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002694
Andreas Gamped58342c2014-06-05 14:18:08 -07002695 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2696 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002697}
2698
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002699// Returns uint64_t representing raw bits from JValue.
2700extern "C" uint64_t artInvokePolymorphic(mirror::Object* raw_receiver, Thread* self, ArtMethod** sp)
Orion Hodsonac141392017-01-13 11:53:47 +00002701 REQUIRES_SHARED(Locks::mutator_lock_) {
2702 ScopedQuickEntrypointChecks sqec(self);
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002703 DCHECK(raw_receiver != nullptr);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002704 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002705
2706 // Start new JNI local reference state
2707 JNIEnvExt* env = self->GetJniEnv();
2708 ScopedObjectAccessUnchecked soa(env);
2709 ScopedJniEnvLocalRefState env_state(env);
2710 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2711
2712 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2713 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2714 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002715 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002716 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2717 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002718 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002719 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002720 const size_t shorty_length = strlen(shorty);
2721 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2722 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002723 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002724
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002725 // Wrap raw_receiver in a Handle for safety.
2726 StackHandleScope<3> hs(self);
2727 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2728 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002729 self->EndAssertNoThreadSuspension(old_cause);
2730
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002731 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002732 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002733 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002734 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002735
Orion Hodsone7732be2017-10-11 14:35:20 +01002736 Handle<mirror::MethodType> method_type(
2737 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
Orion Hodsonac141392017-01-13 11:53:47 +00002738 if (UNLIKELY(method_type.IsNull())) {
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002739 // This implies we couldn't resolve one or more types in this method handle.
Orion Hodsonac141392017-01-13 11:53:47 +00002740 CHECK(self->IsExceptionPending());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002741 return 0UL;
Orion Hodsonac141392017-01-13 11:53:47 +00002742 }
2743
Vladimir Markod7559b72017-09-28 13:50:37 +01002744 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002745 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2746
2747 // Fix references before constructing the shadow frame.
2748 gc_visitor.FixupReferences();
2749
2750 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002751 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2752 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002753 const size_t first_arg = 0;
2754 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002755 CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, resolved_method, dex_pc);
Orion Hodsonac141392017-01-13 11:53:47 +00002756 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2757 ScopedStackedShadowFramePusher
2758 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2759 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2760 kMethodIsStatic,
2761 shorty,
2762 strlen(shorty),
2763 shadow_frame,
2764 first_arg);
2765 shadow_frame_builder.VisitArguments();
2766
2767 // Push a transition back into managed code onto the linked list in thread.
2768 ManagedStack fragment;
2769 self->PushManagedStackFragment(&fragment);
2770
2771 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2772 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002773 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002774 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002775 JValue result;
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002776 bool success = false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002777 if (resolved_method->GetDeclaringClass() == GetClassRoot<mirror::MethodHandle>(linker)) {
Orion Hodson537a4fe2018-05-15 13:57:58 +01002778 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00002779 ObjPtr<mirror::MethodHandle>::DownCast(receiver_handle.Get())));
Orion Hodson537a4fe2018-05-15 13:57:58 +01002780 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2781 success = MethodHandleInvokeExact(self,
2782 *shadow_frame,
2783 method_handle,
2784 method_type,
2785 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002786 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002787 } else {
2788 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2789 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2790 success = MethodHandleInvoke(self,
2791 *shadow_frame,
2792 method_handle,
2793 method_type,
2794 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002795 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002796 }
2797 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002798 DCHECK_EQ(GetClassRoot<mirror::VarHandle>(linker), resolved_method->GetDeclaringClass());
Orion Hodson537a4fe2018-05-15 13:57:58 +01002799 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00002800 ObjPtr<mirror::VarHandle>::DownCast(receiver_handle.Get())));
Orion Hodson537a4fe2018-05-15 13:57:58 +01002801 mirror::VarHandle::AccessMode access_mode =
2802 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2803 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002804 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002805 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002806 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002807 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002808 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002809 &result);
Orion Hodsonac141392017-01-13 11:53:47 +00002810 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002811
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002812 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002813
2814 // Pop transition record.
2815 self->PopManagedStackFragment(fragment);
2816
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002817 return result.GetJ();
Orion Hodsonac141392017-01-13 11:53:47 +00002818}
2819
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002820// Returns uint64_t representing raw bits from JValue.
2821extern "C" uint64_t artInvokeCustom(uint32_t call_site_idx, Thread* self, ArtMethod** sp)
2822 REQUIRES_SHARED(Locks::mutator_lock_) {
2823 ScopedQuickEntrypointChecks sqec(self);
2824 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
2825
2826 // invoke-custom is effectively a static call (no receiver).
2827 static constexpr bool kMethodIsStatic = true;
2828
2829 // Start new JNI local reference state
2830 JNIEnvExt* env = self->GetJniEnv();
2831 ScopedObjectAccessUnchecked soa(env);
2832 ScopedJniEnvLocalRefState env_state(env);
2833
2834 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2835
2836 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2837 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2838 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2839 const DexFile* dex_file = caller_method->GetDexFile();
2840 const dex::ProtoIndex proto_idx(dex_file->GetProtoIndexForCallSite(call_site_idx));
2841 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
2842 const uint32_t shorty_len = strlen(shorty);
2843
2844 // Construct the shadow frame placing arguments consecutively from |first_arg|.
2845 const size_t first_arg = 0;
2846 const size_t num_vregs = ArtMethod::NumArgRegisters(shorty);
2847 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002848 CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, caller_method, dex_pc);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002849 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2850 ScopedStackedShadowFramePusher
2851 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2852 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2853 kMethodIsStatic,
2854 shorty,
2855 shorty_len,
2856 shadow_frame,
2857 first_arg);
2858 shadow_frame_builder.VisitArguments();
2859
2860 // Push a transition back into managed code onto the linked list in thread.
2861 ManagedStack fragment;
2862 self->PushManagedStackFragment(&fragment);
2863 self->EndAssertNoThreadSuspension(old_cause);
2864
2865 // Perform the invoke-custom operation.
2866 RangeInstructionOperands operands(first_arg, num_vregs);
2867 JValue result;
2868 bool success =
2869 interpreter::DoInvokeCustom(self, *shadow_frame, call_site_idx, &operands, &result);
2870 DCHECK(success || self->IsExceptionPending());
2871
2872 // Pop transition record.
2873 self->PopManagedStackFragment(fragment);
2874
2875 return result.GetJ();
2876}
2877
Ian Rogers848871b2013-08-05 10:56:33 -07002878} // namespace art