blob: 79196212a28854c63d239099803d85a203c235d7 [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 Marko0eb882b2017-05-15 13:39:18 +010051#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010052#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070053#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070054#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070055#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070056#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070057#include "thread-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010058#include "var_handles.h"
Orion Hodsonac141392017-01-13 11:53:47 +000059#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070060
61namespace art {
62
Andreas Gampe8228cdf2017-05-30 15:03:54 -070063// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070064class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080065 // Number of bytes for each out register in the caller method's frame.
66 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070067 // Frame size in bytes of a callee-save frame for RefsAndArgs.
68 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markod3083dd2018-05-17 08:43:47 +010069 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
70 // Offset of first GPR arg.
71 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
72 RuntimeCalleeSaveFrame::GetGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
73 // Offset of first FPR arg.
74 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
75 RuntimeCalleeSaveFrame::GetFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
76 // Offset of return address.
77 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset =
78 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070079#if defined(__arm__)
80 // The callee save frame is pointed to by SP.
81 // | argN | |
82 // | ... | |
83 // | arg4 | |
84 // | arg3 spill | | Caller's frame
85 // | arg2 spill | |
86 // | arg1 spill | |
87 // | Method* | ---
88 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080089 // | ... | 4x6 bytes callee saves
90 // | R3 |
91 // | R2 |
92 // | R1 |
93 // | S15 |
94 // | : |
95 // | S0 |
96 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070097 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070098 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
99 static constexpr bool kAlignPairRegister = true;
100 static constexpr bool kQuickSoftFloatAbi = false;
101 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000102 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800103 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -0700104 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800105 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800106 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000107 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800108 }
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109#elif defined(__aarch64__)
110 // The callee save frame is pointed to by SP.
111 // | argN | |
112 // | ... | |
113 // | arg4 | |
114 // | arg3 spill | | Caller's frame
115 // | arg2 spill | |
116 // | arg1 spill | |
117 // | Method* | ---
118 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800119 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100121 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 // | X7 |
123 // | : |
124 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800125 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000126 // | : |
127 // | D0 |
128 // | | padding
129 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500130 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000131 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000132 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800133 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000134 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000135 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
136 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800137 static constexpr bool kGprFprLockstep = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000138 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000139 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000140 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800141#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700142 // The callee save frame is pointed to by SP.
143 // | argN | |
144 // | ... | |
145 // | arg4 | |
146 // | arg3 spill | | Caller's frame
147 // | arg2 spill | |
148 // | arg1 spill | |
149 // | Method* | ---
150 // | RA |
151 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800152 // | T1 | arg5
153 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700154 // | A3 | arg3
155 // | A2 | arg2
156 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800157 // | F19 |
158 // | F18 | f_arg5
159 // | F17 |
160 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000161 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800162 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000163 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800164 // | F12 | f_arg2
165 // | F11 |
166 // | F10 | f_arg1
167 // | F9 |
168 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000169 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700170 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000171 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
172 static constexpr bool kAlignPairRegister = true;
173 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800174 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000175 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800176 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
177 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
178 // passed only in even numbered registers and each
179 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800180 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800181 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000182 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800183 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800184#elif defined(__mips__) && defined(__LP64__)
185 // The callee save frame is pointed to by SP.
186 // | argN | |
187 // | ... | |
188 // | arg4 | |
189 // | arg3 spill | | Caller's frame
190 // | arg2 spill | |
191 // | arg1 spill | |
192 // | Method* | ---
193 // | RA |
194 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800195 // | A7 | arg7
196 // | A6 | arg6
197 // | A5 | arg5
198 // | A4 | arg4
199 // | A3 | arg3
200 // | A2 | arg2
201 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000202 // | F19 | f_arg7
203 // | F18 | f_arg6
204 // | F17 | f_arg5
205 // | F16 | f_arg4
206 // | F15 | f_arg3
207 // | F14 | f_arg2
208 // | F13 | f_arg1
209 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800210 // | | padding
211 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800212 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800213 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800214 static constexpr bool kAlignPairRegister = false;
215 static constexpr bool kQuickSoftFloatAbi = false;
216 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000217 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800218 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
219 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
220 static constexpr bool kGprFprLockstep = true;
221
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800222 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
223 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
224 }
Ian Rogers848871b2013-08-05 10:56:33 -0700225#elif defined(__i386__)
226 // The callee save frame is pointed to by SP.
227 // | argN | |
228 // | ... | |
229 // | arg4 | |
230 // | arg3 spill | | Caller's frame
231 // | arg2 spill | |
232 // | arg1 spill | |
233 // | Method* | ---
234 // | Return |
235 // | EBP,ESI,EDI | callee saves
236 // | EBX | arg3
237 // | EDX | arg2
238 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000239 // | XMM3 | float arg 4
240 // | XMM2 | float arg 3
241 // | XMM1 | float arg 2
242 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700243 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500244 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000245 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000246 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800247 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000248 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800249 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000250 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800251 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800252 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000253 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800255#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800256 // The callee save frame is pointed to by SP.
257 // | argN | |
258 // | ... | |
259 // | reg. arg spills | | Caller's frame
260 // | Method* | ---
261 // | Return |
262 // | R15 | callee save
263 // | R14 | callee save
264 // | R13 | callee save
265 // | R12 | callee save
266 // | R9 | arg5
267 // | R8 | arg4
268 // | RSI/R6 | arg1
269 // | RBP/R5 | callee save
270 // | RBX/R3 | callee save
271 // | RDX/R2 | arg2
272 // | RCX/R1 | arg3
273 // | XMM7 | float arg 8
274 // | XMM6 | float arg 7
275 // | XMM5 | float arg 6
276 // | XMM4 | float arg 5
277 // | XMM3 | float arg 4
278 // | XMM2 | float arg 3
279 // | XMM1 | float arg 2
280 // | XMM0 | float arg 1
281 // | Padding |
282 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500283 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000284 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800285 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800286 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000287 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700288 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700289 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800290 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
292 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000293 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
296 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
297 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700299 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
Elliott Hughesc1896c92018-11-29 11:33:18 -0800300 UNREACHABLE();
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 }
302 }
Ian Rogers848871b2013-08-05 10:56:33 -0700303#else
304#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700305#endif
306
Ian Rogers936b37f2014-02-14 00:52:24 -0800307 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100308 // Special handling for proxy methods. Proxy methods are instance methods so the
309 // 'this' object is the 1st argument. They also have the same frame layout as the
310 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
311 // 1st GPR.
Roland Levillainfa854e42018-02-07 13:09:55 +0000312 static StackReference<mirror::Object>* GetProxyThisObjectReference(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000314 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100315 CHECK_GT(kNumQuickGprArgs, 0u);
316 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
317 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
318 GprIndexToGprOffset(kThisGprIndex);
319 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
Roland Levillainfa854e42018-02-07 13:09:55 +0000320 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address);
Sebastien Hertza836bc92014-11-25 16:30:53 +0100321 }
322
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700325 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100326 }
327
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700328 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 uint8_t* previous_sp =
331 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 }
334
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100337 constexpr size_t callee_frame_size =
338 RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
340 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100342 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
343 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100345 if (current_code->IsOptimized()) {
David Srbecky0d4567f2019-05-30 22:45:40 +0100346 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code);
David Srbecky052f8ca2018-04-26 15:42:54 +0100347 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100348 DCHECK(stack_map.IsValid());
David Srbecky93bd3612018-07-02 19:30:18 +0100349 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
350 if (!inline_infos.empty()) {
351 return inline_infos.back().GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100352 } else {
David Srbecky052f8ca2018-04-26 15:42:54 +0100353 return stack_map.GetDexPc();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100354 }
355 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100356 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100357 }
Ian Rogers848871b2013-08-05 10:56:33 -0700358 }
359
Ian Rogers936b37f2014-02-14 00:52:24 -0800360 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700361 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700362 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markod3083dd2018-05-17 08:43:47 +0100363 uint8_t* return_adress_spill =
364 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_ReturnPcOffset;
365 return *reinterpret_cast<uintptr_t*>(return_adress_spill);
Ian Rogers848871b2013-08-05 10:56:33 -0700366 }
367
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700369 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700370 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700371 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
372 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
373 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800375 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
376 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800377 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
378 "Number of Quick FPR arguments unexpected");
379 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
380 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800381 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
382 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800383 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
384 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700385 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800386 }
Ian Rogers848871b2013-08-05 10:56:33 -0700387
388 virtual ~QuickArgumentVisitor() {}
389
390 virtual void Visit() = 0;
391
Ian Rogers936b37f2014-02-14 00:52:24 -0800392 Primitive::Type GetParamPrimitiveType() const {
393 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700394 }
395
Ian Rogers13735952014-10-08 12:43:28 -0700396 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800397 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 Primitive::Type type = GetParamPrimitiveType();
399 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800400 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
401 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
402 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
403 }
404 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000405 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800406 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700407 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800408 }
409 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800410 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800411 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
412 }
413 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700414 }
415
416 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
418 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800419 return is_split_long_or_double_;
420 } else {
421 return false; // An optimization for when GPR and FPRs are 64bit.
422 }
Ian Rogers848871b2013-08-05 10:56:33 -0700423 }
424
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700426 return GetParamPrimitiveType() == Primitive::kPrimNot;
427 }
428
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700430 Primitive::Type type = GetParamPrimitiveType();
431 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
432 }
433
434 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000435 // The splitted long is always available through the stack.
436 return *reinterpret_cast<uint64_t*>(stack_args_
437 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700438 }
439
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800440 void IncGprIndex() {
441 gpr_index_++;
442 if (kGprFprLockstep) {
443 fpr_index_++;
444 }
445 }
446
447 void IncFprIndex() {
448 fpr_index_++;
449 if (kGprFprLockstep) {
450 gpr_index_++;
451 }
452 }
453
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700454 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800455 // (a) 'stack_args_' should point to the first method's argument
456 // (b) whatever the argument type it is, the 'stack_index_' should
457 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 gpr_index_ = 0;
459 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800460 if (kQuickDoubleRegAlignedFloatBackFilled) {
461 fpr_double_index_ = 0;
462 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800463 stack_index_ = 0;
464 if (!is_static_) { // Handle this.
465 cur_type_ = Primitive::kPrimNot;
466 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700467 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800468 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800469 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800470 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800471 }
Ian Rogers848871b2013-08-05 10:56:33 -0700472 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800473 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
474 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
475 switch (cur_type_) {
476 case Primitive::kPrimNot:
477 case Primitive::kPrimBoolean:
478 case Primitive::kPrimByte:
479 case Primitive::kPrimChar:
480 case Primitive::kPrimShort:
481 case Primitive::kPrimInt:
482 is_split_long_or_double_ = false;
483 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800484 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800485 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800486 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800487 }
488 break;
489 case Primitive::kPrimFloat:
490 is_split_long_or_double_ = false;
491 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800492 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800493 if (kQuickSoftFloatAbi) {
494 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800495 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 }
497 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800498 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800499 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800500 if (kQuickDoubleRegAlignedFloatBackFilled) {
501 // Double should not overlap with float.
502 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
503 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
504 // Float should not overlap with double.
505 if (fpr_index_ % 2 == 0) {
506 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
507 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000508 } else if (kQuickSkipOddFpRegisters) {
509 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800510 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800511 }
512 }
513 break;
514 case Primitive::kPrimDouble:
515 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800516 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800517 if (cur_type_ == Primitive::kPrimLong &&
518#if defined(__mips__) && !defined(__LP64__)
519 (gpr_index_ == 0 || gpr_index_ == 2) &&
520#else
521 gpr_index_ == 0 &&
522#endif
523 kAlignPairRegister) {
524 // Currently, this is only for ARM and MIPS, where we align long parameters with
525 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
526 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800527 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000528 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000529 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800530 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500531 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
532 // We don't want to split this. Pass over this register.
533 gpr_index_++;
534 is_split_long_or_double_ = false;
535 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800536 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800537 if (kBytesStackArgLocation == 4) {
538 stack_index_+= 2;
539 } else {
540 CHECK_EQ(kBytesStackArgLocation, 8U);
541 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800542 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700543 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800544 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000545 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700546 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800547 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700548 }
549 }
550 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800551 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000552 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800553 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800554 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700555 if (kBytesStackArgLocation == 4) {
556 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700558 CHECK_EQ(kBytesStackArgLocation, 8U);
559 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800560 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800561 if (kQuickDoubleRegAlignedFloatBackFilled) {
562 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
563 fpr_double_index_ += 2;
564 // Float should not overlap with double.
565 if (fpr_index_ % 2 == 0) {
566 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
567 }
568 }
569 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800570 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800571 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
572 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800573 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800574 }
575 }
576 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800577 }
578 break;
579 default:
580 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
581 }
Ian Rogers848871b2013-08-05 10:56:33 -0700582 }
583 }
584
Andreas Gampec200a4a2014-06-16 18:39:09 -0700585 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700586 const bool is_static_;
587 const char* const shorty_;
588 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700589
590 private:
Ian Rogers13735952014-10-08 12:43:28 -0700591 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
592 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
593 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800594 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 // Index into spilled FPRs.
596 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
597 // holds a higher register number.
598 uint32_t fpr_index_;
599 // Index into spilled FPRs for aligned double.
600 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
601 // terms of singles, may be behind fpr_index.
602 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800603 uint32_t stack_index_; // Index into arguments on the stack.
604 // The current type of argument during VisitArguments.
605 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700606 // Does a 64bit parameter straddle the register and stack arguments?
607 bool is_split_long_or_double_;
608};
609
Sebastien Hertza836bc92014-11-25 16:30:53 +0100610// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
611// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700612extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700613 REQUIRES_SHARED(Locks::mutator_lock_) {
Roland Levillainfa854e42018-02-07 13:09:55 +0000614 return QuickArgumentVisitor::GetProxyThisObjectReference(sp)->AsMirrorPtr();
615}
Sebastien Hertza836bc92014-11-25 16:30:53 +0100616
Ian Rogers848871b2013-08-05 10:56:33 -0700617// Visits arguments on the stack placing them into the shadow frame.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100618class BuildQuickShadowFrameVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700619 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
621 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700622 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700623
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100624 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Ian Rogers848871b2013-08-05 10:56:33 -0700625
626 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800627 ShadowFrame* const sf_;
628 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700629
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700630 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700631};
632
Andreas Gampec200a4a2014-06-16 18:39:09 -0700633void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700634 Primitive::Type type = GetParamPrimitiveType();
635 switch (type) {
636 case Primitive::kPrimLong: // Fall-through.
637 case Primitive::kPrimDouble:
638 if (IsSplitLongOrDouble()) {
639 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
640 } else {
641 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
642 }
643 ++cur_reg_;
644 break;
645 case Primitive::kPrimNot: {
646 StackReference<mirror::Object>* stack_ref =
647 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
648 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
649 }
650 break;
651 case Primitive::kPrimBoolean: // Fall-through.
652 case Primitive::kPrimByte: // Fall-through.
653 case Primitive::kPrimChar: // Fall-through.
654 case Primitive::kPrimShort: // Fall-through.
655 case Primitive::kPrimInt: // Fall-through.
656 case Primitive::kPrimFloat:
657 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
658 break;
659 case Primitive::kPrimVoid:
660 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700661 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700662 }
663 ++cur_reg_;
664}
665
Mingyao Yang417528d2017-09-13 12:10:40 -0700666// Don't inline. See b/65159206.
667NO_INLINE
668static void HandleDeoptimization(JValue* result,
669 ArtMethod* method,
670 ShadowFrame* deopt_frame,
671 ManagedStack* fragment)
672 REQUIRES_SHARED(Locks::mutator_lock_) {
673 // Coming from partial-fragment deopt.
674 Thread* self = Thread::Current();
675 if (kIsDebugBuild) {
676 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
677 // of the call-stack) corresponds to the called method.
678 ShadowFrame* linked = deopt_frame;
679 while (linked->GetLink() != nullptr) {
680 linked = linked->GetLink();
681 }
682 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
683 << ArtMethod::PrettyMethod(linked->GetMethod());
684 }
685
686 if (VLOG_IS_ON(deopt)) {
687 // Print out the stack to verify that it was a partial-fragment deopt.
688 LOG(INFO) << "Continue-ing from deopt. Stack is:";
689 QuickExceptionHandler::DumpFramesWithType(self, true);
690 }
691
692 ObjPtr<mirror::Throwable> pending_exception;
693 bool from_code = false;
694 DeoptimizationMethodType method_type;
695 self->PopDeoptimizationContext(/* out */ result,
696 /* out */ &pending_exception,
697 /* out */ &from_code,
698 /* out */ &method_type);
699
700 // Push a transition back into managed code onto the linked list in thread.
701 self->PushManagedStackFragment(fragment);
702
703 // Ensure that the stack is still in order.
704 if (kIsDebugBuild) {
705 class DummyStackVisitor : public StackVisitor {
706 public:
707 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
708 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
709
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100710 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang417528d2017-09-13 12:10:40 -0700711 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
712 // logic. Just always say we want to continue.
713 return true;
714 }
715 };
716 DummyStackVisitor dsv(self);
717 dsv.WalkStack();
718 }
719
720 // Restore the exception that was pending before deoptimization then interpret the
721 // deoptimized frames.
722 if (pending_exception != nullptr) {
723 self->SetException(pending_exception);
724 }
725 interpreter::EnterInterpreterFromDeoptimize(self,
726 deopt_frame,
727 result,
728 from_code,
729 DeoptimizationMethodType::kDefault);
730}
731
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700733 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700734 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
735 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700736 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700737
Alex Light9139e002015-10-09 15:59:48 -0700738 if (UNLIKELY(!method->IsInvokable())) {
739 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700740 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700741 }
742
743 JValue tmp_value;
744 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700745 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700746 ManagedStack fragment;
747
David Sehr709b0702016-10-13 09:12:37 -0700748 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700749 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700750 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800751 DCHECK(non_proxy_method->GetCodeItem() != nullptr) << method->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +0000752 CodeItemDataAccessor accessor(non_proxy_method->DexInstructionData());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700753 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
754
755 JValue result;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000756 bool force_frame_pop = false;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700757
Mingyao Yang417528d2017-09-13 12:10:40 -0700758 if (UNLIKELY(deopt_frame != nullptr)) {
759 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700760 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700761 const char* old_cause = self->StartAssertNoThreadSuspension(
762 "Building interpreter shadow frame");
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800763 uint16_t num_regs = accessor.RegistersSize();
Andreas Gampec200a4a2014-06-16 18:39:09 -0700764 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700765 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700766 CREATE_SHADOW_FRAME(num_regs, /* link= */ nullptr, method, /* dex_pc= */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700767 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800768 size_t first_arg_reg = accessor.RegistersSize() - accessor.InsSize();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700769 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800770 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700771 shadow_frame_builder.VisitArguments();
772 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700773 self->PushManagedStackFragment(&fragment);
774 self->PushShadowFrame(shadow_frame);
775 self->EndAssertNoThreadSuspension(old_cause);
776
Vladimir Markobf121912019-06-04 13:49:05 +0100777 if (method->IsStatic()) {
778 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
779 if (LIKELY(declaring_class->IsVisiblyInitialized())) {
780 // Visibly initialized, nothing to do.
781 } else if (!declaring_class->IsInitialized()) {
782 // Ensure static method's class is initialized.
783 StackHandleScope<1> hs(self);
784 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
785 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
786 DCHECK(Thread::Current()->IsExceptionPending())
787 << shadow_frame->GetMethod()->PrettyMethod();
788 self->PopManagedStackFragment(fragment);
789 return 0;
790 }
791 } else {
792 // Initialized but not visibly initialized.
793 if (self->IncrementMakeVisiblyInitializedCounter()) {
794 Runtime::Current()->GetClassLinker()->MakeInitializedClassesVisiblyInitialized(
795 self, /*wait=*/ false);
796 }
Ian Rogers848871b2013-08-05 10:56:33 -0700797 }
798 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200799
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800800 result = interpreter::EnterInterpreterFromEntryPoint(self, accessor, shadow_frame);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000801 force_frame_pop = shadow_frame->GetForcePopFrame();
Ian Rogers848871b2013-08-05 10:56:33 -0700802 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700803
804 // Pop transition.
805 self->PopManagedStackFragment(fragment);
806
807 // Request a stack deoptimization if needed
808 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700809 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700810 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
Alex Light3dacdd62019-03-12 15:45:47 +0000811 // should be done and it knows the real return pc. NB If the upcall is null we don't need to do
812 // anything. This can happen during shutdown or early startup.
813 if (UNLIKELY(
814 caller != nullptr &&
815 caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
816 (self->IsForceInterpreter() || Dbg::IsForcedInterpreterNeededForUpcall(self, caller)))) {
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000817 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
818 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
819 << caller->PrettyMethod();
820 } else {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000821 VLOG(deopt) << "Forcing deoptimization on return from method " << method->PrettyMethod()
822 << " to " << caller->PrettyMethod()
823 << (force_frame_pop ? " for frame-pop" : "");
824 DCHECK(!force_frame_pop || result.GetJ() == 0) << "Force frame pop should have no result.";
825 if (force_frame_pop && self->GetException() != nullptr) {
826 LOG(WARNING) << "Suppressing exception for instruction-retry: "
827 << self->GetException()->Dump();
828 }
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000829 // Push the context of the deoptimization stack so we can restore the return value and the
830 // exception before executing the deoptimized frames.
831 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700832 result,
833 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
Alex Light0aa7a5a2018-10-10 15:58:14 +0000834 force_frame_pop ? nullptr : self->GetException(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700835 /* from_code= */ false,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700836 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700837
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000838 // Set special exception to cause deoptimization.
839 self->SetException(Thread::GetDeoptimizationException());
840 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700841 }
842
843 // No need to restore the args since the method has already been run by the interpreter.
844 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700845}
846
847// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
848// to jobjects.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100849class BuildQuickArgumentVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700850 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700851 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700852 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700853 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100855 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Ian Rogers848871b2013-08-05 10:56:33 -0700856
857 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700858 ScopedObjectAccessUnchecked* const soa_;
859 std::vector<jvalue>* const args_;
Ian Rogers9758f792014-03-13 09:02:55 -0700860
Ian Rogers848871b2013-08-05 10:56:33 -0700861 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
862};
863
Ian Rogers9758f792014-03-13 09:02:55 -0700864void BuildQuickArgumentVisitor::Visit() {
865 jvalue val;
866 Primitive::Type type = GetParamPrimitiveType();
867 switch (type) {
868 case Primitive::kPrimNot: {
869 StackReference<mirror::Object>* stack_ref =
870 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
871 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -0700872 break;
873 }
874 case Primitive::kPrimLong: // Fall-through.
875 case Primitive::kPrimDouble:
876 if (IsSplitLongOrDouble()) {
877 val.j = ReadSplitLongParam();
878 } else {
879 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
880 }
881 break;
882 case Primitive::kPrimBoolean: // Fall-through.
883 case Primitive::kPrimByte: // Fall-through.
884 case Primitive::kPrimChar: // Fall-through.
885 case Primitive::kPrimShort: // Fall-through.
886 case Primitive::kPrimInt: // Fall-through.
887 case Primitive::kPrimFloat:
888 val.i = *reinterpret_cast<jint*>(GetParamAddress());
889 break;
890 case Primitive::kPrimVoid:
891 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700892 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700893 }
894 args_->push_back(val);
895}
896
Ian Rogers848871b2013-08-05 10:56:33 -0700897// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
898// which is responsible for recording callee save registers. We explicitly place into jobjects the
899// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
900// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901extern "C" uint64_t artQuickProxyInvokeHandler(
902 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700903 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700904 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
905 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700906 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
907 const char* old_cause =
908 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
909 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700910 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700911 self->VerifyStack();
912 // Start new JNI local reference state.
913 JNIEnvExt* env = self->GetJniEnv();
914 ScopedObjectAccessUnchecked soa(env);
915 ScopedJniEnvLocalRefState env_state(env);
916 // Create local ref. copies of proxy method and the receiver.
917 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
918
919 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700920 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700921 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
922 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700923 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700924 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +0000926 BuildQuickArgumentVisitor local_ref_visitor(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700927 sp, /* is_static= */ false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700928
Ian Rogers848871b2013-08-05 10:56:33 -0700929 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700930 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700931 args.erase(args.begin());
932
933 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700934 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700935 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
936 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700937 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700938 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700939 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampeee29a072017-11-02 15:28:09 -0700940 ObjPtr<mirror::Method> interface_reflect_method =
941 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), interface_method);
942 if (interface_reflect_method == nullptr) {
943 soa.Self()->AssertPendingOOMException();
944 return 0;
945 }
946 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_reflect_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700947
948 // 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 -0700949 // that performs allocations or instrumentation events.
950 instrumentation::Instrumentation* instr = Runtime::Current()->GetInstrumentation();
951 if (instr->HasMethodEntryListeners()) {
952 instr->MethodEnterEvent(soa.Self(),
Vladimir Marko19711d42019-04-12 14:05:34 +0100953 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700954 proxy_method,
955 0);
956 if (soa.Self()->IsExceptionPending()) {
957 instr->MethodUnwindEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100958 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700959 proxy_method,
960 0);
961 return 0;
962 }
963 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700964 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Alex Lightc9167362018-06-11 16:46:43 -0700965 if (soa.Self()->IsExceptionPending()) {
966 if (instr->HasMethodUnwindListeners()) {
967 instr->MethodUnwindEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100968 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700969 proxy_method,
970 0);
971 }
972 } else if (instr->HasMethodExitListeners()) {
973 instr->MethodExitEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100974 soa.Decode<mirror::Object>(rcvr_jobj),
Alex Lightc9167362018-06-11 16:46:43 -0700975 proxy_method,
976 0,
Alex Lightb7c640d2019-03-20 15:52:13 -0700977 {},
Alex Lightc9167362018-06-11 16:46:43 -0700978 result);
979 }
Ian Rogers848871b2013-08-05 10:56:33 -0700980 return result.GetJ();
981}
982
Roland Levillainad0777d2018-02-12 20:00:18 +0000983// Visitor returning a reference argument at a given position in a Quick stack frame.
984// NOTE: Only used for testing purposes.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100985class GetQuickReferenceArgumentAtVisitor final : public QuickArgumentVisitor {
Roland Levillainad0777d2018-02-12 20:00:18 +0000986 public:
987 GetQuickReferenceArgumentAtVisitor(ArtMethod** sp,
988 const char* shorty,
989 uint32_t shorty_len,
990 size_t arg_pos)
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700991 : QuickArgumentVisitor(sp, /* is_static= */ false, shorty, shorty_len),
Roland Levillainad0777d2018-02-12 20:00:18 +0000992 cur_pos_(0u),
993 arg_pos_(arg_pos),
994 ref_arg_(nullptr) {
995 CHECK_LT(arg_pos, shorty_len) << "Argument position greater than the number arguments";
996 }
997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100998 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override {
Roland Levillainad0777d2018-02-12 20:00:18 +0000999 if (cur_pos_ == arg_pos_) {
1000 Primitive::Type type = GetParamPrimitiveType();
1001 CHECK_EQ(type, Primitive::kPrimNot) << "Argument at searched position is not a reference";
1002 ref_arg_ = reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1003 }
1004 ++cur_pos_;
1005 }
1006
1007 StackReference<mirror::Object>* GetReferenceArgument() {
1008 return ref_arg_;
1009 }
1010
1011 private:
1012 // The position of the currently visited argument.
1013 size_t cur_pos_;
1014 // The position of the searched argument.
1015 const size_t arg_pos_;
1016 // The reference argument, if found.
1017 StackReference<mirror::Object>* ref_arg_;
1018
1019 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentAtVisitor);
1020};
1021
1022// Returning reference argument at position `arg_pos` in Quick stack frame at address `sp`.
1023// NOTE: Only used for testing purposes.
1024extern "C" StackReference<mirror::Object>* artQuickGetProxyReferenceArgumentAt(size_t arg_pos,
1025 ArtMethod** sp)
1026 REQUIRES_SHARED(Locks::mutator_lock_) {
1027 ArtMethod* proxy_method = *sp;
1028 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1029 CHECK(!non_proxy_method->IsStatic())
1030 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1031 uint32_t shorty_len = 0;
1032 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
1033 GetQuickReferenceArgumentAtVisitor ref_arg_visitor(sp, shorty, shorty_len, arg_pos);
1034 ref_arg_visitor.VisitArguments();
1035 StackReference<mirror::Object>* ref_arg = ref_arg_visitor.GetReferenceArgument();
1036 return ref_arg;
1037}
1038
1039// Visitor returning all the reference arguments in a Quick stack frame.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001040class GetQuickReferenceArgumentsVisitor final : public QuickArgumentVisitor {
Roland Levillainad0777d2018-02-12 20:00:18 +00001041 public:
1042 GetQuickReferenceArgumentsVisitor(ArtMethod** sp,
1043 bool is_static,
1044 const char* shorty,
1045 uint32_t shorty_len)
1046 : QuickArgumentVisitor(sp, is_static, shorty, shorty_len) {}
1047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001048 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override {
Roland Levillainad0777d2018-02-12 20:00:18 +00001049 Primitive::Type type = GetParamPrimitiveType();
1050 if (type == Primitive::kPrimNot) {
1051 StackReference<mirror::Object>* ref_arg =
1052 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1053 ref_args_.push_back(ref_arg);
1054 }
1055 }
1056
1057 std::vector<StackReference<mirror::Object>*> GetReferenceArguments() {
1058 return ref_args_;
1059 }
1060
1061 private:
1062 // The reference arguments.
1063 std::vector<StackReference<mirror::Object>*> ref_args_;
1064
1065 DISALLOW_COPY_AND_ASSIGN(GetQuickReferenceArgumentsVisitor);
1066};
1067
1068// Returning all reference arguments in Quick stack frame at address `sp`.
1069std::vector<StackReference<mirror::Object>*> GetProxyReferenceArguments(ArtMethod** sp)
1070 REQUIRES_SHARED(Locks::mutator_lock_) {
1071 ArtMethod* proxy_method = *sp;
1072 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
1073 CHECK(!non_proxy_method->IsStatic())
1074 << proxy_method->PrettyMethod() << " " << non_proxy_method->PrettyMethod();
1075 uint32_t shorty_len = 0;
1076 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001077 GetQuickReferenceArgumentsVisitor ref_args_visitor(sp, /*is_static=*/ false, shorty, shorty_len);
Roland Levillainad0777d2018-02-12 20:00:18 +00001078 ref_args_visitor.VisitArguments();
1079 std::vector<StackReference<mirror::Object>*> ref_args = ref_args_visitor.GetReferenceArguments();
1080 return ref_args;
1081}
1082
Ian Rogers848871b2013-08-05 10:56:33 -07001083// Read object references held in arguments from quick frames and place in a JNI local references,
1084// so they don't get garbage collected.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001085class RememberForGcArgumentVisitor final : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001086 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001087 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1088 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001089 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001091 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001092
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001093 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001094
1095 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001096 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001097 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001098 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1099
Mathieu Chartier590fee92013-09-13 13:46:47 -07001100 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001101};
1102
Ian Rogers9758f792014-03-13 09:02:55 -07001103void RememberForGcArgumentVisitor::Visit() {
1104 if (IsParamAReference()) {
1105 StackReference<mirror::Object>* stack_ref =
1106 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1107 jobject reference =
1108 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1109 references_.push_back(std::make_pair(reference, stack_ref));
1110 }
1111}
1112
1113void RememberForGcArgumentVisitor::FixupReferences() {
1114 // Fixup any references which may have changed.
1115 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001116 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001117 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001118 }
1119}
1120
Alex Lightb7edcda2017-04-27 13:20:31 -07001121extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1122 mirror::Object* this_object,
1123 Thread* self,
1124 ArtMethod** sp)
1125 REQUIRES_SHARED(Locks::mutator_lock_) {
1126 const void* result;
1127 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1128 // that part.
1129 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1130 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Alex Light6cae5ea2018-06-07 17:07:02 -07001131 DCHECK(!method->IsProxyMethod())
1132 << "Proxy method " << method->PrettyMethod()
1133 << " (declaring class: " << method->GetDeclaringClass()->PrettyClass() << ")"
1134 << " should not hit instrumentation entrypoint.";
Alex Lightb7edcda2017-04-27 13:20:31 -07001135 if (instrumentation->IsDeoptimized(method)) {
1136 result = GetQuickToInterpreterBridge();
1137 } else {
Alex Light2d441b12018-06-08 15:33:21 -07001138 // This will get the entry point either from the oat file, the JIT or the appropriate bridge
1139 // method if none of those can be found.
1140 result = instrumentation->GetCodeForInvoke(method);
1141 jit::Jit* jit = Runtime::Current()->GetJit();
1142 DCHECK_NE(result, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod();
1143 DCHECK(jit == nullptr ||
1144 // Native methods come through here in Interpreter entrypoints. We might not have
1145 // disabled jit-gc but that is fine since we won't return jit-code for native methods.
1146 method->IsNative() ||
1147 !jit->GetCodeCache()->GetGarbageCollectCode());
1148 DCHECK(!method->IsNative() ||
1149 jit == nullptr ||
1150 !jit->GetCodeCache()->ContainsPc(result))
1151 << method->PrettyMethod() << " code will jump to possibly cleaned up jit code!";
Alex Lightb7edcda2017-04-27 13:20:31 -07001152 }
1153
1154 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1155 bool is_static = method->IsStatic();
1156 uint32_t shorty_len;
1157 const char* shorty =
1158 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1159
1160 ScopedObjectAccessUnchecked soa(self);
1161 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1162 visitor.VisitArguments();
1163
1164 instrumentation->PushInstrumentationStackFrame(self,
1165 is_static ? nullptr : this_object,
1166 method,
1167 QuickArgumentVisitor::GetCallingPc(sp),
1168 interpreter_entry);
1169
1170 visitor.FixupReferences();
1171 if (UNLIKELY(self->IsExceptionPending())) {
1172 return nullptr;
1173 }
1174 CHECK(result != nullptr) << method->PrettyMethod();
1175 return result;
1176}
1177
1178extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1179 ArtMethod** sp,
1180 uint64_t* gpr_result,
1181 uint64_t* fpr_result)
1182 REQUIRES_SHARED(Locks::mutator_lock_) {
1183 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1184 CHECK(gpr_result != nullptr);
1185 CHECK(fpr_result != nullptr);
1186 // Instrumentation exit stub must not be entered with a pending exception.
1187 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1188 << self->GetException()->Dump();
1189 // Compute address of return PC and sanity check that it currently holds 0.
Vladimir Markod3083dd2018-05-17 08:43:47 +01001190 constexpr size_t return_pc_offset =
1191 RuntimeCalleeSaveFrame::GetReturnPcOffset(CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001192 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1193 return_pc_offset);
1194 CHECK_EQ(*return_pc, 0U);
1195
1196 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1197 // deoptimization shouldn't be performed with the high-half having the return address. When
1198 // deoptimization should be performed the low half is zero and the high-half the address of the
1199 // deoptimization entry point.
1200 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1201 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1202 self, return_pc, gpr_result, fpr_result);
Vladimir Markofac21782018-03-13 17:01:09 +00001203 if (self->IsExceptionPending() || self->ObserveAsyncException()) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001204 return GetTwoWordFailureValue();
1205 }
1206 return return_or_deoptimize_pc;
1207}
1208
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001209static std::string DumpInstruction(ArtMethod* method, uint32_t dex_pc)
1210 REQUIRES_SHARED(Locks::mutator_lock_) {
1211 if (dex_pc == static_cast<uint32_t>(-1)) {
1212 CHECK(method == jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt));
1213 return "<native>";
1214 } else {
1215 CodeItemInstructionAccessor accessor = method->DexInstructions();
1216 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1217 return accessor.InstructionAt(dex_pc).DumpString(method->GetDexFile());
1218 }
1219}
1220
Vladimir Marko606adb32018-04-05 14:49:24 +01001221static void DumpB74410240ClassData(ObjPtr<mirror::Class> klass)
1222 REQUIRES_SHARED(Locks::mutator_lock_) {
1223 std::string storage;
1224 const char* descriptor = klass->GetDescriptor(&storage);
1225 LOG(FATAL_WITHOUT_ABORT) << " " << DescribeLoaders(klass->GetClassLoader(), descriptor);
1226 const OatDexFile* oat_dex_file = klass->GetDexFile().GetOatDexFile();
1227 if (oat_dex_file != nullptr) {
1228 const OatFile* oat_file = oat_dex_file->GetOatFile();
1229 const char* dex2oat_cmdline =
1230 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
1231 LOG(FATAL_WITHOUT_ABORT) << " OatFile: " << oat_file->GetLocation()
1232 << "; " << (dex2oat_cmdline != nullptr ? dex2oat_cmdline : "<not recorded>");
1233 }
1234}
1235
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001236static void DumpB74410240DebugData(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
1237 // Mimick the search for the caller and dump some data while doing so.
Vladimir Marko606adb32018-04-05 14:49:24 +01001238 LOG(FATAL_WITHOUT_ABORT) << "Dumping debugging data, please attach a bugreport to b/74410240.";
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001239
1240 constexpr CalleeSaveType type = CalleeSaveType::kSaveRefsAndArgs;
1241 CHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
1242
Vladimir Markod3083dd2018-05-17 08:43:47 +01001243 constexpr size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001244 auto** caller_sp = reinterpret_cast<ArtMethod**>(
1245 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +01001246 constexpr size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001247 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
1248 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
1249 ArtMethod* outer_method = *caller_sp;
1250
1251 if (UNLIKELY(caller_pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
1252 LOG(FATAL_WITHOUT_ABORT) << "Method: " << outer_method->PrettyMethod()
1253 << " native pc: " << caller_pc << " Instrumented!";
1254 return;
1255 }
1256
1257 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
1258 CHECK(current_code != nullptr);
1259 CHECK(current_code->IsOptimized());
1260 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky052f8ca2018-04-26 15:42:54 +01001261 CodeInfo code_info(current_code);
David Srbecky052f8ca2018-04-26 15:42:54 +01001262 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001263 CHECK(stack_map.IsValid());
David Srbecky052f8ca2018-04-26 15:42:54 +01001264 uint32_t dex_pc = stack_map.GetDexPc();
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001265
1266 // Log the outer method and its associated dex file and class table pointer which can be used
1267 // to find out if the inlined methods were defined by other dex file(s) or class loader(s).
1268 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1269 LOG(FATAL_WITHOUT_ABORT) << "Outer: " << outer_method->PrettyMethod()
1270 << " native pc: " << caller_pc
1271 << " dex pc: " << dex_pc
1272 << " dex file: " << outer_method->GetDexFile()->GetLocation()
1273 << " class table: " << class_linker->ClassTableForClassLoader(outer_method->GetClassLoader());
Vladimir Marko606adb32018-04-05 14:49:24 +01001274 DumpB74410240ClassData(outer_method->GetDeclaringClass());
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001275 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(outer_method, dex_pc);
1276
1277 ArtMethod* caller = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +01001278 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
1279 for (InlineInfo inline_info : inline_infos) {
1280 const char* tag = "";
1281 dex_pc = inline_info.GetDexPc();
1282 if (inline_info.EncodesArtMethod()) {
1283 tag = "encoded ";
1284 caller = inline_info.GetArtMethod();
1285 } else {
David Srbecky8cd54542018-07-15 23:58:44 +01001286 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
David Srbecky93bd3612018-07-02 19:30:18 +01001287 if (dex_pc == static_cast<uint32_t>(-1)) {
1288 tag = "special ";
1289 CHECK(inline_info.Equals(inline_infos.back()));
1290 caller = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
1291 CHECK_EQ(caller->GetDexMethodIndex(), method_index);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001292 } else {
David Srbecky93bd3612018-07-02 19:30:18 +01001293 ObjPtr<mirror::DexCache> dex_cache = caller->GetDexCache();
1294 ObjPtr<mirror::ClassLoader> class_loader = caller->GetClassLoader();
1295 caller = class_linker->LookupResolvedMethod(method_index, dex_cache, class_loader);
1296 CHECK(caller != nullptr);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001297 }
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001298 }
David Srbecky93bd3612018-07-02 19:30:18 +01001299 LOG(FATAL_WITHOUT_ABORT) << "InlineInfo #" << inline_info.Row()
1300 << ": " << tag << caller->PrettyMethod()
1301 << " dex pc: " << dex_pc
1302 << " dex file: " << caller->GetDexFile()->GetLocation()
1303 << " class table: "
1304 << class_linker->ClassTableForClassLoader(caller->GetClassLoader());
1305 DumpB74410240ClassData(caller->GetDeclaringClass());
1306 LOG(FATAL_WITHOUT_ABORT) << " instruction: " << DumpInstruction(caller, dex_pc);
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001307 }
1308}
1309
Ian Rogers848871b2013-08-05 10:56:33 -07001310// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311extern "C" const void* artQuickResolutionTrampoline(
1312 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001313 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001314 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1315 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1316 // does not have the same stack layout as the callee-save method).
1317 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001318 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001319 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001320 ScopedObjectAccessUnchecked soa(env);
1321 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001322 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001323
1324 // Compute details about the called method (avoid GCs)
1325 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001326 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001327 MethodReference called_method(nullptr, 0);
1328 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001329 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001330 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001331 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001332 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001333
David Srbecky2c762572018-06-27 10:09:11 +01001334 {
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001335 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
David Sehr0225f8e2018-01-31 08:52:24 +00001336 CodeItemInstructionAccessor accessor(caller->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001337 CHECK_LT(dex_pc, accessor.InsnsSizeInCodeUnits());
1338 const Instruction& instr = accessor.InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01001339 Instruction::Code instr_code = instr.Opcode();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001340 bool is_range;
1341 switch (instr_code) {
1342 case Instruction::INVOKE_DIRECT:
1343 invoke_type = kDirect;
1344 is_range = false;
1345 break;
1346 case Instruction::INVOKE_DIRECT_RANGE:
1347 invoke_type = kDirect;
1348 is_range = true;
1349 break;
1350 case Instruction::INVOKE_STATIC:
1351 invoke_type = kStatic;
1352 is_range = false;
1353 break;
1354 case Instruction::INVOKE_STATIC_RANGE:
1355 invoke_type = kStatic;
1356 is_range = true;
1357 break;
1358 case Instruction::INVOKE_SUPER:
1359 invoke_type = kSuper;
1360 is_range = false;
1361 break;
1362 case Instruction::INVOKE_SUPER_RANGE:
1363 invoke_type = kSuper;
1364 is_range = true;
1365 break;
1366 case Instruction::INVOKE_VIRTUAL:
1367 invoke_type = kVirtual;
1368 is_range = false;
1369 break;
1370 case Instruction::INVOKE_VIRTUAL_RANGE:
1371 invoke_type = kVirtual;
1372 is_range = true;
1373 break;
1374 case Instruction::INVOKE_INTERFACE:
1375 invoke_type = kInterface;
1376 is_range = false;
1377 break;
1378 case Instruction::INVOKE_INTERFACE_RANGE:
1379 invoke_type = kInterface;
1380 is_range = true;
1381 break;
1382 default:
Vladimir Marko5b4b9a02018-03-16 09:42:09 +00001383 DumpB74410240DebugData(sp);
Vladimir Markod7559b72017-09-28 13:50:37 +01001384 LOG(FATAL) << "Unexpected call into trampoline: " << instr.DumpString(nullptr);
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001385 UNREACHABLE();
1386 }
Vladimir Markod7559b72017-09-28 13:50:37 +01001387 called_method.index = (is_range) ? instr.VRegB_3rc() : instr.VRegB_35c();
David Srbecky2c762572018-06-27 10:09:11 +01001388 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
1389 << called_method.index;
Ian Rogers848871b2013-08-05 10:56:33 -07001390 }
Ian Rogers848871b2013-08-05 10:56:33 -07001391 } else {
1392 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001393 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001394 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001395 }
1396 uint32_t shorty_len;
1397 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001398 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001399 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001400 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001401 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001402 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001403 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001404 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001405 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001406 mirror::Object* dummy = nullptr;
1407 HandleWrapper<mirror::Object> h_receiver(
1408 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001409 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001410 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001411 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001412
1413 // Update .bss entry in oat file if any.
1414 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
Vladimir Markof3c52b42017-11-17 17:32:12 +00001415 size_t bss_offset = IndexBssMappingLookup::GetBssOffset(
1416 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping(),
1417 called_method.index,
1418 called_method.dex_file->NumMethodIds(),
1419 static_cast<size_t>(kRuntimePointerSize));
1420 if (bss_offset != IndexBssMappingLookup::npos) {
1421 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1422 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1423 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1424 oat_file->BssBegin() + bss_offset));
1425 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1426 DCHECK_LT(method_entry,
1427 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
Vladimir Markod5fd5c32019-07-02 14:46:32 +01001428 std::atomic<ArtMethod*>* atomic_entry =
1429 reinterpret_cast<std::atomic<ArtMethod*>*>(method_entry);
1430 static_assert(sizeof(*method_entry) == sizeof(*atomic_entry), "Size check.");
1431 atomic_entry->store(called, std::memory_order_release);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001432 }
1433 }
Ian Rogers848871b2013-08-05 10:56:33 -07001434 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001435 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001436 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001437 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001438 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001439 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001440 if (virtual_or_interface || invoke_type == kSuper) {
1441 // Refine called method based on receiver for kVirtual/kInterface, and
1442 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001443 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001444 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001445 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001446 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001447 } else if (invoke_type == kInterface) {
1448 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001449 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001450 } else {
1451 DCHECK_EQ(invoke_type, kSuper);
1452 CHECK(caller != nullptr) << invoke_type;
Vladimir Markoba118822017-06-12 15:41:56 +01001453 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001454 caller->GetDexFile()->GetMethodId(called_method.index).class_idx_, caller);
Alex Lightfedd91d2016-01-07 14:49:16 -08001455 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001456 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001457 } else {
1458 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001459 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001460 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001461 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001462
David Sehr709b0702016-10-13 09:12:37 -07001463 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1464 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001465 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001466 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001467
Ian Rogers848871b2013-08-05 10:56:33 -07001468 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001469 StackHandleScope<1> hs(soa.Self());
1470 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001471 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Alex Light3dacdd62019-03-12 15:45:47 +00001472 bool force_interpreter = self->IsForceInterpreter() && !called->IsNative();
Ian Rogers848871b2013-08-05 10:56:33 -07001473 if (LIKELY(called_class->IsInitialized())) {
Alex Light3dacdd62019-03-12 15:45:47 +00001474 if (UNLIKELY(force_interpreter ||
1475 Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001476 // If we are single-stepping or the called method is deoptimized (by a
1477 // breakpoint, for example), then we have to execute the called method
1478 // with the interpreter.
1479 code = GetQuickToInterpreterBridge();
1480 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1481 // If the caller is deoptimized (by a breakpoint, for example), we have to
1482 // continue its execution with interpreter when returning from the called
1483 // method. Because we do not want to execute the called method with the
1484 // interpreter, we wrap its execution into the instrumentation stubs.
1485 // When the called method returns, it will execute the instrumentation
1486 // exit hook that will determine the need of the interpreter with a call
1487 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1488 // it is needed.
1489 code = GetQuickInstrumentationEntryPoint();
1490 } else {
1491 code = called->GetEntryPointFromQuickCompiledCode();
1492 }
Ian Rogers848871b2013-08-05 10:56:33 -07001493 } else if (called_class->IsInitializing()) {
Alex Light3dacdd62019-03-12 15:45:47 +00001494 if (UNLIKELY(force_interpreter ||
1495 Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001496 // If we are single-stepping or the called method is deoptimized (by a
1497 // breakpoint, for example), then we have to execute the called method
1498 // with the interpreter.
1499 code = GetQuickToInterpreterBridge();
1500 } else if (invoke_type == kStatic) {
Nicolas Geoffray7989ac92019-04-10 12:42:30 +01001501 // Class is still initializing, go to JIT or oat and grab code (trampoline must be
1502 // left in place until class is initialized to stop races between threads).
Nicolas Geoffray32384402019-07-17 20:06:44 +01001503 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001504 } else {
1505 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001506 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001507 }
1508 } else {
1509 DCHECK(called_class->IsErroneous());
1510 }
1511 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001512 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001513 // Fixup any locally saved objects may have moved during a GC.
1514 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001515 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001516 *sp = called;
1517
Ian Rogers848871b2013-08-05 10:56:33 -07001518 return code;
1519}
1520
Andreas Gampec147b002014-03-06 18:11:06 -08001521/*
1522 * This class uses a couple of observations to unite the different calling conventions through
1523 * a few constants.
1524 *
1525 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1526 * possible alignment.
1527 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1528 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1529 * when we have to split things
1530 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1531 * and we can use Int handling directly.
1532 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1533 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1534 * extension should be compatible with Aarch64, which mandates copying the available bits
1535 * into LSB and leaving the rest unspecified.
1536 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1537 * the stack.
1538 * 6) There is only little endian.
1539 *
1540 *
1541 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1542 * follows:
1543 *
1544 * void PushGpr(uintptr_t): Add a value for the next GPR
1545 *
1546 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1547 * padding, that is, think the architecture is 32b and aligns 64b.
1548 *
1549 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1550 * split this if necessary. The current state will have aligned, if
1551 * necessary.
1552 *
1553 * void PushStack(uintptr_t): Push a value to the stack.
1554 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001555 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001556 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001557 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001558 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001559 *
1560 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001561template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001562 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563#if defined(__arm__)
1564 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001565 static constexpr bool kNativeSoftFloatAbi = true;
1566 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001567 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1568
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001569 static constexpr size_t kRegistersNeededForLong = 2;
1570 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001571 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001572 static constexpr bool kMultiFPRegistersWidened = false;
1573 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001574 static constexpr bool kAlignLongOnStack = true;
1575 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001576#elif defined(__aarch64__)
1577 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1578 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1579 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1580
1581 static constexpr size_t kRegistersNeededForLong = 1;
1582 static constexpr size_t kRegistersNeededForDouble = 1;
1583 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001584 static constexpr bool kMultiFPRegistersWidened = false;
1585 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001586 static constexpr bool kAlignLongOnStack = false;
1587 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001588#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001589 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001590 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1591 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001592
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001593 static constexpr size_t kRegistersNeededForLong = 2;
1594 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001595 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001596 static constexpr bool kMultiFPRegistersWidened = true;
1597 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001598 static constexpr bool kAlignLongOnStack = true;
1599 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001600#elif defined(__mips__) && defined(__LP64__)
1601 // Let the code prepare GPRs only and we will load the FPRs with same data.
1602 static constexpr bool kNativeSoftFloatAbi = true;
1603 static constexpr size_t kNumNativeGprArgs = 8;
1604 static constexpr size_t kNumNativeFprArgs = 0;
1605
1606 static constexpr size_t kRegistersNeededForLong = 1;
1607 static constexpr size_t kRegistersNeededForDouble = 1;
1608 static constexpr bool kMultiRegistersAligned = false;
1609 static constexpr bool kMultiFPRegistersWidened = false;
1610 static constexpr bool kMultiGPRegistersWidened = true;
1611 static constexpr bool kAlignLongOnStack = false;
1612 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001613#elif defined(__i386__)
1614 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001615 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001616 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1617 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1618
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001619 static constexpr size_t kRegistersNeededForLong = 2;
1620 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001621 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001622 static constexpr bool kMultiFPRegistersWidened = false;
1623 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001624 static constexpr bool kAlignLongOnStack = false;
1625 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001626#elif defined(__x86_64__)
1627 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1628 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1629 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1630
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001631 static constexpr size_t kRegistersNeededForLong = 1;
1632 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001633 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001634 static constexpr bool kMultiFPRegistersWidened = false;
1635 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001636 static constexpr bool kAlignLongOnStack = false;
1637 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001638#else
1639#error "Unsupported architecture"
1640#endif
1641
Andreas Gampec147b002014-03-06 18:11:06 -08001642 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001643 explicit BuildNativeCallFrameStateMachine(T* delegate)
1644 : gpr_index_(kNumNativeGprArgs),
1645 fpr_index_(kNumNativeFprArgs),
1646 stack_entries_(0),
1647 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001648 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1649 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001650 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1651 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001652 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001653
Andreas Gampec200a4a2014-06-16 18:39:09 -07001654 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001655
Ian Rogers1428dce2014-10-21 15:02:15 -07001656 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001657 return gpr_index_ > 0;
1658 }
1659
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001661 if (HavePointerGpr()) {
1662 gpr_index_--;
1663 PushGpr(reinterpret_cast<uintptr_t>(val));
1664 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001665 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001666 PushStack(reinterpret_cast<uintptr_t>(val));
1667 gpr_index_ = 0;
1668 }
1669 }
1670
Ian Rogers1428dce2014-10-21 15:02:15 -07001671 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001672 return gpr_index_ > 0;
1673 }
1674
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001675 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001676 uintptr_t handle = PushHandle(ptr);
1677 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001678 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001679 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001680 } else {
1681 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001682 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001683 gpr_index_ = 0;
1684 }
1685 }
1686
Ian Rogers1428dce2014-10-21 15:02:15 -07001687 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001688 return gpr_index_ > 0;
1689 }
1690
1691 void AdvanceInt(uint32_t val) {
1692 if (HaveIntGpr()) {
1693 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001694 if (kMultiGPRegistersWidened) {
1695 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001696 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001697 } else {
1698 PushGpr(val);
1699 }
Andreas Gampec147b002014-03-06 18:11:06 -08001700 } else {
1701 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001702 if (kMultiGPRegistersWidened) {
1703 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001704 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001705 } else {
1706 PushStack(val);
1707 }
Andreas Gampec147b002014-03-06 18:11:06 -08001708 gpr_index_ = 0;
1709 }
1710 }
1711
Ian Rogers1428dce2014-10-21 15:02:15 -07001712 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001713 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1714 }
1715
Ian Rogers1428dce2014-10-21 15:02:15 -07001716 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001717 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1718 kAlignLongOnStack && // and when it needs alignment
1719 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1720 }
1721
Ian Rogers1428dce2014-10-21 15:02:15 -07001722 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001723 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1724 kAlignLongOnStack && // and when it needs 8B alignment
1725 (stack_entries_ & 1) == 1; // counter is odd
1726 }
1727
1728 void AdvanceLong(uint64_t val) {
1729 if (HaveLongGpr()) {
1730 if (LongGprNeedsPadding()) {
1731 PushGpr(0);
1732 gpr_index_--;
1733 }
1734 if (kRegistersNeededForLong == 1) {
1735 PushGpr(static_cast<uintptr_t>(val));
1736 } else {
1737 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1738 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1739 }
1740 gpr_index_ -= kRegistersNeededForLong;
1741 } else {
1742 if (LongStackNeedsPadding()) {
1743 PushStack(0);
1744 stack_entries_++;
1745 }
1746 if (kRegistersNeededForLong == 1) {
1747 PushStack(static_cast<uintptr_t>(val));
1748 stack_entries_++;
1749 } else {
1750 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1751 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1752 stack_entries_ += 2;
1753 }
1754 gpr_index_ = 0;
1755 }
1756 }
1757
Ian Rogers1428dce2014-10-21 15:02:15 -07001758 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001759 return fpr_index_ > 0;
1760 }
1761
Andreas Gampec147b002014-03-06 18:11:06 -08001762 void AdvanceFloat(float val) {
1763 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001764 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001765 } else {
1766 if (HaveFloatFpr()) {
1767 fpr_index_--;
1768 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001769 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001770 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001771 } else {
1772 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001773 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001774 }
1775 } else {
1776 PushFpr4(val);
1777 }
1778 } else {
1779 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001780 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001781 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782 // Note: We need to jump through those hoops to make the compiler happy.
1783 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001784 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001785 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001786 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001787 }
1788 fpr_index_ = 0;
1789 }
1790 }
1791 }
1792
Ian Rogers1428dce2014-10-21 15:02:15 -07001793 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001794 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1795 }
1796
Ian Rogers1428dce2014-10-21 15:02:15 -07001797 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001798 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1799 kAlignDoubleOnStack && // and when it needs alignment
1800 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1801 }
1802
Ian Rogers1428dce2014-10-21 15:02:15 -07001803 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001804 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1805 kAlignDoubleOnStack && // and when it needs 8B alignment
1806 (stack_entries_ & 1) == 1; // counter is odd
1807 }
1808
1809 void AdvanceDouble(uint64_t val) {
1810 if (kNativeSoftFloatAbi) {
1811 AdvanceLong(val);
1812 } else {
1813 if (HaveDoubleFpr()) {
1814 if (DoubleFprNeedsPadding()) {
1815 PushFpr4(0);
1816 fpr_index_--;
1817 }
1818 PushFpr8(val);
1819 fpr_index_ -= kRegistersNeededForDouble;
1820 } else {
1821 if (DoubleStackNeedsPadding()) {
1822 PushStack(0);
1823 stack_entries_++;
1824 }
1825 if (kRegistersNeededForDouble == 1) {
1826 PushStack(static_cast<uintptr_t>(val));
1827 stack_entries_++;
1828 } else {
1829 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1830 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1831 stack_entries_ += 2;
1832 }
1833 fpr_index_ = 0;
1834 }
1835 }
1836 }
1837
Ian Rogers1428dce2014-10-21 15:02:15 -07001838 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001839 return stack_entries_;
1840 }
1841
Ian Rogers1428dce2014-10-21 15:02:15 -07001842 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001843 return kNumNativeGprArgs - gpr_index_;
1844 }
1845
Ian Rogers1428dce2014-10-21 15:02:15 -07001846 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001847 return kNumNativeFprArgs - fpr_index_;
1848 }
1849
1850 private:
1851 void PushGpr(uintptr_t val) {
1852 delegate_->PushGpr(val);
1853 }
1854 void PushFpr4(float val) {
1855 delegate_->PushFpr4(val);
1856 }
1857 void PushFpr8(uint64_t val) {
1858 delegate_->PushFpr8(val);
1859 }
1860 void PushStack(uintptr_t val) {
1861 delegate_->PushStack(val);
1862 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001863 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001864 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001865 }
1866
1867 uint32_t gpr_index_; // Number of free GPRs
1868 uint32_t fpr_index_; // Number of free FPRs
1869 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1870 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001871 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001872};
1873
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1875// in subclasses.
1876//
1877// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1878// them with handles.
1879class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001880 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1882
1883 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001884
Ian Rogers1428dce2014-10-21 15:02:15 -07001885 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001886 return num_stack_entries_ * sizeof(uintptr_t);
1887 }
1888
Ian Rogers1428dce2014-10-21 15:02:15 -07001889 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001890 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001891 // Align by kStackAlignment.
1892 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001893 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001894 }
1895
Ian Rogers1428dce2014-10-21 15:02:15 -07001896 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1897 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001898 // Assumption is OK right now, as we have soft-float arm
1899 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1900 sp8 -= fregs * sizeof(uintptr_t);
1901 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1902 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1903 sp8 -= iregs * sizeof(uintptr_t);
1904 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1905 return sp8;
1906 }
Andreas Gampec147b002014-03-06 18:11:06 -08001907
Andreas Gampec200a4a2014-06-16 18:39:09 -07001908 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001909 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 // Native call stack.
1911 sp8 = LayoutCallStack(sp8);
1912 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001913
Andreas Gampec200a4a2014-06-16 18:39:09 -07001914 // Put fprs and gprs below.
1915 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001916
Andreas Gampec200a4a2014-06-16 18:39:09 -07001917 // Return the new bottom.
1918 return sp8;
1919 }
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921 virtual void WalkHeader(
1922 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001923 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001924 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001925
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001926 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001927 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1928
1929 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001930
1931 for (uint32_t i = 1; i < shorty_len; ++i) {
1932 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1933 switch (cur_type_) {
1934 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001935 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001936 sm.AdvanceHandleScope(
1937 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001938 break;
1939
1940 case Primitive::kPrimBoolean:
1941 case Primitive::kPrimByte:
1942 case Primitive::kPrimChar:
1943 case Primitive::kPrimShort:
1944 case Primitive::kPrimInt:
1945 sm.AdvanceInt(0);
1946 break;
1947 case Primitive::kPrimFloat:
1948 sm.AdvanceFloat(0);
1949 break;
1950 case Primitive::kPrimDouble:
1951 sm.AdvanceDouble(0);
1952 break;
1953 case Primitive::kPrimLong:
1954 sm.AdvanceLong(0);
1955 break;
1956 default:
1957 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001958 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001959 }
1960 }
1961
Ian Rogers1428dce2014-10-21 15:02:15 -07001962 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001963 }
1964
1965 void PushGpr(uintptr_t /* val */) {
1966 // not optimizing registers, yet
1967 }
1968
1969 void PushFpr4(float /* val */) {
1970 // not optimizing registers, yet
1971 }
1972
1973 void PushFpr8(uint64_t /* val */) {
1974 // not optimizing registers, yet
1975 }
1976
1977 void PushStack(uintptr_t /* val */) {
1978 // counting is already done in the superclass
1979 }
1980
Andreas Gampec200a4a2014-06-16 18:39:09 -07001981 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001982 return reinterpret_cast<uintptr_t>(nullptr);
1983 }
1984
Andreas Gampec200a4a2014-06-16 18:39:09 -07001985 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001986 uint32_t num_stack_entries_;
1987};
1988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001989class ComputeGenericJniFrameSize final : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001990 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001991 explicit ComputeGenericJniFrameSize(bool critical_native)
1992 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001993
Andreas Gampec200a4a2014-06-16 18:39:09 -07001994 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1995 // is at *m = sp. Will update to point to the bottom of the save frame.
1996 //
1997 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001999 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002000 ArtMethod* method = **m;
2001
Andreas Gampe542451c2016-07-26 09:02:02 -07002002 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002003
Andreas Gampec200a4a2014-06-16 18:39:09 -07002004 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
2005
2006 // First, fix up the layout of the callee-save frame.
2007 // We have to squeeze in the HandleScope, and relocate the method pointer.
2008
2009 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07002010 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002011
2012 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002013 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002015
2016 sp8 -= scope_and_method;
2017 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002018 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07002021 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
2022 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023
2024 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
2025 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
2027 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002028 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002029 }
2030
Andreas Gampec200a4a2014-06-16 18:39:09 -07002031 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07002032 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033 // Reference cookie and padding
2034 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002035 }
2036
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
2038 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002040 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002041 // First, fix up the layout of the callee-save frame.
2042 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07002043 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002044
2045 // The bottom of the callee-save frame is now where the method is, *m.
2046 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
2047
2048 // Add space for cookie.
2049 LayoutCookie(&sp8);
2050
2051 return sp8;
2052 }
2053
2054 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
2056 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
2057 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002058 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002059 Walk(shorty, shorty_len);
2060
2061 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07002062 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002063
2064 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
2065
2066 // Return the new bottom.
2067 return sp8;
2068 }
2069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002070 uintptr_t PushHandle(mirror::Object* /* ptr */) override;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002071
2072 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002073 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002074 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002075
2076 private:
2077 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002078 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002079};
2080
2081uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
2082 num_handle_scope_references_++;
2083 return reinterpret_cast<uintptr_t>(nullptr);
2084}
2085
2086void ComputeGenericJniFrameSize::WalkHeader(
2087 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002088 // First 2 parameters are always excluded for @CriticalNative.
2089 if (UNLIKELY(critical_native_)) {
2090 return;
2091 }
2092
Andreas Gampec200a4a2014-06-16 18:39:09 -07002093 // JNIEnv
2094 sm->AdvancePointer(nullptr);
2095
2096 // Class object or this as first argument
2097 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
2098}
2099
2100// Class to push values to three separate regions. Used to fill the native call part. Adheres to
2101// the template requirements of BuildGenericJniFrameStateMachine.
2102class FillNativeCall {
2103 public:
2104 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
2105 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
2106
2107 virtual ~FillNativeCall() {}
2108
2109 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
2110 cur_gpr_reg_ = gpr_regs;
2111 cur_fpr_reg_ = fpr_regs;
2112 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08002113 }
2114
2115 void PushGpr(uintptr_t val) {
2116 *cur_gpr_reg_ = val;
2117 cur_gpr_reg_++;
2118 }
2119
2120 void PushFpr4(float val) {
2121 *cur_fpr_reg_ = val;
2122 cur_fpr_reg_++;
2123 }
2124
2125 void PushFpr8(uint64_t val) {
2126 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
2127 *tmp = val;
2128 cur_fpr_reg_ += 2;
2129 }
2130
2131 void PushStack(uintptr_t val) {
2132 *cur_stack_arg_ = val;
2133 cur_stack_arg_++;
2134 }
2135
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002136 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002137 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002138 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002139 }
2140
2141 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002142 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002143 uint32_t* cur_fpr_reg_;
2144 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002145};
Andreas Gampec147b002014-03-06 18:11:06 -08002146
Andreas Gampec200a4a2014-06-16 18:39:09 -07002147// Visits arguments on the stack placing them into a region lower down the stack for the benefit
2148// of transitioning into native code.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002149class BuildGenericJniFrameVisitor final : public QuickArgumentVisitor {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002150 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002151 BuildGenericJniFrameVisitor(Thread* self,
2152 bool is_static,
2153 bool critical_native,
2154 const char* shorty,
2155 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002156 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002157 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002158 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2159 sm_(&jni_call_) {
2160 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002161 uintptr_t* start_gpr_reg;
2162 uint32_t* start_fpr_reg;
2163 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002165 &handle_scope_,
2166 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002167 &start_gpr_reg, &start_fpr_reg);
2168
Andreas Gampec200a4a2014-06-16 18:39:09 -07002169 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2170
Igor Murashkin06a04e02016-09-13 15:57:37 -07002171 // First 2 parameters are always excluded for CriticalNative methods.
2172 if (LIKELY(!critical_native)) {
2173 // jni environment is always first argument
2174 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002175
Igor Murashkin06a04e02016-09-13 15:57:37 -07002176 if (is_static) {
Vladimir Markod93e3742018-07-18 10:58:13 +01002177 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass().Ptr());
Igor Murashkin06a04e02016-09-13 15:57:37 -07002178 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002179 }
2180 }
2181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002182 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) override;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002183
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002184 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002185
Vladimir Markof39745e2016-01-26 12:16:55 +00002186 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002187 return handle_scope_->GetHandle(0).GetReference();
2188 }
2189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002190 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002191 return handle_scope_->GetHandle(0).ToJObject();
2192 }
2193
Ian Rogers1428dce2014-10-21 15:02:15 -07002194 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002195 return bottom_of_used_area_;
2196 }
2197
2198 private:
2199 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002200 class FillJniCall final : public FillNativeCall {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002201 public:
2202 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002203 HandleScope* handle_scope, bool critical_native)
2204 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2205 handle_scope_(handle_scope),
2206 cur_entry_(0),
2207 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002209 uintptr_t PushHandle(mirror::Object* ref) override REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002210
2211 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2212 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2213 handle_scope_ = scope;
2214 cur_entry_ = 0U;
2215 }
2216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002217 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002218 // Initialize padding entries.
2219 size_t expected_slots = handle_scope_->NumberOfReferences();
2220 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002221 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002222 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002223
2224 if (!critical_native_) {
2225 // Non-critical natives have at least the self class (jclass) or this (jobject).
2226 DCHECK_NE(cur_entry_, 0U);
2227 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002228 }
2229
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002230 bool CriticalNative() const {
2231 return critical_native_;
2232 }
2233
Andreas Gampec200a4a2014-06-16 18:39:09 -07002234 private:
2235 HandleScope* handle_scope_;
2236 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002237 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002238 };
2239
2240 HandleScope* handle_scope_;
2241 FillJniCall jni_call_;
2242 void* bottom_of_used_area_;
2243
2244 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002245
2246 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2247};
2248
Andreas Gampec200a4a2014-06-16 18:39:09 -07002249uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2250 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002251 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002252 h.Assign(ref);
2253 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2254 cur_entry_++;
2255 return tmp;
2256}
2257
Ian Rogers9758f792014-03-13 09:02:55 -07002258void BuildGenericJniFrameVisitor::Visit() {
2259 Primitive::Type type = GetParamPrimitiveType();
2260 switch (type) {
2261 case Primitive::kPrimLong: {
2262 jlong long_arg;
2263 if (IsSplitLongOrDouble()) {
2264 long_arg = ReadSplitLongParam();
2265 } else {
2266 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2267 }
2268 sm_.AdvanceLong(long_arg);
2269 break;
2270 }
2271 case Primitive::kPrimDouble: {
2272 uint64_t double_arg;
2273 if (IsSplitLongOrDouble()) {
2274 // Read into union so that we don't case to a double.
2275 double_arg = ReadSplitLongParam();
2276 } else {
2277 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2278 }
2279 sm_.AdvanceDouble(double_arg);
2280 break;
2281 }
2282 case Primitive::kPrimNot: {
2283 StackReference<mirror::Object>* stack_ref =
2284 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002285 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002286 break;
2287 }
2288 case Primitive::kPrimFloat:
2289 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2290 break;
2291 case Primitive::kPrimBoolean: // Fall-through.
2292 case Primitive::kPrimByte: // Fall-through.
2293 case Primitive::kPrimChar: // Fall-through.
2294 case Primitive::kPrimShort: // Fall-through.
2295 case Primitive::kPrimInt: // Fall-through.
2296 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2297 break;
2298 case Primitive::kPrimVoid:
2299 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002300 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002301 }
2302}
2303
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002304void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002305 // Clear out rest of the scope.
2306 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002307 if (!jni_call_.CriticalNative()) {
2308 // Install HandleScope.
2309 self->PushHandleScope(handle_scope_);
2310 }
Ian Rogers9758f792014-03-13 09:02:55 -07002311}
2312
Alex Lightd78ddec2017-04-18 15:20:38 -07002313extern "C" const void* artFindNativeMethod(Thread* self);
Andreas Gampe90546832014-03-12 18:07:19 -07002314
Igor Murashkin06a04e02016-09-13 15:57:37 -07002315static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2316 uint32_t cookie,
2317 bool fast_native ATTRIBUTE_UNUSED,
2318 jobject l,
2319 jobject lock) {
2320 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002321 if (lock != nullptr) {
2322 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2323 } else {
2324 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2325 }
2326}
2327
Igor Murashkin06a04e02016-09-13 15:57:37 -07002328static void artQuickGenericJniEndJNINonRef(Thread* self,
2329 uint32_t cookie,
2330 bool fast_native,
2331 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002332 if (lock != nullptr) {
2333 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002334 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002335 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002336 if (UNLIKELY(fast_native)) {
2337 JniMethodFastEnd(cookie, self);
2338 } else {
2339 JniMethodEnd(cookie, self);
2340 }
Andreas Gampead615172014-04-04 16:20:13 -07002341 }
2342}
2343
Andreas Gampec147b002014-03-06 18:11:06 -08002344/*
2345 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002346 * 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 -08002347 * The final element on the stack is a pointer to the native code.
2348 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002349 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002350 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002351 *
Andreas Gampec147b002014-03-06 18:11:06 -08002352 * The return of this function denotes:
2353 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2354 * 2) An error, if the value is negative.
2355 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002356extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002357 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002358 // Note: We cannot walk the stack properly until fixed up below.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002360 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Vladimir Marko2196c652017-11-30 16:16:07 +00002361 Runtime* runtime = Runtime::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002362 uint32_t shorty_len = 0;
2363 const char* shorty = called->GetShorty(&shorty_len);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002364 bool critical_native = called->IsCriticalNative();
2365 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -07002366 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002367
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002368 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002369 BuildGenericJniFrameVisitor visitor(self,
2370 called->IsStatic(),
2371 critical_native,
2372 shorty,
2373 shorty_len,
2374 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002375 {
2376 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2377 visitor.VisitArguments();
2378 // FinalizeHandleScope pushes the handle scope on the thread.
2379 visitor.FinalizeHandleScope(self);
2380 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002381
Vladimir Markob0a6aee2017-10-27 10:34:04 +01002382 // Fix up managed-stack things in Thread. After this we can walk the stack.
Vladimir Marko2196c652017-11-30 16:16:07 +00002383 self->SetTopOfStackTagged(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002384
Ian Rogerse0dcd462014-03-08 15:21:04 -08002385 self->VerifyStack();
2386
Vladimir Markof8655b32018-03-21 17:53:56 +00002387 // We can now walk the stack if needed by JIT GC from MethodEntered() for JIT-on-first-use.
2388 jit::Jit* jit = runtime->GetJit();
2389 if (jit != nullptr) {
2390 jit->MethodEntered(self, called);
2391 }
2392
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002393 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002394 uint32_t* sp32;
2395 // Skip calling JniMethodStart for @CriticalNative.
2396 if (LIKELY(!critical_native)) {
2397 // Start JNI, save the cookie.
2398 if (called->IsSynchronized()) {
2399 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2400 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2401 if (self->IsExceptionPending()) {
2402 self->PopHandleScope();
2403 // A negative value denotes an error.
2404 return GetTwoWordFailureValue();
2405 }
2406 } else {
2407 if (fast_native) {
2408 cookie = JniMethodFastStart(self);
2409 } else {
2410 DCHECK(normal_native);
2411 cookie = JniMethodStart(self);
2412 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002413 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002414 sp32 = reinterpret_cast<uint32_t*>(sp);
2415 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002416 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002417
Andreas Gampe90546832014-03-12 18:07:19 -07002418 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002419 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002420
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002421 // There are two cases for the content of nativeCode:
2422 // 1) Pointer to the native function.
2423 // 2) Pointer to the trampoline for native code binding.
2424 // In the second case, we need to execute the binding and continue with the actual native function
2425 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002426 DCHECK(nativeCode != nullptr);
2427 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002428 nativeCode = artFindNativeMethod(self);
Andreas Gampe90546832014-03-12 18:07:19 -07002429
2430 if (nativeCode == nullptr) {
2431 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002432
Igor Murashkin06a04e02016-09-13 15:57:37 -07002433 // @CriticalNative calls do not need to call back into JniMethodEnd.
2434 if (LIKELY(!critical_native)) {
2435 // End JNI, as the assembly will move to deliver the exception.
2436 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2437 if (shorty[0] == 'L') {
2438 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2439 } else {
2440 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2441 }
Andreas Gampead615172014-04-04 16:20:13 -07002442 }
2443
Andreas Gampec200a4a2014-06-16 18:39:09 -07002444 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002445 }
2446 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002447 }
2448
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002449#if defined(__mips__) && !defined(__LP64__)
2450 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2451 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2452 // and load into floating-point registers.
2453 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2454 // view):
2455 // (1)
2456 // | DOUBLE | DOUBLE | other args, if any
2457 // | F12 | F13 | F14 | F15 |
2458 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2459 // (2)
2460 // | DOUBLE | FLOAT | (PAD) | other args, if any
2461 // | F12 | F13 | F14 | |
2462 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2463 // (3)
2464 // | FLOAT | (PAD) | DOUBLE | other args, if any
2465 // | F12 | | F14 | F15 |
2466 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2467 // (4)
2468 // | FLOAT | FLOAT | other args, if any
2469 // | F12 | F14 |
2470 // | SP+0 | SP+4 | SP+8
2471 // As you can see, only the last case (4) is special. In all others we can just
2472 // load F12/F13 and F14/F15 in the same manner.
2473 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2474 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2475 if (nativeCode != nullptr &&
2476 shorty != nullptr &&
2477 shorty_len >= 3 &&
2478 shorty[1] == 'F' &&
2479 shorty[2] == 'F') {
2480 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2481 }
2482#endif
2483
Andreas Gampe21a6ec52019-03-28 10:55:50 -07002484 VLOG(third_party_jni) << "GenericJNI: "
2485 << called->PrettyMethod()
2486 << " -> "
2487 << std::hex << reinterpret_cast<uintptr_t>(nativeCode);
2488
Andreas Gampec200a4a2014-06-16 18:39:09 -07002489 // Return native code addr(lo) and bottom of alloca address(hi).
2490 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2491 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002492}
2493
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002494// Defined in quick_jni_entrypoints.cc.
2495extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2496 jvalue result, uint64_t result_f, ArtMethod* called,
2497 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002498/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002499 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002500 * unlocking.
2501 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002502extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2503 jvalue result,
2504 uint64_t result_f) {
2505 // We're here just back from a native call. We don't have the shared mutator lock at this point
2506 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2507 // anything that requires a mutator lock before that would cause problems as GC may have the
2508 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002509 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Vladimir Marko2196c652017-11-30 16:16:07 +00002510 DCHECK(self->GetManagedStack()->GetTopQuickFrameTag());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002511 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002512 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002513 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002514 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2515 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002516}
2517
Andreas Gamped58342c2014-06-05 14:18:08 -07002518// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2519// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002520//
Andreas Gamped58342c2014-06-05 14:18:08 -07002521// 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 -07002522// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002523
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002524template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002525static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2526 ObjPtr<mirror::Object> this_object,
2527 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002528 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002529 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002530 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002531 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002532 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002533 if (UNLIKELY(method == nullptr)) {
Vladimir Marko7e097372018-11-28 16:40:59 +00002534 const DexFile* dex_file = caller_method->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002535 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002536 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002537 {
2538 // Remember the args in case a GC happens in FindMethodFromCode.
2539 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2540 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2541 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002542 method = FindMethodFromCode<type, access_check>(method_idx,
2543 &this_object,
2544 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002545 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002546 visitor.FixupReferences();
2547 }
2548
Ian Rogerse0a02da2014-12-02 14:10:53 -08002549 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002550 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002551 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002552 }
2553 }
2554 DCHECK(!self->IsExceptionPending());
2555 const void* code = method->GetEntryPointFromQuickCompiledCode();
2556
2557 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002558 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002559 << " location: "
2560 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002561
Andreas Gamped58342c2014-06-05 14:18:08 -07002562 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2563 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002564}
2565
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002566// Explicit artInvokeCommon template function declarations to please analysis tool.
2567#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002568 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002569 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002570 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002571
2572EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2573EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2574EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2575EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2576EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2577EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2578EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2579EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2580EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2581EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2582#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2583
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002584// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002585extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002587 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002588 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002589}
2590
Andreas Gampec200a4a2014-06-16 18:39:09 -07002591extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002592 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002593 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002594 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002595}
2596
Andreas Gampec200a4a2014-06-16 18:39:09 -07002597extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002598 uint32_t method_idx,
2599 mirror::Object* this_object ATTRIBUTE_UNUSED,
2600 Thread* self,
2601 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2602 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2603 // it doesn't cause ObjPtr alignment failure check.
2604 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002605}
2606
Andreas Gampec200a4a2014-06-16 18:39:09 -07002607extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002608 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002609 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002610 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002611}
2612
Andreas Gampec200a4a2014-06-16 18:39:09 -07002613extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002614 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002615 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002616 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002617}
2618
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002619// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2620extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2621 REQUIRES_SHARED(Locks::mutator_lock_) {
2622 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2623 DCHECK(!referrer->IsProxyMethod());
2624 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2625 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2626 DCHECK(result == nullptr ||
2627 result->GetDeclaringClass()->IsInterface() ||
2628 result->GetDeclaringClass() ==
2629 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2630 << result->PrettyMethod();
2631 return result;
2632}
2633
Jeff Hao5667f562017-02-27 19:32:01 -08002634// Determine target of interface dispatch. The interface method and this object are known non-null.
2635// The interface method is the method returned by the dex cache in the conflict trampoline.
2636extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002637 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002638 Thread* self,
2639 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002640 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002641 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002642 StackHandleScope<2> hs(self);
2643 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2644 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002645
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002646 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002647 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002648 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002649
Vladimir Marko302f69c2017-07-25 15:27:15 +01002650 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002651 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002652 // Fetch the dex_method_idx of the target interface method from the caller.
2653 uint32_t dex_method_idx;
2654 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002655 const Instruction& instr = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002656 Instruction::Code instr_code = instr.Opcode();
Jeff Hao5667f562017-02-27 19:32:01 -08002657 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2658 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Vladimir Markod7559b72017-09-28 13:50:37 +01002659 << "Unexpected call into interface trampoline: " << instr.DumpString(nullptr);
Jeff Hao5667f562017-02-27 19:32:01 -08002660 if (instr_code == Instruction::INVOKE_INTERFACE) {
Vladimir Markod7559b72017-09-28 13:50:37 +01002661 dex_method_idx = instr.VRegB_35c();
Jeff Hao5667f562017-02-27 19:32:01 -08002662 } else {
2663 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
Vladimir Markod7559b72017-09-28 13:50:37 +01002664 dex_method_idx = instr.VRegB_3rc();
Jeff Hao5667f562017-02-27 19:32:01 -08002665 }
2666
Vladimir Marko7e097372018-11-28 16:40:59 +00002667 const DexFile& dex_file = *caller_method->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002668 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002669 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2670 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002671 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002672 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002673 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2674 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2675 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002676 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2677 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2678 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002679 visitor.FixupReferences();
2680 }
2681
Vladimir Marko302f69c2017-07-25 15:27:15 +01002682 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002683 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002684 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002685 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002686 }
2687
2688 DCHECK(!interface_method->IsRuntimeMethod());
2689 // Look whether we have a match in the ImtConflictTable.
David Srbeckye36e7f22018-11-14 14:21:23 +00002690 uint32_t imt_index = interface_method->GetImtIndex();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002691 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2692 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2693 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2694 DCHECK(current_table != nullptr);
2695 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2696 } else {
2697 // It seems we aren't really a conflict method!
2698 if (kIsDebugBuild) {
2699 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2700 CHECK_EQ(conflict_method, m)
2701 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2702 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2703 }
2704 method = conflict_method;
2705 }
2706 if (method != nullptr) {
2707 return GetTwoWordSuccessValue(
2708 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2709 reinterpret_cast<uintptr_t>(method));
2710 }
2711
2712 // No match, use the IfTable.
2713 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2714 if (UNLIKELY(method == nullptr)) {
2715 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2716 interface_method, this_object.Get(), caller_method);
2717 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002718 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002719
2720 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2721 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002722 DCHECK(conflict_method->IsRuntimeMethod());
2723 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2724 cls.Get(),
2725 conflict_method,
2726 interface_method,
2727 method,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002728 /*force_new_conflict_method=*/false);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002729 if (new_conflict_method != conflict_method) {
2730 // Update the IMT if we create a new conflict method. No fence needed here, as the
2731 // data is consistent.
2732 imt->Set(imt_index,
2733 new_conflict_method,
2734 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002735 }
2736
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002737 const void* code = method->GetEntryPointFromQuickCompiledCode();
2738
2739 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002740 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002741 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002742
Andreas Gamped58342c2014-06-05 14:18:08 -07002743 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2744 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002745}
2746
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002747// Returns uint64_t representing raw bits from JValue.
2748extern "C" uint64_t artInvokePolymorphic(mirror::Object* raw_receiver, Thread* self, ArtMethod** sp)
Orion Hodsonac141392017-01-13 11:53:47 +00002749 REQUIRES_SHARED(Locks::mutator_lock_) {
2750 ScopedQuickEntrypointChecks sqec(self);
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002751 DCHECK(raw_receiver != nullptr);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002752 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002753
2754 // Start new JNI local reference state
2755 JNIEnvExt* env = self->GetJniEnv();
2756 ScopedObjectAccessUnchecked soa(env);
2757 ScopedJniEnvLocalRefState env_state(env);
2758 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2759
2760 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2761 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2762 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002763 const Instruction& inst = caller_method->DexInstructions().InstructionAt(dex_pc);
Vladimir Markod7559b72017-09-28 13:50:37 +01002764 DCHECK(inst.Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2765 inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
Orion Hodson06d10a72018-05-14 08:53:38 +01002766 const dex::ProtoIndex proto_idx(inst.VRegH());
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002767 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00002768 const size_t shorty_length = strlen(shorty);
2769 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2770 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002771 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002772
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002773 // Wrap raw_receiver in a Handle for safety.
2774 StackHandleScope<3> hs(self);
2775 Handle<mirror::Object> receiver_handle(hs.NewHandle(raw_receiver));
2776 raw_receiver = nullptr;
Orion Hodsonac141392017-01-13 11:53:47 +00002777 self->EndAssertNoThreadSuspension(old_cause);
2778
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002779 // Resolve method.
Orion Hodsonac141392017-01-13 11:53:47 +00002780 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002781 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002782 self, inst.VRegB(), caller_method, kVirtual);
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002783
Orion Hodsone7732be2017-10-11 14:35:20 +01002784 Handle<mirror::MethodType> method_type(
2785 hs.NewHandle(linker->ResolveMethodType(self, proto_idx, caller_method)));
Orion Hodsonac141392017-01-13 11:53:47 +00002786 if (UNLIKELY(method_type.IsNull())) {
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002787 // This implies we couldn't resolve one or more types in this method handle.
Orion Hodsonac141392017-01-13 11:53:47 +00002788 CHECK(self->IsExceptionPending());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002789 return 0UL;
Orion Hodsonac141392017-01-13 11:53:47 +00002790 }
2791
Vladimir Markod7559b72017-09-28 13:50:37 +01002792 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst.VRegA());
Orion Hodsonac141392017-01-13 11:53:47 +00002793 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2794
2795 // Fix references before constructing the shadow frame.
2796 gc_visitor.FixupReferences();
2797
2798 // Construct shadow frame placing arguments consecutively from |first_arg|.
Vladimir Markod7559b72017-09-28 13:50:37 +01002799 const bool is_range = (inst.Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2800 const size_t num_vregs = is_range ? inst.VRegA_4rcc() : inst.VRegA_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00002801 const size_t first_arg = 0;
2802 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002803 CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, resolved_method, dex_pc);
Orion Hodsonac141392017-01-13 11:53:47 +00002804 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2805 ScopedStackedShadowFramePusher
2806 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2807 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2808 kMethodIsStatic,
2809 shorty,
2810 strlen(shorty),
2811 shadow_frame,
2812 first_arg);
2813 shadow_frame_builder.VisitArguments();
2814
2815 // Push a transition back into managed code onto the linked list in thread.
2816 ManagedStack fragment;
2817 self->PushManagedStackFragment(&fragment);
2818
2819 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2820 // consecutive order.
Orion Hodson960d4f72017-11-10 15:32:38 +00002821 RangeInstructionOperands operands(first_arg + 1, num_vregs - 1);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002822 Intrinsics intrinsic = static_cast<Intrinsics>(resolved_method->GetIntrinsic());
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002823 JValue result;
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002824 bool success = false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002825 if (resolved_method->GetDeclaringClass() == GetClassRoot<mirror::MethodHandle>(linker)) {
Orion Hodson537a4fe2018-05-15 13:57:58 +01002826 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00002827 ObjPtr<mirror::MethodHandle>::DownCast(receiver_handle.Get())));
Orion Hodson537a4fe2018-05-15 13:57:58 +01002828 if (intrinsic == Intrinsics::kMethodHandleInvokeExact) {
2829 success = MethodHandleInvokeExact(self,
2830 *shadow_frame,
2831 method_handle,
2832 method_type,
2833 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002834 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002835 } else {
2836 DCHECK_EQ(static_cast<uint32_t>(intrinsic),
2837 static_cast<uint32_t>(Intrinsics::kMethodHandleInvoke));
2838 success = MethodHandleInvoke(self,
2839 *shadow_frame,
2840 method_handle,
2841 method_type,
2842 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002843 &result);
Orion Hodson537a4fe2018-05-15 13:57:58 +01002844 }
2845 } else {
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01002846 DCHECK_EQ(GetClassRoot<mirror::VarHandle>(linker), resolved_method->GetDeclaringClass());
Orion Hodson537a4fe2018-05-15 13:57:58 +01002847 Handle<mirror::VarHandle> var_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00002848 ObjPtr<mirror::VarHandle>::DownCast(receiver_handle.Get())));
Orion Hodson537a4fe2018-05-15 13:57:58 +01002849 mirror::VarHandle::AccessMode access_mode =
2850 mirror::VarHandle::GetAccessModeByIntrinsic(intrinsic);
2851 success = VarHandleInvokeAccessor(self,
Orion Hodson960d4f72017-11-10 15:32:38 +00002852 *shadow_frame,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002853 var_handle,
Orion Hodson960d4f72017-11-10 15:32:38 +00002854 method_type,
Orion Hodson537a4fe2018-05-15 13:57:58 +01002855 access_mode,
Orion Hodson960d4f72017-11-10 15:32:38 +00002856 &operands,
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002857 &result);
Orion Hodsonac141392017-01-13 11:53:47 +00002858 }
Orion Hodson537a4fe2018-05-15 13:57:58 +01002859
Orion Hodson43f0cdb2017-10-10 14:47:32 +01002860 DCHECK(success || self->IsExceptionPending());
Orion Hodsonac141392017-01-13 11:53:47 +00002861
2862 // Pop transition record.
2863 self->PopManagedStackFragment(fragment);
2864
Orion Hodsoncd260eb2018-06-06 09:04:17 +01002865 return result.GetJ();
Orion Hodsonac141392017-01-13 11:53:47 +00002866}
2867
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002868// Returns uint64_t representing raw bits from JValue.
2869extern "C" uint64_t artInvokeCustom(uint32_t call_site_idx, Thread* self, ArtMethod** sp)
2870 REQUIRES_SHARED(Locks::mutator_lock_) {
2871 ScopedQuickEntrypointChecks sqec(self);
2872 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
2873
2874 // invoke-custom is effectively a static call (no receiver).
2875 static constexpr bool kMethodIsStatic = true;
2876
2877 // Start new JNI local reference state
2878 JNIEnvExt* env = self->GetJniEnv();
2879 ScopedObjectAccessUnchecked soa(env);
2880 ScopedJniEnvLocalRefState env_state(env);
2881
2882 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2883
2884 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2885 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2886 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2887 const DexFile* dex_file = caller_method->GetDexFile();
2888 const dex::ProtoIndex proto_idx(dex_file->GetProtoIndexForCallSite(call_site_idx));
2889 const char* shorty = caller_method->GetDexFile()->GetShorty(proto_idx);
2890 const uint32_t shorty_len = strlen(shorty);
2891
2892 // Construct the shadow frame placing arguments consecutively from |first_arg|.
2893 const size_t first_arg = 0;
2894 const size_t num_vregs = ArtMethod::NumArgRegisters(shorty);
2895 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe98ea9d92018-10-19 14:06:15 -07002896 CREATE_SHADOW_FRAME(num_vregs, /* link= */ nullptr, caller_method, dex_pc);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002897 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2898 ScopedStackedShadowFramePusher
2899 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2900 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2901 kMethodIsStatic,
2902 shorty,
2903 shorty_len,
2904 shadow_frame,
2905 first_arg);
2906 shadow_frame_builder.VisitArguments();
2907
2908 // Push a transition back into managed code onto the linked list in thread.
2909 ManagedStack fragment;
2910 self->PushManagedStackFragment(&fragment);
2911 self->EndAssertNoThreadSuspension(old_cause);
2912
2913 // Perform the invoke-custom operation.
2914 RangeInstructionOperands operands(first_arg, num_vregs);
2915 JValue result;
2916 bool success =
2917 interpreter::DoInvokeCustom(self, *shadow_frame, call_site_idx, &operands, &result);
2918 DCHECK(success || self->IsExceptionPending());
2919
2920 // Pop transition record.
2921 self->PopManagedStackFragment(fragment);
2922
2923 return result.GetJ();
2924}
2925
Ian Rogers848871b2013-08-05 10:56:33 -07002926} // namespace art